"""Load verified v8055 INT16 storage and mixed INT8 variants with explicit bindings. 使用显式绑定加载已核验的v8055 INT16存储与混合INT8变体。 """ import hashlib import json import sys from pathlib import Path def sha(path): """Return a complete SHA-256 for the exact file supplied. 返回所给确切文件的完整SHA-256。 """ with Path(path).open('rb') as stream: return hashlib.file_digest(stream,'sha256').hexdigest() def map_name(canonical): """Translate only the proven Qwen checkpoint prefix and reject other namespaces. 仅转换已证明的Qwen检查点前缀,其他命名空间明确拒绝。 """ prefix='model.language_model.' if not canonical.startswith(prefix): raise ValueError('Unexpected canonical Qwen parameter namespace') return 'model.'+canonical[len(prefix):] def load_variant(name,base): """Return a strictly loaded CPU model; the caller must run every neural operation on CUDA. 返回严格加载的CPU模型;调用方必须将所有神经计算放在CUDA。 """ import torch from safetensors.torch import load_file from transformers import AutoConfig,Qwen3_5ForCausalLM base=Path(base) expected='ce8e905daaa5726b2ef162a08120955c52700d3ed686ca2f429ce8b01a60cb35' if name=='int16_storage': folder=base/'variants/int16-storage' metadata=json.loads((folder/'int16-storage-manifest.json').read_bytes()) path=folder/'weights-int16.safetensors' if metadata['source_model_sha256']!=expected or sha(path)!='c80ddd299149daca2516659d65d0c7f575a30daceaa096ff82123bd279cf8aca': raise ValueError('The exact INT16 storage binding changed') packed=load_file(str(path),device='cpu') state={} for canonical,entry in metadata['mapping'].items(): if entry['kind']=='symmetric_rowwise_int16': value=packed[entry['storage']].float()*packed[entry['scale']] elif entry['kind']=='unchanged_float32': value=packed[entry['storage']] else: raise ValueError('Unknown INT16 storage mapping kind') if list(value.shape)!=entry['shape'] or value.dtype!=torch.float32: raise ValueError('The dequantized tensor contract changed') state[map_name(canonical)]=value if len(state)!=320 or sum(value.numel() for value in state.values())!=752393024: raise ValueError('INT16 dequantization does not reconstruct the full model') config=AutoConfig.from_pretrained(folder,local_files_only=True) with torch.device('cpu'): model=Qwen3_5ForCausalLM(config) model.tie_weights() if set(dict(model.named_parameters()))!=set(state) or not model.lm_head.weight is model.get_input_embeddings().weight: raise ValueError('The confirmed320-parameter namespace or tied head changed') state['lm_head.weight']=state['model.embed_tokens.weight'] model.load_state_dict(state,strict=True,assign=True) model.tie_weights() if any(parameter.is_meta for parameter in model.parameters()) or any(value.is_meta for _,value in model.named_buffers()): raise ValueError('A model parameter or derived rotary buffer remains uninitialized') if model.config._attn_implementation!='sdpa': raise ValueError('The confirmed native SDPA implementation changed') return model if name=='int8_mixed_delta': import torchao from torchao.dtypes.affine_quantized_tensor import AffineQuantizedTensor if torchao.__version__!='0.14.1': raise ValueError('The pinned standalone TorchAO version changed') path=base/'variants/int8-mixed-delta/weights-int8-torchao-delta.pt' if sha(base/'model.safetensors')!=expected or sha(path)!='2d31ebd846f58266f134f3536cdcbefd8576e0b5e1b7607fcc49d1e0a41e7332': raise ValueError('The exact INT8 base/delta binding changed') model,loading=Qwen3_5ForCausalLM.from_pretrained(base,dtype=torch.float32,attn_implementation='sdpa',local_files_only=True,output_loading_info=True) if any(loading[key] for key in ('missing_keys','unexpected_keys','mismatched_keys','error_msgs')): raise ValueError('The INT8 FP32 base did not load exactly') packed=torch.load(path,weights_only=True,map_location='cpu') expected_names={module_name+'.weight' for module_name,module in model.named_modules() if isinstance(module,torch.nn.Linear) and ('.mlp.' in module_name or '.self_attn.' in module_name)} if len(packed)!=96 or {map_name(key) for key in packed}!=expected_names: raise ValueError('The96 protected-mixed INT8 linear bindings changed') for canonical,value in packed.items(): if not isinstance(value,AffineQuantizedTensor) or value.tensor_impl.int_data.dtype!=torch.int8: raise ValueError('A packed weight is not the declared INT8 tensor') module=model.get_submodule(map_name(canonical)[:-len('.weight')]) if list(module.weight.shape)!=list(value.shape): raise ValueError('An INT8 linear shape changed') module.weight=torch.nn.Parameter(value,requires_grad=False) return model raise ValueError('Undeclared precision variant: '+name)