ZONOS3-Mini / host /prefix.hpp
mlboydaisuke's picture
Replace the Android runner with a faster one
9b130f3 verified
Raw History Blame Contribute Delete
4.88 kB
// Local checkpoint for the recurrent LM after the common Savannah prefix.
#pragma once
#include "core.hpp"
#include <cstdio>
#include <cstdlib>
#include <dlfcn.h>
#include <sstream>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <zlib.h>
#ifdef __ANDROID__
#include <sys/system_properties.h>
#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<<path.string()<<'\n'<<st.st_dev<<' '<<st.st_ino<<' '<<st.st_size<<' '
<<modified.tv_sec<<' '<<modified.tv_nsec<<' '<<changed.tv_sec<<' '<<changed.tv_nsec<<'\n';
return out.str();
}
extern "C" const char* TfLiteVersion();
inline std::string binding(const fs::path& lm) {
Dl_info binary{},library{};
require(dladdr(reinterpret_cast<const void*>(&open_graph),&binary) &&
dladdr(reinterpret_cast<const void*>(&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"<<rows<<'\n'<<TfLiteVersion()<<'\n'
<<u.sysname<<' '<<u.release<<' '<<u.machine<<' '<<u.nodename<<'\n';
#ifdef __ANDROID__
char value[PROP_VALUE_MAX]{};
for (auto key:{"ro.product.device","ro.soc.model","ro.build.fingerprint"}) {
__system_property_get(key,value);
out<<key<<'='<<value<<'\n';
}
#endif
out<<file_stamp(binary.dli_fname)<<file_stamp(library.dli_fname)<<file_stamp(lm);
return out.str();
}
using Bytes=std::vector<unsigned char>;
inline void append(Bytes& bytes,const void* data,size_t n) {
const auto* p=static_cast<const unsigned char*>(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<contexts;++h) append(bytes,kv.data()+h*capacity*width,rows*width*sizeof(float));
append(bytes,gdn.data(),gdn.size()*sizeof(float));
append(bytes,conv.data(),conv.size()*sizeof(float));
uint32_t crc=crc32(0,bytes.data(),bytes.size());
append(bytes,&crc,sizeof(crc));
// Publish a complete image with a single rename, including on concurrent preparation.
std::string tmp=path.string()+".XXXXXX";
int fd=mkstemp(tmp.data());
require(fd>=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<std::streamoff>(size)) return false;
f.seekg(0);
Bytes bytes(size);
if (!f.read(reinterpret_cast<char*>(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<contexts;++h) copy(kv.data()+h*capacity*width,rows*width);
copy(gdn.data(),gdn.size());
copy(conv.data(),conv.size());
return true;
}
} // namespace prefix