mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
29 lines
744 B
Python
29 lines
744 B
Python
import torch
|
|
import struct
|
|
|
|
def export_model_weights(model: torch.nn.Module, filename):
|
|
with open(filename, 'wb') as f:
|
|
|
|
header = ""
|
|
offset = 0
|
|
|
|
|
|
for name, param in model.named_parameters():
|
|
if 'weight' not in name and 'bias' not in name:
|
|
continue
|
|
|
|
tensor_values = param.flatten().tolist()
|
|
tensor_bytes = struct.pack('f' * len(tensor_values), *tensor_values)
|
|
|
|
tensor_size = param.numel()
|
|
|
|
header += f"{name},{tensor_size},{offset}\n"
|
|
|
|
offset += len(tensor_bytes)
|
|
|
|
f.write(tensor_bytes)
|
|
|
|
f.seek(0)
|
|
f.write(struct.pack('q', len(header)))
|
|
f.write(header.encode('utf-8'))
|