mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Fix model weights export
This commit is contained in:
@@ -43,6 +43,7 @@ float* Model::predict(const float* input) {
|
||||
float* d_input = inputLayer->forward(input);
|
||||
|
||||
for (auto& layer : layers) {
|
||||
std::cout << layer << std::endl;
|
||||
d_input = layer->forward(d_input);
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@@ -1,6 +1,8 @@
|
||||
import torch
|
||||
import struct
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def print_cpp_vector(vector, name="expected"):
|
||||
print("std::vector<float> " + name + " = {", end="")
|
||||
@@ -16,27 +18,25 @@ def export_model_weights(model: torch.nn.Module, filename):
|
||||
|
||||
header = ""
|
||||
offset = 0
|
||||
|
||||
tensor_data = b""
|
||||
|
||||
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_bytes = param.type(torch.float32).detach().numpy().tobytes()
|
||||
tensor_size = param.numel()
|
||||
|
||||
header += f"{name},{tensor_size},{offset}\n"
|
||||
|
||||
header += f"{name},{tensor_size},{offset}\n"
|
||||
offset += len(tensor_bytes)
|
||||
|
||||
f.write(tensor_bytes)
|
||||
tensor_data += tensor_bytes
|
||||
|
||||
f.seek(0)
|
||||
f.write(struct.pack('q', len(header)))
|
||||
f.write(struct.pack('q', len(header)))
|
||||
f.write(header.encode('utf-8'))
|
||||
f.write(tensor_data)
|
||||
|
||||
def print_model_parameters(model: torch.nn.Module):
|
||||
for name, param in model.named_parameters():
|
||||
print(name, param.numel())
|
||||
print(name, param.numel())
|
||||
|
||||
Reference in New Issue
Block a user