okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1#include "strata/core/latent_dense.hpp" 2 3#include <cerrno> 4#include <cstdio> 5#include <cstring> 6 7#if defined(_WIN32 ) 8#include <windows.h> 9#else 10#include <fcntl.h> 11#include <sys/mman.h> 12#include <sys/stat.h> 13#include <unistd.h> 14#endif 15 16namespace strata::core { 17 18#if defined(_WIN32 ) 19// Windows: CreateFileA + CreateFileMappingA + MapViewOfFile. Same contract as the POSIX path: 20// strict size check, read-only mapping, unmap on close. The mapping handle keeps the section 21// alive after the file handle closes. 22static void win_err (std::string & err ,const std::string & path ,const char * what ) { 23char buf [64 ]; 24 std::snprintf (buf ,sizeof (buf )," (win32 error %lu)" , (unsigned long )GetLastError ()); 25err = path + ": " + what + buf ; 26} 27#endif 28 29LatentDense ::~LatentDense () {close (); } 30 31void LatentDense ::close () { 32#if defined(_WIN32 ) 33if (base_ )UnmapViewOfFile (base_ ); 34if (map_ )CloseHandle (map_ ); 35#else 36if (base_ )munmap (base_ ,size_ ); 37#endif 38base_ = nullptr ;map_ = nullptr ; 39size_ = 0 ;bytes_ = 0 ; 40} 41 42bool LatentDense ::open (const std::string & path ,int64_t n_layers ,int64_t n_groups ,int64_t n_embd , 43int64_t latent_dim , std::string & err ) { 44close (); 45n_layers_ = n_layers ;n_groups_ = n_groups ;n_embd_ = n_embd ;latent_dim_ = latent_dim ; 46// Q4DU: STRATA_LATENT_DENSE_FMT=4 selects the Q4_0 pack (18 B/block), 8 or unset the 47// historical Q8_0 one (34 B/block). The file's size must then match the derived layout - 48// a mismatched file/env pair fails the open with both candidate sizes spelled out. 49fmt_ = 8 ; 50if (const char * f = ::getenv ("STRATA_LATENT_DENSE_FMT" )) { 51if (f [0 ]== '4' && f [1 ]== 0 )fmt_ = 4 ; 52else if (f [0 ]== '8' && f [1 ]== 0 )fmt_ = 8 ; 53else {err = path + ": STRATA_LATENT_DENSE_FMT must be 4 or 8 (got '" + std::string (f )+ "')" ;return false; } 54 } 55const size_t db = d_bytes (),ub = u_bytes (); 56const uint64_t want = (uint64_t )n_layers * (db + ub ); 57#if defined(_WIN32 ) 58HANDLE fh = CreateFileA (path .c_str (),GENERIC_READ ,FILE_SHARE_READ ,nullptr ,OPEN_EXISTING , 59FILE_ATTRIBUTE_NORMAL ,nullptr ); 60if (fh == INVALID_HANDLE_VALUE ) {win_err (err ,path ,"CreateFile" );return false; } 61LARGE_INTEGER fsz ; 62if (!GetFileSizeEx (fh ,& fsz )) {win_err (err ,path ,"GetFileSizeEx" );CloseHandle (fh );return false; } 63if ((uint64_t )fsz .QuadPart != want ) { 64const uint64_t alt = (uint64_t )n_layers * 65 ((size_t )n_groups_ * (size_t )latent_dim_ * (size_t ) (n_embd_ /32 )* (fmt_ == 4 ?34 :18 )+ 66 (size_t )n_groups_ * (size_t )n_embd_ * (size_t ) (latent_dim_ /32 )* (fmt_ == 4 ?34 :18 )); 67err = path + ": size " + std::to_string ((long long )fsz .QuadPart )+ " != " + std::to_string (want )+ 68" (fmt " + std::to_string (fmt_ )+ " wants " + std::to_string (want )+ 69"; the other fmt wants " + std::to_string ((long long )alt )+ ")" ; 70CloseHandle (fh );return false; 71 } 72HANDLE mh = CreateFileMappingA (fh ,nullptr ,PAGE_READONLY ,0 ,0 ,nullptr ); 73if (!mh ) {win_err (err ,path ,"CreateFileMapping" );CloseHandle (fh );return false; } 74CloseHandle (fh );// the mapping object holds the section 75void * p = MapViewOfFile (mh ,FILE_MAP_READ ,0 ,0 ,0 ); 76if (!p ) {win_err (err ,path ,"MapViewOfFile" );CloseHandle (mh );return false; } 77map_ = mh ; 78#else 79int fd = ::open (path .c_str (),O_RDONLY ); 80if (fd < 0 ) {err = path + ": " + std::strerror (errno );return false; } 81struct stat st ; 82if (fstat (fd ,& st )!= 0 ) {err = path + ": fstat: " + std::strerror (errno ); ::close (fd );return false; } 83if ((uint64_t )st .st_size != want ) {// strict: the env's format and the file's size disagree. 84// NO silent fallback - a misread layout would produce garbage answers. Name both 85// candidate sizes so the operator sees exactly which format the file actually is. 86const uint64_t alt = (uint64_t )n_layers * 87 ((size_t )n_groups_ * (size_t )latent_dim_ * (size_t ) (n_embd_ /32 )* (fmt_ == 4 ?34 :18 )+ 88 (size_t )n_groups_ * (size_t )n_embd_ * (size_t ) (latent_dim_ /32 )* (fmt_ == 4 ?34 :18 )); 89err = path + ": size " + std::to_string ((long long )st .st_size )+ " != " + std::to_string (want )+ 90" (fmt " + std::to_string (fmt_ )+ " wants " + std::to_string (want )+ 91"; the other fmt wants " + std::to_string ((long long )alt )+ ")" ; 92 ::close (fd );return false; 93 } 94void * p = mmap (nullptr , (size_t )want ,PROT_READ ,MAP_PRIVATE ,fd ,0 ); 95 ::close (fd ); 96if (p == MAP_FAILED ) {err = path + ": mmap: " + std::strerror (errno );return false; } 97#endif 98base_ = (uint8_t * )p ;size_ = (size_t )want ;bytes_ = want ; 99return true; 100} 101 102const uint8_t * LatentDense ::d (int64_t layer )const { 103if (!base_ || layer < 0 || layer >=n_layers_ )return nullptr ; 104return base_ + (size_t )layer * (d_bytes ()+ u_bytes ()); 105} 106 107const uint8_t * LatentDense ::u (int64_t layer )const { 108if (!base_ || layer < 0 || layer >=n_layers_ )return nullptr ; 109return d (layer )+ d_bytes (); 110} 111 112LatentA ::~LatentA () {close (); } 113void LatentA ::close () { 114#if defined(_WIN32 ) 115if (base_ )UnmapViewOfFile (base_ ); 116if (map_ )CloseHandle (map_ ); 117#else 118if (base_ )munmap (base_ ,size_ ); 119#endif 120base_ = nullptr ;map_ = nullptr ;size_ = 0 ; 121} 122bool LatentA ::open (const std::string & path ,int64_t n_layers ,int64_t n_expert ,int64_t blob_bytes , std::string & err ) { 123close (); 124n_layers_ = n_layers ;n_expert_ = n_expert ;blob_bytes_ = blob_bytes ; 125const uint64_t want = (uint64_t )n_layers * (uint64_t )n_expert * (uint64_t )blob_bytes ; 126#if defined(_WIN32 ) 127HANDLE fh = CreateFileA (path .c_str (),GENERIC_READ ,FILE_SHARE_READ ,nullptr ,OPEN_EXISTING , 128FILE_ATTRIBUTE_NORMAL ,nullptr ); 129if (fh == INVALID_HANDLE_VALUE ) {win_err (err ,path ,"CreateFile" );return false; } 130LARGE_INTEGER fsz ; 131if (!GetFileSizeEx (fh ,& fsz )) {win_err (err ,path ,"GetFileSizeEx" );CloseHandle (fh );return false; } 132if ((uint64_t )fsz .QuadPart != want ) { 133err = path + ": size " + std::to_string ((long long )fsz .QuadPart )+ " != " + std::to_string (want ); 134CloseHandle (fh );return false; 135 } 136HANDLE mh = CreateFileMappingA (fh ,nullptr ,PAGE_READONLY ,0 ,0 ,nullptr ); 137if (!mh ) {win_err (err ,path ,"CreateFileMapping" );CloseHandle (fh );return false; } 138CloseHandle (fh ); 139void * p = MapViewOfFile (mh ,FILE_MAP_READ ,0 ,0 ,0 ); 140if (!p ) {win_err (err ,path ,"MapViewOfFile" );CloseHandle (mh );return false; } 141map_ = mh ; 142#else 143int fd = ::open (path .c_str (),O_RDONLY ); 144if (fd < 0 ) {err = path + ": " + std::strerror (errno );return false; } 145struct stat st ; 146if (fstat (fd ,& st )!= 0 || (uint64_t )st .st_size != want ) { 147err = path + ": size " + std::to_string ((long long )st .st_size )+ " != " + std::to_string (want ); 148 ::close (fd );return false; 149 } 150void * p = mmap (nullptr , (size_t )want ,PROT_READ ,MAP_PRIVATE ,fd ,0 ); 151 ::close (fd ); 152if (p == MAP_FAILED ) {err = path + ": mmap: " + std::strerror (errno );return false; } 153#endif 154base_ = (uint8_t * )p ;size_ = (size_t )want ; 155return true; 156} 157const uint8_t * LatentA ::blob (int64_t layer ,int64_t expert )const { 158if (!base_ || layer < 0 || layer >=n_layers_ || expert < 0 || expert >=n_expert_ )return nullptr ; 159return base_ + ((size_t )layer * (size_t )n_expert_ + (size_t )expert )* (size_t )blob_bytes_ ; 160} 161 162}// namespace strata::core