#include "kar_ple_cache.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { struct Mapped { int fd = -1; const uint8_t* data = nullptr; size_t size = 0; ~Mapped() { if (data) munmap(const_cast(data), size); if (fd >= 0) ::close(fd); } bool open(const char* path, uint64_t expected) { fd = ::open(path, O_RDONLY | O_CLOEXEC); struct stat st {}; if (fd < 0 || fstat(fd, &st) || st.st_size <= 0 || uint64_t(st.st_size) != expected) return false; size = size_t(st.st_size); void* p = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0); if (p == MAP_FAILED) return false; data = static_cast(p); return true; } }; bool decode_cpu(const uint8_t* archive, const std::vector& parts, uint32_t row, float* out) { const auto it = std::upper_bound(parts.begin(), parts.end(), uint64_t(row), [](uint64_t wanted, const strata::kar::PlePart& part) { return wanted < part.row_base; }); if (it == parts.begin()) return false; const auto part = std::prev(it); const kar_tensor_info* t = part->tensor; const uint64_t local = uint64_t(row) - part->row_base; if (local >= t->row_count) return false; for (uint32_t value = 0; value < 160; ++value) { const uint32_t word = value / 32, bit = value % 32; uint16_t scale, book; uint32_t plane0, plane1; std::memcpy(&scale, archive + t->scales_offset + local * 10 + word * 2, 2); std::memcpy(&plane0, archive + t->planes_offset + local * 20 + word * 4, 4); std::memcpy(&plane1, archive + t->planes_offset + t->plane_stride_bytes + local * 20 + word * 4, 4); const uint32_t code = (((plane0 >> bit) & 1u) << 1) | ((plane1 >> bit) & 1u); std::memcpy(&book, archive + t->books_offset + 4 + code * 2, 2); out[value] = __half2float(__ushort_as_half(book)) * __half2float(__ushort_as_half(scale)); } return true; } bool cuda_ok(cudaError_t status, const char* step) { if (status == cudaSuccess) return true; std::fprintf(stderr, "%s: %s\n", step, cudaGetErrorString(status)); return false; } } // namespace int main(int argc, char** argv) { const bool trust_verified = argc == 5 && std::strcmp(argv[4], "--trust-previously-verified-archive") == 0; if (argc != 4 && !trust_verified) { std::fprintf(stderr, "usage: strata-kar-ple-cache-probe ARCHIVE PART_ONE PART_TWO [--trust-previously-verified-archive]\n"); return 2; } kar_archive* raw = nullptr; if (kar_open(argv[1], trust_verified ? KAR_OPEN_DEFAULT : KAR_OPEN_VERIFY_PAYLOAD, &raw) != KAR_OK) { std::fprintf(stderr, "kar_open: %s\n", kar_last_error()); return 1; } std::unique_ptr archive(raw, kar_close); const int a = kar_find(raw, argv[2]), b = kar_find(raw, argv[3]); if (a < 0 || b < 0) { std::fprintf(stderr, "PLE part missing\n"); return 1; } const kar_tensor_info* first = kar_tensor(raw, uint32_t(a)); const kar_tensor_info* second = kar_tensor(raw, uint32_t(b)); if (first->row_count == 0 || first->row_count > UINT32_MAX || second->row_count > UINT32_MAX - first->row_count) return 1; Mapped mapped; if (!mapped.open(argv[1], kar_info(raw)->archive_bytes)) return 1; const uint32_t boundary = uint32_t(first->row_count); std::vector parts{{0, first}, {boundary, second}}; strata::kar::PleRowCache cache; std::string err; if (!cache.open(mapped.data, mapped.size, parts, 1, 20000, err)) { std::fprintf(stderr, "cache open: %s\n", err.c_str()); return 1; } cudaStream_t stream = nullptr; float* device_output = nullptr; int result = 1; do { if (!cuda_ok(cudaStreamCreate(&stream), "stream") || !cuda_ok(cudaMalloc(&device_output, uint64_t(20000) * 160 * sizeof(float)), "output")) break; const std::vector> batches{ {0, 1, boundary - 1, boundary, boundary + 1, boundary + 2}, {boundary + 2, boundary + 1, boundary, boundary - 1, 1, 0}, {boundary - 4, boundary + 4, boundary - 3, boundary + 5, 0, boundary} }; bool failed = false; uint64_t checks = 0; for (const auto& rows : batches) { if (!cache.enqueue(rows.data(), rows.size(), device_output, stream, err) || !cuda_ok(cudaStreamSynchronize(stream), "decode completion")) { std::fprintf(stderr, "cache decode: %s\n", err.c_str()); failed = true; break; } std::vector gpu(rows.size() * 160), cpu(rows.size() * 160); if (!cuda_ok(cudaMemcpy(gpu.data(), device_output, gpu.size() * sizeof(float), cudaMemcpyDeviceToHost), "readback")) { failed = true; break; } for (size_t i = 0; i < rows.size(); ++i) if (!decode_cpu(mapped.data, parts, rows[i], cpu.data() + i * 160)) failed = true; if (failed || std::memcmp(gpu.data(), cpu.data(), gpu.size() * sizeof(float)) != 0) { std::fprintf(stderr, "CPU/CUDA row mismatch\n"); failed = true; break; } checks += rows.size(); } if (failed) break; std::vector overfull(cache.slots() + 1); for (uint32_t i = 0; i < overfull.size(); ++i) overfull[i] = i; if (cache.enqueue(overfull.data(), overfull.size(), device_output, stream, err) || err != "KAR PLE batch has more distinct rows than cache slots") { std::fprintf(stderr, "overfull batch was not rejected as expected: %s\n", err.c_str()); break; } const auto stats = cache.stats(); std::printf("parts=2 boundary=%u batches=%zu rows_checked=%llu parity=exact slots=%u hits=%llu misses=%llu overfull=rejected payload_crc_verified=%s\n", boundary, batches.size(), static_cast(checks), cache.slots(), static_cast(stats.hits), static_cast(stats.misses), trust_verified ? "no" : "yes"); result = 0; } while (false); cache.close(); if (device_output) cudaFree(device_output); if (stream) cudaStreamDestroy(stream); return result; }