"""Source-byte, strict-state, native-feature/token and codec-path verification.""" import argparse,bisect,gc,hashlib,json,pathlib,struct,sys,time,io import torch import numpy as np from PIL import Image from component import build_model,load_model,tokenizer_view class SourceRanges: def __init__(self,root,mapping): self.shards={};self.root=root;self.dcp=any('chunks' in e for e in mapping.values()) if self.dcp:return for shard in {e['source_shard'] for e in mapping.values()}: parts=sorted((int(p.name.rsplit('_',1)[1]),p) for p in (root/'.download').glob(shard.replace('/','__')+'_*') if not p.name.endswith('.part')) assert parts self.shards[shard]=(parts,parts[0][0]-min(e['data_offsets'][0] for e in mapping.values() if e['source_shard']==shard)) def read(self,e): if self.dcp: t=torch.empty(e['shape'],dtype=DTYPES[e['dtype']]);coverage=torch.zeros(e['shape'][0],dtype=torch.bool) for c in e['chunks']: raw=(self.root/'.download'/c['cache_file']).read_bytes();assert hashlib.sha256(raw).hexdigest()==c['serialized_sha256'] part=torch.load(io.BytesIO(raw),map_location='cpu',weights_only=True);assert list(part.shape)==c['tensor_sizes'] and part.dtype==t.dtype a=c['tensor_offsets'][0];b=a+c['tensor_sizes'][0];assert not coverage[a:b].any();coverage[a:b]=True assert c['tensor_offsets'][1:]==[0]*(t.ndim-1) and c['tensor_sizes'][1:]==list(t.shape)[1:] t[tuple(slice(a,a+b) for a,b in zip(c['tensor_offsets'],c['tensor_sizes']))]=part assert coverage.all();return bytearray(t.view(torch.uint8).numpy().tobytes()) parts,base=self.shards[e['source_shard']];starts=[p[0] for p in parts];a,b=e['data_offsets'];a+=base;b+=base;result=bytearray() while a=0)&(ids