okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f5aa356f1
main
1// src/artifact/gguf_reader_test.cpp - the reader refuses what it cannot arbitrate (synthetic GGUF, no model, 2// no GPU). 3// 4// 1. two tensors with distinct names: the file opens and find() sees both; 5// 2. the same name twice: refused at open, and the error names the tensor and the file - GGUF has no 6// index to say which of the two a name means, and find() is first-match, so the alternative is a 7// lookup that silently picks by position. 8// 3. no tensors, and the file ends at its header, before the 32-byte-aligned data start: the file opens, 9// with data_start() clamped to the file size. A split model's first shard can hold metadata only and 10// end unpadded (unsloth's UD-Q5_K_XL shard 1 ends 6 bytes before its aligned data start; llama.cpp's 11// gguf-py reads it); nothing is ever read from its data section. 12// 4. one tensor, and the file ends at its header: still refused at open ("data section starts past EOF"). 13// 14// The fixture is a minimal GGUF v3 written here (header, no metadata, two F32[8] tensors, 32-byte 15// alignment), so the test needs neither gguf-py nor a shard. The file is closed before it is removed: 16// on Windows an open mapping keeps it. 17#include "strata/artifact/gguf_reader.hpp" 18 19#include <cstdint> 20#include <cstdio> 21#include <cstring> 22#include <filesystem> 23#include <fstream> 24#include <string> 25#include <vector> 26 27namespace { 28int g_fail = 0 ; 29void check (bool ok ,const char * what ) { 30 std::printf (" %-66s %s\n" ,what ,ok ?"ok" :"FAIL" ); 31if (!ok )++ g_fail ; 32} 33 34template < class T > void put (std::vector < uint8_t >& b ,T v ) { 35const auto n = b .size (); 36b .resize (n + sizeof (T )); 37 std::memcpy (b .data ()+ n ,& v ,sizeof (T )); 38} 39void put_str (std::vector < uint8_t >& b ,const std::string & s ) { 40put < uint64_t > (b ,s .size ()); 41b .insert (b .end (),s .begin (),s .end ()); 42} 43 44// A GGUF v3 file of F32[8] tensors named `names`, laid out 32 bytes apart from a 32-byte-aligned data start. 45// `header_only`: the file ends right after the tensor directory - no alignment padding, no data. 46std::filesystem::path write_gguf (const std::vector < std::string >& names ,bool header_only = false) { 47 std::vector < uint8_t > b ; 48put < uint32_t > (b ,0x46554747u );// "GGUF" 49put < uint32_t > (b ,3 ); 50put < uint64_t > (b ,names .size ());// n_tensors 51put < uint64_t > (b ,0 );// n_kv 52for (size_t i = 0 ;i < names .size ();++ i ) { 53put_str (b ,names [i ]); 54put < uint32_t > (b ,1 );// n_dims 55put < uint64_t > (b ,8 ); 56put < uint32_t > (b ,0 );// F32 57put < uint64_t > (b ,32 * i );// offset from data_start 58 } 59if (!header_only )b .resize ((b .size ()+ 31 ) /32 * 32 + 32 * names .size (),0 ); 60const auto path = std::filesystem::temp_directory_path () /"strata_gguf_reader_test.gguf" ; 61 std::ofstream (path , std::ios::binary ).write (reinterpret_cast < const char *> (b .data ()), (std::streamsize )b .size ()); 62return path ; 63} 64}// namespace 65 66int main () { 67 std::printf ("gguf_reader_test\n" ); 68 { 69const auto path = write_gguf ({"blk.0.attn_q.weight" ,"blk.0.attn_k.weight" }); 70 std::string err ; 71size_t n = 0 ; 72bool both = false; 73try { 74 strata::GgufFile g (path .string ()); 75n = g .tensors ().size (); 76both = g .find ("blk.0.attn_q.weight" )&& g .find ("blk.0.attn_k.weight" ); 77 }catch (const std::exception & e ) { 78err = e .what (); 79 } 80 std::filesystem::remove (path ); 81check (err .empty ()&& n == 2 && both ,"distinct names: the file opens and find() sees both tensors" ); 82 } 83 { 84const auto path = write_gguf ({"blk.0.attn_q.weight" ,"blk.0.attn_q.weight" }); 85 std::string err ; 86try { 87 strata::GgufFile g (path .string ()); 88 }catch (const std::exception & e ) { 89err = e .what (); 90 } 91 std::filesystem::remove (path ); 92check (!err .empty (),"the same name twice: refused at open" ); 93check (err .find ("blk.0.attn_q.weight" )!= std::string::npos ," the error names the tensor" ); 94check (err .find (path .filename ().string ())!= std::string::npos ," the error names the file" ); 95if (!err .empty ()) std::printf (" (%s)\n" ,err .c_str ()); 96 } 97 { 98const auto path = write_gguf ({}, true);// 24 bytes: the aligned data start (32) is past EOF 99 std::string err ; 100uint64_t size = 0 ,start = 1 ; 101size_t n = 1 ; 102try { 103 strata::GgufFile g (path .string ()); 104size = g .file_size (); 105start = g .data_start (); 106n = g .tensors ().size (); 107 }catch (const std::exception & e ) { 108err = e .what (); 109 } 110 std::filesystem::remove (path ); 111check (err .empty ()&& n == 0 ,"no tensors, ends before the aligned data start: the file opens" ); 112check (err .empty ()&& start == size ," data_start() is the file size (nothing to read past it)" ); 113if (!err .empty ()) std::printf (" (%s)\n" ,err .c_str ()); 114 } 115 { 116const auto path = write_gguf ({"blk.0.attn_q.weight" }, true); 117 std::string err ; 118try { 119 strata::GgufFile g (path .string ()); 120 }catch (const std::exception & e ) { 121err = e .what (); 122 } 123 std::filesystem::remove (path ); 124check (err .find ("data section starts past EOF" )!= std::string::npos , 125"one tensor, ends before the aligned data start: refused at open" ); 126 } 127 std::printf (g_fail ?"gguf_reader_test: %d FAILED\n" :"gguf_reader_test: all passed\n" ,g_fail ); 128return g_fail ?1 :0 ; 129}