Download runtime/training/decoder_cache.py from hscr/luxfer: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/hscr/luxfer/resolve/main/runtime/training/decoder_cache.py
- Command line
-
hf download hf://hscr/luxfer/runtime/training/decoder_cache.py
-
curl -L -o decoder_cache.py https://huggingface.co/hscr/luxfer/resolve/main/runtime/training/decoder_cache.py
1.82 kB
| """Integrity-checked frozen-prefix cache reusable across decoder architecture ablations.""" | |
| import hashlib,json,os | |
| from pathlib import Path | |
| from safetensors.torch import load_file,save_file | |
| def identity(row,base_sha256,first_layer,implementation_sha256): | |
| return dict(format_version=1,row_sha256=hashlib.sha256(json.dumps(row,sort_keys=True,ensure_ascii=False).encode()).hexdigest(),base_sha256=base_sha256,first_layer=first_layer,implementation_sha256=implementation_sha256) | |
| def key(config):return hashlib.sha256(json.dumps(config,sort_keys=True).encode()).hexdigest() | |
| def load(directory,config): | |
| stem=Path(directory)/key(config);metadata=stem.with_suffix('.json');tensor=stem.with_suffix('.safetensors') | |
| if not metadata.exists():return None | |
| saved=json.loads(metadata.read_text()) | |
| if saved['identity']!=config:raise ValueError('Prefix-cache identity differs') | |
| if hashlib.sha256(tensor.read_bytes()).hexdigest()!=saved['tensor_sha256']:raise ValueError('Prefix-cache tensor checksum mismatch') | |
| result=load_file(str(tensor));result.setdefault('mask',None);return result | |
| def save(directory,config,cache): | |
| directory=Path(directory);directory.mkdir(parents=True,exist_ok=True);stem=directory/key(config);tensor=stem.with_suffix('.safetensors') | |
| temporary=tensor.with_suffix('.part');save_file({k:v.contiguous() for k,v in cache.items() if k in ['hidden','mask','labels'] and v is not None},str(temporary)) | |
| with temporary.open('rb') as f:os.fsync(f.fileno()) | |
| os.replace(temporary,tensor) | |
| metadata=dict(identity=config,tensor_sha256=hashlib.sha256(tensor.read_bytes()).hexdigest()) | |
| temporary=stem.with_suffix('.next.json') | |
| with temporary.open('w') as f:json.dump(metadata,f,indent=2);f.flush();os.fsync(f.fileno()) | |
| os.replace(temporary,stem.with_suffix('.json')) | |