Download cpu/fit.cpp from ScAGI/agi-counts-fast-trainers: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/ScAGI/agi-counts-fast-trainers/resolve/main/cpu/fit.cpp
- Command line
-
hf download hf://ScAGI/agi-counts-fast-trainers/cpu/fit.cpp
-
curl -L -o fit.cpp https://huggingface.co/ScAGI/agi-counts-fast-trainers/resolve/main/cpu/fit.cpp
4.23 kB
| using U=uint64_t;using I=uint32_t;using Bytes=std::vector<unsigned char>; | |
| constexpr unsigned S=2048,W=4; | |
| inline U hash(U x){x^=x>>30;x*=0xbf58476d1ce4e5b9ULL;x^=x>>27;x*=0x94d049bb133111ebULL;return x^(x>>31);} | |
| struct alignas(32) Row { U key[4]{}; unsigned freq[2]{}; unsigned used=0, parity=0; }; | |
| struct Result {Bytes bytes; unsigned entries=0;}; | |
| inline void put(Bytes &b,U x,int n){for(int i=0;i<n;i++)b.push_back(static_cast<unsigned char>(x>>(i*8)));} | |
| inline void emit(Result&r,unsigned d,U k,const I*c){ | |
| unsigned used=0,total=0;for(unsigned y=0;y<256;y++){used+=c[y]!=0;total+=c[y];} | |
| r.entries++;put(r.bytes,d,1);put(r.bytes,k,8);put(r.bytes,total,4);put(r.bytes,used,2); | |
| for(unsigned y=0;y<256;y++)if(c[y]){put(r.bytes,y,1);put(r.bytes,c[y],4);} | |
| } | |
| void hist(const Bytes& in,Result &a,Result &b){ | |
| auto big=std::make_unique<I[]>(65536);std::array<I,256> one{};std::array<unsigned,256> first; first.fill(~0u); | |
| for(unsigned i=0;i<in.size();i++)++one[in[i]]; | |
| for(unsigned i=1;i<in.size();i++) {unsigned x=in[i-1];++big[x*256+in[i]];if(first[x]==~0u)first[x]=i;} | |
| emit(a,0,0,one.data()); | |
| std::array<unsigned,256> order{};for(unsigned x=0;x<256;x++)order[x]=x; | |
| std::sort(order.begin(),order.end(),[&](unsigned x,unsigned y){unsigned bx=hash(x)&2047,by=hash(y)&2047;return bx==by?first[x]<first[y]:bx<by;}); | |
| for(unsigned x:order)if(first[x]!=~0u)emit(b,1,x,big.get()+x*256); | |
| } | |
| template<int Depth> inline U roll(U k,unsigned v){if constexpr(Depth==8)return (k<<8)|v;else return ((k<<8)|v)&((1ULL<<(Depth*8))-1);} | |
| inline unsigned hit(const Row&r,U k){ | |
| const auto u=_mm256_cmpeq_epi64(_mm256_load_si256(reinterpret_cast<const __m256i*>(r.key)),_mm256_set1_epi64x(k)); | |
| return unsigned(_mm256_movemask_pd(_mm256_castsi256_pd(u)))&r.used; | |
| } | |
| inline unsigned admit(Row&r,U k,unsigned q,unsigned hitmask,I*c){ | |
| unsigned f=r.freq[q];if(f<65535)r.freq[q]=++f; | |
| if(hitmask)return __builtin_ctz(hitmask); | |
| unsigned slot; | |
| if(r.used<15)slot=__builtin_ctz((~r.used)&15u); | |
| else { | |
| if(f<=r.freq[q^1u])return 4; | |
| unsigned m=(q?~r.parity:r.parity)&15u;if(!m)return 4; | |
| slot=__builtin_ctz(m); | |
| std::memset(c+slot*256,0,1024); | |
| } | |
| r.used|=1u<<slot;r.parity=(r.parity&~(1u<<slot))|(q<<slot);r.key[slot]=k; | |
| return slot; | |
| } | |
| template<int Depth> void fit(const Bytes& in,Result &r){ | |
| if(in.size()<=Depth)return; | |
| auto rows=std::make_unique<Row[]>(S); | |
| auto counts=std::make_unique<I[]>(S*W*256); | |
| U k=0;for(int i=0;i<Depth;i++)k=(k<<8)|in[i]; | |
| for(unsigned i=Depth;i<in.size();i++){ | |
| unsigned y=in[i], h=hash(k)&4095, q=h>>11,set=h&2047; | |
| Row& rr=rows[set];unsigned m=hit(rr,k);I*c=counts.get()+set*1024; | |
| unsigned p=admit(rr,k,q,m,c); | |
| if(p<4)++c[p*256+y]; | |
| k=roll<Depth>(k,y); | |
| } | |
| r.bytes.reserve(600000); | |
| for(unsigned j=0;j<S;j++)for(unsigned p=0;p<4;p++)if(rows[j].used&(1u<<p))emit(r,Depth,rows[j].key[p],counts.get()+j*1024+p*256); | |
| } | |
| int main(int argc,char**argv){ | |
| if(argc!=3)return 2; | |
| auto t=std::chrono::steady_clock::now(); | |
| std::ifstream f(argv[1],std::ios::binary|std::ios::ate);if(!f||f.tellg()<1||f.tellg()>2147483000)return 3; | |
| Bytes in(f.tellg());f.seekg(0);f.read(reinterpret_cast<char*>(in.data()),in.size());if(!f)return 3; | |
| Result a[9]; | |
| std::thread z0([&]{hist(in,a[0],a[1]);}); | |
| std::thread z2([&]{fit<2>(in,a[2]);}); | |
| std::thread z3([&]{fit<3>(in,a[3]);}); | |
| std::thread z4([&]{fit<4>(in,a[4]);}); | |
| std::thread z5([&]{fit<5>(in,a[5]);}); | |
| std::thread z6([&]{fit<6>(in,a[6]);}); | |
| std::thread z7([&]{fit<7>(in,a[7]);}); | |
| fit<8>(in,a[8]); | |
| for(auto* j:{&z0,&z2,&z3,&z4,&z5,&z6,&z7}) j->join(); | |
| Bytes b={'C','N','T','1'};unsigned rows=0;for(auto& r:a)rows+=r.entries; | |
| put(b,rows,4); | |
| std::ofstream o(argv[2],std::ios::binary);o.write(reinterpret_cast<const char*>(b.data()),b.size()); | |
| for(auto& r:a)o.write(reinterpret_cast<const char*>(r.bytes.data()),r.bytes.size());o.close(); | |
| std::cout<<"wall_s="<<std::chrono::duration<double>(std::chrono::steady_clock::now()-t).count()<<" bytes="<<in.size()<<" contexts="<<rows<<'\n'; | |
| return o?0:4; | |
| } | |