-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimized_executor.py
More file actions
317 lines (249 loc) · 14.3 KB
/
Copy pathoptimized_executor.py
File metadata and controls
317 lines (249 loc) · 14.3 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
307
308
309
310
311
312
313
314
315
316
317
"""
Enhanced quantized executor with PyTorch kernels, per-channel quantization, and fusion
"""
import torch
import torch.nn.functional as F
from typing import Dict, Any
from executor.quantized_kernels import run_mir_quantized_fast
from passes.per_channel_quantize import linear_per_channel_quantized
from passes.fusion import execute_fused_linear_relu, execute_fused_conv_relu
def run_mir_optimized(mir: Dict[str, Any], model: torch.nn.Module,
input_tensor: torch.Tensor) -> torch.Tensor:
"""
Run MIR with all optimizations: PyTorch kernels, per-channel quantization, and fusion.
Args:
mir: MIR dictionary with optimizations applied
model: Original PyTorch model
input_tensor: Input tensor
Returns:
Output tensor after optimized execution
"""
print("🚀 OPTIMIZED QUANTIZED EXECUTION")
print("=" * 50)
print("Features: PyTorch Kernels + Per-Channel Quantization + Operator Fusion")
model.eval()
named_modules = dict(model.named_modules())
nodes = mir["graph"]["nodes"]
# Get quantization metadata
per_channel_quant = mir.get("metadata", {}).get("per_channel_quantization", {})
weight_quant = mir.get("metadata", {}).get("quantization", {})
act_quant = mir.get("metadata", {}).get("activation_quantization", {})
# Check if fusion was applied
fusion_applied = mir.get("metadata", {}).get("fusion_applied", False)
if fusion_applied:
fusions_count = mir.get("metadata", {}).get("fusions_count", 0)
print(f"🔧 Fusion optimizations: {fusions_count} fusions applied")
# Check if per-channel quantization is available
per_channel_available = len(per_channel_quant) > 0
if per_channel_available:
per_channel_count = len([k for k, v in per_channel_quant.items() if v.get('per_channel', False)])
print(f"📊 Per-channel quantization: {per_channel_count} layers optimized")
# Quantize input
input_range = input_tensor.max() - input_tensor.min()
input_scale = input_range.item() / 255.0 if input_range > 0 else 0.01
print(f"Input: {input_tensor.shape}, range=[{input_tensor.min():.4f}, {input_tensor.max():.4f}]")
print(f"Input scale: {input_scale:.6f}")
x = input_tensor.clone()
# Execute nodes with optimizations
for i, node in enumerate(nodes):
node_id = node["id"]
op_type = node["op_type"]
origin_name = node.get("metadata", {}).get("origin_name")
print(f"Executing optimized node {i}: {node_id} ({op_type})")
# Handle fused operations
if op_type == "linear_relu_fused":
x = _execute_fused_linear_relu_optimized(x, node, named_modules,
per_channel_quant, weight_quant)
elif op_type == "conv2d_relu_fused":
x = _execute_fused_conv_relu_optimized(x, node, named_modules,
per_channel_quant, weight_quant)
# Handle individual operations
elif op_type == "linear":
x = _execute_linear_optimized(x, node, named_modules,
per_channel_quant, weight_quant)
elif op_type == "conv2d":
x = _execute_conv2d_optimized(x, node, named_modules,
per_channel_quant, weight_quant)
elif op_type == "relu":
x = F.relu(x)
print(f" ✅ ReLU applied")
elif op_type == "adaptive_avg_pool2d":
# Handle AdaptiveAvgPool2d operations
params = node.get("params", {})
output_size = params.get("output_size", (1, 1))
x = F.adaptive_avg_pool2d(x, output_size)
print(f" ✅ AdaptiveAvgPool2d applied, output_size={output_size}")
elif op_type == "max_pool2d":
# Handle MaxPool2d operations
params = node.get("params", {})
kernel_size = params.get("kernel_size", 2)
stride = params.get("stride", None)
padding = params.get("padding", 0)
dilation = params.get("dilation", 1)
x = F.max_pool2d(x, kernel_size, stride, padding, dilation)
print(f" ✅ MaxPool2d applied, kernel_size={kernel_size}")
elif op_type == "avg_pool2d":
# Handle AvgPool2d operations
params = node.get("params", {})
kernel_size = params.get("kernel_size", 2)
stride = params.get("stride", None)
padding = params.get("padding", 0)
x = F.avg_pool2d(x, kernel_size, stride, padding)
print(f" ✅ AvgPool2d applied, kernel_size={kernel_size}")
elif op_type == "flatten":
# Handle Flatten operations
params = node.get("params", {})
start_dim = params.get("start_dim", 1)
end_dim = params.get("end_dim", -1)
x = torch.flatten(x, start_dim, end_dim)
print(f" ✅ Flatten applied, start_dim={start_dim}, end_dim={end_dim}")
else:
print(f" ⚠️ Unsupported operation: {op_type}")
# Apply activation quantization if available
if node_id in act_quant:
act_scale = act_quant[node_id]
x_quantized = torch.round(x / act_scale).clamp(-127, 127)
x = x_quantized * act_scale
print(f" 📊 Activation quantized with scale={act_scale:.6f}")
print(f" Output: {x.shape}, range=[{x.min():.4f}, {x.max():.4f}]")
print(f"\n✅ Optimized execution complete: {x}")
return x
def _execute_fused_linear_relu_optimized(x: torch.Tensor, node: Dict, named_modules: Dict,
per_channel_quant: Dict, weight_quant: Dict) -> torch.Tensor:
"""Execute fused Linear+ReLU with optimizations"""
origin_name = node.get("metadata", {}).get("origin_name")
if origin_name and origin_name in named_modules:
module = named_modules[origin_name]
weight = module.weight.data
bias = module.bias.data if module.bias is not None else None
# Check for per-channel quantization
weight_key = f"{origin_name}.weight"
bias_key = f"{origin_name}.bias"
if weight_key in per_channel_quant and per_channel_quant[weight_key].get("per_channel", False):
# Use per-channel quantization
weight_scales = torch.tensor(per_channel_quant[weight_key]["scales"])
bias_scale = per_channel_quant.get(bias_key, {}).get("scale", 0.01)
x = execute_fused_linear_relu(x, weight, bias, weight_scales, bias_scale, per_channel=True)
print(f" ✅ Fused Linear+ReLU (per-channel), channels={len(weight_scales)}")
elif weight_key in weight_quant:
# Use per-tensor quantization
weight_scale = weight_quant[weight_key]["scale"]
bias_scale = weight_quant.get(bias_key, {}).get("scale", 0.01)
x = execute_fused_linear_relu(x, weight, bias, weight_scale, bias_scale, per_channel=False)
print(f" ✅ Fused Linear+ReLU (per-tensor), scale={weight_scale:.6f}")
else:
# No quantization metadata, use float
x = execute_fused_linear_relu(x, weight, bias)
print(f" ✅ Fused Linear+ReLU (float)")
return x
def _execute_fused_conv_relu_optimized(x: torch.Tensor, node: Dict, named_modules: Dict,
per_channel_quant: Dict, weight_quant: Dict) -> torch.Tensor:
"""Execute fused Conv2D+ReLU with optimizations"""
origin_name = node.get("metadata", {}).get("origin_name")
if origin_name and origin_name in named_modules:
module = named_modules[origin_name]
weight = module.weight.data
bias = module.bias.data if module.bias is not None else None
stride = module.stride[0] if hasattr(module, 'stride') else 1
padding = module.padding[0] if hasattr(module, 'padding') else 0
# Check for per-channel quantization
weight_key = f"{origin_name}.weight"
bias_key = f"{origin_name}.bias"
if weight_key in per_channel_quant and per_channel_quant[weight_key].get("per_channel", False):
# Use per-channel quantization
weight_scales = torch.tensor(per_channel_quant[weight_key]["scales"])
bias_scale = per_channel_quant.get(bias_key, {}).get("scale", 0.01)
x = execute_fused_conv_relu(x, weight, bias, stride, padding,
weight_scales, bias_scale, per_channel=True)
print(f" ✅ Fused Conv2D+ReLU (per-channel), channels={len(weight_scales)}")
elif weight_key in weight_quant:
# Use per-tensor quantization
weight_scale = weight_quant[weight_key]["scale"]
bias_scale = weight_quant.get(bias_key, {}).get("scale", 0.01)
x = execute_fused_conv_relu(x, weight, bias, stride, padding,
weight_scale, bias_scale, per_channel=False)
print(f" ✅ Fused Conv2D+ReLU (per-tensor), scale={weight_scale:.6f}")
else:
# No quantization metadata, use float
x = execute_fused_conv_relu(x, weight, bias, stride, padding)
print(f" ✅ Fused Conv2D+ReLU (float)")
return x
def _execute_linear_optimized(x: torch.Tensor, node: Dict, named_modules: Dict,
per_channel_quant: Dict, weight_quant: Dict) -> torch.Tensor:
"""Execute Linear operation with optimizations"""
origin_name = node.get("metadata", {}).get("origin_name")
if origin_name and origin_name in named_modules:
module = named_modules[origin_name]
weight = module.weight.data
bias = module.bias.data if module.bias is not None else None
# Check for per-channel quantization
weight_key = f"{origin_name}.weight"
bias_key = f"{origin_name}.bias"
if weight_key in per_channel_quant and per_channel_quant[weight_key].get("per_channel", False):
# Use per-channel quantization
weight_scales = torch.tensor(per_channel_quant[weight_key]["scales"])
bias_scale = per_channel_quant.get(bias_key, {}).get("scale", 0.01)
x = linear_per_channel_quantized(x, weight, bias, weight_scales, bias_scale)
print(f" ✅ Linear (per-channel), channels={len(weight_scales)}")
elif weight_key in weight_quant:
# Use per-tensor quantization
weight_scale = weight_quant[weight_key]["scale"]
bias_scale = weight_quant.get(bias_key, {}).get("scale", 0.01)
# Quantize weights
weight_quantized = torch.round(weight / weight_scale).clamp(-127, 127)
weight_dequantized = weight_quantized * weight_scale
bias_dequantized = None
if bias is not None:
bias_quantized = torch.round(bias / bias_scale).clamp(-127, 127)
bias_dequantized = bias_quantized * bias_scale
x = F.linear(x, weight_dequantized, bias_dequantized)
print(f" ✅ Linear (per-tensor), scale={weight_scale:.6f}")
else:
# No quantization metadata, use float
x = F.linear(x, weight, bias)
print(f" ✅ Linear (float)")
return x
def _execute_conv2d_optimized(x: torch.Tensor, node: Dict, named_modules: Dict,
per_channel_quant: Dict, weight_quant: Dict) -> torch.Tensor:
"""Execute Conv2D operation with optimizations"""
origin_name = node.get("metadata", {}).get("origin_name")
if origin_name and origin_name in named_modules:
module = named_modules[origin_name]
weight = module.weight.data
bias = module.bias.data if module.bias is not None else None
stride = module.stride[0] if hasattr(module, 'stride') else 1
padding = module.padding[0] if hasattr(module, 'padding') else 0
# Check for per-channel quantization
weight_key = f"{origin_name}.weight"
bias_key = f"{origin_name}.bias"
if weight_key in per_channel_quant and per_channel_quant[weight_key].get("per_channel", False):
# Use per-channel quantization
from passes.per_channel_quantize import quantize_per_channel, dequantize_per_channel
weight_scales = torch.tensor(per_channel_quant[weight_key]["scales"])
bias_scale = per_channel_quant.get(bias_key, {}).get("scale", 0.01)
weight_quantized = quantize_per_channel(weight, weight_scales)
weight_dequantized = dequantize_per_channel(weight_quantized, weight_scales)
bias_dequantized = None
if bias is not None:
bias_quantized = torch.round(bias / bias_scale).clamp(-127, 127)
bias_dequantized = bias_quantized * bias_scale
x = F.conv2d(x, weight_dequantized, bias_dequantized, stride, padding)
print(f" ✅ Conv2D (per-channel), channels={len(weight_scales)}")
elif weight_key in weight_quant:
# Use per-tensor quantization
weight_scale = weight_quant[weight_key]["scale"]
bias_scale = weight_quant.get(bias_key, {}).get("scale", 0.01)
# Quantize weights
weight_quantized = torch.round(weight / weight_scale).clamp(-127, 127)
weight_dequantized = weight_quantized * weight_scale
bias_dequantized = None
if bias is not None:
bias_quantized = torch.round(bias / bias_scale).clamp(-127, 127)
bias_dequantized = bias_quantized * bias_scale
x = F.conv2d(x, weight_dequantized, bias_dequantized, stride, padding)
print(f" ✅ Conv2D (per-tensor), scale={weight_scale:.6f}")
else:
# No quantization metadata, use float
x = F.conv2d(x, weight, bias, stride, padding)
print(f" ✅ Conv2D (float)")
return x