-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
306 lines (269 loc) · 9.29 KB
/
converter.py
File metadata and controls
306 lines (269 loc) · 9.29 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import torch
import numpy as np
from dataclasses import dataclass
from typing import List, Dict, Any, Optional
import json
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
@dataclass
class IRNode:
op_type: str
inputs: List[str]
outputs: List[str]
attributes: Dict[str, Any]
weights: Optional[Dict[str, np.ndarray]] = None
class IR:
def __init__(self):
self.nodes: List[IRNode] = []
self.weights: Dict[str, np.ndarray] = {}
self.input_name: str = "input"
self.output_name: str = "output"
def add_node(self, node: IRNode):
self.nodes.append(node)
def print_graph(self):
print("\nIR Graph Structure:")
for i, node in enumerate(self.nodes):
print(f"\nNode {i}:")
print(f" Type: {node.op_type}")
print(f" Inputs: {node.inputs}")
print(f" Outputs: {node.outputs}")
print(f" Attributes: {node.attributes}")
def save(self, prefix: str):
# Save weights to binary file
weight_metadata = {}
weight_offset = 0
with open(f"{prefix}.weights.bin", "wb") as f:
for name, weight in self.weights.items():
weight.tofile(f)
weight_metadata[name] = {
"offset": weight_offset,
"shape": list(weight.shape),
"dtype": str(weight.dtype)
}
weight_offset += weight.nbytes
# Save graph structure to JSON
graph_structure = {
"nodes": [
{
"op_type": node.op_type,
"inputs": node.inputs,
"outputs": node.outputs,
"attributes": node.attributes
} for node in self.nodes
],
"weights": weight_metadata,
"input_name": self.input_name,
"output_name": self.output_name,
"input_shape": [1, 1, 28, 28], # MNIST specific
"output_shape": [1, 10] # MNIST specific
}
with open(f"{prefix}.graph.json", "w") as f:
json.dump(graph_structure, f, indent=2)
print(f"\nSaved IR to {prefix}.graph.json and {prefix}.weights.bin")
class ModelConverter:
def __init__(self):
self.ir = IR()
self.logger = logging.getLogger("ModelConverter")
def _get_tensor_name(self, node_type: str, idx: int) -> str:
"""Generate a unique tensor name"""
return f"{node_type}_{idx}"
def convert(self, model_path: str, output_prefix: str):
"""Convert PyTorch model to IR format"""
self.logger.info(f"Loading model from {model_path}")
model = torch.jit.load(model_path)
model.eval() # Important for inference
# Print the model graph for debugging
self.logger.info("TorchScript Graph:")
self.logger.info(model.graph)
# Extract model parameters
self.logger.info("\nExtracting parameters:")
for name, param in model.named_parameters():
self.logger.info(f"Found parameter: {name} with shape {param.shape}")
clean_name = name.replace('.', '_')
self.ir.weights[clean_name] = param.detach().numpy()
# Create IR nodes
self.logger.info("\nCreating IR nodes...")
self._create_nodes()
# Print final IR structure
self.ir.print_graph()
# Save IR
self.ir.save(output_prefix)
return self.ir
def _create_nodes(self):
"""Create properly connected IR nodes"""
current_tensor = "input"
# Conv1 + ReLU
conv1_out = self._get_tensor_name("conv1", 0)
self.ir.add_node(IRNode(
op_type="conv2d",
inputs=[current_tensor],
outputs=[conv1_out],
attributes={
"in_channels": 1,
"out_channels": 10,
"kernel_size": 3,
"stride": 1,
"padding": 0,
"weights_key": "conv1_weight",
"bias_key": "conv1_bias"
}
))
relu1_out = self._get_tensor_name("relu1", 0)
self.ir.add_node(IRNode(
op_type="relu",
inputs=[conv1_out],
outputs=[relu1_out],
attributes={}
))
current_tensor = relu1_out
# MaxPool1
pool1_out = self._get_tensor_name("pool1", 0)
self.ir.add_node(IRNode(
op_type="maxpool2d",
inputs=[current_tensor],
outputs=[pool1_out],
attributes={
"kernel_size": 2,
"stride": 2,
"padding": 0
}
))
current_tensor = pool1_out
# Conv2 + ReLU
conv2_out = self._get_tensor_name("conv2", 0)
self.ir.add_node(IRNode(
op_type="conv2d",
inputs=[current_tensor],
outputs=[conv2_out],
attributes={
"in_channels": 10,
"out_channels": 20,
"kernel_size": 3,
"stride": 1,
"padding": 0,
"weights_key": "conv2_weight",
"bias_key": "conv2_bias"
}
))
relu2_out = self._get_tensor_name("relu2", 0)
self.ir.add_node(IRNode(
op_type="relu",
inputs=[conv2_out],
outputs=[relu2_out],
attributes={}
))
current_tensor = relu2_out
# MaxPool2
pool2_out = self._get_tensor_name("pool2", 0)
self.ir.add_node(IRNode(
op_type="maxpool2d",
inputs=[current_tensor],
outputs=[pool2_out],
attributes={
"kernel_size": 2,
"stride": 2,
"padding": 0
}
))
current_tensor = pool2_out
# Flatten
flatten_out = self._get_tensor_name("flatten", 0)
self.ir.add_node(IRNode(
op_type="reshape",
inputs=[current_tensor],
outputs=[flatten_out],
attributes={
"shape": [-1, 20 * 5 * 5]
}
))
current_tensor = flatten_out
# FC1 + ReLU
fc1_out = self._get_tensor_name("fc1", 0)
self.ir.add_node(IRNode(
op_type="linear",
inputs=[current_tensor],
outputs=[fc1_out],
attributes={
"in_features": 20 * 5 * 5,
"out_features": 50,
"weights_key": "fc1_weight",
"bias_key": "fc1_bias"
}
))
relu3_out = self._get_tensor_name("relu3", 0)
self.ir.add_node(IRNode(
op_type="relu",
inputs=[fc1_out],
outputs=[relu3_out],
attributes={}
))
current_tensor = relu3_out
# FC2
fc2_out = self._get_tensor_name("fc2", 0)
self.ir.add_node(IRNode(
op_type="linear",
inputs=[current_tensor],
outputs=[fc2_out],
attributes={
"in_features": 50,
"out_features": 10,
"weights_key": "fc2_weight",
"bias_key": "fc2_bias"
}
))
current_tensor = fc2_out
# LogSoftmax
self.ir.add_node(IRNode(
op_type="log_softmax",
inputs=[current_tensor],
outputs=["output"],
attributes={
"dim": 1
}
))
def create_sample_model():
"""Create and save a sample MNIST model"""
class SimpleCNN(torch.nn.Module):
def __init__(self):
super(SimpleCNN, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=3)
self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=3)
self.fc1 = torch.nn.Linear(20 * 5 * 5, 50)
self.fc2 = torch.nn.Linear(50, 10)
def forward(self, x):
x = torch.relu(self.conv1(x))
x = torch.max_pool2d(x, 2)
x = torch.relu(self.conv2(x))
x = torch.max_pool2d(x, 2)
x = x.view(-1, 20 * 5 * 5)
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return torch.log_softmax(x, dim=1)
model = SimpleCNN()
model.eval()
# Create dummy input
dummy_input = torch.randn(1, 1, 28, 28)
model_path = "mnist_cnn.pt"
# Export model
traced_model = torch.jit.trace(model, dummy_input)
traced_model.save(model_path)
print(f"Created sample model: {model_path}")
return model_path
if __name__ == "__main__":
import sys
if len(sys.argv) != 3:
# If no arguments provided, create a sample model
if len(sys.argv) == 1:
model_path = create_sample_model()
output_prefix = "mnist_cnn_ir"
else:
print("Usage: python model_converter.py <input_model.pt> <output_prefix>")
sys.exit(1)
else:
model_path = sys.argv[1]
output_prefix = sys.argv[2]
converter = ModelConverter()
converter.convert(model_path, output_prefix)