MCPcopy Create free account
hub / github.com/Niko1221/Strata / main

Function main

engine/tests/quant/quant_bench.cpp:44–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42} // namespace
43
44int main(int argc, char** argv) {
45 const char* only = nullptr;
46 int mb = 256, reps = 7, n_el = 4096;
47 sq::Arith want = sq::default_arith();
48 for (int i = 1; i < argc; ++i) {
49 if (!std::strcmp(argv[i], "--type") && i + 1 < argc) only = argv[++i];
50 else if (!std::strcmp(argv[i], "--mb") && i + 1 < argc) mb = std::atoi(argv[++i]);
51 else if (!std::strcmp(argv[i], "--reps") && i + 1 < argc) reps = std::atoi(argv[++i]);
52 else if (!std::strcmp(argv[i], "--n") && i + 1 < argc) n_el = std::atoi(argv[++i]);
53 else if (!std::strcmp(argv[i], "--arith") && i + 1 < argc) {
54 const std::string a = argv[++i];
55 want = a == "avx" ? sq::Arith::Avx : a == "generic" ? sq::Arith::Generic : sq::Arith::Avx2;
56 }
57 }
58 std::printf("quant_bench: contract %s, best isa %s, DRAM set %d MB, L2 set 256 KB, n=%d, median of %d\n", sq::arith_name(want), sq::isa_name(sq::best_isa()),
59 mb, n_el, reps);
60 float sink = 0.f;
61 for (int t = 0; t < 150; ++t) {
62 const sq::TypeInfo* ti = sq::type_info(t);
63 if (ti == nullptr || !ti->has_dot || (only && std::strcmp(only, ti->name))) continue;
64 const sq::DotKernel* ks[16];
65 const int nk = sq::dot_kernels(ti->type, want, ks, 16);
66 std::vector<const sq::DotKernel*> run_k;
67 for (int i = 0; i < nk; ++i)
68 if (sq::isa_ok(ks[i]->isa)) run_k.push_back(ks[i]);
69 if (run_k.empty()) continue;
70 const sq::TypeInfo* ai = sq::type_info(ti->act);
71 const int unit = std::max(ti->blck, ai->blck);
72 const int n = n_el / unit * unit;
73 const size_t rb = sq::row_bytes(t, n);
74 const size_t rows_dram = (size_t) mb * 1048576 / rb, rows_l2 = std::max<size_t>(1, 256 * 1024 / rb);
75 std::vector<uint8_t> w(rows_dram * rb);
76 for (auto& b : w) b = (uint8_t) rnd();
77 // sane scales: first two bytes of each block as fp16 ~ 0.01
78 for (size_t o = 0; o + ti->bytes <= w.size(); o += ti->bytes) {
79 if (ti->type == sq::Type::IQ1_M || ti->type == sq::Type::NVFP4 || ti->type == sq::Type::MXFP4 || ti->type == sq::Type::Q2_K ||
80 ti->type == sq::Type::Q3_K || ti->type == sq::Type::Q6_K) continue; // scale not at the front: random bytes still give finite-enough sums
81 w[o] = 0x66, w[o + 1] = 0x24;
82 }
83 std::vector<float> x((size_t) n);
84 for (auto& v : x) v = (float) ((int) (rnd() % 2001) - 1000) * 1e-3f;
85 std::vector<uint8_t> act((size_t) sq::row_bytes((int) ti->act, n));
86 const sq::ActKernel* ak = sq::act_kernel(ti->act, want);
87 if (ak == nullptr) continue;
88 ak->fn(x.data(), act.data(), n);
89 std::vector<std::vector<double>> td(run_k.size()), tl(run_k.size());
90 for (int rep = 0; rep < reps; ++rep)
91 for (size_t i = 0; i < run_k.size(); ++i) {
92 td[i].push_back(run(run_k[i], w, rb, n, rows_dram, act.data(), &sink));
93 double best = 1e9;
94 for (int r2 = 0; r2 < 8; ++r2) best = std::min(best, run(run_k[i], w, rb, n, rows_l2, act.data(), &sink));
95 tl[i].push_back(best);
96 }
97 for (size_t i = 0; i < run_k.size(); ++i) {
98 std::sort(td[i].begin(), td[i].end());
99 std::sort(tl[i].begin(), tl[i].end());
100 const double gd = (double) rows_dram * rb / td[i][td[i].size() / 2] / 1e9, gl = (double) rows_l2 * rb / tl[i][tl[i].size() / 2] / 1e9;
101 std::printf(" %-8s %-26s %-6s DRAM %6.2f GB/s L2 %6.2f GB/s\n", ti->name, run_k[i]->name, sq::isa_name(run_k[i]->isa), gd, gl);

Callers

nothing calls this directly

Calls 15

default_arithFunction · 0.85
arith_nameFunction · 0.85
dot_kernelsFunction · 0.85
act_kernelFunction · 0.85
sortFunction · 0.85
push_backMethod · 0.80
rndFunction · 0.70
runFunction · 0.70
isa_nameFunction · 0.50
best_isaFunction · 0.50
type_infoFunction · 0.50
isa_okFunction · 0.50

Tested by

no test coverage detected