File size: 1,963 Bytes
c394b65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""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)