import sys,json,random from pathlib import Path import numpy as np, torch from torch import nn import torch.nn.functional as F sys.path.insert(0,'/home/maxwelhelp/all/math2nn') from bench import get_dataset,make_model,train_model,sweep_baseline,make_report EPOCHS=12; NTRAIN=1200; NTEST=400; LRS=[1e-3,3e-3,6e-3] def seed_all(s): random.seed(s);np.random.seed(s);torch.manual_seed(s) if torch.cuda.is_available(): try: torch.cuda.manual_seed_all(s) except Exception: pass class SubsetAttn(nn.Module): def __init__(self,d=64,n=4): super().__init__();self.n=n self.score=nn.Linear(2*d,1);self.pair=nn.Sequential(nn.Linear(2*d,32),nn.Tanh(),nn.Linear(32,1));self.dec=nn.Sequential(nn.Linear(2*d,64),nn.Tanh(),nn.Linear(64,d)) self.register_buffer('masks',torch.tensor([[(m>>i)&1 for i in range(n)] for m in range(1,1<bkd',M,x);dec=self.dec(torch.cat((agg,z[:,None,:].expand(-1, M.shape[0],-1)),-1)) pm=torch.einsum('ki,kj->kij',M,M)*(1-torch.eye(n,device=x.device));lw=torch.einsum('bn,kn->bk',s,M)+.5*torch.einsum('bij,kij->bk',logq,pm) if boost: lw=lw+boost*M[:,0]*M[:,1] pi=torch.softmax(lw,-1);r=torch.einsum('bk,kn->bn',pi,M);u=torch.einsum('bk,bkd->bd',pi,dec) return r,u,(pi*M[:,0]*M[:,1]).sum(-1) def forward(self,x): B,T,D=x.shape;parts=[] for st in range(0,T,self.n): c=x[:,st:st+self.n];actual=c.shape[1] if actualfloat(b.mean()))} rep=make_report('sequence','transformer_tiny',base,idea,{'mechanism_signature':sig,'selection':{'baseline_best_cfg':base['best_cfg'],'idea_best_cfg':best['cfg'],'shared_lr_union':LRS},'structural_match':'multi-token correlated sequence window'}) rep['protocol_note']='Eight paired seeds; three shared learning rates; reduced 1200/400 samples and 12 epochs for runtime.' Path('bench_report.json').write_text(json.dumps(rep,indent=2));print(json.dumps(rep,indent=2)) if __name__=='__main__':main()