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

Function main

engine/src/split/tools/convert_parallel_test.cpp:106–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104} // namespace
105
106int main() {
107 const char* tmp = std::getenv("TMPDIR");
108 const std::string path = std::string(tmp ? tmp : "/tmp") + "/convert_parallel_test_" + std::to_string((long long) std::rand()) + ".gguf";
109 std::vector<Spec> ts;
110 ts.push_back(raw("blk.0.ffn_gate_exps.weight", fnx::T_IQ3_S, 256, 8, 256, 110)); // the routed experts: IQ3_S, left as they are
111 for (int l = 0; l < 24; ++l) {
112 const std::string p = "blk." + std::to_string(l) + ".";
113 ts.push_back(bf16_matrix(p + "attn_q.weight", 256, 24)); // BF16 -> Q8_0
114 ts.push_back(q6k_matrix(p + "attn_k.weight", 256, 16)); // Q6_K -> Q8_0
115 ts.push_back(bf16_matrix(p + "ffn_gate_inp.weight", 64, 8)); // BF16 -> F32
116 }
117 ts.push_back(raw("blk.3.ffn_down_exps.weight", fnx::T_Q2_0, 64, 6, 64, 18)); // Q2_0 -> Q4_0 blocks
118 ts.push_back(raw("blk.4.attn_v.weight", fnx::T_Q8_0, 32, 4, 32, 34)); // already Q8_0: not converted
119 ts.push_back(bf16_matrix("token_embd.weight", 128, 400)); // the big one: BF16 -> Q8_0
120 check(write_gguf(path, ts), "the test file is written");
121
122 setenv("STRATA_CONVERT_THREADS", "1", 1);
123 fnx::Gguf serial(path);
124 setenv("STRATA_CONVERT_THREADS", "8", 1);
125 fnx::Gguf parallel(path);
126 check(serial.converted() == 24 * 3 + 2 + 0 && serial.converted() > 0, "the single-threaded load converts 24 x 3 dense tensors, the Q2_0 one and the embedding table: " + std::to_string(serial.converted()));
127 check(serial.converted() == parallel.converted(), "the same number of tensors converted");
128 check(serial.tensors().size() == parallel.tensors().size(), "the same tensors");
129 for (const auto& kv : serial.tensors()) {
130 const fnx::Tensor* b = parallel.find(kv.first);
131 check(b != nullptr, kv.first + " is in both");
132 if (!b) continue;
133 const fnx::Tensor& a = kv.second;
134 check(a.type == b->type && a.nbytes == b->nbytes && a.shard == b->shard, kv.first + ": type, size and origin");
135 check(a.nbytes == b->nbytes && std::memcmp(a.data, b->data, a.nbytes) == 0, kv.first + ": the same bytes");
136 }
137 check(serial.t("blk.0.attn_q.weight").type == fnx::T_Q8_0 && parallel.t("blk.0.attn_q.weight").shard == -1, "a converted tensor lives in memory as Q8_0");
138 check(parallel.t("blk.0.ffn_gate_inp.weight").type == fnx::T_F32, "the router is F32, type " + std::to_string(parallel.t("blk.0.ffn_gate_inp.weight").type));
139 { // the router's values are the BF16 values widened, not another conversion's
140 const Spec& src = ts[3];
141 uint32_t u0 = (uint32_t) src.data[0] | ((uint32_t) src.data[1] << 8);
142 u0 <<= 16;
143 float want0, got0;
144 std::memcpy(&want0, &u0, 4);
145 std::memcpy(&got0, parallel.t("blk.0.ffn_gate_inp.weight").data, 4);
146 check(src.name == "blk.0.ffn_gate_inp.weight" && want0 == got0, "the router's first value is the BF16 source's");
147 }
148 check(parallel.t("blk.3.ffn_down_exps.weight").type == fnx::T_Q4_0, "the routed Q2_0 tensor is Q4_0 blocks");
149 check(parallel.t("blk.4.attn_v.weight").shard == 0, "a tensor already in the wanted type stays in the file");
150
151 // a tensor that cannot be converted ends the load with the error, on one thread and on eight
152 ts.push_back(bf16_matrix("blk.9.attn_output.weight", 30, 4)); // 30 columns: not a whole number of Q8_0 blocks
153 check(write_gguf(path, ts), "the second test file is written");
154 for (const char* n : {"1", "8"}) {
155 setenv("STRATA_CONVERT_THREADS", n, 1);
156 bool threw = false;
157 try {
158 fnx::Gguf g(path);
159 } catch (const std::exception& e) {
160 threw = std::string(e.what()).find("blk.9.attn_output.weight") != std::string::npos;
161 }
162 check(threw, std::string("a bad tensor ends the load with its name, STRATA_CONVERT_THREADS=") + n);
163 }

Callers

nothing calls this directly

Calls 15

bf16_matrixFunction · 0.85
q6k_matrixFunction · 0.85
setenvFunction · 0.85
memcpyFunction · 0.85
push_backMethod · 0.80
convertedMethod · 0.80
rawFunction · 0.70
checkFunction · 0.70
write_ggufFunction · 0.70
stringFunction · 0.50
to_stringFunction · 0.50
removeFunction · 0.50

Tested by

no test coverage detected