-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
1412 lines (1168 loc) · 62 KB
/
benchmark.py
File metadata and controls
1412 lines (1168 loc) · 62 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
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import random
import time
import unittest
from functools import cache
from itertools import product
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
from tinygrad import TinyJit
from tinygrad.device import CompileError, Device
from tinygrad.helpers import GlobalCounters, flat_mv
from tinygrad.runtime.ops_metal import MetalAllocator, MetalProgram
from tinygrad.tensor import Tensor
@cache
def compile(src: str) -> bytes:
if os.environ.get("PRINT_KERNEL"):
print(src)
return Device[Device.DEFAULT].compiler.compile(src)
metalalloc = MetalAllocator(Device[Device.DEFAULT])
base = """
#include <metal_stdlib>
using namespace metal;
kernel void base(device float *data_out, device float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
size_t gid0 = gid.x;
float max_val = data_in[0];
for (size_t i = 1; i < {global_size[0]}; ++i) {{
max_val = fmax(max_val, data_in[i]);
}}
float sum_exp = 0.0f;
for (size_t i = 0; i < {global_size[0]}; ++i) {{
sum_exp += exp(data_in[i] - max_val);
}}
data_out[gid0] = exp(data_in[gid0] - max_val) / sum_exp;
}}
"""
fast_exp = """
#include <metal_stdlib>
using namespace metal;
inline float fast_exp(float y) {{
float x = y * 1.44269504f;
float x_int_f = round(x);
float x_frac = x - x_int_f;
float p = fma(x_frac, fma(x_frac, fma(x_frac, 0.05700169f, 0.24858144f), 0.69282515f), 0.99916080f);
int x_int = (int)x_int_f;
int biased_exp = x_int + 127;
if (biased_exp <= 0) return 0.0f;
return as_type<float>(biased_exp << 23) * p;
}}
"""
generic_reduce_max = """
#include <metal_stdlib>
using namespace metal;
kernel void generic_reduce_max(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x;
uint lid0 = lid.x;
float p_max = -FLT_MAX;
size_t chunk_start = (size_t)gid0 * {items_per_workgroup};
size_t chunk_end = chunk_start + {items_per_workgroup};
for (size_t i = chunk_start + lid0; i < chunk_end; i += {local_size[0]}) {{
if (i < {n}) {{ // Guard against reading out of bounds.
p_max = fmax(p_max, data_in[i]);
}}
}}
// 2. Perform a parallel reduction within the workgroup on the partial maximums.
threadgroup float local_data[{local_size[0]}];
local_data[lid0] = p_max;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
local_data[lid0] = fmax(local_data[lid0], local_data[lid0 + stride]);
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
// 3. Thread 0 writes the workgroup's final result.
if (lid0 == 0) {{
data_out[gid0] = local_data[0];
}}
}}
"""
generic_reduce_sum = """
#include <metal_stdlib>
using namespace metal;
kernel void generic_reduce_sum(device float *data_out, device const float *data_in, device const float* global_max_val, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x;
uint lid0 = lid.x;
float max_val = *global_max_val;
// 1. Each thread computes a partial sum over its assigned chunk of data.
float p_sum = 0.0f;
size_t chunk_start = (size_t)gid0 * {items_per_workgroup};
size_t chunk_end = chunk_start + {items_per_workgroup};
for (size_t i = chunk_start + lid0; i < chunk_end; i += {local_size[0]}) {{
if (i < {n}) {{ // Guard against reading out of bounds.
p_sum += exp(data_in[i] - max_val);
}}
}}
// 2. Perform a parallel reduction within the workgroup on the partial sums.
threadgroup float local_data[{local_size[0]}];
local_data[lid0] = p_sum;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
local_data[lid0] += local_data[lid0 + stride];
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
// 3. Thread 0 writes the workgroup's final result.
if (lid0 == 0) {{
data_out[gid0] = local_data[0];
}}
}}
"""
final_division = """
#include <metal_stdlib>
using namespace metal;
kernel void final_division(device float *data_out, device const float *data_in, device const float *reduction_results, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float max_val = *reduction_results;
float sum_exp = *(reduction_results + 1);
float inv_sum_exp = 1.0f / sum_exp;
for (size_t i = (size_t)gid.x * {local_size[0]} + lid.x; i < {n}; i += (size_t){global_size[0]} * {local_size[0]}) {{
data_out[i] = exp(data_in[i] - max_val) * inv_sum_exp;
}}
}}
"""
"""
kernel fusion 5
- local_max
- global_max
- local_sum
- global_sum
- global_div
"""
fusion5_reg_reduce_max_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void fusion5_reg_reduce_max(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float thread_max = -FLT_MAX;
size_t block_start_idx = (size_t)gid.x * {items_per_block};
size_t block_end_idx = block_start_idx + {items_per_block};
// Each thread reduces a strided chunk of floats from global memory. No bounds check needed due to input padding.
for (size_t i = block_start_idx + lid.x; i < block_end_idx; i += {local_size[0]}) {{
thread_max = fmax(thread_max, data_in[i]);
}}
// Workgroup-level reduction on `thread_max` using SIMD shuffles
threadgroup float scratch_pad[{local_size[0]} / {threads_per_simdgroup}];
uint simd_group_id = lid.x / {threads_per_simdgroup};
uint simd_lane_id = lid.x % {threads_per_simdgroup};
uint num_simd_groups = {local_size[0]} / {threads_per_simdgroup};
for (uint s = {threads_per_simdgroup} / 2; s > 0; s /= 2) {{
thread_max = fmax(thread_max, simd_shuffle_xor(thread_max, s));
}}
if (simd_lane_id == 0) scratch_pad[simd_group_id] = thread_max;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (simd_group_id == 0) {{
thread_max = (simd_lane_id < num_simd_groups) ? scratch_pad[simd_lane_id] : -FLT_MAX;
for (uint s = {threads_per_simdgroup} / 2; s > 0; s /= 2) {{
thread_max = fmax(thread_max, simd_shuffle_xor(thread_max, s));
}}
}}
if (lid.x == 0) {{
data_out[gid.x] = thread_max;
}}
}}
"""
fusion5_reg_reduce_sum_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void fusion5_reg_reduce_sum(device float *data_out, device const float *data_in, device const float* global_max_val_buf, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float global_max = *global_max_val_buf;
float thread_sum = 0.0f;
size_t block_start_idx = (size_t)gid.x * {items_per_block};
size_t block_end_idx = block_start_idx + {items_per_block};
// Each thread computes a partial sum over its assigned chunk of data. No bounds check needed due to padding.
for (size_t i = block_start_idx + lid.x; i < block_end_idx; i += {local_size[0]}) {{
thread_sum += exp(data_in[i] - global_max);
}}
// Workgroup-level reduction for sum using SIMD shuffles
threadgroup float scratch_pad[{local_size[0]} / {threads_per_simdgroup}];
uint simd_group_id = lid.x / {threads_per_simdgroup};
uint simd_lane_id = lid.x % {threads_per_simdgroup};
uint num_simd_groups = {local_size[0]} / {threads_per_simdgroup};
for (uint s = {threads_per_simdgroup} / 2; s > 0; s /= 2) {{
thread_sum += simd_shuffle_xor(thread_sum, s);
}}
if (simd_lane_id == 0) scratch_pad[simd_group_id] = thread_sum;
threadgroup_barrier(mem_flags::mem_threadgroup);
if (simd_group_id == 0) {{
thread_sum = (simd_lane_id < num_simd_groups) ? scratch_pad[simd_lane_id] : 0.0f;
for (uint s = {threads_per_simdgroup} / 2; s > 0; s /= 2) {{
thread_sum += simd_shuffle_xor(thread_sum, s);
}}
}}
if (lid.x == 0) {{
data_out[gid.x] = thread_sum;
}}
}}
"""
fusion5_vec_reduce_max_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void fusion5_vec_reduce_max(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float thread_max = -FLT_MAX;
size_t block_start_vec_idx = (size_t)gid.x * ({items_per_block} / 4);
device const float4* data_in_vec = (device const float4*)data_in;
// Each thread reduces a strided chunk of float4s from global memory. No bounds check needed due to input padding.
for (uint i = lid.x; i < ({items_per_block} / 4); i += {local_size[0]}) {{
float4 val = data_in_vec[block_start_vec_idx + i];
thread_max = fmax(thread_max, fmax(fmax(val.x, val.y), fmax(val.z, val.w)));
}}
// Workgroup-level reduction on `thread_max` using traditional shared memory approach
threadgroup float local_data[{local_size[0]}];
local_data[lid.x] = thread_max;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid.x < stride) {{
local_data[lid.x] = fmax(local_data[lid.x], local_data[lid.x + stride]);
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
if (lid.x == 0) {{
data_out[gid.x] = local_data[0];
}}
}}
"""
fusion5_vec_reduce_sum_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void fusion5_vec_reduce_sum(device float *data_out, device const float *data_in, device const float* global_max_val_buf, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float global_max = *global_max_val_buf;
float thread_sum = 0.0f;
// Calculate start index for this block
size_t block_start_vec_idx = (size_t)gid.x * ({items_per_block} / 4);
device const float4* data_in_vec = (device const float4*)data_in;
float4 global_max_vec = float4(global_max);
// 1. Each thread reduces a strided chunk of float4s directly from global memory.
// This avoids the large shared memory buffer, improving occupancy.
for (uint i = lid.x; i < ({items_per_block} / 4); i += {local_size[0]}) {{
float4 val = data_in_vec[block_start_vec_idx + i] - global_max_vec;
float4 exp_val;
exp_val.x = exp(val.x);
exp_val.y = exp(val.y);
exp_val.z = exp(val.z);
exp_val.w = exp(val.w);
thread_sum += (exp_val.x + exp_val.y + exp_val.z + exp_val.w);
}}
// 2. Workgroup-level reduction for sum using traditional shared memory.
// This part is fine as it uses a small, fixed-size buffer.
threadgroup float local_data[{local_size[0]}];
local_data[lid.x] = thread_sum;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid.x < stride) {{
local_data[lid.x] += local_data[lid.x + stride];
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
// 3. Thread 0 writes the final result for the block.
if (lid.x == 0) {{
data_out[gid.x] = local_data[0];
}}
}}
"""
global_reduce_max_vector_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void global_reduce_max_vector(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
threadgroup float local_data[{local_size[0]}];
float p_max = -FLT_MAX;
size_t global_thread_idx = (size_t)gid.x * {local_size[0]} + lid.x;
size_t start_idx = global_thread_idx * 4;
// Vectorized load with tail handling. Each thread is responsible for up to 4 elements.
if (start_idx < {n}) {{
device const float4* data_in_vec = (device const float4*)(data_in + start_idx);
float4 val = *data_in_vec;
if ({n} - start_idx == 1) {{
p_max = val.x;
}} else if ({n} - start_idx == 2) {{
p_max = fmax(val.x, val.y);
}} else if ({n} - start_idx == 3) {{
p_max = fmax(fmax(val.x, val.y), val.z);
}} else {{
p_max = fmax(fmax(val.x, val.y), fmax(val.z, val.w));
}}
}}
local_data[lid.x] = p_max;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid.x < stride) {{
local_data[lid.x] = fmax(local_data[lid.x], local_data[lid.x + stride]);
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
if (lid.x == 0) {{
data_out[gid.x] = local_data[0];
}}
}}
"""
global_reduce_sum_vector_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void global_reduce_sum_vector(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
threadgroup float local_data[{local_size[0]}];
float p_sum = 0.0f;
size_t global_thread_idx = (size_t)gid.x * {local_size[0]} + lid.x;
size_t start_idx = global_thread_idx * 4;
if (start_idx < {n}) {{
device const float4* data_in_vec = (device const float4*)(data_in + start_idx);
float4 val = *data_in_vec;
p_sum += val.x;
if (start_idx + 1 < {n}) p_sum += val.y;
if (start_idx + 2 < {n}) p_sum += val.z;
if (start_idx + 3 < {n}) p_sum += val.w;
}}
local_data[lid.x] = p_sum;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid.x < stride) {{
local_data[lid.x] += local_data[lid.x + stride];
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
if (lid.x == 0) {{
data_out[gid.x] = local_data[0];
}}
}}
"""
copy_sum_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void copy_sum(device float *data_out, device const float *data_in) {
data_out[1] = data_in[0];
}
"""
local_max_and_sum = """
#include <metal_stdlib>
using namespace metal;
kernel void local_max_and_sum(device float *data_out, device float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x; /* global_size[0] == num_workgroup */
uint lid0 = lid.x; /* local_size[0] */
size_t global_idx = (size_t
)gid0 * {local_size[0]} + lid0;
threadgroup float local_values[{local_size[0]}];
threadgroup float temp_storage[{local_size[0]}];
// 1. Load data into threadgroup memory once.
local_values[lid0] = data_in[global_idx];
threadgroup_barrier(mem_flags::mem_threadgroup);
// 2. Find local max without destroying original values.
temp_storage[lid0] = local_values[lid0];
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
temp_storage[lid0] = fmax(temp_storage[lid0], temp_storage[lid0 + stride]);
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
float local_max = temp_storage[0];
threadgroup_barrier(mem_flags::mem_threadgroup);
// 3. Compute exp(x - local_max) using cached values and perform sum reduction.
local_values[lid0] = exp(local_values[lid0] - local_max);
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
local_values[lid0] += local_values[lid0 + stride];
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
float local_sum = local_values[0];
// 4. Write out results with coalesced access.
if (lid0 == 0) {{
data_out[gid0] = local_max;
data_out[gid0 + {num_workgroup}] = local_sum;
}}
}}
"""
global_reduce_max = """
#include <metal_stdlib>
using namespace metal;
kernel void global_reduce_max(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x;
uint lid0 = lid.x;
size_t global_idx = (size_t)gid0 * {local_size[0]} + lid0;
threadgroup float local_data[{local_size[0]}];
local_data[lid0] = (global_idx < {n}) ? data_in[global_idx] : -FLT_MAX;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
local_data[lid0] = fmax(local_data[lid0], local_data[lid0 + stride]);
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
if (lid0 == 0) {{
data_out[gid0] = local_data[0];
}}
}}
"""
global_sum = """
#include <metal_stdlib>
using namespace metal;
kernel void global_sum(device float *data_out, device const float *data_in, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x;
uint lid0 = lid.x;
size_t global_idx = (size_t)gid0 * {local_size[0]} + lid0;
threadgroup float local_data[{local_size[0]}];
local_data[lid0] = (global_idx < {n}) ? data_in[global_idx] : 0.0f;
threadgroup_barrier(mem_flags::mem_threadgroup);
for (uint stride = {local_size[0]} / 2; stride > 0; stride /= 2) {{
if (lid0 < stride) {{
local_data[lid0] += local_data[lid0 + stride];
}}
threadgroup_barrier(mem_flags::mem_threadgroup);
}}
if (lid0 == 0) {{
data_out[gid0] = local_data[0];
}}
}}
"""
global_div = """
#include <metal_stdlib>
using namespace metal;
kernel void global_div(device float *data_out, device float *data_in0, device const float *reduction_results, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
uint gid0 = gid.x; /* global_size[0] */
uint lid0 = lid.x; /* local_size[0] */
size_t global_idx = (size_t)gid0 * {local_size[0]} + lid0;
float max_val = *reduction_results;
float sum_exp = *(reduction_results + 1);
*(data_out + global_idx) = exp(*(data_in0 + global_idx) - max_val) / sum_exp;
}}
"""
adjust_sums_kernel = """
#include <metal_stdlib>
using namespace metal;
kernel void adjust_sums(device float *adjusted_sums_out, device const float *intermediate_buf, device const float* global_max_val_buf, uint3 gid [[threadgroup_position_in_grid]]) {{
// intermediate_buf layout: [max_0, ..., max_{{N-1}}, sum_0, ..., sum_{{N-1}}]
// N = num_workgroup
uint num_wg = {num_workgroup};
float local_max = intermediate_buf[gid.x];
float local_sum = intermediate_buf[gid.x + num_wg];
adjusted_sums_out[gid.x] = local_sum * exp(local_max - global_max_val_buf[0]);
}}
"""
global_div_opt = """
#include <metal_stdlib>
using namespace metal;
kernel void global_div_opt(device float *data_out, device const float *data_in0, device const float *reduction_results, uint3 gid [[threadgroup_position_in_grid]], uint3 lid [[thread_position_in_threadgroup]]) {{
float max_val = *reduction_results;
float sum_exp = *(reduction_results + 1);
float inv_sum_exp = 1.0f / sum_exp;
// Each thread processes a chunk of ITEMS_PER_THREAD elements
size_t chunk_idx = (size_t)gid.x * {local_size[0]} + lid.x;
size_t start_elem_idx = chunk_idx * {ITEMS_PER_THREAD};
device const float4* data_in_vec = (device const float4*)(data_in0 + start_elem_idx);
device float4* data_out_vec = (device float4*)(data_out + start_elem_idx);
float4 max_val_vec = float4(max_val);
// Loop to process the chunk, vectorized by 4
for (uint i = 0; i < ({ITEMS_PER_THREAD} / 4); ++i) {{
float4 val = data_in_vec[i];
float4 diff = val - max_val_vec;
float4 exp_diff;
exp_diff.x = exp(diff.x);
exp_diff.y = exp(diff.y);
exp_diff.z = exp(diff.z);
exp_diff.w = exp(diff.w);
data_out_vec[i] = exp_diff * inv_sum_exp;
}}
}}
"""
final_division_fast_exp = fast_exp + final_division.replace("exp(", "fast_exp(")
generic_reduce_sum_fast_exp = fast_exp + generic_reduce_sum.replace("exp(", "fast_exp(")
def fine_tune_scheduler(
scheduler_func: Callable,
n: int,
param_configs: dict[str, list],
verbose: bool = True,
) -> tuple[float, dict]:
if verbose:
print(f"\n--- Fine-tuning '{scheduler_func.__name__}' for n={n} via Grid Search over params: {list(param_configs.keys())} ---")
best_time = float('inf')
best_params_combo = None
rng = np.random.default_rng()
d1_timed = rng.standard_normal(size=(n), dtype=np.float32)
param_names = list(param_configs.keys())
param_values_list = list(param_configs.values())
all_combinations = list(product(*param_values_list))
if verbose:
print(f" Testing {len(all_combinations)} combinations...")
for i, combo in enumerate(all_combinations):
current_params = dict(zip(param_names, combo))
kwargs = {k: [v, 1, 1] for k, v in current_params.items()}
try:
# Warm-up run for kernel compilation. Result is discarded.
_, res = scheduler_func(n, d1_timed, **kwargs)
del res
Device[Device.DEFAULT].synchronize()
# Timed run. Result is discarded.
st = time.perf_counter()
_, res = scheduler_func(n, d1_timed, **kwargs)
del res
Device[Device.DEFAULT].synchronize()
run_time = time.perf_counter() - st
if verbose:
print(f" Trying combo {i+1}/{len(all_combinations)}: {current_params} -> {run_time:.4f}s")
if run_time < best_time:
best_time = run_time
best_params_combo = current_params
except Exception as e:
if verbose:
print(f" Trying {current_params}: FAILED ({type(e).__name__})")
if best_params_combo is None:
raise RuntimeError(f"Tuning failed for {scheduler_func.__name__}. No valid parameter combination found.")
if verbose:
print(f"--- Best overall parameters found for n={n}: {best_params_combo} ({best_time:.4f}s) ---")
return best_time, best_params_combo
@unittest.skipIf(Device.DEFAULT != "METAL", "metal kernel")
class Softmax(unittest.TestCase):
def _driver(self, name: str, n: int, custom_executor: Callable[[int, np.ndarray], tuple[float, np.ndarray]], rtol=1e-6, atol=1e-6):
NUM_RUNS = 2 ** 0
rng = np.random.default_rng()
d1_warmup = rng.standard_normal(size=(n), dtype=np.float32)
d1_for_verification = rng.standard_normal(size=(n), dtype=np.float32)
# 2. Run the custom implementation FIRST
# WARM UP
custom_executor(n, d1_warmup)
Device[Device.DEFAULT].synchronize() # Explicitly wait for GPU to finish warm-up run.
# TIMED RUN
custom_times = []
for _ in range(NUM_RUNS):
# Regenerate data for each run to flush cache
d1_timed_custom = rng.standard_normal(size=(n), dtype=np.float32)
run_time, _ = custom_executor(n, d1_timed_custom)
custom_times.append(run_time)
custom_time = np.median(custom_times)
# Get the custom result for verification. The custom_executor frees its own resources.
_, d0 = custom_executor(n, d1_for_verification)
# 3. Run tinygrad baseline to get correct result and timing
@TinyJit
def tiny_jit(t: Tensor) -> Tensor:
return t.softmax().realize()
try:
# WARM UP
tiny_jit(Tensor(d1_warmup)).realize()
Device[Device.DEFAULT].synchronize()
# TIMED RUN for tinygrad
tiny_times = []
for _ in range(NUM_RUNS):
d1_timed = rng.standard_normal(size=(n), dtype=np.float32)
GlobalCounters.reset()
st_tiny = time.perf_counter()
tiny_jit(Tensor(d1_timed)).realize()
Device[Device.DEFAULT].synchronize()
tiny_times.append(time.perf_counter() - st_tiny)
tiny_time = np.median(tiny_times)
# The op count is consistent for a given shape, so getting it from the last run is fine.
ops = GlobalCounters.global_ops
# Get the tinygrad result for verification using the same dedicated input
tiny_softmax_tensor = tiny_jit(Tensor(d1_for_verification))
Device[Device.DEFAULT].synchronize()
tiny_softmax = tiny_softmax_tensor.numpy()
except (CompileError, RuntimeError) as e: # Catch runtime errors for OOM etc.
print(f"Tinygrad failed for N={n}: {e}")
tiny_time, ops, tiny_softmax = float('nan'), -1, None
# 4. Verify results
if ops != -1 and tiny_softmax is not None:
np.testing.assert_allclose(d0, tiny_softmax, rtol=rtol, atol=atol)
# 5. Print results
title_suffix = f" ({name})" if name else ""
print(
f"\nSoftmax {n}{title_suffix}\n"
+ f"custom median time ({NUM_RUNS} runs): {custom_time:.4f}s, GFLOPS: {(ops / custom_time / 1e9) if ops != -1 and custom_time > 0 else 0.00:.2f}\n"
)
if ops != -1:
print(f"tinygrad median time ({NUM_RUNS} runs): {tiny_time:.4f}s, GFLOPS: {ops / tiny_time / 1e9:.2f}\n")
def _sched_base(self, n: int, d1: np.ndarray) -> tuple[float, np.ndarray]:
if n == 0: return 0.0, np.array([], dtype=np.float32)
global_size = [n, 1, 1]
prog = MetalProgram(Device[Device.DEFAULT], "base", compile(base.format(global_size=global_size)))
data_out_size = n * 4
data_in_size = n * 4
data_out = metalalloc.alloc(data_out_size)
data_in = metalalloc.alloc(data_in_size)
metalalloc._copyin(data_in, d1.tobytes())
st_custom = time.perf_counter()
prog(data_out, data_in, global_size=global_size, local_size=[1, 1, 1], wait=True)
custom_time = time.perf_counter() - st_custom
d0 = np.empty((n), dtype=np.float32)
metalalloc._copyout(flat_mv(d0.data), data_out)
metalalloc.free(data_out, data_out_size)
metalalloc.free(data_in, data_in_size)
return custom_time, d0
def _sched_fusion_5(self, n: int, d1: np.ndarray, **kwargs) -> tuple[float, np.ndarray]:
if n == 0: return 0.0, np.array([], dtype=np.float32)
default_reduce_ls = [256, 1, 1]
default_div_ls = [256, 1, 1]
reduce_fat_ls = kwargs.get("reduce_fat_ls", default_reduce_ls)
reduce_thin_ls = kwargs.get("reduce_thin_ls", default_reduce_ls)
div_ls = kwargs.get("div_ls", default_div_ls)
# 1. Setup buffers and parameters
max_workgroups = 16384 # A safe cap for grid size
# Buffers
data_in_buf_size = n * 4
data_out_buf_size = n * 4
temp_buf_size = max_workgroups * 4
reduction_results_buf_size = 2 * 4
data_in_buf = metalalloc.alloc(data_in_buf_size)
metalalloc._copyin(data_in_buf, d1.tobytes())
data_out_buf = metalalloc.alloc(data_out_buf_size)
temp_buf_A = metalalloc.alloc(temp_buf_size)
temp_buf_B = metalalloc.alloc(temp_buf_size)
reduction_results_buf = metalalloc.alloc(reduction_results_buf_size)
prog_copy_sum = MetalProgram(Device[Device.DEFAULT], "copy_sum", compile(copy_sum_kernel))
st_custom = time.perf_counter()
# Stage 1 & 2: Find Global Max
if n > 0:
local_size_max1 = reduce_fat_ls
items_per_thread_max1 = (n + max_workgroups * local_size_max1[0] - 1) // (max_workgroups * local_size_max1[0]) or 1
items_per_workgroup_max1 = items_per_thread_max1 * local_size_max1[0]
num_wg_stage1 = (n + items_per_workgroup_max1 - 1) // items_per_workgroup_max1
if num_wg_stage1 > max_workgroups: num_wg_stage1 = max_workgroups
params_max_stage1 = {"n": f"{n}UL", "local_size": local_size_max1, "items_per_workgroup": items_per_workgroup_max1}
prog_max_stage1 = MetalProgram(Device[Device.DEFAULT], "generic_reduce_max", compile(generic_reduce_max.format(**params_max_stage1)))
prog_max_stage1(temp_buf_A, data_in_buf, global_size=[num_wg_stage1, 1, 1], local_size=local_size_max1)
# Stage 2: Recursive reduction on partial maxes in temp_buf_A
src_buf_max, current_size_max = temp_buf_A, num_wg_stage1
i_max = 0
temp_bufs_max = [temp_buf_B, temp_buf_A]
while current_size_max > 1:
local_size_thin = reduce_thin_ls
num_wg_thin = (current_size_max + local_size_thin[0] - 1) // local_size_thin[0]
params_max_thin = {"n": f"{current_size_max}UL", "local_size": local_size_thin}
prog_max_thin = MetalProgram(Device[Device.DEFAULT], "global_reduce_max", compile(global_reduce_max.format(**params_max_thin)))
dest_buf_max = temp_bufs_max[i_max % 2]
prog_max_thin(dest_buf_max, src_buf_max, global_size=[num_wg_thin, 1, 1], local_size=local_size_thin)
src_buf_max = dest_buf_max
current_size_max = num_wg_thin
i_max += 1
final_max_buf = src_buf_max
metalalloc._transfer(reduction_results_buf, final_max_buf, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
# Stage 3 & 4: Find Global Sum
num_wg_stage3 = 0
if n > 0:
# Stage 3: First-level reduction for sum (data -> partial sums)
local_size_sum1 = reduce_fat_ls
items_per_thread_sum1 = (n + max_workgroups * local_size_sum1[0] - 1) // (max_workgroups * local_size_sum1[0]) or 1
items_per_workgroup_sum1 = items_per_thread_sum1 * local_size_sum1[0]
num_wg_stage3 = (n + items_per_workgroup_sum1 - 1) // items_per_workgroup_sum1
if num_wg_stage3 > max_workgroups: num_wg_stage3 = max_workgroups
params_sum_stage3 = {"n": f"{n}UL", "local_size": local_size_sum1, "items_per_workgroup": items_per_workgroup_sum1}
prog_sum_stage3 = MetalProgram(Device[Device.DEFAULT], "generic_reduce_sum", compile(generic_reduce_sum.format(**params_sum_stage3)))
prog_sum_stage3(temp_buf_A, data_in_buf, reduction_results_buf, global_size=[num_wg_stage3, 1, 1], local_size=local_size_sum1)
# Stage 4: Recursive reduction on partial sums in temp_buf_A
if num_wg_stage3 > 0:
src_buf_sum, current_size_sum = temp_buf_A, num_wg_stage3
i_sum = 0
temp_bufs_sum = [temp_buf_B, temp_buf_A]
while current_size_sum > 1:
local_size_thin = reduce_thin_ls
num_wg_thin = (current_size_sum + local_size_thin[0] - 1) // local_size_thin[0]
params_sum_thin = {"n": f"{current_size_sum}UL", "local_size": local_size_thin}
prog_sum_thin = MetalProgram(Device[Device.DEFAULT], "global_sum", compile(global_sum.format(**params_sum_thin)))
dest_buf_sum = temp_bufs_sum[i_sum % 2]
prog_sum_thin(dest_buf_sum, src_buf_sum, global_size=[num_wg_thin, 1, 1], local_size=local_size_thin)
src_buf_sum = dest_buf_sum
current_size_sum = num_wg_thin
i_sum += 1
# After reduction, src_buf_sum holds the buffer with the final sum. Copy it.
prog_copy_sum(reduction_results_buf, src_buf_sum, global_size=[1,1,1], local_size=[1,1,1])
# Stage 5: Final division
if n > 0:
final_div_gs = min(max_workgroups, (n + div_ls[0] - 1) // div_ls[0])
prog_final_div = MetalProgram(Device[Device.DEFAULT], "final_division", compile(final_division.format(local_size=div_ls, global_size=[final_div_gs,1,1], n=f"{n}UL")))
prog_final_div(data_out_buf, data_in_buf, reduction_results_buf, global_size=[final_div_gs, 1, 1], local_size=div_ls)
Device[Device.DEFAULT].synchronize()
custom_time = time.perf_counter() - st_custom
d0 = np.empty((n), dtype=np.float32)
if n > 0:
metalalloc._copyout(flat_mv(d0.data), data_out_buf)
metalalloc.free(data_in_buf, data_in_buf_size)
metalalloc.free(data_out_buf, data_out_buf_size)
metalalloc.free(temp_buf_A, temp_buf_size)
metalalloc.free(temp_buf_B, temp_buf_size)
metalalloc.free(reduction_results_buf, reduction_results_buf_size)
return custom_time, d0
def _sched_fusion_5_fast_exp(self, n: int, d1: np.ndarray) -> tuple[float, np.ndarray]:
if n == 0: return 0.0, np.array([], dtype=np.float32)
# Re-implementing based on the correct multi-stage reduction logic from _sched_fusion_5,
# but using fast_exp kernels. The previous implementation had a logic error in its
# generic `reduce_op` helper.
reduce_fat_ls = [256, 1, 1]
reduce_thin_ls = [256, 1, 1]
div_ls = [256, 1, 1]
# 1. Setup buffers and parameters
max_workgroups = 16384 # A safe cap for grid size
# Buffers
data_in_buf_size = n * 4
data_out_buf_size = n * 4
temp_buf_size = max_workgroups * 4
reduction_results_buf_size = 2 * 4
data_in_buf = metalalloc.alloc(data_in_buf_size)
metalalloc._copyin(data_in_buf, d1.tobytes())
data_out_buf = metalalloc.alloc(data_out_buf_size)
temp_buf_A = metalalloc.alloc(temp_buf_size)
temp_buf_B = metalalloc.alloc(temp_buf_size)
reduction_results_buf = metalalloc.alloc(reduction_results_buf_size)
prog_copy_sum = MetalProgram(Device[Device.DEFAULT], "copy_sum", compile(copy_sum_kernel))
st_custom = time.perf_counter()
# Stage 1 & 2: Find Global Max (no change from _sched_fusion_5, no exp involved)
if n > 0:
local_size_max1 = reduce_fat_ls
items_per_thread_max1 = (n + max_workgroups * local_size_max1[0] - 1) // (max_workgroups * local_size_max1[0]) or 1
items_per_workgroup_max1 = items_per_thread_max1 * local_size_max1[0]
num_wg_stage1 = (n + items_per_workgroup_max1 - 1) // items_per_workgroup_max1
if num_wg_stage1 > max_workgroups: num_wg_stage1 = max_workgroups
params_max_stage1 = {"n": f"{n}UL", "local_size": local_size_max1, "items_per_workgroup": items_per_workgroup_max1}
prog_max_stage1 = MetalProgram(Device[Device.DEFAULT], "generic_reduce_max", compile(generic_reduce_max.format(**params_max_stage1)))
prog_max_stage1(temp_buf_A, data_in_buf, global_size=[num_wg_stage1, 1, 1], local_size=local_size_max1)
# Stage 2: Recursive reduction on partial maxes in temp_buf_A
src_buf_max, current_size_max = temp_buf_A, num_wg_stage1
i_max = 0
temp_bufs_max = [temp_buf_B, temp_buf_A]
while current_size_max > 1:
local_size_thin = reduce_thin_ls
num_wg_thin = (current_size_max + local_size_thin[0] - 1) // local_size_thin[0]
params_max_thin = {"n": f"{current_size_max}UL", "local_size": local_size_thin}
prog_max_thin = MetalProgram(Device[Device.DEFAULT], "global_reduce_max", compile(global_reduce_max.format(**params_max_thin)))
dest_buf_max = temp_bufs_max[i_max % 2]
prog_max_thin(dest_buf_max, src_buf_max, global_size=[num_wg_thin, 1, 1], local_size=local_size_thin)
src_buf_max = dest_buf_max
current_size_max = num_wg_thin
i_max += 1
final_max_buf = src_buf_max
metalalloc._transfer(reduction_results_buf, final_max_buf, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
# Stage 3 & 4: Find Global Sum (using fast_exp for first stage)
num_wg_stage3 = 0
if n > 0:
# Stage 3: First-level reduction for sum (data -> partial sums)
local_size_sum1 = reduce_fat_ls
items_per_thread_sum1 = (n + max_workgroups * local_size_sum1[0] - 1) // (max_workgroups * local_size_sum1[0]) or 1
items_per_workgroup_sum1 = items_per_thread_sum1 * local_size_sum1[0]
num_wg_stage3 = (n + items_per_workgroup_sum1 - 1) // items_per_workgroup_sum1
if num_wg_stage3 > max_workgroups: num_wg_stage3 = max_workgroups
params_sum_stage3 = {"n": f"{n}UL", "local_size": local_size_sum1, "items_per_workgroup": items_per_workgroup_sum1}
prog_sum_stage3 = MetalProgram(Device[Device.DEFAULT], "generic_reduce_sum", compile(generic_reduce_sum_fast_exp.format(**params_sum_stage3)))
prog_sum_stage3(temp_buf_A, data_in_buf, reduction_results_buf, global_size=[num_wg_stage3, 1, 1], local_size=local_size_sum1)
# Stage 4: Recursive reduction on partial sums (no exp, same as _sched_fusion_5)
if num_wg_stage3 > 0:
src_buf_sum, current_size_sum = temp_buf_A, num_wg_stage3
i_sum = 0
temp_bufs_sum = [temp_buf_B, temp_buf_A]
while current_size_sum > 1:
local_size_thin = reduce_thin_ls
num_wg_thin = (current_size_sum + local_size_thin[0] - 1) // local_size_thin[0]
params_sum_thin = {"n": f"{current_size_sum}UL", "local_size": local_size_thin}
prog_sum_thin = MetalProgram(Device[Device.DEFAULT], "global_sum", compile(global_sum.format(**params_sum_thin)))
dest_buf_sum = temp_bufs_sum[i_sum % 2]
prog_sum_thin(dest_buf_sum, src_buf_sum, global_size=[num_wg_thin, 1, 1], local_size=local_size_thin)
src_buf_sum = dest_buf_sum
current_size_sum = num_wg_thin
i_sum += 1
# After reduction, src_buf_sum holds the buffer with the final sum. Copy it.
prog_copy_sum(reduction_results_buf, src_buf_sum, global_size=[1,1,1], local_size=[1,1,1])
# Stage 5: Final division (using fast_exp)
if n > 0:
final_div_gs = min(max_workgroups, (n + div_ls[0] - 1) // div_ls[0])
prog_final_div = MetalProgram(Device[Device.DEFAULT], "final_division", compile(final_division_fast_exp.format(local_size=div_ls, global_size=[final_div_gs,1,1], n=f"{n}UL")))
prog_final_div(data_out_buf, data_in_buf, reduction_results_buf, global_size=[final_div_gs, 1, 1], local_size=div_ls)
Device[Device.DEFAULT].synchronize()
custom_time = time.perf_counter() - st_custom
d0 = np.empty((n), dtype=np.float32)
if n > 0:
metalalloc._copyout(flat_mv(d0.data), data_out_buf)
metalalloc.free(data_in_buf, data_in_buf_size)
metalalloc.free(data_out_buf, data_out_buf_size)
metalalloc.free(temp_buf_A, temp_buf_size)
metalalloc.free(temp_buf_B, temp_buf_size)
metalalloc.free(reduction_results_buf, reduction_results_buf_size)
return custom_time, d0
def _run_recursive_reduction(self, n_items: int, src_buf, kernel_name: str, kernel_src_template: str, result_buf = None):
"""Helper to run a generic, multi-stage recursive reduction."""
reduce_ls = [256, 1, 1]
max_wg_recursive = 16384
temp_reduction_buf_A = metalalloc.alloc(max_wg_recursive * 4)
temp_reduction_buf_B = metalalloc.alloc(max_wg_recursive * 4)
temp_bufs = [temp_reduction_buf_A, temp_reduction_buf_B]
current_size = n_items
i = 0
src_buf_recursive = src_buf
while current_size > 1:
num_wg_reduce = (current_size + reduce_ls[0] - 1) // reduce_ls[0]
prog_reduce = MetalProgram(Device[Device.DEFAULT], kernel_name, compile(kernel_src_template.format(n=f"{current_size}UL", local_size=reduce_ls)))
# On the final step, write to the final result buffer if provided, otherwise to the last temp buffer.
is_final_step = (num_wg_reduce == 1)
dest_buf = (result_buf if is_final_step and result_buf is not None else temp_bufs[i % 2])
prog_reduce(dest_buf, src_buf_recursive, global_size=[num_wg_reduce, 1, 1], local_size=reduce_ls)
src_buf_recursive = dest_buf
current_size = num_wg_reduce
i += 1
if n_items == 1 and result_buf is not None: # Handle case where only one partial result was produced
metalalloc._transfer(result_buf, src_buf_recursive, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
elif current_size == 1 and result_buf is not None and src_buf_recursive != result_buf: # Result ended up in a temp buffer
metalalloc._transfer(result_buf, src_buf_recursive, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
metalalloc.free(temp_reduction_buf_A, max_wg_recursive * 4)
metalalloc.free(temp_reduction_buf_B, max_wg_recursive * 4)
return src_buf_recursive
def _run_recursive_reduction_vector(self, n_items: int, src_buf, kernel_name: str, kernel_src_template: str, result_buf=None):
"""Helper to run a multi-stage recursive reduction using vectorized kernels that handle unaligned tails."""
reduce_ls = [256, 1, 1]
max_wg_recursive = 16384
temp_reduction_buf_A = metalalloc.alloc(max_wg_recursive * 4)
temp_reduction_buf_B = metalalloc.alloc(max_wg_recursive * 4)
temp_bufs = [temp_reduction_buf_A, temp_reduction_buf_B]
current_size = n_items
i = 0
src_buf_recursive = src_buf
while current_size > 1:
num_threads = (current_size + 3) // 4
num_wg_reduce = (num_threads + reduce_ls[0] - 1) // reduce_ls[0]
prog_reduce = MetalProgram(Device[Device.DEFAULT], kernel_name, compile(kernel_src_template.format(n=f"{current_size}UL", local_size=reduce_ls)))
is_final_step = (num_wg_reduce == 1)
dest_buf = (result_buf if is_final_step and result_buf is not None else temp_bufs[i % 2])
prog_reduce(dest_buf, src_buf_recursive, global_size=[num_wg_reduce, 1, 1], local_size=reduce_ls)
src_buf_recursive = dest_buf
current_size = num_wg_reduce
i += 1
if n_items == 1 and result_buf is not None:
metalalloc._transfer(result_buf, src_buf_recursive, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
elif current_size == 1 and result_buf is not None and src_buf_recursive != result_buf:
metalalloc._transfer(result_buf, src_buf_recursive, 4, src_dev=metalalloc.dev, dest_dev=metalalloc.dev)
metalalloc.free(temp_reduction_buf_A, max_wg_recursive * 4)
metalalloc.free(temp_reduction_buf_B, max_wg_recursive * 4)
return src_buf_recursive