Load three-shard Uncensored weights while preserving Q5_0 PLE rows and Q4_K/Q5 expert bytes. Add native CUDA and MMQ paths with real-weight parity checks. Rebased onto 0.1.31 (Niko1221): 0.1.31 already loads each expert role from its own shard (the per-role shard column of native_experts.txt, iq_pack.py, expert_source.cpp, native_expert_parity's multi-shard mode) and has its own Q4_K/Q5_K/Q5_1/Q8_0 GPU expert decoders, so this PR's gguf_role_file / v4 manifest, its iq_pack.py and test_iq_pack.py changes and its iq_kernels.cu decoders are dropped. Kept: Q5_1 in native_mmvq (type 7 through 0.1.31's iq_mmvq) and in the BF16 dequantizer, the Q5_0 PLE table (mapped reader), the Q4_K/Q5_0/Q5_1 MMQ instances under the STRATA_ORCA_Q4KS_MMQ option (OFF), ple_q5_parity / q5_projection_parity and the doc.
63 lines
2.8 KiB
C++
63 lines
2.8 KiB
C++
// Q5_1 shared-expert projection: Strata CUDA dequantization vs ggml.
|
|
#define NOMINMAX
|
|
#include "strata/artifact/gguf_reader.hpp"
|
|
#include "strata/kernels/dequant_bf16.hpp"
|
|
#include "ggml.h"
|
|
|
|
#include <cuda_runtime.h>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <vector>
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc != 2) {
|
|
std::fprintf(stderr, "usage: q5_projection_parity <shard1.gguf>\n");
|
|
return 2;
|
|
}
|
|
strata::GgufFile gguf(argv[1]);
|
|
const auto* t = gguf.find("blk.0.ffn_down_shexp.weight");
|
|
if (!t || t->type != 7 || t->shape.size() != 2) {
|
|
std::fprintf(stderr, "expected blk.0.ffn_down_shexp.weight Q5_1\n");
|
|
return 2;
|
|
}
|
|
const int64_t cols = (int64_t) t->shape[0], rows = std::min<int64_t>(4, (int64_t) t->shape[1]);
|
|
const size_t weight_bytes = (size_t) rows * (size_t) (cols / 32) * 24;
|
|
const size_t elements = (size_t) rows * (size_t) cols;
|
|
const uint8_t* source = gguf.tensor_data(*t);
|
|
void *dweight = nullptr, *dfloat = nullptr, *dbf16 = nullptr;
|
|
if (cudaMalloc(&dweight, weight_bytes) != cudaSuccess ||
|
|
cudaMalloc(&dfloat, elements * sizeof(float)) != cudaSuccess ||
|
|
cudaMalloc(&dbf16, elements * sizeof(uint16_t)) != cudaSuccess) {
|
|
std::fprintf(stderr, "CUDA allocation failed\n");
|
|
return 1;
|
|
}
|
|
cudaMemcpy(dweight, source, weight_bytes, cudaMemcpyHostToDevice);
|
|
strata::kernels::dequant_f32(7, dweight, 0, rows, cols, (float*) dfloat, nullptr);
|
|
strata::kernels::dequant_bf16(7, dweight, 0, rows, cols, (uint16_t*) dbf16, nullptr);
|
|
std::vector<float> got(elements), ref(elements);
|
|
std::vector<uint16_t> got_bf16(elements);
|
|
cudaMemcpy(got.data(), dfloat, elements * sizeof(float), cudaMemcpyDeviceToHost);
|
|
cudaMemcpy(got_bf16.data(), dbf16, elements * sizeof(uint16_t), cudaMemcpyDeviceToHost);
|
|
cudaFree(dweight); cudaFree(dfloat); cudaFree(dbf16);
|
|
const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_1);
|
|
for (int64_t r = 0; r < rows; ++r)
|
|
traits->to_float(source + (size_t) r * (size_t) (cols / 32) * 24,
|
|
ref.data() + (size_t) r * (size_t) cols, cols);
|
|
double max_abs = 0;
|
|
size_t bf16_bad = 0;
|
|
for (size_t i = 0; i < elements; ++i) {
|
|
max_abs = std::max(max_abs, (double) std::fabs(got[i] - ref[i]));
|
|
uint32_t bits;
|
|
std::memcpy(&bits, &ref[i], sizeof(bits));
|
|
const uint16_t expected = (uint16_t) ((bits + 0x7fffu + ((bits >> 16) & 1u)) >> 16);
|
|
if (got_bf16[i] != expected) ++bf16_bad;
|
|
}
|
|
const bool ok = max_abs <= 1e-6 && bf16_bad == 0;
|
|
std::printf("Q5_1 projection: %zu elements, max_abs %.3e, bf16 differences %zu %s\n",
|
|
elements, max_abs, bf16_bad, ok ? "PASS" : "FAIL");
|
|
return ok ? 0 : 1;
|
|
}
|