-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_executor.py
More file actions
436 lines (329 loc) · 17.3 KB
/
Copy pathenhanced_executor.py
File metadata and controls
436 lines (329 loc) · 17.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env python3
"""
Enhanced Executor with Advanced Fusion Support
Supports Conv2D+ReLU, Conv2D+BN+ReLU, and optimized kernel implementations
"""
import sys
import os
# Add the parent directory to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
parent_dir = os.path.dirname(current_dir)
sys.path.insert(0, parent_dir)
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Dict, Any, List, Tuple, Optional
class EnhancedOptimizedExecutor:
"""Enhanced executor with advanced fusion and optimization support"""
def __init__(self, original_model: nn.Module = None):
self.original_model = original_model
self.debug = True
def run_mir_enhanced(self, mir: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""
Execute MIR graph with enhanced optimizations
Features:
- Conv2D+ReLU fusion
- Conv2D+BatchNorm+ReLU fusion
- Linear+ReLU fusion
- Per-channel quantization
- PyTorch kernel acceleration
"""
if self.debug:
print("🚀 ENHANCED OPTIMIZED EXECUTION")
print("=" * 60)
print("Features: Advanced Fusion + Per-Channel Quantization + PyTorch Kernels")
# Count fusion types
fusion_counts = {}
for node in mir['graph']['nodes']:
if 'fusion_info' in node:
fusion_type = node['fusion_info']['type']
fusion_counts[fusion_type] = fusion_counts.get(fusion_type, 0) + 1
if fusion_counts:
print("🔧 Fusion optimizations:")
for fusion_type, count in fusion_counts.items():
print(f" {fusion_type}: {count} fusions")
print(f"Input: {input_tensor.shape}, range=[{input_tensor.min():.4f}, {input_tensor.max():.4f}]")
# Track activations and intermediate results
activations = {'input': input_tensor}
current_tensor = input_tensor
# Apply input quantization if specified
if 'input_quantization' in mir['metadata']:
input_scale = mir['metadata']['input_quantization']['scale']
if self.debug:
print(f"Input scale: {input_scale:.6f}")
# Note: Input quantization simulation (actual quantization would convert to int8)
# Execute each node in the graph
nodes = mir['graph']['nodes']
for i, node in enumerate(nodes):
node_id = node['id']
op_type = node['op_type']
if self.debug:
print(f"Executing enhanced node {i}: {node_id} ({op_type})")
# Execute based on operation type
if op_type == 'conv2d':
current_tensor = self._execute_conv2d(node, current_tensor)
elif op_type == 'conv2d_relu_fused':
current_tensor = self._execute_conv2d_relu_fused(node, current_tensor)
elif op_type == 'conv2d_bn_relu_fused':
current_tensor = self._execute_conv2d_bn_relu_fused(node, current_tensor)
elif op_type == 'batchnorm2d':
current_tensor = self._execute_batchnorm2d(node, current_tensor)
elif op_type == 'relu':
current_tensor = self._execute_relu(node, current_tensor)
elif op_type == 'linear':
current_tensor = self._execute_linear(node, current_tensor)
elif op_type == 'linear_relu_fused':
current_tensor = self._execute_linear_relu_fused(node, current_tensor)
elif op_type == 'maxpool2d':
current_tensor = self._execute_maxpool2d(node, current_tensor)
elif op_type == 'max_pool2d':
current_tensor = self._execute_maxpool2d(node, current_tensor)
elif op_type == 'avgpool2d':
current_tensor = self._execute_avgpool2d(node, current_tensor)
elif op_type == 'avg_pool2d':
current_tensor = self._execute_avgpool2d(node, current_tensor)
elif op_type == 'adaptive_avg_pool2d':
current_tensor = self._execute_adaptive_avg_pool2d(node, current_tensor)
elif op_type == 'flatten':
current_tensor = self._execute_flatten(node, current_tensor)
elif op_type == 'dropout':
current_tensor = self._execute_dropout(node, current_tensor)
else:
if self.debug:
print(f" ⚠️ Unsupported operation: {op_type}")
# Pass through unchanged for unsupported ops
# Apply activation quantization if present
if 'activation_quantization' in node:
scale = node['activation_quantization']['scale']
current_tensor = self._quantize_activation(current_tensor, scale)
# Store activation for debugging
activations[node_id] = current_tensor
if self.debug:
print(f" Output: {current_tensor.shape}, range=[{current_tensor.min():.4f}, {current_tensor.max():.4f}]")
if self.debug:
print(f"\n✅ Enhanced execution complete: {current_tensor}")
return current_tensor
def _execute_conv2d(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute Conv2D operation with per-channel quantization"""
weights = node.get('weights', {})
if 'weight' in weights and 'bias' in weights:
weight = weights['weight']
bias = weights['bias']
# Get convolution parameters from node params or defaults
params = node.get('params', {})
stride = params.get('stride', [1, 1]) if 'stride' in params else node.get('stride', [1, 1])
padding = params.get('padding', [0, 0]) if 'padding' in params else node.get('padding', [0, 0])
dilation = node.get('dilation', [1, 1])
groups = params.get('groups', 1) if 'groups' in params else node.get('groups', 1)
# Debug tensor shapes
if self.debug:
print(f" Conv2D: input {input_tensor.shape}, weight {weight.shape}")
print(f" Params: stride={stride}, padding={padding}, groups={groups}")
# Apply convolution using PyTorch
output = F.conv2d(input_tensor, weight, bias, stride, padding, dilation, groups)
if self.debug:
if 'per_channel_quantization' in node:
channels = weight.shape[0]
print(f" ✅ Conv2D (per-channel), channels={channels}")
else:
print(f" ✅ Conv2D applied")
return output
else:
if self.debug:
print(f" ⚠️ Conv2D weights not found in node {node.get('id', 'unknown')}")
print(f" Available keys: {list(node.keys())}")
if 'weights' in node:
print(f" Weight keys: {list(node['weights'].keys())}")
return input_tensor
def _execute_conv2d_relu_fused(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute fused Conv2D + ReLU operation"""
# First apply Conv2D
conv_output = self._execute_conv2d(node, input_tensor)
# Then apply ReLU
output = F.relu(conv_output)
if self.debug:
print(f" ✅ Fused Conv2D+ReLU applied")
return output
def _execute_conv2d_bn_relu_fused(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute fused Conv2D + BatchNorm + ReLU operation"""
# Apply Conv2D (which should have folded BatchNorm parameters)
conv_output = self._execute_conv2d(node, input_tensor)
# Apply ReLU (BatchNorm is folded into Conv2D weights)
output = F.relu(conv_output)
if self.debug:
print(f" ✅ Fused Conv2D+BatchNorm+ReLU applied")
return output
def _execute_batchnorm2d(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute BatchNorm2D operation (should be folded in optimized graph)"""
weights = node.get('weights', {})
if all(k in weights for k in ['weight', 'bias', 'running_mean', 'running_var']):
weight = weights['weight']
bias = weights['bias']
running_mean = weights['running_mean']
running_var = weights['running_var']
eps = node.get('eps', 1e-5)
output = F.batch_norm(input_tensor, running_mean, running_var, weight, bias,
training=False, eps=eps)
if self.debug:
print(f" ✅ BatchNorm2D applied")
return output
else:
if self.debug:
print(f" ⚠️ BatchNorm2D parameters not found")
return input_tensor
def _execute_relu(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute ReLU activation"""
output = F.relu(input_tensor)
if self.debug:
print(f" ✅ ReLU applied")
return output
def _execute_linear(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute Linear operation with per-channel quantization"""
weights = node.get('weights', {})
if 'weight' in weights and 'bias' in weights:
weight = weights['weight']
bias = weights['bias']
output = F.linear(input_tensor, weight, bias)
if self.debug:
if 'per_channel_quantization' in node:
channels = weight.shape[0]
print(f" ✅ Linear (per-channel), channels={channels}")
else:
print(f" ✅ Linear applied")
return output
else:
if self.debug:
print(f" ⚠️ Linear weights not found")
return input_tensor
def _execute_linear_relu_fused(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute fused Linear + ReLU operation"""
# First apply Linear
linear_output = self._execute_linear(node, input_tensor)
# Then apply ReLU
output = F.relu(linear_output)
if self.debug:
print(f" ✅ Fused Linear+ReLU applied")
return output
def _execute_maxpool2d(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute MaxPool2D operation"""
# Get parameters from node params or defaults
params = node.get('params', {})
kernel_size = params.get('kernel_size', [2, 2]) if 'kernel_size' in params else node.get('kernel_size', [2, 2])
stride = params.get('stride', kernel_size) if 'stride' in params else node.get('stride', kernel_size)
padding = params.get('padding', [0, 0]) if 'padding' in params else node.get('padding', [0, 0])
# Ensure proper format for PyTorch
if isinstance(kernel_size, list) and len(kernel_size) == 1:
kernel_size = kernel_size[0]
if isinstance(stride, list) and len(stride) == 1:
stride = stride[0]
if isinstance(padding, list) and len(padding) == 1:
padding = padding[0]
output = F.max_pool2d(input_tensor, kernel_size, stride, padding)
if self.debug:
print(f" ✅ MaxPool2D applied, kernel_size={kernel_size}, stride={stride}")
return output
def _execute_avgpool2d(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute AvgPool2D operation"""
# Get parameters from node params or defaults
params = node.get('params', {})
kernel_size = params.get('kernel_size', [2, 2]) if 'kernel_size' in params else node.get('kernel_size', [2, 2])
stride = params.get('stride', kernel_size) if 'stride' in params else node.get('stride', kernel_size)
padding = params.get('padding', [0, 0]) if 'padding' in params else node.get('padding', [0, 0])
# Ensure proper format for PyTorch
if isinstance(kernel_size, list) and len(kernel_size) == 1:
kernel_size = kernel_size[0]
if isinstance(stride, list) and len(stride) == 1:
stride = stride[0]
if isinstance(padding, list) and len(padding) == 1:
padding = padding[0]
output = F.avg_pool2d(input_tensor, kernel_size, stride, padding)
if self.debug:
print(f" ✅ AvgPool2D applied, kernel_size={kernel_size}, stride={stride}")
return output
def _execute_adaptive_avg_pool2d(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute AdaptiveAvgPool2D operation"""
# Get output_size from params (MIR format) or directly from node (HIR format)
params = node.get('params', {})
output_size = params.get('output_size', node.get('output_size', [1, 1]))
output = F.adaptive_avg_pool2d(input_tensor, output_size)
if self.debug:
print(f" ✅ AdaptiveAvgPool2D applied, output_size={output_size}")
return output
def _execute_flatten(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute Flatten operation"""
start_dim = node.get('start_dim', 1)
end_dim = node.get('end_dim', -1)
output = torch.flatten(input_tensor, start_dim, end_dim)
if self.debug:
print(f" ✅ Flatten applied, start_dim={start_dim}, end_dim={end_dim}")
return output
def _execute_dropout(self, node: Dict[str, Any], input_tensor: torch.Tensor) -> torch.Tensor:
"""Execute Dropout operation (no-op during inference)"""
if self.debug:
print(f" ✅ Dropout (inference mode - no-op)")
return input_tensor
def _quantize_activation(self, tensor: torch.Tensor, scale: float) -> torch.Tensor:
"""Apply activation quantization simulation"""
if self.debug:
print(f" 📊 Activation quantized with scale={scale:.6f}")
# Quantization simulation (actual implementation would use int8)
quantized = torch.round(tensor / scale) * scale
return quantized
def run_enhanced_optimized_execution(mir: Dict[str, Any], input_tensor: torch.Tensor,
original_model: nn.Module = None) -> torch.Tensor:
"""
Run enhanced optimized execution with advanced fusion support
Args:
mir: MIR graph with enhanced optimizations
input_tensor: Input tensor
original_model: Original PyTorch model (optional)
Returns:
Output tensor from enhanced optimized execution
"""
executor = EnhancedOptimizedExecutor(original_model)
return executor.run_mir_enhanced(mir, input_tensor)
if __name__ == "__main__":
# Test enhanced executor
from parser.pytorch_parser import parse_pytorch_model
from passes.lowerer import hir_to_mir
from passes.enhanced_fusion import enhanced_fusion_pass
from passes.batchnorm_folding import batchnorm_folding_pass
# Create test model
class TestEnhancedCNN(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.relu2 = nn.ReLU()
self.pool = nn.AdaptiveAvgPool2d((4, 4))
self.flatten = nn.Flatten()
self.fc = nn.Linear(64 * 4 * 4, 10)
self.relu3 = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.pool(x)
x = self.flatten(x)
x = self.fc(x)
x = self.relu3(x)
return x
model = TestEnhancedCNN()
input_tensor = torch.randn(1, 3, 32, 32)
# Parse and optimize
hir = parse_pytorch_model(model, input_tensor)
mir = hir_to_mir(hir.to_dict())
print("Applying optimizations...")
# Apply BatchNorm folding first
folded_mir = batchnorm_folding_pass(mir, model)
# Then apply enhanced fusion
optimized_mir = enhanced_fusion_pass(folded_mir)
# Test execution
print("\nTesting enhanced execution...")
output = run_enhanced_optimized_execution(optimized_mir, input_tensor, model)
print(f"\nFinal output shape: {output.shape}")
print(f"Output range: [{output.min():.4f}, {output.max():.4f}]")