Archived
core/format, tools: GGUF Q8_0, Q4_K and Q6_K tensors import as their planes -- rad-convert writes them without a recipe
A GGUF tensor of block type Q8_0, Q4_K or Q6_K could only be dequantised, so rad-convert reached it through a recipe rule and re-quantised what was already quantised. ckpt_resolve now states the planes libquant's ggml quantiser writes for the same blocks and marks the weight imported, and ckpt_plane_rows splits each row's blocks into them bit for bit. The split is core/format/ggml_planes.h, the reader's copy of lq_ggml's unpack functions, since the core cannot call libquant; a type there is one table row, its planes and its split. Pieces of one block type joined along rows import, an expert's slices of a stacked tensor included; mixed types and column joins keep a GGUF reason. The weight is not trivial, so the loader refuses it with the hint to serve the container rad-convert writes, and ckpt_rows_f32 still dequantises it for a recipe. A rule that declines such a weight is an error in rad-convert, since keeping its planes would hide that the rule did not apply (spec.md §4.4). Tests: quant_ggml_test holds every type of the reader's table to the quantiser's encoding, checkpoint_test decodes the planes of Q8_0, Q4_K (one-block rows and a row join) and Q6_K against the block definition and holds an expert's gate|up, two slices of stacked Q4_K tensors, to the same blocks stored on their own, and convert_test runs a declining rule over a Q8_0 tensor, with the GGUF writer moved to tests/gguf_writer.h for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6f034d989f
commit
385318f81a
10 files changed
+602
-57
No files matched your search
@@ -0,0 +1,76 @@
|
||||
/* gguf_writer.h -- GGUF v3 files written by tests: header keys and tensors, the tensors' bytes as
|
||||
* the caller built them. */
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ggufw {
|
||||
|
||||
struct GKv {
|
||||
std::string key;
|
||||
uint32_t type; /* 4 u32, 6 f32, 8 string, 9 array of strings */
|
||||
double num = 0;
|
||||
std::vector<std::string> strs;
|
||||
};
|
||||
struct GTensor {
|
||||
std::string name;
|
||||
uint32_t type;
|
||||
std::vector<uint64_t> ne; /* fastest dimension first, as GGUF stores it */
|
||||
std::vector<uint8_t> bytes;
|
||||
};
|
||||
|
||||
inline void put_bytes(std::string* b, const void* p, size_t n) { b->append((const char*)p, n); }
|
||||
inline void put_str(std::string* b, const std::string& s) {
|
||||
const uint64_t n = s.size();
|
||||
put_bytes(b, &n, 8);
|
||||
b->append(s);
|
||||
}
|
||||
|
||||
/* A GGUF v3 file of `kvs` and `ts`, every tensor at a 32-byte aligned offset. */
|
||||
inline bool write_gguf(const std::string& path, const std::vector<GKv>& kvs,
|
||||
const std::vector<GTensor>& ts) {
|
||||
std::string b = "GGUF";
|
||||
const uint32_t ver = 3;
|
||||
const uint64_t nt = ts.size(), nk = kvs.size();
|
||||
put_bytes(&b, &ver, 4);
|
||||
put_bytes(&b, &nt, 8);
|
||||
put_bytes(&b, &nk, 8);
|
||||
for (const GKv& kv : kvs) {
|
||||
put_str(&b, kv.key);
|
||||
put_bytes(&b, &kv.type, 4);
|
||||
if (kv.type == 4) { const uint32_t v = (uint32_t)kv.num; put_bytes(&b, &v, 4); }
|
||||
else if (kv.type == 6) { const float v = (float)kv.num; put_bytes(&b, &v, 4); }
|
||||
else if (kv.type == 8) put_str(&b, kv.strs.at(0));
|
||||
else {
|
||||
const uint32_t et = 8;
|
||||
const uint64_t n = kv.strs.size();
|
||||
put_bytes(&b, &et, 4);
|
||||
put_bytes(&b, &n, 8);
|
||||
for (const std::string& s : kv.strs) put_str(&b, s);
|
||||
}
|
||||
}
|
||||
uint64_t off = 0;
|
||||
for (const GTensor& t : ts) {
|
||||
put_str(&b, t.name);
|
||||
const uint32_t nd = (uint32_t)t.ne.size();
|
||||
put_bytes(&b, &nd, 4);
|
||||
for (uint64_t d : t.ne) put_bytes(&b, &d, 8);
|
||||
put_bytes(&b, &t.type, 4);
|
||||
put_bytes(&b, &off, 8);
|
||||
off += (t.bytes.size() + 31) / 32 * 32;
|
||||
}
|
||||
while (b.size() % 32) b.push_back('\0');
|
||||
for (const GTensor& t : ts) {
|
||||
b.append((const char*)t.bytes.data(), t.bytes.size());
|
||||
while (b.size() % 32) b.push_back('\0');
|
||||
}
|
||||
FILE* f = std::fopen(path.c_str(), "wb");
|
||||
if (!f) return false;
|
||||
const bool ok = std::fwrite(b.data(), 1, b.size(), f) == b.size();
|
||||
return std::fclose(f) == 0 && ok;
|
||||
}
|
||||
|
||||
} /* namespace ggufw */
|
||||
Reference in new issue
Block a user