okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
2.0 KiB48 linesraw
1// Writes what the loader makes of tensors of a GGUF (src/tensor_convert.hpp): for each tensor named on the command line, the
2// converted tensor's values as float32 to OUTDIR/<name>.f32 plus a line "name type_after nbytes" on stdout. tests/split/convert_check.py
3// compares those with gguf-py's own dequantization of the same tensors in the file. CPU only.
4//   bin/convert_dump MODEL.gguf OUTDIR name [name ...]      (a name of "all" dumps every tensor the loader converted)
5#include <cstdio>
6#include <cstdlib>
7#include <string>
8#include <vector>
9
10#include "../src/gguf.hpp"
11#include "../src/tensor_convert.hpp"
12
13int main(int argc, char** argv) {
14    if (argc < 4) {
15        std::fprintf(stderr, "usage: convert_dump MODEL.gguf OUTDIR name [name ...]\n");
16        return 2;
17    }
18    try {
19        fnx::Gguf g(argv[1]);
20        std::printf("converted %d\n", g.converted());
21        std::vector<std::string> names;
22        for (int i = 3; i < argc; ++i) {
23            if (std::string(argv[i]) == "all") {
24                for (const auto& kv : g.tensors())
25                    if (fnx::conv::wanted_type(kv.first) != 0xFFFFFFFFu) names.push_back(kv.first);
26            } else {
27                names.push_back(argv[i]);
28            }
29        }
30        for (const std::string& n : names) {
31            const fnx::Tensor& t = g.t(n);
32            size_t cnt = 1;
33            for (int d = 0; d < 4; ++d) cnt *= (size_t) t.ne[d];
34            std::vector<float> f(cnt);
35            fnx::conv::dequantize(t.type, t.data, cnt, f.data());
36            const std::string path = std::string(argv[2]) + "/" + n + ".f32";
37            FILE* fp = std::fopen(path.c_str(), "wb");
38            if (!fp) throw std::runtime_error("cannot write " + path);
39            std::fwrite(f.data(), 4, cnt, fp);
40            std::fclose(fp);
41            std::printf("%s %u %zu\n", n.c_str(), t.type, t.nbytes);
42        }
43    } catch (const std::exception& e) {
44        std::fprintf(stderr, "convert_dump: %s\n", e.what());
45        return 1;
46    }
47    return 0;
48}