// Local checkpoint for the recurrent LM after the common Savannah prefix. #pragma once #include "core.hpp" #include #include #include #include #include #include #include #include #ifdef __ANDROID__ #include #endif namespace prefix { constexpr int rows=184; constexpr int contexts=16, width=32, capacity=768, hidden=1024; constexpr size_t prefix_values=rows*hidden; inline std::string file_stamp(const fs::path& file) { const auto path=fs::canonical(file); struct stat st{}; require(stat(path.c_str(),&st)==0,"stat "+path.string()); #ifdef __APPLE__ auto modified=st.st_mtimespec, changed=st.st_ctimespec; #else auto modified=st.st_mtim, changed=st.st_ctim; #endif std::ostringstream out; out<(&open_graph),&binary) && dladdr(reinterpret_cast(&TfLiteVersion),&library),"identify LM runtime"); struct utsname u{}; require(uname(&u)==0,"identify local host"); std::ostringstream out; out<<"ZONOS-PREFIX-1\nLM C signature runner, fp32, 4 threads\n"<; inline void append(Bytes& bytes,const void* data,size_t n) { const auto* p=static_cast(data); bytes.insert(bytes.end(),p,p+n); } // Store the actual binding and prefix bytes, so matching does not rely on a hash. inline Bytes key(const fs::path& lm,const Floats& prompt,const Floats& cos,const Floats& sin) { require(prompt.size()>=prefix_values,"short prompt for prefix preparation"); auto stamp=binding(lm); Bytes result(stamp.begin(),stamp.end()); append(result,prompt.data(),prefix_values*sizeof(float)); append(result,cos.data(),rows*16*sizeof(float)); append(result,sin.data(),rows*16*sizeof(float)); return result; } inline void save(const fs::path& path,Bytes bytes,const Floats& kv,const Floats& gdn,const Floats& conv) { for (int h=0;h=0,"create local prefix cache next to "+path.string()); auto* f=fdopen(fd,"wb"); if (!f) { close(fd); unlink(tmp.c_str()); throw std::runtime_error("open cache stream"); } bool ok=fwrite(bytes.data(),1,bytes.size(),f)==bytes.size() && fflush(f)==0 && fsync(fd)==0; if (fclose(f)!=0 || !ok || rename(tmp.c_str(),path.c_str())!=0) { unlink(tmp.c_str()); throw std::runtime_error("write prefix cache "+path.string()); } } inline bool restore(const fs::path& path,const Bytes& expected,Floats& kv,Floats& gdn,Floats& conv) { size_t size=expected.size()+(contexts*rows*width+gdn.size()+conv.size())*sizeof(float)+sizeof(uint32_t); // Open first so a concurrent rename cannot substitute a different file between size check and read. std::ifstream f(path,std::ios::binary|std::ios::ate); if (!f || f.tellg()!=static_cast(size)) return false; f.seekg(0); Bytes bytes(size); if (!f.read(reinterpret_cast(bytes.data()),size) || !std::equal(expected.begin(),expected.end(),bytes.begin())) return false; uint32_t stored; std::memcpy(&stored,bytes.data()+size-sizeof(stored),sizeof(stored)); if (crc32(0,bytes.data(),size-sizeof(stored))!=stored) return false; const auto* next=bytes.data()+expected.size(); auto copy=[&](float* dst,size_t values) { std::memcpy(dst,next,values*sizeof(float)); next+=values*sizeof(float); }; for (int h=0;h