okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
11.2 KiB184 linesraw
1// Actual gated proposal packet contract, CPU-only; hand-specified expectations.
2// Includes immutable q bits, root rescue on all branches, NULL versus budget, and invalid packets.
3// Does not validate a GPU execution or main's final window copy/commit loop.
4#include "strata/core/gated_proposal.hpp"
5#include "strata/core/spec_proposal.hpp"
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <cstdint>
10#include <cstdio>
11#include <cstring>
12#include <limits>
13#include <stdexcept>
14#include <string>
15
16namespace {
17namespace ap = strata::core::gated;
18namespace ac = strata::core::proposal;
19constexpr int A=7, B=4097, C=8190, V=8192;
20constexpr float theta=.5f;
21using Row=std::array<int32_t,ap::kStride>;
22using Rows=std::array<int32_t,ap::kRows*ap::kStride>;
23using Floats=std::array<float,ap::kRows>;
24using Drafts=std::array<int32_t,ap::kRows>;
25int checks=0;
26void need(bool ok,const char* msg) {++checks;if(!ok)throw std::runtime_error(msg);}
27uint32_t bits(float f) {uint32_t n;std::memcpy(&n,&f,4);return n;}
28Row row(float a=.625f,float b=.25f,float c=.125f) {
29    Row r; r.fill(-1);
30    // IDs/padding deliberately unlike target order. Padding must survive masking.
31    for(int i=0;i<ap::kEntries;++i)r[ap::kEntries+i]=(int32_t)(0x3e800000u+(uint32_t)i);
32    const int ids[]={C,A,B};const float probs[]={c,a,b};
33    for(int i=0;i<3;++i){r[i]=ids[i];std::memcpy(r.data()+ap::kEntries+i,&probs[i],4);}
34    return r;
35}
36void put(Rows& rows,int n,const Row& r) {std::memcpy(rows.data()+n*ap::kStride,r.data(),sizeof(r));}
37void expect_mask(const int32_t* actual,const Row& original,float a,float b,float c) {
38    Row expected=original;const float p[]={c,a,b};
39    for(int i=0;i<3;++i)std::memcpy(expected.data()+ap::kEntries+i,&p[i],4);
40    need(std::memcmp(actual,expected.data(),sizeof(expected))==0,"mask differs in retained bits / IDs / padding / +0");
41}
42struct Expected {int T,nq,offers,attempts,failed;bool suffix,nulltail;};
43void packet(const ap::Packet& p,Expected x) {
44    need(!p.error,"unexpected packet error");
45    need(p.T==x.T && p.n_q==x.nq && p.offers==x.offers && p.attempts==x.attempts && p.failed_index==x.failed && p.from_sfx==x.suffix && p.null_tail==x.nulltail,"packet fields differ from hand-written expectation");
46    need(p.T==p.offers+1,"one terminal output after offered prefix");
47    need(ac::rowkind(p.T-1,p.T,p.n_q)==(p.null_tail?ac::RowKind::NullTail:ac::RowKind::PlainBonus),"AA packet / Hubble terminal rowkind mismatch");
48}
49struct Fixture {
50    Rows raw{};Drafts d{};Floats scores{};
51    Fixture(){raw.fill(-1);d.fill(-777);scores.fill(std::numeric_limits<float>::quiet_NaN());}
52    void set(int j,const Row& r,int id,float score){put(raw,j,r);d[j]=id;scores[j]=score;}
53    bool call(ap::Packet& out,int generated,int budget,const ap::RootChoice& root={},bool initial=false,int capacity=6) const {
54        const Rows saved=raw;const Drafts ds=d;const Floats ss=scores;
55        const bool ok=ap::make_packet(out,raw.data(),d.data(),scores.data(),generated,budget,capacity,V,theta,initial,root);
56        need(std::memcmp(raw.data(),saved.data(),sizeof raw)==0,"raw q mutated");
57        need(std::memcmp(d.data(),ds.data(),sizeof d)==0,"raw drafts mutated");
58        need(std::memcmp(scores.data(),ss.data(),sizeof scores)==0,"own-score mutated");
59        need(ok==(out.error==nullptr),"return/error inconsistent");
60        return ok;
61    }
62};
63void positive(const char* name,const Fixture& f,int n,int budget,ap::RootChoice root,Expected x) {
64    ap::Packet p;need(f.call(p,n,budget,root),name);packet(p,x);
65    std::printf("{\"case\":\"%s\",\"passed\":true}\n",name);
66}
67void tests() {
68    const Row raw=row();Row masked{};const Row saved=raw;
69    need(ap::mask_row(raw.data(),masked.data(),theta,-1,V),"mask H");
70    expect_mask(masked.data(),raw,.625f,0,0); // exact GPU T1_NULL mu
71    need(ap::mask_row(raw.data(),masked.data(),theta,B,V),"mask UNION");
72    expect_mask(masked.data(),raw,.625f,.25f,0); // GPU UNION mu, sum=.875 !=1
73    need(std::memcmp(raw.data(),saved.data(),sizeof raw)==0,"mask modified raw");
74    const Row edge=row(.5f,std::nextafter(.5f,0.0f),0x1p-25f);
75    need(ap::mask_row(edge.data(),masked.data(),theta,-1,V),"mask inclusive edge");
76    expect_mask(masked.data(),edge,.5f,0,0);
77    const Row above=row(std::nextafter(.5f,1.0f),.25f,.25f-0x1p-24f);
78    need(ap::mask_row(above.data(),masked.data(),theta,-1,V),"mask above edge");
79    expect_mask(masked.data(),above,std::nextafter(.5f,1.0f),0,0);
80    const Row none=row(.375f,.375f,.25f);
81    need(ap::mask_row(none.data(),masked.data(),theta,-1,V),"mask zero measure");
82    expect_mask(masked.data(),none,0,0,0);
83    const Row negzero=row(1.0f,-0.0f,0.0f);
84    need(ap::mask_row(negzero.data(),masked.data(),0.0f,-1,V),"theta zero mask");
85    expect_mask(masked.data(),negzero,1.0f,-0.0f,0.0f);
86    need(bits(ap::mass_at(masked.data(),B))==bits(-0.0f),"retained negative zero bits changed");
87
88    ap::Packet p;
89    need(ap::make_packet(p,nullptr,nullptr,nullptr,0,3,6,V,theta,true,{}),"initial no buffers");
90    packet(p,{1,0,0,0,-1,false,false});
91    need(ap::make_packet(p,nullptr,nullptr,nullptr,0,0,6,V,theta,false,{}),"zero producer budget");
92    packet(p,{1,0,0,0,-1,false,false});
93
94    Fixture firstnull;firstnull.set(0,raw,B,.25f);
95    positive("root_NULL",firstnull,1,3,{}, {1,1,0,1,0,false,true});
96    Fixture allzero;allzero.set(0,none,B,.375f);
97    positive("all_zero_mu_NULL",allzero,1,3,{}, {1,1,0,1,0,false,true});
98    Fixture one;one.set(0,raw,A,.625f);
99    positive("actual_max_steps_1_is_bonus",one,1,1,{}, {2,1,1,1,-1,false,false});
100    need(!one.call(p,1,3),"incomplete generated1/budget3 must not become bonus");
101    Fixture later;later.set(0,raw,A,.625f);later.set(1,raw,C,.125f);
102    positive("second_attempt_NULL",later,2,3,{}, {2,2,1,2,1,false,true});
103    need(later.call(p,2,3),"NULL packet");
104    need(p.offers==1 && p.failed_index==1 && p.T-1==1,"failed D excluded from offered window input span");
105    // Packet contains no token array. These counts exclude failed D from the
106    // intended copy span; actual main window construction remains integration work.
107    expect_mask(p.mu.data()+ap::kStride,raw,.625f,0,0);
108    Fixture full;full.set(0,raw,A,.625f);full.set(1,raw,A,.625f);full.set(2,raw,A,.625f);
109    positive("actual_max_steps_3_bonus",full,3,3,{}, {4,3,3,3,-1,false,false});
110    need(!full.call(p,3,4),"generated3/budget4 must not guess exhausted budget");
111
112    const ap::RootChoice E{true,B,6};
113    positive("UNION_low_D_suffix",firstnull,1,3,E, {6,1,5,1,-1,true,false});
114    need(firstnull.call(p,1,3,E),"UNION rescue");expect_mask(p.mu.data(),raw,.625f,.25f,0);
115    const Row union_root_snapshot=[](){Row r=row();Row out;need(ap::mask_row(r.data(),out.data(),theta,B,V),"snapshot union");return out;}();
116    Fixture high;high.set(0,raw,A,.625f);
117    need(high.call(p,1,1,E),"UNION D!=s offer");packet(p,{2,1,1,1,-1,false,false});
118    need(std::memcmp(p.mu.data(),union_root_snapshot.data(),sizeof(Row))==0,"UNION must exist on MTP branch");
119    Fixture otherlow;otherlow.set(0,raw,C,.125f);
120    need(otherlow.call(p,1,3,E),"UNION D!=s NULL");packet(p,{1,1,0,1,0,false,true});
121    need(std::memcmp(p.mu.data(),union_root_snapshot.data(),sizeof(Row))==0,"UNION must exist on NULL branch");
122    // Later row's B is NOT rescued by the root set.
123    Fixture next;next.set(0,raw,A,.625f);next.set(1,raw,B,.25f);
124    need(next.call(p,2,3,E),"later H only");packet(p,{2,2,1,2,1,false,true});
125    expect_mask(p.mu.data(),raw,.625f,.25f,0);
126    expect_mask(p.mu.data()+ap::kStride,raw,.625f,0,0);
127    // Eligibility false despite populated rescue fields must mean plain H.
128    positive("ineligible_rescue_ignored",firstnull,1,3,{false,B,6}, {1,1,0,1,0,false,true});
129
130    // Frozen source choice must not inspect invalid discarded later materialization.
131    // A future row's poison may be present, but only root is consumed if suffix wins.
132    Fixture eager=firstnull;
133    positive("suffix_ignores_later_D_score_q",eager,3,3,E, {6,1,5,1,-1,true,false});
134    Fixture stopped=firstnull;
135    positive("NULL_ignores_later_materialization",stopped,3,3,{}, {1,1,0,1,0,false,true});
136    Fixture edgeoffer;edgeoffer.set(0,edge,A,.5f);
137    positive("inclusive_D_at_theta",edgeoffer,1,1,{}, {2,1,1,1,-1,false,false});
138    Fixture edgenull;edgenull.set(0,edge,B,std::nextafter(.5f,0.0f));
139    positive("D_just_below_theta_NULL",edgenull,1,3,{}, {1,1,0,1,0,false,true});
140
141    Fixture bad=one;bad.scores[0]=std::nextafter(.625f,1.0f);
142    need(!bad.call(p,1,1),"own-score mismatch must fail");
143    bad=one;bad.d[0]=123;
144    need(!bad.call(p,1,1),"D outside q support must fail");
145    bad=one;bad.raw[1]=bad.raw[0];
146    need(!bad.call(p,1,1),"duplicate support must fail");
147    bad=one;float nan=std::numeric_limits<float>::quiet_NaN();std::memcpy(bad.raw.data()+ap::kEntries+1,&nan,4);
148    need(!bad.call(p,1,1),"nonfinite q must fail");
149    need(!one.call(p,1,0),"n_generated exceeds actual budget");
150    need(!one.call(p,1,1,{},true),"initial stale attempts");
151    need(!one.call(p,1,1,{true,V,6}),"out-of-vocab rescue");
152    need(!one.call(p,1,1,{true,B,7}),"suffix width exceeds capacity");
153    need(!one.call(p,1,6),"budget cannot fill terminal slot");
154    need(!ap::make_packet(p,nullptr,nullptr,nullptr,0,1,6,V,theta,false,{}),"missing expected attempt");
155    need(!ap::make_packet(p,nullptr,nullptr,nullptr,1,1,6,V,theta,false,{}),"null buffers");
156    need(!ap::mask_row(raw.data(),masked.data(),nan,-1,V),"NaN theta");
157    need(!ap::mask_row(raw.data(),masked.data(),-.1f,-1,V),"negative theta");
158    // Concrete producer/context boundaries: budget is not inferred from count.
159    // The failed row may be the LAST physically generated row at actual budget.
160    Fixture last_attempt;last_attempt.set(0,raw,A,.625f);last_attempt.set(1,raw,B,.25f);
161    need(last_attempt.call(p,2,2,{},false,3),"last producer attempt failed at exact budget");
162    packet(p,{2,2,1,2,1,false,true});
163    expect_mask(p.mu.data()+ap::kStride,raw,.625f,0,0);
164    // Zero remaining proposal budget has NO attempt, regardless of stale bytes.
165    Fixture poison;
166    need(poison.call(p,0,0,{},false,1),"capacity1 zero producer budget");
167    packet(p,{1,0,0,0,-1,false,false});
168    need(std::all_of(p.mu.begin(),p.mu.end(),[](int32_t word){return word==0;}),"zero-budget packet retained old mu");
169    need(!last_attempt.call(p,1,0,{},false,1),"actual generated row cannot be relabeled zero budget");
170    // If a caller has already materialized a failed row at the final target
171    // capacity, it must preserve NULL or explicitly reject this packet. Passing
172    // min(generated,room-1) here would erase evidence and evade this contract.
173    Fixture edge_failed;edge_failed.set(0,raw,B,.25f);
174    const bool edge_ok=edge_failed.call(p,1,1,{},false,1);
175    if(edge_ok)packet(p,{1,1,0,1,0,false,true});
176    else need(p.error!=nullptr,"capacity failure must be explicit, never bonus");
177    // A subsequent successful call must clear every stale error/count/mode field.
178    need(one.call(p,1,1),"packet reset after errors");packet(p,{2,1,1,1,-1,false,false});
179}
180} // namespace
181int main() {
182    try {tests();std::printf("{\"summary\":\"gated_proposal_test\",\"checks\":%d,\"passed\":true,\"GPU_executed\":false,\"actual_main_window_copy_tested\":false}\n",checks);return 0;}
183    catch(const std::exception& e){std::fprintf(stderr,"gated_proposal_test FAILED after %d checks: %s\n",checks,e.what());return 1;}
184}