okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/artifact/gguf_reader.cpp - the `strata-gguf` CLI. The reader itself is header-only in 2// include/strata/artifact/gguf_reader.hpp; this file is just the entry point, so it cannot drift 3// from the library it exercises. 4#include "strata/artifact/gguf_reader.hpp" 5 6#ifndef STRATA_GGUF_MAIN_DISABLED 7int main (int argc ,char ** argv ) { 8if (argc < 2 ) { 9 std::printf ("usage: gguf_reader <file.gguf> [--check]\n" ); 10return 2 ; 11 } 12const bool check = (argc > 2 && std::string (argv [2 ])== "--check" ); 13try { 14 strata::GgufFile g (argv [1 ]); 15 std::printf ("%s\n" ,argv [1 ]); 16 std::printf (" version %u tensors %zu metadata %zu data_start %llu size %llu\n" ,g .version (), 17g .tensors ().size (),g .metadata ().size (), (unsigned long long )g .data_start (), 18 (unsigned long long )g .file_size ()); 19 20if (check ) { 21// Independent expectations, all established by earlier phases. 22size_t n_blk = 0 ; 23for (const auto & t :g .tensors ()) 24if (t .name .rfind ("blk." ,0 )== 0 )++ n_blk ; 25 std::printf (" blk.* tensors %zu globals %zu\n" ,n_blk ,g .tensors ().size ()- n_blk ); 26 std::printf (" Q2_0 %llu BF16 %llu F32 %llu\n" , (unsigned long long )g .count_type ("Q2_0" ), 27 (unsigned long long )g .count_type ("BF16" ), (unsigned long long )g .count_type ("F32" )); 28 29// Every tensor's byte size must land inside the gap to the next tensor's offset, since 30// GGUF aligns every tensor. That is the same bracket test tools/verify_q2_0_geometry.py 31// uses, and it validates block_geometry() against the file rather than asserting it. 32 std::vector < const strata::TensorInfo *> ordered ; 33for (const auto & t :g .tensors ())ordered .push_back (& t ); 34 std::sort (ordered .begin (),ordered .end (), [](auto a ,auto b ) {return a -> offset < b -> offset ; }); 35size_t bad = 0 ,unknown = 0 ; 36for (size_t i = 0 ;i < ordered .size ();++ i ) { 37const auto * t = ordered [i ]; 38int el = 0 ,by = 0 ; 39if (!strata::block_geometry (t -> type ,el ,by )) { 40++ unknown ; 41continue ; 42 } 43if (t -> elements () % (uint64_t )el ) { 44++ bad ; 45continue ; 46 } 47const uint64_t nb = t -> elements () / (uint64_t )el * (uint64_t )by ; 48// The bracket is on the GAP to the next tensor's offset, not on that offset itself - 49// offsets are relative to data_start, so the gap is a difference. Comparing against 50// the raw offset flagged almost every tensor (213/214) while the same test in Python 51// (tools/verify_q2_0_geometry.py) passed. 52const uint64_t end = (i + 1 < ordered .size ()) ?ordered [i + 1 ]-> offset - t -> offset 53 :g .file_size ()- g .data_start ()- t -> offset ; 54if (!(nb <=end && end < nb + 32 ))++ bad ; 55 } 56 std::printf (" geometry bracket: %zu out of range, %zu types unknown\n" ,bad ,unknown ); 57const std::string err = strata::check_architecture (g ); 58 std::printf (" architecture guard: %s\n" ,err .empty () ?"PASS" : ("FAIL - " + err ).c_str ()); 59if (bad )return 1 ; 60 } 61return 0 ; 62 }catch (const std::exception & e ) { 63 std::printf ("ERROR: %s\n" ,e .what ()); 64return 1 ; 65 } 66} 67#endif // STRATA_GGUF_MAIN_DISABLED