Download export_release.py from anudit/tinyemo: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/anudit/tinyemo/resolve/main/export_release.py
- Command line
-
hf download hf://anudit/tinyemo/export_release.py
-
curl -L -o export_release.py https://huggingface.co/anudit/tinyemo/resolve/main/export_release.py
1.96 kB
| """Rebuild standard exports from the evaluated TEM2 artifact.""" | |
| import json | |
| import struct | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from tinyemo.model import TinyEmo, TinyEmoConfig | |
| from tinyemo.runtime import TinyEmoRuntime | |
| HERE = Path(__file__).resolve().parent | |
| rt = TinyEmoRuntime(str(HERE / 'tiny_emo.bin')) | |
| model = TinyEmo(TinyEmoConfig(**rt.meta)) | |
| model.load_state_dict({k: torch.from_numpy(v.copy()) for k, v in rt.w.items()}) | |
| model.eval() | |
| (HERE / 'config.json').write_text(json.dumps({'model_type': 'tinyemo', 'architecture': 'TinyEmo', 'config': rt.meta, 'labels': rt.labels}, ensure_ascii=False, indent=2) + '\n') | |
| # Safetensors: 8-byte header length, JSON header, then contiguous little-endian data. | |
| tensors = {k: v.detach().cpu().numpy().astype('<f4') for k, v in model.state_dict().items()} | |
| header, chunks, offset = {'__metadata__': {'format': 'pt', 'source': 'dequantized TEM2 artifact'}}, [], 0 | |
| for name, arr in tensors.items(): | |
| blob = arr.tobytes(order='C') | |
| header[name] = {'dtype': 'F32', 'shape': list(arr.shape), 'data_offsets': [offset, offset + len(blob)]} | |
| chunks.append(blob) | |
| offset += len(blob) | |
| h = json.dumps(header, separators=(',', ':')).encode('utf-8') | |
| h += b' ' * (-len(h) % 8) | |
| (HERE / 'model.safetensors').write_bytes(struct.pack('<Q', len(h)) + h + b''.join(chunks)) | |
| ids, mask, buckets, signs = rt.encode_batch(['walk the dog', 'happy birthday']) | |
| inputs = (torch.from_numpy(ids), torch.from_numpy(mask), torch.from_numpy(buckets), | |
| torch.from_numpy(signs).float(), torch.from_numpy((signs[:, :, 0] != 0).sum(1)).float()) | |
| torch.onnx.export(model, inputs, str(HERE / 'model.onnx'), opset_version=17, | |
| input_names=['tok_ids', 'tok_mask', 'ng_bucket', 'ng_sign', 'ng_count'], output_names=['logits'], | |
| dynamic_axes={name: {0: 'batch'} for name in ['tok_ids', 'tok_mask', 'ng_bucket', 'ng_sign', 'ng_count', 'logits']}, | |
| do_constant_folding=True, dynamo=False) | |
| print('Exported', HERE) | |