okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
3.9 KiB86 linesraw
1// Q5_1 shared-expert projection: Strata CUDA dequantization vs ggml.
2#define NOMINMAX
3#define DPCT_PROFILING_ENABLED
4#include <sycl/sycl.hpp>
5#include <dpct/dpct.hpp>
6#include "strata/artifact/gguf_reader.hpp"
7#include "strata/kernels/dequant_bf16.hpp"
8#include "ggml.h"
9
10#include <algorithm>
11#include <cmath>
12#include <cstdio>
13#include <cstdint>
14#include <cstring>
15#include <vector>
16
17int main(int argc, char **argv) try {
18    if (argc != 2) {
19        std::fprintf(stderr, "usage: q5_projection_parity <shard1.gguf>\n");
20        return 2;
21    }
22    strata::GgufFile gguf(argv[1]);
23    const auto* t = gguf.find("blk.0.ffn_down_shexp.weight");
24    if (!t || t->type != 7 || t->shape.size() != 2) {
25        std::fprintf(stderr, "expected blk.0.ffn_down_shexp.weight Q5_1\n");
26        return 2;
27    }
28    const int64_t cols = (int64_t) t->shape[0], rows = std::min<int64_t>(4, (int64_t) t->shape[1]);
29    const size_t weight_bytes = (size_t) rows * (size_t) (cols / 32) * 24;
30    const size_t elements = (size_t) rows * (size_t) cols;
31    const uint8_t* source = gguf.tensor_data(*t);
32    void *dweight = nullptr, *dfloat = nullptr, *dbf16 = nullptr;
33    if (DPCT_CHECK_ERROR(dweight = (void *)sycl::malloc_device(
34                             weight_bytes, dpct::get_in_order_queue())) != 0 ||
35        DPCT_CHECK_ERROR(
36            dfloat = (void *)sycl::malloc_device(
37                elements * sizeof(float), dpct::get_in_order_queue())) != 0 ||
38        DPCT_CHECK_ERROR(dbf16 = (void *)sycl::malloc_device(
39                             elements * sizeof(uint16_t),
40                             dpct::get_in_order_queue())) != 0) {
41        std::fprintf(stderr, "CUDA allocation failed\n");
42        return 1;
43    }
44    /*
45    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
46    the original code the source host memory is pageable memory. If the memory
47    is not pageable, call wait() on event return by memcpy API to ensure
48    synchronization behavior.
49    */
50    (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(dweight, source, weight_bytes).wait();
51    strata::kernels::dequant_f32(7, dweight, 0, rows, cols, (float*) dfloat, nullptr);
52    strata::kernels::dequant_bf16(7, dweight, 0, rows, cols, (uint16_t*) dbf16, nullptr);
53    std::vector<float> got(elements), ref(elements);
54    std::vector<uint16_t> got_bf16(elements);
55    (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
56        .memcpy(got.data(), dfloat, elements * sizeof(float))
57        .wait();
58    (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
59        .memcpy(got_bf16.data(), dbf16, elements * sizeof(uint16_t))
60        .wait();
61    sycl::free(dweight, dpct::get_in_order_queue());
62        sycl::free(dfloat, dpct::get_in_order_queue());
63        sycl::free(dbf16, dpct::get_in_order_queue());
64    const auto* traits = ggml_get_type_traits(GGML_TYPE_Q5_1);
65    for (int64_t r = 0; r < rows; ++r)
66        traits->to_float(source + (size_t) r * (size_t) (cols / 32) * 24,
67                         ref.data() + (size_t) r * (size_t) cols, cols);
68    double max_abs = 0;
69    size_t bf16_bad = 0;
70    for (size_t i = 0; i < elements; ++i) {
71        max_abs = std::max(max_abs, (double) std::fabs(got[i] - ref[i]));
72        uint32_t bits;
73        std::memcpy(&bits, &ref[i], sizeof(bits));
74        const uint16_t expected = (uint16_t) ((bits + 0x7fffu + ((bits >> 16) & 1u)) >> 16);
75        if (got_bf16[i] != expected) ++bf16_bad;
76    }
77    const bool ok = max_abs <= 1e-6 && bf16_bad == 0;
78    std::printf("Q5_1 projection: %zu elements, max_abs %.3e, bf16 differences %zu %s\n",
79                elements, max_abs, bf16_bad, ok ? "PASS" : "FAIL");
80    return ok ? 0 : 1;
81}
82catch (sycl::exception const &exc) {
83  std::cerr << exc.what() << "Exception caught at file:" << __FILE__
84            << ", line:" << __LINE__ << std::endl;
85  std::exit(1);
86}