Files
leleka/engine/strata/tests/hip/q2_zero.cpp
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

51 lines
2.2 KiB
C++

// Q2_0 dequant must preserve signed zero when a negative scale meets code 1.
// This caught HIP 5.7/gfx1012 folding the half product to positive zero.
#include "strata/kernels/iq_kernels.hpp"
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define CHECK(call) do { const auto e = (call); if (e != hipSuccess) { \
std::fprintf(stderr, "%s: %s\n", #call, hipGetErrorString(e)); std::exit(2); } } while (0)
int main() {
// GGUF Q2_0: one half scale followed by sixteen packed 2-bit bytes.
struct Block { uint16_t d; uint8_t qs[16]; };
static_assert(sizeof(Block) == 18);
constexpr int blocks = 16, values = blocks * 64;
std::vector<Block> input(blocks);
std::vector<uint16_t> expected(values), got(values);
for (int b = 0; b < blocks; ++b) {
const float scale = (b & 1) ? -0.5f : 0.5f;
const __half half_scale = __float2half(scale);
std::memcpy(&input[b].d, &half_scale, 2);
for (int i = 0; i < 16; ++i) input[b].qs[i] = 0xe4; // codes 0,1,2,3
for (int i = 0; i < 64; ++i) {
const __half value = __float2half(scale * float((i % 4) - 1));
std::memcpy(&expected[b * 64 + i], &value, 2);
}
}
void* weights = nullptr;
uint16_t* output = nullptr;
CHECK(hipMalloc(&weights, input.size() * sizeof(Block)));
CHECK(hipMalloc(reinterpret_cast<void**>(&output), values * sizeof(uint16_t)));
CHECK(hipMemcpy(weights, input.data(), input.size() * sizeof(Block), hipMemcpyHostToDevice));
strata::kernels::iq_dequant_f16(42, weights, values, output, nullptr);
CHECK(hipDeviceSynchronize());
CHECK(hipMemcpy(got.data(), output, values * sizeof(uint16_t), hipMemcpyDeviceToHost));
int failures = 0;
for (int i = 0; i < values; ++i) {
if (got[i] == expected[i]) continue;
if (failures++ < 4)
std::printf("index=%d got=0x%04x expected=0x%04x\n", i, unsigned(got[i]), unsigned(expected[i]));
}
CHECK(hipFree(weights));
CHECK(hipFree(output));
std::printf("hip_q2_zero: %d values, %d failures\n", values, failures);
return failures ? 1 : 0;
}