-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplement_kernel_fusion.py
More file actions
494 lines (396 loc) · 14.7 KB
/
implement_kernel_fusion.py
File metadata and controls
494 lines (396 loc) · 14.7 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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env python3
"""
Kernel Fusion Implementation for ProteinMPNN
Strategy: Fuse message passing operations into single Metal kernel
Goal: Reduce memory bandwidth bottleneck via MLX custom operations
This implementation demonstrates kernel fusion for the message passing step.
"""
import sys
from pathlib import Path
print("=" * 70)
print("KERNEL FUSION IMPLEMENTATION")
print("=" * 70)
# Check dependencies
print("\n1. CHECKING DEPENDENCIES")
print("-" * 70)
deps_available = True
try:
import torch
import torch.nn as nn
print(f"✅ PyTorch {torch.__version__}")
TORCH_AVAILABLE = True
except ImportError:
print("❌ PyTorch not available")
deps_available = False
TORCH_AVAILABLE = False
try:
import mlx
import mlx.core as mx
import mlx.nn as mlxnn
print(f"✅ MLX {mlx.__version__}")
MLX_AVAILABLE = True
except ImportError:
print("❌ MLX not available - install with: pip install mlx")
deps_available = False
MLX_AVAILABLE = False
if not deps_available:
print("\n⚠️ Missing dependencies. Please install:")
print(" pip install mlx torch")
print("\nProceeding with implementation design...")
print("\n2. MESSAGE PASSING OPERATION ANALYSIS")
print("-" * 70)
print("""
Current ProteinMPNN message passing (non-fused):
Step 1: Gather neighbors
h_neighbors = h[E_idx] # Shape: (B, L, k, D)
→ Memory reads: B*L*k*D values
Step 2: Compute edge features
edge_features = EdgeFeatures(X, E_idx) # Distances, angles
→ Memory reads: B*L*k*3 values
→ Compute: Distance calculations
→ Memory writes: B*L*k*edge_dim values
Step 3: Message MLP
messages = MessageMLP(concat(h_i, h_neighbors, edge_features))
→ Memory reads: B*L*k*(2*D + edge_dim) values
→ Compute: Linear + GELU + Linear
→ Memory writes: B*L*k*D values
Step 4: Aggregate
m_agg = messages.mean(dim=2) # Average over k neighbors
→ Memory reads: B*L*k*D values
→ Memory writes: B*L*D values
Step 5: Update MLP
h_update = UpdateMLP(m_agg)
→ Memory reads: B*L*D values
→ Compute: Linear + GELU + Linear
→ Memory writes: B*L*D values
Step 6: Residual + Norm
h_new = LayerNorm(h + h_update)
→ Memory reads: 2 * B*L*D values
→ Memory writes: B*L*D values
Total memory operations (unfused):
- Reads: ~8-10 passes over data
- Writes: ~4-5 passes over data
→ If 50% memory-bound: Fusion can provide 2-3x speedup
""")
print("\n3. FUSED KERNEL DESIGN")
print("-" * 70)
print("""
Fused Message Passing Kernel:
Input:
- h: Node features (B, L, D)
- X: Node coordinates (B, L, 3)
- E_idx: Neighbor indices (B, L, k)
- Weights: MLP weights (W1, b1, W2, b2, W3, b3, W4, b4)
- Mask: Node mask (B, L)
Output:
- h_new: Updated node features (B, L, D)
Pseudo-code for fused kernel:
```metal
kernel void fused_message_passing(
device const float* h [[buffer(0)]],
device const float* X [[buffer(1)]],
device const int* E_idx [[buffer(2)]],
device const float* weights [[buffer(3)]],
device float* h_out [[buffer(4)]],
uint2 gid [[thread_position_in_grid]])
{
// Each thread processes one node
int b = gid.y; // Batch
int i = gid.x; // Node index
// Load node feature into threadgroup memory (tile memory)
threadgroup float h_local[D];
load_vector(h_local, &h[b*L*D + i*D], D);
// Message passing loop
float messages[D] = {0};
for (int k_idx = 0; k_idx < k; k_idx++) {
// 1. Gather neighbor
int j = E_idx[b*L*k + i*k + k_idx];
threadgroup float h_j[D];
load_vector(h_j, &h[b*L*D + j*D], D);
// 2. Compute edge features (in registers)
float3 pos_i = load_float3(&X[b*L*3 + i*3]);
float3 pos_j = load_float3(&X[b*L*3 + j*3]);
float dist = distance(pos_i, pos_j);
float3 direction = normalize(pos_j - pos_i);
// 3. Message MLP (all in registers/tile memory)
float concat[2*D + 4]; // [h_i, h_j, dist, direction]
copy(concat, h_local, D);
copy(concat + D, h_j, D);
concat[2*D] = dist;
concat[2*D + 1] = direction.x;
concat[2*D + 2] = direction.y;
concat[2*D + 3] = direction.z;
// MLP: W1(concat) + b1
float hidden[2*D];
matmul(hidden, weights.W1, concat, 2*D, 2*D+4);
add_bias(hidden, weights.b1, 2*D);
gelu(hidden, 2*D);
// MLP: W2(hidden) + b2
float message[D];
matmul(message, weights.W2, hidden, D, 2*D);
add_bias(message, weights.b2, D);
// 4. Accumulate message (in registers)
add_vectors(messages, message, D);
}
// 5. Average messages
scale_vector(messages, 1.0 / k, D);
// 6. Update MLP
float hidden2[2*D];
matmul(hidden2, weights.W3, messages, 2*D, D);
add_bias(hidden2, weights.b3, 2*D);
gelu(hidden2, 2*D);
float h_update[D];
matmul(h_update, weights.W4, hidden2, D, 2*D);
add_bias(h_update, weights.b4, D);
// 7. Residual + LayerNorm
float h_new[D];
add_vectors(h_new, h_local, h_update, D);
layer_norm(h_new, D); // In-place
// 8. Write output (single write)
store_vector(&h_out[b*L*D + i*D], h_new, D);
}
```
Memory operations (fused):
- Reads: 2 passes (h, X once; E_idx for indices)
- Writes: 1 pass (h_out)
→ 3 total passes vs 12-15 unfused
Expected speedup:
- If 80% memory-bound: 4-5x reduction in memory traffic → 3-4x speedup
- If 50% memory-bound: 4-5x reduction → 2-2.5x speedup
- Realistic: 1.5-2.5x on message passing step
- If message passing is 60% of time: 1.3-1.5x overall
""")
print("\n4. MLX IMPLEMENTATION")
print("-" * 70)
if MLX_AVAILABLE and TORCH_AVAILABLE:
print("Implementing fused message passing in MLX...\n")
# MLX Implementation
class FusedMessagePassing(mlxnn.Module):
"""
Fused message passing operation using MLX.
This combines gather, edge features, message MLP, aggregation,
update MLP, and layer norm into a single operation.
"""
def __init__(self, hidden_dim, k_neighbors):
super().__init__()
self.hidden_dim = hidden_dim
self.k_neighbors = k_neighbors
# Message MLP
self.message_mlp = mlxnn.Sequential(
mlxnn.Linear(2 * hidden_dim + 4, 2 * hidden_dim),
mlxnn.GELU(),
mlxnn.Linear(2 * hidden_dim, hidden_dim)
)
# Update MLP
self.update_mlp = mlxnn.Sequential(
mlxnn.Linear(hidden_dim, 2 * hidden_dim),
mlxnn.GELU(),
mlxnn.Linear(2 * hidden_dim, hidden_dim)
)
# Layer norm
self.norm = mlxnn.LayerNorm(hidden_dim)
def __call__(self, h, X, E_idx, mask):
"""
Fused message passing.
Args:
h: Node features (B, L, D)
X: Node coordinates (B, L, 3)
E_idx: Neighbor indices (B, L, k)
mask: Node mask (B, L)
Returns:
h_new: Updated features (B, L, D)
"""
B, L, D = h.shape
k = self.k_neighbors
# This would be implemented as a custom Metal kernel
# For now, showing the logical flow:
# 1. Gather neighbors
# h_neighbors = h[E_idx] # (B, L, k, D)
h_flat = h.reshape(B * L, D)
E_idx_flat = E_idx.reshape(B * L * k)
h_neighbors = h_flat[E_idx_flat].reshape(B, L, k, D)
# 2. Compute edge features
X_i = mx.expand_dims(X, 2) # (B, L, 1, 3)
X_j_indices = E_idx.reshape(B, L * k)
X_flat = X.reshape(B * L, 3)
X_j = mx.take(X_flat, X_j_indices, axis=0).reshape(B, L, k, 3)
# Edge features: distance and direction
delta = X_j - X_i # (B, L, k, 3)
dist = mx.sqrt(mx.sum(delta ** 2, axis=-1, keepdims=True)) # (B, L, k, 1)
direction = delta / (dist + 1e-8) # (B, L, k, 3)
edge_features = mx.concatenate([dist, direction], axis=-1) # (B, L, k, 4)
# 3. Message MLP
h_i = mx.expand_dims(h, 2) # (B, L, 1, D)
h_i = mx.broadcast_to(h_i, (B, L, k, D))
message_input = mx.concatenate([h_i, h_neighbors, edge_features], axis=-1)
messages = self.message_mlp(message_input) # (B, L, k, D)
# 4. Aggregate
m_agg = mx.mean(messages, axis=2) # (B, L, D)
# 5. Update MLP
h_update = self.update_mlp(m_agg) # (B, L, D)
# 6. Residual + Norm
h_new = self.norm(h + h_update)
# Apply mask
mask_expanded = mx.expand_dims(mask, -1)
h_new = h_new * mask_expanded
return h_new
print("✅ MLX FusedMessagePassing class defined")
print(" This is the logical implementation.")
print(" For true fusion, would need custom Metal kernel via @mx.custom_vjp")
else:
print("⚠️ Cannot implement - MLX or PyTorch not available")
print(" Showing design only")
print("\n5. PYTORCH BASELINE FOR COMPARISON")
print("-" * 70)
if TORCH_AVAILABLE:
print("Implementing PyTorch baseline (unfused)...\n")
class UnfusedMessagePassing(nn.Module):
"""Standard PyTorch message passing (unfused)."""
def __init__(self, hidden_dim, k_neighbors):
super().__init__()
self.hidden_dim = hidden_dim
self.k_neighbors = k_neighbors
# Message MLP
self.message_mlp = nn.Sequential(
nn.Linear(2 * hidden_dim + 4, 2 * hidden_dim),
nn.GELU(),
nn.Linear(2 * hidden_dim, hidden_dim)
)
# Update MLP
self.update_mlp = nn.Sequential(
nn.Linear(hidden_dim, 2 * hidden_dim),
nn.GELU(),
nn.Linear(2 * hidden_dim, hidden_dim)
)
# Layer norm
self.norm = nn.LayerNorm(hidden_dim)
def forward(self, h, X, E_idx, mask):
"""Unfused message passing."""
B, L, D = h.shape
k = self.k_neighbors
# 1. Gather neighbors (memory read)
h_flat = h.reshape(B * L, D)
E_idx_flat = E_idx.reshape(B * L * k)
h_neighbors = torch.index_select(h_flat, 0, E_idx_flat).reshape(B, L, k, D)
# 2. Edge features (memory read + write)
X_i = X.unsqueeze(2)
X_j = torch.index_select(
X.reshape(B * L, 3), 0, E_idx.reshape(B, L * k)
).reshape(B, L, k, 3)
delta = X_j - X_i
dist = torch.sqrt(torch.sum(delta ** 2, dim=-1, keepdim=True))
direction = delta / (dist + 1e-8)
edge_features = torch.cat([dist, direction], dim=-1)
# 3. Message MLP (memory read + write)
h_i = h.unsqueeze(2).expand(-1, -1, k, -1)
message_input = torch.cat([h_i, h_neighbors, edge_features], dim=-1)
messages = self.message_mlp(message_input)
# 4. Aggregate (memory read + write)
m_agg = messages.mean(dim=2)
# 5. Update MLP (memory read + write)
h_update = self.update_mlp(m_agg)
# 6. Residual + Norm (memory read + write)
h_new = self.norm(h + h_update)
# Apply mask
h_new = h_new * mask.unsqueeze(-1)
return h_new
print("✅ PyTorch UnfusedMessagePassing class defined")
else:
print("⚠️ Cannot implement - PyTorch not available")
print("\n6. BENCHMARKING PLAN")
print("-" * 70)
print("""
Benchmark setup:
1. Create test inputs: h, X, E_idx, mask
2. Initialize both models with same weights
3. Warmup: 10 runs each
4. Timing: 100 runs each with proper synchronization
5. Compare: PyTorch MPS vs MLX fused
Metrics:
- Time per forward pass (ms)
- Speedup ratio (PyTorch / MLX)
- Memory bandwidth utilization
- GPU kernel execution time
Expected results:
- If memory-bound (likely): 1.5-2.5x speedup
- If compute-bound: 1.1-1.3x speedup
- If overhead-bound: 0.9-1.1x (no benefit)
Success criteria:
- >1.2x speedup on forward pass
- Numerically equivalent outputs (< 1e-5 difference)
- Stable timing (low variance)
""")
print("\n7. INTEGRATION WITH PROTEINMPNN")
print("-" * 70)
print("""
If kernel fusion is successful:
1. Replace message passing in encoder layers
- Current: 3 encoder layers × message passing
- Fused: 3 × FusedMessagePassing
2. Replace message passing in decoder layers
- Current: 3 decoder layers × message passing
- Fused: 3 × FusedMessagePassing
3. Full model speedup estimate:
- If message passing is 60% of time
- If fusion gives 2x on message passing
- Overall: 1 / (0.4 + 0.6/2) = 1 / 0.7 = 1.43x speedup
4. Combined with EXTREME-v2 (8.18x):
- New total: 8.18 × 1.43 = 11.7x speedup
5. Throughput:
- Current: 55,613 res/sec
- With fusion: 79,526 res/sec
""")
print("\n8. IMPLEMENTATION STATUS")
print("-" * 70)
if MLX_AVAILABLE and TORCH_AVAILABLE:
print("✅ Dependencies available")
print("✅ Design complete")
print("✅ Logical implementation complete")
print("⚠️ Custom Metal kernel required for true fusion")
print("⚠️ Benchmarking requires test data")
print("\nNext steps:")
print("1. Implement custom Metal kernel via MLX")
print("2. Create benchmark script")
print("3. Test on real ProteinMPNN data")
print("4. Measure actual speedup")
else:
print("❌ Dependencies not available")
print("✅ Design documented")
print("✅ Implementation plan complete")
print("\nTo proceed:")
print("1. Install MLX: pip install mlx")
print("2. Ensure PyTorch with MPS")
print("3. Run this script again")
print("\n" + "=" * 70)
print("KERNEL FUSION IMPLEMENTATION DESIGN COMPLETE")
print("=" * 70)
# Save design summary
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
design_summary = {
'optimization': 'Kernel Fusion',
'strategy': 'Fuse message passing into single Metal kernel',
'operations_fused': [
'Gather neighbors',
'Compute edge features',
'Message MLP',
'Aggregate messages',
'Update MLP',
'Residual + LayerNorm'
],
'memory_reduction': '12-15 passes → 3 passes',
'expected_speedup': {
'message_passing': '1.5-2.5x',
'full_model': '1.3-1.5x',
'combined_with_extreme_v2': '11.7x (from 8.18x)'
},
'implementation_status': 'Design complete, requires custom Metal kernel',
'dependencies': {
'mlx': MLX_AVAILABLE if 'MLX_AVAILABLE' in locals() else False,
'pytorch': TORCH_AVAILABLE if 'TORCH_AVAILABLE' in locals() else False
}
}
import json
with open(output_dir / 'kernel_fusion_design.json', 'w') as f:
json.dump(design_summary, f, indent=2)
print(f"\n✅ Design summary saved to: {output_dir / 'kernel_fusion_design.json'}")