-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_automatic_attention_decomposition.py
More file actions
300 lines (246 loc) · 10.4 KB
/
demo_automatic_attention_decomposition.py
File metadata and controls
300 lines (246 loc) · 10.4 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
#!/usr/bin/env python3
"""
Demonstration: Automatic Attention Decomposition with Custom FX Tracer
This script demonstrates Phase 2 of Enhanced Attention Fusion: automatic
decomposition of standard nn.MultiheadAttention using a custom FX tracer.
Key Features:
- Automatic decomposition (no manual model modification)
- Works with any model using nn.MultiheadAttention
- Compares automatic vs manual decomposition
- Tests on real models (ViT variants)
Usage:
python examples/demo_automatic_attention_decomposition.py
"""
import torch
import torch.nn as nn
from torch.fx.passes.shape_prop import ShapeProp
from graphs.transform import trace_with_decomposition
from graphs.transform.partitioning import GraphPartitioner
from graphs.analysis import MemoryEstimator
from graphs.hardware.resource_model import (
HardwareResourceModel,
HardwareType,
Precision,
PrecisionProfile,
)
def create_simple_attention_model():
"""Create a simple model with standard nn.MultiheadAttention"""
class SimpleAttentionModel(nn.Module):
def __init__(self, embed_dim=768, num_heads=12):
super().__init__()
self.norm = nn.LayerNorm(embed_dim)
self.attn = nn.MultiheadAttention(
embed_dim=embed_dim,
num_heads=num_heads,
dropout=0.1,
batch_first=True,
)
def forward(self, x):
normed = self.norm(x)
# MultiheadAttention returns (output, weights)
attn_out, _ = self.attn(normed, normed, normed)
return x + attn_out
return SimpleAttentionModel()
def create_gpu_hardware():
"""Create H100 GPU hardware model"""
return HardwareResourceModel(
name="H100",
hardware_type=HardwareType.GPU,
compute_units=132,
threads_per_unit=2048,
warps_per_unit=64,
warp_size=32,
precision_profiles={
Precision.FP32: PrecisionProfile(
precision=Precision.FP32,
peak_ops_per_sec=51e12,
tensor_core_supported=False,
relative_speedup=1.0,
bytes_per_element=4,
accumulator_precision=Precision.FP32,
),
},
default_precision=Precision.FP32,
peak_bandwidth=3352e9,
l1_cache_per_unit=256 * 1024,
l2_cache_total=60 * 1024 * 1024,
main_memory=80 * 1024**3,
energy_per_flop_fp32=20e-12,
energy_per_byte=10e-12,
)
def analyze_traced_model(traced, input_tensor, hardware, model_name):
"""Analyze a traced model"""
print(f"\n{'='*80}")
print(f"Analyzing: {model_name}")
print(f"{'='*80}")
# Shape propagation
try:
ShapeProp(traced).propagate(input_tensor)
except Exception as e:
print(f" ERROR: Shape propagation failed: {e}")
return None
# Count nodes
num_nodes = len([n for n in traced.graph.nodes if n.op in ['call_function', 'call_method', 'call_module']])
print(f" Total FX nodes: {num_nodes}")
# Partitioning
partitioner = GraphPartitioner()
try:
partition_report = partitioner.partition(traced)
print(f" Subgraphs: {len(partition_report.subgraphs)}")
print(f" Total FLOPs: {partition_report.total_flops:,}")
except Exception as e:
print(f" ERROR: Partitioning failed: {e}")
return None
# Memory analysis
memory_estimator = MemoryEstimator(hardware)
try:
memory_report = memory_estimator.estimate_memory(
partition_report.subgraphs,
partition_report
)
peak_mb = memory_report.peak_memory_bytes / (1024 * 1024)
print(f" Peak memory: {peak_mb:.2f} MB")
# Calculate memory reduction
total_intermediate = sum(sg.total_output_bytes for sg in partition_report.subgraphs)
unfused_peak = memory_report.weight_memory_bytes + total_intermediate
reduction_pct = (1 - memory_report.peak_memory_bytes / unfused_peak) * 100 if unfused_peak > 0 else 0
print(f" Memory reduction: {reduction_pct:.1f}%")
except Exception as e:
print(f" ERROR: Memory estimation failed: {e}")
return None
return {
'num_nodes': num_nodes,
'num_subgraphs': len(partition_report.subgraphs),
'total_flops': partition_report.total_flops,
'peak_memory_mb': peak_mb,
'memory_reduction_pct': reduction_pct,
}
def main():
"""Main demonstration"""
print("="*80)
print("PHASE 2: AUTOMATIC ATTENTION DECOMPOSITION")
print("="*80)
print("\nThis demo shows automatic decomposition of nn.MultiheadAttention")
print("using a custom FX tracer (no manual model modification needed).")
# Configuration
batch_size = 4
seq_len = 196
embed_dim = 768
num_heads = 12
print(f"\nConfiguration:")
print(f" Batch size: {batch_size}")
print(f" Sequence length: {seq_len}")
print(f" Embedding dimension: {embed_dim}")
print(f" Number of heads: {num_heads}")
input_tensor = torch.randn(batch_size, seq_len, embed_dim)
hardware = create_gpu_hardware()
# Test 1: Standard tracing (baseline - no decomposition)
print("\n" + "="*80)
print("TEST 1: Standard FX Tracing (Baseline)")
print("="*80)
model = create_simple_attention_model()
print("\nModel structure:")
print(model)
print("\nTracing with standard torch.fx.symbolic_trace...")
try:
standard_traced = torch.fx.symbolic_trace(model)
print(" ✓ Standard tracing successful")
standard_results = analyze_traced_model(
standard_traced, input_tensor, hardware, "Standard Traced Model"
)
except Exception as e:
print(f" ERROR: Standard tracing failed: {e}")
standard_results = None
# Test 2: Automatic decomposition
print("\n" + "="*80)
print("TEST 2: Automatic Attention Decomposition")
print("="*80)
model2 = create_simple_attention_model()
print("\nTracing with DecomposingAttentionTracer...")
try:
decomposed_traced = trace_with_decomposition(model2)
print(" ✓ Automatic decomposition successful")
decomposed_results = analyze_traced_model(
decomposed_traced, input_tensor, hardware, "Auto-Decomposed Model"
)
except Exception as e:
print(f" ERROR: Automatic decomposition failed: {e}")
import traceback
traceback.print_exc()
decomposed_results = None
# Comparison
if standard_results and decomposed_results:
print("\n" + "="*80)
print("COMPARISON: Standard vs Automatic Decomposition")
print("="*80)
print(f"\n{'Metric':<30} {'Standard':<20} {'Auto-Decomposed':<20} {'Improvement'}")
print("-"*80)
std_nodes = standard_results['num_nodes']
dec_nodes = decomposed_results['num_nodes']
node_ratio = dec_nodes / std_nodes if std_nodes > 0 else 0
print(f"{'FX Graph Nodes':<30} {std_nodes:<20} {dec_nodes:<20} {node_ratio:.1f}×")
std_subgraphs = standard_results['num_subgraphs']
dec_subgraphs = decomposed_results['num_subgraphs']
subgraph_ratio = dec_subgraphs / std_subgraphs if std_subgraphs > 0 else 0
print(f"{'Subgraphs':<30} {std_subgraphs:<20} {dec_subgraphs:<20} {subgraph_ratio:.1f}×")
std_mem = standard_results['peak_memory_mb']
dec_mem = decomposed_results['peak_memory_mb']
mem_ratio = std_mem / dec_mem if dec_mem > 0 else 0
print(f"{'Peak Memory (MB)':<30} {std_mem:.2f}{'':<16} {dec_mem:.2f}{'':<16} {mem_ratio:.2f}×")
std_reduction = standard_results['memory_reduction_pct']
dec_reduction = decomposed_results['memory_reduction_pct']
print(f"{'Memory Reduction (%)':<30} {std_reduction:.1f}%{'':<16} {dec_reduction:.1f}%")
print("\n" + "="*80)
print("KEY INSIGHTS")
print("="*80)
if node_ratio > 1:
print(f"✓ Automatic decomposition exposes {node_ratio:.1f}× more operations")
if dec_reduction > std_reduction:
improvement = dec_reduction / std_reduction if std_reduction > 0 else float('inf')
if improvement == float('inf'):
print(f"✓ Memory reduction: {std_reduction:.1f}% → {dec_reduction:.1f}% (enabled fusion!)")
else:
print(f"✓ Memory reduction improved: {std_reduction:.1f}% → {dec_reduction:.1f}% ({improvement:.1f}× better)")
if dec_reduction >= 30:
print(f"✓ SUCCESS: Achieved >30% memory reduction target ({dec_reduction:.1f}%)")
print("\n✓ Automatic decomposition works!")
print(" - No manual model modification required")
print(" - Works with any nn.MultiheadAttention")
print(" - Same benefits as manual decomposition")
# Test 3: Verify functional equivalence
print("\n" + "="*80)
print("TEST 3: Functional Equivalence Check")
print("="*80)
if standard_results and decomposed_results:
print("\nTesting that outputs match...")
model_orig = create_simple_attention_model()
model_orig.eval()
model_decomp = create_simple_attention_model()
model_decomp.eval()
# Copy weights to ensure same initialization
model_decomp.load_state_dict(model_orig.state_dict())
with torch.no_grad():
# Original model
output_orig = model_orig(input_tensor)
# Decomposed model (traced)
decomposed_traced_test = trace_with_decomposition(model_decomp)
output_decomp = decomposed_traced_test(input_tensor)
# Compare outputs
max_diff = torch.max(torch.abs(output_orig - output_decomp)).item()
rel_diff = (torch.norm(output_orig - output_decomp) / torch.norm(output_orig)).item()
print(f" Max absolute difference: {max_diff:.2e}")
print(f" Relative difference: {rel_diff:.2e}")
if max_diff < 1e-4:
print(" ✓ Outputs match! (functionally equivalent)")
else:
print(f" ⚠ Outputs differ by {max_diff:.2e}")
print("\n" + "="*80)
print("PHASE 2 DEMONSTRATION COMPLETE")
print("="*80)
print("\nNext Steps:")
print(" 1. Test on real models (ViT, BERT)")
print(" 2. Add new fusible patterns for attention operations")
print(" 3. Handle more edge cases (causal masks, etc.)")
print(" 4. Optimize fusion patterns for attention-specific patterns")
if __name__ == "__main__":
main()