-
Notifications
You must be signed in to change notification settings - Fork 16
/
metal_puzzles.py
788 lines (645 loc) · 19.7 KB
/
metal_puzzles.py
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import mlx.core as mx
from utils import MetalProblem, MetalKernel
############################################################
### Puzzle 1: Map
############################################################
# Implement a "kernel" (GPU function) that adds 10 to each
# position of the array `a` and stores it in the array `out`.
# You have 1 thread per position.
#
# Note: The `source` string below is the body of your Metal kernel, the
# function signature with be automatically generated for you. Below you'll
# notice the `input_names` and `output_names` parameters. These define the
# parameters for your Metal kernel.
#
# Tip: If you need a tool for debugging your kernel read the Metal Debugger
# section at the bottom of the README. Also, you can print out the generated
# Metal kernel by setting the environment variable `VERBOSE=1`.
def map_spec(a: mx.array):
return a + 10
def map_test(a: mx.array):
source = """
uint local_i = thread_position_in_grid.x;
// FILL ME IN (roughly 1 line)
"""
kernel = MetalKernel(
name="map",
input_names=["a"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 4
a = mx.arange(SIZE)
output_shape = (SIZE,)
problem = MetalProblem(
"Map",
map_test,
[a],
output_shape,
grid=(SIZE,1,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 2: Zip
############################################################
# Implement a kernel that takes two arrays `a` and `b`, adds each
# element together, and stores the result in an output array `out`.
# You have 1 thread per position.
def zip_spec(a: mx.array, b: mx.array):
return a + b
def zip_test(a: mx.array, b: mx.array):
source = """
uint local_i = thread_position_in_grid.x;
// FILL ME IN (roughly 1 line)
"""
kernel = MetalKernel(
name="zip",
input_names=["a", "b"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 4
a = mx.arange(SIZE)
b = mx.arange(SIZE)
output_shapes = (SIZE,)
problem = MetalProblem(
"Zip",
zip_test,
[a, b],
output_shapes,
grid=(SIZE,1,1),
spec=zip_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 3: Guard
############################################################
# Implement a kernel that adds 10 to each position of `a` and
# stores it in `out`. You have more threads than positions.
#
# Warning: Be careful of out-of-bounds access.
#
# Note: You can append `_shape`, `_strides`, or `_ndim` to any
# input parameter to automatically add that data as a paramter
# to your kerenls. So, in the following puzzle you could use
# `a_shape`, `a_strides`, or `a_ndim`.
def map_guard_test(a: mx.array):
source = """
uint local_i = thread_position_in_grid.x;
// FILL ME IN (roughly 1-3 lines)
"""
kernel = MetalKernel(
name="guard",
input_names=["a"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 4
a = mx.arange(SIZE)
output_shape = (SIZE,)
problem = MetalProblem(
"Guard",
map_guard_test,
[a],
output_shape,
grid=(8,1,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 4: Map 2D
############################################################
# Implement a kernel that adds 10 to each position of `a` and
# stores it in `out`. Input `a` is 2D and square. You have more
# threads than positions.
#
# Note: All memory in Metal is represented as a 1D array, so
# direct 2D indexing is not supported.
def map_2D_test(a: mx.array):
source = """
uint thread_x = thread_position_in_grid.x;
uint thread_y = thread_position_in_grid.y;
// FILL ME IN (roughly 4 lines)
"""
kernel = MetalKernel(
name="map_2D",
input_names=["a"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 2
a = mx.arange(SIZE * SIZE).reshape((SIZE, SIZE))
output_shape = (SIZE, SIZE)
problem = MetalProblem(
"Map 2D",
map_2D_test,
[a],
output_shape,
grid=(3,3,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 5: Broadcast
############################################################
# Implement a kernel that adds `a` and `b` and stores it in `out`.
# Inputs `a` and `b` are arrays. You have more threads than positions.
def broadcast_test(a: mx.array, b: mx.array):
source = """
uint thread_x = thread_position_in_grid.x;
uint thread_y = thread_position_in_grid.y;
// FILL ME IN (roughly 4 lines)
"""
kernel = MetalKernel(
name="broadcast",
input_names=["a", "b"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 2
a = mx.arange(SIZE).reshape(SIZE, 1)
b = mx.arange(SIZE).reshape(1, SIZE)
output_shape = (SIZE, SIZE)
problem = MetalProblem(
"Broadcast",
broadcast_test,
[a, b],
output_shape,
grid=(3,3,1),
spec=zip_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 6: Threadgroups
############################################################
# Implement a kernel that adds 10 to each position of `a` and
# stores it in `out`. You have fewer threads per threadgroup
# than the size of `a`, but more threads than positions.
#
# Note: A threadgroup is simply a group of threads within the
# thread grid. The number of threads per threadgroup is limited
# to a defined number, but we can have multiple different
# threadgroups. The Metal parameter `threadgroup_position_in_grid`
# tells us what threadgroup we are currently in.
def map_threadgroup_test(a: mx.array):
source = """
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
// FILL ME IN (roughly 1-3 lines)
"""
kernel = MetalKernel(
name="threadgroups",
input_names=["a"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 9
a = mx.arange(SIZE)
output_shape = (SIZE,)
problem = MetalProblem(
"Threadgroups",
map_threadgroup_test,
[a],
output_shape,
grid=(12,1,1),
threadgroup=(4,1,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 7: Threadgroups 2D
############################################################
# Implement the same kernel in 2D. You have fewer threads per
# threadgroup than the size of `a` in both directions, but more
# threads than positions in the grid.
def map_threadgroup_2D_test(a: mx.array):
source = """
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
// FILL ME IN (roughly 5 lines)
"""
kernel = MetalKernel(
name="threadgroups_2D",
input_names=["a"],
output_names=["out"],
source=source,
)
return kernel
SIZE = 5
a = mx.ones((SIZE, SIZE))
output_shape = (SIZE, SIZE)
problem = MetalProblem(
"Threadgroups 2D",
map_threadgroup_2D_test,
[a],
output_shape,
grid=(6,6,1),
threadgroup=(3,3,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 8: Threadgroup Memory
############################################################
# Implement a kernel that adds 10 to each position of `a` and
# stores it in `out`. You have fewer threads per block than
# the size of `a`.
#
# Warning: Each threadgroup can only have a *constant* amount
# of threadgroup memory that the threads can read and write to.
# After writing to threadgroup memory, you need to call
# `threadgroup_barrier(mem_flags::mem_threadgroup)` to ensure
# that threads are synchronized. In this puzzle we add the `header`
# variable as a new parameter to the `MetalKernel` object, which
# simply defines values outside of the kernel body (often used
# for header imports).
#
# For more information read section 4.4 "Threadgroup Address Space"
# and section 6.9 "Synchronization and SIMD-Group Functions" in the
# Metal Shading Language Specification.
#
# (This example does not really need threadgroup memory or synchronization,
# but it's a demo.)
def shared_test(a: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 4;
"""
source = """
threadgroup float shared[THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
if (i < a_shape[0]) {
shared[local_i] = a[i];
threadgroup_barrier(mem_flags::mem_threadgroup);
}
// FILL ME IN (roughly 1-3 lines)
"""
kernel = MetalKernel(
name="threadgroup_memory",
input_names=["a"],
output_names=["out"],
header=header,
source=source,
)
return kernel
SIZE = 8
a = mx.ones(SIZE)
output_shape = (SIZE,)
problem = MetalProblem(
"Threadgroup Memory",
shared_test,
[a],
output_shape,
grid=(SIZE,1,1),
threadgroup=(4,1,1),
spec=map_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 9: Pooling
############################################################
# Implement a kernel that sums together the last 3 position of
# `a` and stores it in `out`. You have 1 thread per position.
#
# Note: `threadgroup` memory is often faster than sharing data
# in `device` memory because it is located closer the the GPU's
# compute units. Be careful of uncessary reads and writes from
# global parameters (`a` and `out`), since their data is stored
# in `device` memory. You only need 1 global read and 1 global
# write per thread.
#
# Tip: Remember to be careful about syncing.
def pooling_spec(a: mx.array):
out = mx.zeros(*a.shape)
for i in range(a.shape[0]):
out[i] = a[max(i - 2, 0) : i + 1].sum()
return out
def pooling_test(a: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 8;
"""
source = """
threadgroup float shared[THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
// FILL ME IN (roughly 11 lines)
"""
kernel = MetalKernel(
name="pooling",
input_names=["a"],
output_names=["out"],
header=header,
source=source,
)
return kernel
SIZE = 8
a = mx.arange(SIZE)
output_shape = (SIZE,)
problem = MetalProblem(
"Pooling",
pooling_test,
[a],
output_shape,
grid=(SIZE,1,1),
threadgroup=(SIZE,1,1),
spec=pooling_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 10: Dot Product
############################################################
# Implement a kernel that computes the dot product of `a` and `b`
# and stores it in `out`. You have 1 thread per position. You only
# need 2 global reads and 1 global write per thread.
#
# Note: For this problem you don't need to worry about number
# of reads to the `threadgroup` memory. We will handle that
# challenge later.
def dot_spec(a: mx.array, b: mx.array):
return a @ b
def dot_test(a: mx.array, b: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 8;
"""
source = """
threadgroup float shared[THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
// FILL ME IN (roughly 11 lines)
"""
kernel = MetalKernel(
name="dot_product",
input_names=["a", "b"],
output_names=["out"],
header=header,
source=source,
)
return kernel
SIZE = 8
a = mx.arange(SIZE, dtype=mx.float32)
b = mx.arange(SIZE, dtype=mx.float32)
output_shape = (1,)
problem = MetalProblem(
"Dot Product",
dot_test,
[a, b],
output_shape,
grid=(SIZE,1,1),
threadgroup=(SIZE,1,1),
spec=dot_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 11: 1D Convolution
############################################################
# Implement a kernel that computes a 1D convolution between `a`
# and `b` and stores it in `out`. You need to handle the general
# case. You only need 2 global reads and 1 global write per thread.
def conv_spec(a: mx.array, b: mx.array):
out = mx.zeros(*a.shape)
len = b.shape[0]
for i in range(a.shape[0]):
out[i] = sum([a[i + j] * b[j] for j in range(len) if i + j < a.shape[0]])
return out
def conv_test(a: mx.array, b: mx.array):
header = """
constant uint THREADGROUP_MAX_CONV_SIZE = 12;
constant uint MAX_CONV_SIZE = 4;
"""
source = """
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
// FILL ME IN (roughly 24 lines)
"""
kernel = MetalKernel(
name="1D_conv",
input_names=["a", "b"],
output_names=["out"],
header=header,
source=source,
)
return kernel
# Test 1
SIZE = 6
CONV = 3
a = mx.arange(SIZE, dtype=mx.float32)
b = mx.arange(CONV, dtype=mx.float32)
output_shape = (SIZE,)
problem = MetalProblem(
"1D Conv (Simple)",
conv_test,
[a, b],
output_shape,
grid=(8,1,1),
threadgroup=(8,1,1),
spec=conv_spec
)
problem.show()
problem.check()
# Test 2
a = mx.arange(15, dtype=mx.float32)
b = mx.arange(4, dtype=mx.float32)
output_shape = (15,)
problem = MetalProblem(
"1D Conv (Full)",
conv_test,
[a, b],
output_shape,
grid=(16,1,1),
threadgroup=(8,1,1),
spec=conv_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 12: Prefix Sum
############################################################
# Implement a kernel that computes a sum over `a` and stores it
# in `out`. If the size of `a` is greater than the threadgroup
# size, only store the sum of each threadgroup.
#
# We will do this using the parallel prefix sum algorithm in
# `threadgroup` memory. In each step, the algorithm will sum
# half of the remaining elements together.
THREADGROUP_MEM_SIZE = 8
def prefix_sum_spec(a: mx.array):
out = mx.zeros((a.shape[0] + THREADGROUP_MEM_SIZE - 1) // THREADGROUP_MEM_SIZE)
for j, i in enumerate(range(0, a.shape[-1], THREADGROUP_MEM_SIZE)):
out[j] = a[i : i + THREADGROUP_MEM_SIZE].sum()
return out
def prefix_sum_test(a: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 8;
"""
source = """
threadgroup float cache[THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
// FILL ME IN (roughly 14 lines)
"""
kernel = MetalKernel(
name="prefix_sum",
input_names=["a"],
output_names=["out"],
header=header,
source=source,
)
return kernel
# Test 1
SIZE = 8
a = mx.arange(SIZE)
output_shape = (1,)
problem = MetalProblem(
"Prefix Sum (Simple)",
prefix_sum_test,
[a],
output_shape,
grid=(8,1,1),
threadgroup=(8,1,1),
spec=prefix_sum_spec
)
problem.show()
problem.check()
# Test 2
SIZE = 15
a = mx.arange(SIZE)
output_shape = (2,)
problem = MetalProblem(
"Prefix Sum (Full)",
prefix_sum_test,
[a],
output_shape,
grid=(16,1,1),
threadgroup=(8,1,1),
spec=prefix_sum_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 13: Axis Sum
############################################################
# Implement a kernel that computes the sum over each column
# in the input array `a` and stores it in `out`.
THREADGROUP_MEM_SIZE = 8
def axis_sum_spec(a: mx.array):
out = mx.zeros((a.shape[0], (a.shape[1] + THREADGROUP_MEM_SIZE - 1) // THREADGROUP_MEM_SIZE))
for j, i in enumerate(range(0, a.shape[-1], THREADGROUP_MEM_SIZE)):
out[..., j] = a[..., i : i + THREADGROUP_MEM_SIZE].sum(-1)
return out
def axis_sum_test(a: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 8;
"""
source = """
threadgroup float cache[THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint local_i = thread_position_in_threadgroup.x;
uint batch = threadgroup_position_in_grid.y;
// FILL ME IN (roughly 16 lines)
"""
kernel = MetalKernel(
name="axis_sum",
input_names=["a"],
output_names=["out"],
header=header,
source=source,
)
return kernel
BATCH = 4
SIZE = 6
a = mx.arange(BATCH * SIZE).reshape((BATCH, SIZE))
output_shape = (BATCH, 1)
problem = MetalProblem(
"Axis Sum",
axis_sum_test,
[a],
output_shape,
grid=(8,BATCH,1),
threadgroup=(8,1,1),
spec=axis_sum_spec
)
problem.show()
problem.check()
############################################################
### Puzzle 14: Matrix Multiply!
############################################################
# Implement a kernel that multiplies square matrices `a` and `b`
# and stores the result in `out`.
#
# Tip: The most efficient algorithm will copy a block of data into
# `threadgroup` memory before computing each of the individual
# row-column dot products. This is straightforward if the matrix fits
# entirely in `threadgroup` memory (start by implementing that case first).
# Then, modify your code to compute partial dot products and iteratively
# move portions of the matrix into `threadgroup` memory. You should be
# able to handle the hard test in 6 device memory reads.
def matmul_spec(a: mx.array, b: mx.array):
return a @ b
def matmul_test(a: mx.array, b: mx.array):
header = """
constant uint THREADGROUP_MEM_SIZE = 3;
"""
source = """
threadgroup float a_shared[THREADGROUP_MEM_SIZE][THREADGROUP_MEM_SIZE];
threadgroup float b_shared[THREADGROUP_MEM_SIZE][THREADGROUP_MEM_SIZE];
uint i = threadgroup_position_in_grid.x * threads_per_threadgroup.x + thread_position_in_threadgroup.x;
uint j = threadgroup_position_in_grid.y * threads_per_threadgroup.y + thread_position_in_threadgroup.y;
uint local_i = thread_position_in_threadgroup.x;
uint local_j = thread_position_in_threadgroup.y;
// FILL ME IN (roughly 19 lines)
"""
kernel = MetalKernel(
name="matmul",
input_names=["a", "b"],
output_names=["out"],
header=header,
source=source,
)
return kernel
# Test 1
SIZE = 2
a = mx.arange(SIZE * SIZE, dtype=mx.float32).reshape((SIZE, SIZE))
b = mx.arange(SIZE * SIZE, dtype=mx.float32).reshape((SIZE, SIZE)).T
output_shape = (SIZE, SIZE)
problem = MetalProblem(
"Matmul (Simple)",
matmul_test,
[a, b],
output_shape,
grid=(3,3,1),
threadgroup=(3,3,1),
spec=matmul_spec
)
problem.show()
problem.check()
# Test 2
SIZE = 8
a = mx.arange(SIZE * SIZE, dtype=mx.float32).reshape((SIZE, SIZE))
b = mx.arange(SIZE * SIZE, dtype=mx.float32).reshape((SIZE, SIZE)).T
output_shape = (SIZE, SIZE)
problem = MetalProblem(
"Matmul (Full)",
matmul_test,
[a, b],
output_shape,
grid=(9,9,1),
threadgroup=(3,3,1),
spec=matmul_spec
)
problem.show()
problem.check()