| 48 | } // namespace |
| 49 | |
| 50 | int main(int argc, char** argv) { |
| 51 | if (argc < 2) { |
| 52 | std::fprintf(stderr, "usage: dequant_bf16_test SHARD.gguf [SHARD2.gguf]\n"); |
| 53 | return 2; |
| 54 | } |
| 55 | std::map<int, bool> done; |
| 56 | int fails = 0, checked = 0; |
| 57 | for (int a = 1; a < argc; ++a) { |
| 58 | strata::GgufFile gguf(argv[a]); |
| 59 | for (const auto& t : gguf.tensors()) { |
| 60 | const int type = (int) t.type; |
| 61 | if (done.count(type) || !strata::kernels::dequant_bf16_supported(type) || t.shape.size() != 2) continue; |
| 62 | int be = 0, bb = 0; |
| 63 | if (!strata::block_geometry(t.type, be, bb)) continue; |
| 64 | const int64_t cols = (int64_t) t.shape[0]; |
| 65 | const int64_t rows = (std::min<int64_t>)(4, (int64_t) t.shape[1]); |
| 66 | const int64_t row_bytes = cols / be * bb; |
| 67 | const uint8_t* host = (const uint8_t*) gguf.tensor_data(t); |
| 68 | void* d_blocks = nullptr; |
| 69 | float* d_f = nullptr; |
| 70 | uint16_t* d_h = nullptr; |
| 71 | cudaMalloc(&d_blocks, (size_t) (rows * row_bytes)); |
| 72 | cudaMalloc(&d_f, (size_t) (rows * cols) * 4); |
| 73 | cudaMalloc(&d_h, (size_t) (rows * cols) * 2); |
| 74 | cudaMemcpy(d_blocks, host, (size_t) (rows * row_bytes), cudaMemcpyHostToDevice); |
| 75 | strata::kernels::dequant_f32(type, d_blocks, 0, rows, cols, d_f, nullptr); |
| 76 | strata::kernels::dequant_bf16(type, d_blocks, 0, rows, cols, d_h, nullptr); |
| 77 | std::vector<float> gf((size_t) (rows * cols)); |
| 78 | std::vector<uint16_t> gh((size_t) (rows * cols)); |
| 79 | cudaMemcpy(gf.data(), d_f, gf.size() * 4, cudaMemcpyDeviceToHost); |
| 80 | cudaMemcpy(gh.data(), d_h, gh.size() * 2, cudaMemcpyDeviceToHost); |
| 81 | cudaFree(d_blocks); |
| 82 | cudaFree(d_f); |
| 83 | cudaFree(d_h); |
| 84 | std::vector<float> ref((size_t) (rows * cols)); |
| 85 | for (int64_t r = 0; r < rows; ++r) |
| 86 | for (int64_t b = 0; b < cols / be; ++b) |
| 87 | cpu_block(type, host + r * row_bytes + b * bb, ref.data() + r * cols + b * be); |
| 88 | int64_t bad_f = 0, bad_h = 0; |
| 89 | double worst = 0; |
| 90 | for (size_t i = 0; i < ref.size(); ++i) { |
| 91 | const double e = std::fabs((double) gf[i] - ref[i]); |
| 92 | const double tol = 1e-6 * std::fabs((double) ref[i]) + 1e-30; |
| 93 | if (e > tol) ++bad_f; |
| 94 | worst = (std::max)(worst, e / (std::fabs((double) ref[i]) + 1e-30)); |
| 95 | const double eh = std::fabs((double) bf16_to_f32(gh[i]) - ref[i]); |
| 96 | if (eh > std::ldexp(std::fabs((double) ref[i]), -8) + 1e-30) ++bad_h; |
| 97 | } |
| 98 | ++checked; |
| 99 | done[type] = true; |
| 100 | const bool ok = bad_f == 0 && bad_h == 0; |
| 101 | if (!ok) ++fails; |
| 102 | std::printf("type %2d %-40s %lld x %lld: f32 %s (%lld off, worst rel %.2e), bf16 %s (%lld off)\n", type, |
| 103 | t.name.c_str(), (long long) rows, (long long) cols, bad_f ? "FAIL" : "ok", (long long) bad_f, |
| 104 | worst, bad_h ? "FAIL" : "ok", (long long) bad_h); |
| 105 | } |
| 106 | } |
| 107 | std::printf("dequant_bf16_test: %d types checked, %s\n", checked, fails ? "FAILED" : "OK"); |
nothing calls this directly
no test coverage detected