"""Reproducible BF16 -> calibrated NVFP4 with rank-128 BF16 correction.""" import os,json,time,shutil,statistics from pathlib import Path import torch from safetensors.torch import load_file,save_file from probe_dynamic import BASE,OLD,INDEX,weight,split,prepare,run,error ROOT=Path(os.environ['IMAGE21_WORK_DIR']) OUT=ROOT/'release'/'transformer';OUT.mkdir(parents=True,exist_ok=True) REPORT=ROOT/'release'/'reports';REPORT.mkdir(exist_ok=True) CACHE=ROOT/'calibrated-layers-dynamic';CACHE.mkdir(exist_ok=True) @torch.inference_mode() def main(): names=json.loads((OLD/'calibration/manifest.json').read_text())['modules'];results={};specs={} for i,name in enumerate(names): record=CACHE/(name+'.json');saved=CACHE/(name+'.safetensors') if record.exists() and saved.exists():result=json.loads(record.read_text()) else: w=weight(name);x=load_file(str(OLD/'calibration'/f'{name}.safetensors'))['activations'].cuda();fit,check=split(x) rf=fit@w.T;rc=check@w.T naive=prepare(w,fit,0.,0);naive_error=error(run(check,naive),rc) best=None;candidates=[] for smoothing in [0.,.25,.5,.75,1.]: for mse in [False,True]: p=prepare(w,fit,smoothing,128,method='svd',mse=mse) err=error(run(fit,p),rf) item={'smoothing_alpha':smoothing,'weight_scale_search':mse,'fit_nrmse':err};candidates.append(item) if best is None or err3_000_000_000:shards.append(s);s={};size=0 s[n]=v.contiguous();size+=nb if s:shards.append(s) for i,s in enumerate(shards):save_file(s,str(OUT/f'model-{i+1:05d}-of-{len(shards):05d}.safetensors'),metadata={'format':'pt','notice':cfg['modified_notice']}) total=sum(v.numel()*v.element_size() for v in state.values()) summary={'modules':len(names),'transformer_bytes':total,'mean_diagnostic_nrmse':statistics.mean(r['diagnostic_nrmse'] for r in results.values()),'max_diagnostic_nrmse':max(r['diagnostic_nrmse'] for r in results.values()),'naive_mean_diagnostic_nrmse':statistics.mean(r['naive_nvfp4_diagnostic_nrmse'] for r in results.values()),'protocol':'64 original BF16 trajectories;384 fit and384 disjoint diagnostic rows per layer, stratified across prompts and timesteps. Five smoothing exponents, two weight-scale methods. Rank128 randomized SVD (seed1234,q160,niter3); optional activation-weighted15-point block-scale MSE search. Native W4A4 kernels with dynamic activation amax used for all calibrated selection/diagnostic output errors. Plain reference uses original static range. No held-out image evaluation prompts used for calibration.'} (REPORT/'calibration_search.json').write_text(json.dumps(results,indent=2));(REPORT/'quantization_summary.json').write_text(json.dumps(summary,indent=2));shutil.copy(OLD/'calibration/manifest.json',REPORT/'calibration_manifest.json') print('QUANTIZATION_COMPLETE',json.dumps(summary),flush=True) if __name__=='__main__':main()