#include "core.hpp" constexpr int P=768, C=10, V=4098, STOP=4096, PAD=4097, H=1024; using Rows=std::vector; static_assert(sizeof(Row)==10*sizeof(int32_t)); class Generation { const fs::path shared; Floats embeds, text_pad, cosines, sines; Floats kv=Floats(4*3072*32), gdn=Floats(9*4*128*128), conv=Floats(9*3*1536); Floats new_kv=Floats(4*4*32), mask=Floats(P+1); std::unique_ptr graph; std::ifstream replay_logits, replay_x; int pos=0; public: Floats logits=Floats(C*V); Generation(const fs::path& s, const fs::path& lm, const fs::path& replay):shared(s) { embeds=read_file(s/"embeddings.f32",C*V*H); text_pad=read_file(s/"text_pad.f32",H); if (replay.empty()) { graph=open_graph(lm,false); cosines=read_file(s/"lm_cos.f32",P*16); sines=read_file(s/"lm_sin.f32",P*16); } else { replay_logits.open(replay/"logits.f32",std::ios::binary); replay_x.open(replay/"rows.f32",std::ios::binary); require(replay_logits && replay_x,"open replay"); } } void step(const float* x) { require(posrun({{x,H},{cosines.data()+pos*16,16},{sines.data()+pos*16,16}, view(mask),view(kv),view(gdn),view(conv)}, {&logits,&new_kv,&gdn,&conv}); for (int h=0; h<4; ++h) for (int j=0; j<4; ++j) std::copy_n(new_kv.data()+(h*4+j)*32,32,kv.data()+((h*4+j)*P+pos)*32); } else { float check[H]; require(bool(replay_x.read(reinterpret_cast(check),sizeof(check))),"missing replay row"); require(std::memcmp(check,x,sizeof(check))==0,"replay input row mismatch at "+std::to_string(pos)); require(bool(replay_logits.read(reinterpret_cast(logits.data()),logits.size()*4)), "missing logits"); } ++pos; } void feed(const Row& row) { Floats x(H); for (int cb=0; cb order(STOP+1); Floats x(STOP+1), weights(STOP+1); for (int cb=0; cb recent{}; if (cb<6) { int begin=std::max(0,static_cast(generated.size())-96); for (size_t r=begin; r0 ? value/2.4f : value*2.4f; x[id]=value/0.75f; } std::iota(order.begin(),order.end(),0); auto higher=[&](int a,int b){return x[a]==x[b] ? ax[b];}; std::nth_element(order.begin(),order.begin()+79,order.end(),higher); float cutoff=x[order[79]]; auto end=std::partition(order.begin(),order.end(),[&](int i){return x[i]>=cutoff;}); std::sort(order.begin(),end,higher); double total=0; for (auto it=order.begin(); it!=end; ++it) { weights[*it]=std::exp(x[*it]-x[order.front()]); total+=weights[*it]; } double prefix=0; auto kept_end=end; double kept_sum=0; for (auto it=order.begin(); it!=end; ++it) { int id=*it; float probability=static_cast(weights[id]/total); prefix+=probability; if (static_cast(prefix)-probability>0.69f) { kept_end=it; break; } weights[id]=probability; kept_sum+=probability; } float best=-1; for (auto it=order.begin(); it!=kept_end; ++it) { int id=*it; float normalized=weights[id]/static_cast(kept_sum); float race=normalized/draws[cb*V+id]; if (race>best || (race==best && id(take/"prompt.f32"); require(!prompt.empty() && prompt.size()%H==0 && prompt.size()/H(take/"silence.i32"); Generation gen(shared,lm,replay); for (size_t i=0; i(draws.data()),draws.size()*4)),"draw stream exhausted"); auto accepted=sample(gen.logits,rows,draws); rows.push_back(accepted); for (int j=C-1; j>=0 && eos<0; --j) { if (accepted[j]==STOP) { eos=std::max(0,n-j); countdown=C+1; } } if (countdown>0 && --countdown==0) break; gen.feed(accepted); } gen.finish(); int prefill=silence.size(), target=prefill+eos; for (int r=0; r(rows.size()); ++r) for (int j=0; j(silence.size())-C+1); if (target<=prefill) return {}; require(target<=150,"BucketExceeded: "+std::to_string(target)+" frames"); Rows codes(target); for (int t=0; t=0 && codes[t][j](shared/"books.f32",C*STOP*8); Floats vectors(150*80); for (size_t t=0; t inputs; inputs.push_back(std::move(vectors)); for (auto name:{"dec_cos.f32","dec_sin.f32"}) inputs.push_back(read_file(shared/name,2400*32)); for (int i=0; i<5; ++i) { int length=150<(shared/("window"+std::to_string(i)+".f32"),length*length)); } for (int i=0; i<5; ++i) { inputs.emplace_back(150< views; for (auto& input:inputs) views.push_back(view(input)); head.resize(2400*2948); open_graph(model,true)->run(views,{&head}); } else { require(vectors==read_file(replay/"cv.f32",150*80),"replay codec input mismatch"); head=read_file(replay/"head.f32",2400*2948); } return decode_audio(head,codes.size(),shared); } int main(int argc,char** argv) { try { require(argc==6 || argc==7,"usage: zonos_host KIT TAG LM_GRAPH DEC_GRAPH OUTPUT [REPLAY_TAKE_DIR]"); fs::path kit=argv[1], dest=argv[5], replay=argc==7 ? argv[6] : ""; auto shared=kit/"shared", take=kit/"takes"/argv[2]; require(!fs::exists(dest),"output directory already exists"); std::ios::sync_with_stdio(false); std::cin.tie(nullptr); const auto start=std::chrono::steady_clock::now(); auto codes=generate(take,shared,argv[3],replay); auto wave=decode(codes,shared,argv[4],replay); fs::create_directories(dest); write_file(dest/"codes.i32",codes); save_audio(dest,wave); double seconds=std::chrono::duration(std::chrono::steady_clock::now()-start).count(); std::cerr<<"TAKE "<