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

Function write_format_table

engine/src/ngram/ple_reader_test.cpp:138–170  ·  view source on GitHub ↗

`claimed_rows` is the row count the header states; `rows` rows are actually written (equal unless a test lies)

Source from the content-addressed store, hash-verified

136
137// `claimed_rows` is the row count the header states; `rows` rows are actually written (equal unless a test lies)
138std::string write_format_table(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows, uint64_t claimed_rows = 0) {
139 const std::string path = dir + "/ple_format_selftest.gguf";
140 std::ofstream out(path, std::ios::binary);
141 if (!out) { std::fprintf(stderr, "cannot write %s\n", path.c_str()); return {}; }
142 const auto le = [](auto value, int bytes) {
143 std::string s((size_t) bytes, '\0');
144 for (int i = 0; i < bytes; ++i) s[(size_t) i] = (char) ((value >> (8 * i)) & 0xff);
145 return s;
146 };
147 const auto str = [&](const char* s) { return le((uint64_t) std::strlen(s), 8) + std::string(s); };
148 const uint64_t n_kv = f.needs_scale ? 3 : 1;
149 std::string head = "GGUF" + le(3u, 4) + le(1ull, 8) + le(n_kv, 8);
150 head += str("general.architecture") + le(8u, 4) + str("strata-ple");
151 if (f.needs_scale) {
152 const float scale = 0.75f;
153 uint32_t sb;
154 std::memcpy(&sb, &scale, 4);
155 head += str("strata.ple.format") + le(8u, 4) + str(f.id == k::PleFormat::NVFP4 ? "nvfp4x16" : "f8_e4m3");
156 head += str("strata.ple.scale") + le(6u, 4) + le(sb, 4); // 6 = FLOAT32
157 }
158 head += str("per_layer_token_embd.weight") + le(2u, 4);
159 head += le(f.id == k::PleFormat::NVFP4 ? (uint64_t) f.row_bytes : (uint64_t) k::PLE_HEAD_DIM, 8) + le(claimed_rows != 0 ? claimed_rows : (uint64_t) rows, 8);
160 head += le(gguf_type_id(f.name), 4) + le(0ull, 8);
161 while (head.size() % 32) head += '\0';
162 out.write(head.data(), (std::streamsize) head.size());
163 std::vector<uint8_t> r(f.row_bytes);
164 for (uint32_t i = 0; i < rows; ++i) {
165 expected_row(i, f.row_bytes, r.data());
166 out.write((const char*) r.data(), (std::streamsize) f.row_bytes);
167 }
168 out.close();
169 return out ? path : std::string();
170}
171
172void format_round_trip(const std::string& dir, const k::PleFormatInfo& f, uint32_t rows) {
173 CHECK(gguf_type_id(f.name) != 0xFFFFFFFFu, "%s: no GGUF type id in this test", f.name);

Callers 5

format_round_tripFunction · 0.85
bad_sizes_are_refusedFunction · 0.85
mainFunction · 0.85

Calls 10

strFunction · 0.85
memcpyFunction · 0.85
gguf_type_idFunction · 0.85
expected_rowFunction · 0.85
leFunction · 0.50
stringFunction · 0.50
sizeMethod · 0.45
writeMethod · 0.45
dataMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected