mlx-cpu 0.32.0__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. mlx/__main__.py +27 -0
  2. mlx/_reprlib_fix.py +16 -0
  3. mlx/extension.py +88 -0
  4. mlx/include/mlx/3rdparty/pocketfft.h +3581 -0
  5. mlx/include/mlx/allocator.h +77 -0
  6. mlx/include/mlx/api.h +29 -0
  7. mlx/include/mlx/array.h +657 -0
  8. mlx/include/mlx/backend/common/binary.h +97 -0
  9. mlx/include/mlx/backend/common/broadcasting.h +11 -0
  10. mlx/include/mlx/backend/common/buffer_cache.h +158 -0
  11. mlx/include/mlx/backend/common/compiled.h +97 -0
  12. mlx/include/mlx/backend/common/copy.h +50 -0
  13. mlx/include/mlx/backend/common/hadamard.h +109 -0
  14. mlx/include/mlx/backend/common/matmul.h +67 -0
  15. mlx/include/mlx/backend/common/metal_kernel.h +27 -0
  16. mlx/include/mlx/backend/common/quantized.h +14 -0
  17. mlx/include/mlx/backend/common/reduce.h +59 -0
  18. mlx/include/mlx/backend/common/slicing.h +20 -0
  19. mlx/include/mlx/backend/common/ternary.h +85 -0
  20. mlx/include/mlx/backend/common/unary.h +29 -0
  21. mlx/include/mlx/backend/common/utils.h +238 -0
  22. mlx/include/mlx/backend/cpu/arange.h +28 -0
  23. mlx/include/mlx/backend/cpu/binary.h +517 -0
  24. mlx/include/mlx/backend/cpu/binary_ops.h +98 -0
  25. mlx/include/mlx/backend/cpu/binary_two.h +166 -0
  26. mlx/include/mlx/backend/cpu/compiled_preamble.h +12 -0
  27. mlx/include/mlx/backend/cpu/copy.h +36 -0
  28. mlx/include/mlx/backend/cpu/device_info.h +28 -0
  29. mlx/include/mlx/backend/cpu/encoder.h +70 -0
  30. mlx/include/mlx/backend/cpu/eval.h +15 -0
  31. mlx/include/mlx/backend/cpu/gemm.h +26 -0
  32. mlx/include/mlx/backend/cpu/gemms/simd_gemm.h +139 -0
  33. mlx/include/mlx/backend/cpu/jit_compiler.h +29 -0
  34. mlx/include/mlx/backend/cpu/lapack.h +80 -0
  35. mlx/include/mlx/backend/cpu/simd/accelerate_fp16_simd.h +56 -0
  36. mlx/include/mlx/backend/cpu/simd/accelerate_simd.h +329 -0
  37. mlx/include/mlx/backend/cpu/simd/base_simd.h +319 -0
  38. mlx/include/mlx/backend/cpu/simd/math.h +193 -0
  39. mlx/include/mlx/backend/cpu/simd/neon_fp16_simd.h +212 -0
  40. mlx/include/mlx/backend/cpu/simd/simd.h +4 -0
  41. mlx/include/mlx/backend/cpu/simd/type.h +11 -0
  42. mlx/include/mlx/backend/cpu/slicing.h +21 -0
  43. mlx/include/mlx/backend/cpu/ternary.h +154 -0
  44. mlx/include/mlx/backend/cpu/threefry.h +21 -0
  45. mlx/include/mlx/backend/cpu/unary.h +281 -0
  46. mlx/include/mlx/backend/cpu/unary_ops.h +175 -0
  47. mlx/include/mlx/backend/cuda/allocator.h +94 -0
  48. mlx/include/mlx/backend/cuda/conv/conv.h +126 -0
  49. mlx/include/mlx/backend/cuda/cublas_utils.h +99 -0
  50. mlx/include/mlx/backend/cuda/cuda.h +21 -0
  51. mlx/include/mlx/backend/cuda/cuda_utils.h +84 -0
  52. mlx/include/mlx/backend/cuda/cudnn_utils.h +196 -0
  53. mlx/include/mlx/backend/cuda/device/config.h +12 -0
  54. mlx/include/mlx/backend/cuda/device.h +218 -0
  55. mlx/include/mlx/backend/cuda/event.h +79 -0
  56. mlx/include/mlx/backend/cuda/gemms/block_mask.h +28 -0
  57. mlx/include/mlx/backend/cuda/gemms/cublas_gemm.h +114 -0
  58. mlx/include/mlx/backend/cuda/gemms/gather_gemm.h +23 -0
  59. mlx/include/mlx/backend/cuda/gemms/gemv.h +34 -0
  60. mlx/include/mlx/backend/cuda/gemms/grouped_gemm.h +39 -0
  61. mlx/include/mlx/backend/cuda/jit_module.h +119 -0
  62. mlx/include/mlx/backend/cuda/lru_cache.h +196 -0
  63. mlx/include/mlx/backend/cuda/quantized/cublas_qqmm.h +100 -0
  64. mlx/include/mlx/backend/cuda/quantized/qmm/qmm.h +145 -0
  65. mlx/include/mlx/backend/cuda/quantized/qmm/qmm_utils.h +72 -0
  66. mlx/include/mlx/backend/cuda/quantized/qqmm_impl.h +37 -0
  67. mlx/include/mlx/backend/cuda/quantized/qqmm_utils.h +62 -0
  68. mlx/include/mlx/backend/cuda/quantized/quantized.h +57 -0
  69. mlx/include/mlx/backend/cuda/quantized/quantized_utils.h +50 -0
  70. mlx/include/mlx/backend/cuda/utils.h +49 -0
  71. mlx/include/mlx/backend/cuda/worker.h +59 -0
  72. mlx/include/mlx/backend/gpu/copy.h +59 -0
  73. mlx/include/mlx/backend/gpu/device_info.h +36 -0
  74. mlx/include/mlx/backend/gpu/eval.h +21 -0
  75. mlx/include/mlx/backend/gpu/scan.h +17 -0
  76. mlx/include/mlx/backend/gpu/slicing.h +36 -0
  77. mlx/include/mlx/backend/metal/allocator.h +80 -0
  78. mlx/include/mlx/backend/metal/binary.h +33 -0
  79. mlx/include/mlx/backend/metal/device.h +258 -0
  80. mlx/include/mlx/backend/metal/event.h +33 -0
  81. mlx/include/mlx/backend/metal/jit/includes.h +61 -0
  82. mlx/include/mlx/backend/metal/jit/indexing.h +82 -0
  83. mlx/include/mlx/backend/metal/kernels/arange.h +9 -0
  84. mlx/include/mlx/backend/metal/kernels/atomic.h +358 -0
  85. mlx/include/mlx/backend/metal/kernels/bf16.h +16 -0
  86. mlx/include/mlx/backend/metal/kernels/bf16_math.h +380 -0
  87. mlx/include/mlx/backend/metal/kernels/binary.h +199 -0
  88. mlx/include/mlx/backend/metal/kernels/binary_ops.h +330 -0
  89. mlx/include/mlx/backend/metal/kernels/binary_two.h +244 -0
  90. mlx/include/mlx/backend/metal/kernels/cexpf.h +134 -0
  91. mlx/include/mlx/backend/metal/kernels/complex.h +173 -0
  92. mlx/include/mlx/backend/metal/kernels/copy.h +276 -0
  93. mlx/include/mlx/backend/metal/kernels/defines.h +24 -0
  94. mlx/include/mlx/backend/metal/kernels/erf.h +69 -0
  95. mlx/include/mlx/backend/metal/kernels/expm1f.h +90 -0
  96. mlx/include/mlx/backend/metal/kernels/fft/radix.h +328 -0
  97. mlx/include/mlx/backend/metal/kernels/fft/readwrite.h +624 -0
  98. mlx/include/mlx/backend/metal/kernels/fft.h +486 -0
  99. mlx/include/mlx/backend/metal/kernels/fp4.h +48 -0
  100. mlx/include/mlx/backend/metal/kernels/fp8.h +80 -0
  101. mlx/include/mlx/backend/metal/kernels/fp_quantized.h +2100 -0
  102. mlx/include/mlx/backend/metal/kernels/fp_quantized_nax.h +1018 -0
  103. mlx/include/mlx/backend/metal/kernels/gemv.h +766 -0
  104. mlx/include/mlx/backend/metal/kernels/gemv_masked.h +827 -0
  105. mlx/include/mlx/backend/metal/kernels/hadamard.h +182 -0
  106. mlx/include/mlx/backend/metal/kernels/indexing/gather.h +51 -0
  107. mlx/include/mlx/backend/metal/kernels/indexing/gather_axis.h +44 -0
  108. mlx/include/mlx/backend/metal/kernels/indexing/gather_front.h +24 -0
  109. mlx/include/mlx/backend/metal/kernels/indexing/indexing.h +23 -0
  110. mlx/include/mlx/backend/metal/kernels/indexing/masked_scatter.h +41 -0
  111. mlx/include/mlx/backend/metal/kernels/indexing/scatter.h +139 -0
  112. mlx/include/mlx/backend/metal/kernels/indexing/scatter_axis.h +52 -0
  113. mlx/include/mlx/backend/metal/kernels/logging.h +26 -0
  114. mlx/include/mlx/backend/metal/kernels/logsumexp.h +140 -0
  115. mlx/include/mlx/backend/metal/kernels/quantized.h +2760 -0
  116. mlx/include/mlx/backend/metal/kernels/quantized_nax.h +1681 -0
  117. mlx/include/mlx/backend/metal/kernels/quantized_utils.h +90 -0
  118. mlx/include/mlx/backend/metal/kernels/reduce.h +5 -0
  119. mlx/include/mlx/backend/metal/kernels/reduce_utils.h +6 -0
  120. mlx/include/mlx/backend/metal/kernels/reduction/ops.h +275 -0
  121. mlx/include/mlx/backend/metal/kernels/reduction/reduce_all.h +66 -0
  122. mlx/include/mlx/backend/metal/kernels/reduction/reduce_col.h +398 -0
  123. mlx/include/mlx/backend/metal/kernels/reduction/reduce_init.h +8 -0
  124. mlx/include/mlx/backend/metal/kernels/reduction/reduce_row.h +369 -0
  125. mlx/include/mlx/backend/metal/kernels/scan.h +514 -0
  126. mlx/include/mlx/backend/metal/kernels/sdpa_vector.h +394 -0
  127. mlx/include/mlx/backend/metal/kernels/softmax.h +190 -0
  128. mlx/include/mlx/backend/metal/kernels/sort.h +731 -0
  129. mlx/include/mlx/backend/metal/kernels/steel/attn/attn.h +296 -0
  130. mlx/include/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.h +476 -0
  131. mlx/include/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.h +482 -0
  132. mlx/include/mlx/backend/metal/kernels/steel/attn/loader.h +264 -0
  133. mlx/include/mlx/backend/metal/kernels/steel/attn/mma.h +750 -0
  134. mlx/include/mlx/backend/metal/kernels/steel/attn/nax.h +887 -0
  135. mlx/include/mlx/backend/metal/kernels/steel/attn/params.h +44 -0
  136. mlx/include/mlx/backend/metal/kernels/steel/attn/transforms.h +71 -0
  137. mlx/include/mlx/backend/metal/kernels/steel/conv/conv.h +13 -0
  138. mlx/include/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.h +176 -0
  139. mlx/include/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_3d.h +135 -0
  140. mlx/include/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.h +225 -0
  141. mlx/include/mlx/backend/metal/kernels/steel/conv/loader.h +6 -0
  142. mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_l.h +955 -0
  143. mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_n.h +319 -0
  144. mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_general.h +381 -0
  145. mlx/include/mlx/backend/metal/kernels/steel/conv/params.h +103 -0
  146. mlx/include/mlx/backend/metal/kernels/steel/defines.h +7 -0
  147. mlx/include/mlx/backend/metal/kernels/steel/gemm/gemm.h +295 -0
  148. mlx/include/mlx/backend/metal/kernels/steel/gemm/gemm_nax.h +131 -0
  149. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.h +346 -0
  150. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.h +215 -0
  151. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.h +459 -0
  152. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.h +136 -0
  153. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.h +719 -0
  154. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.h +266 -0
  155. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented_nax.h +125 -0
  156. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.h +227 -0
  157. mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk_nax.h +145 -0
  158. mlx/include/mlx/backend/metal/kernels/steel/gemm/loader.h +137 -0
  159. mlx/include/mlx/backend/metal/kernels/steel/gemm/mma.h +1146 -0
  160. mlx/include/mlx/backend/metal/kernels/steel/gemm/nax.h +887 -0
  161. mlx/include/mlx/backend/metal/kernels/steel/gemm/params.h +65 -0
  162. mlx/include/mlx/backend/metal/kernels/steel/gemm/transforms.h +72 -0
  163. mlx/include/mlx/backend/metal/kernels/steel/utils/integral_constant.h +130 -0
  164. mlx/include/mlx/backend/metal/kernels/steel/utils/type_traits.h +55 -0
  165. mlx/include/mlx/backend/metal/kernels/steel/utils.h +42 -0
  166. mlx/include/mlx/backend/metal/kernels/ternary.h +145 -0
  167. mlx/include/mlx/backend/metal/kernels/ternary_ops.h +10 -0
  168. mlx/include/mlx/backend/metal/kernels/unary.h +63 -0
  169. mlx/include/mlx/backend/metal/kernels/unary_ops.h +454 -0
  170. mlx/include/mlx/backend/metal/kernels/utils.h +445 -0
  171. mlx/include/mlx/backend/metal/matmul.h +144 -0
  172. mlx/include/mlx/backend/metal/metal.h +30 -0
  173. mlx/include/mlx/backend/metal/reduce.h +41 -0
  174. mlx/include/mlx/backend/metal/resident.h +34 -0
  175. mlx/include/mlx/backend/metal/ternary.h +21 -0
  176. mlx/include/mlx/backend/metal/unary.h +21 -0
  177. mlx/include/mlx/backend/metal/utils.h +99 -0
  178. mlx/include/mlx/backend/no_gpu/apple_memory.h +16 -0
  179. mlx/include/mlx/backend/no_gpu/linux_memory.h +22 -0
  180. mlx/include/mlx/compile.h +45 -0
  181. mlx/include/mlx/compile_impl.h +73 -0
  182. mlx/include/mlx/device.h +63 -0
  183. mlx/include/mlx/distributed/distributed.h +61 -0
  184. mlx/include/mlx/distributed/distributed_impl.h +59 -0
  185. mlx/include/mlx/distributed/jaccl/jaccl.h +14 -0
  186. mlx/include/mlx/distributed/jaccl/lib/jaccl/group.h +59 -0
  187. mlx/include/mlx/distributed/jaccl/lib/jaccl/jaccl.h +85 -0
  188. mlx/include/mlx/distributed/jaccl/lib/jaccl/mesh.h +84 -0
  189. mlx/include/mlx/distributed/jaccl/lib/jaccl/mesh_impl.h +427 -0
  190. mlx/include/mlx/distributed/jaccl/lib/jaccl/rdma.h +348 -0
  191. mlx/include/mlx/distributed/jaccl/lib/jaccl/reduction_ops.h +117 -0
  192. mlx/include/mlx/distributed/jaccl/lib/jaccl/ring.h +84 -0
  193. mlx/include/mlx/distributed/jaccl/lib/jaccl/ring_impl.h +631 -0
  194. mlx/include/mlx/distributed/jaccl/lib/jaccl/tcp.h +67 -0
  195. mlx/include/mlx/distributed/jaccl/lib/jaccl/types.h +263 -0
  196. mlx/include/mlx/distributed/mpi/mpi.h +12 -0
  197. mlx/include/mlx/distributed/mpi/mpi_declarations.h +28 -0
  198. mlx/include/mlx/distributed/nccl/nccl.h +12 -0
  199. mlx/include/mlx/distributed/ops.h +57 -0
  200. mlx/include/mlx/distributed/primitives.h +156 -0
  201. mlx/include/mlx/distributed/reduction_ops.h +38 -0
  202. mlx/include/mlx/distributed/ring/ring.h +12 -0
  203. mlx/include/mlx/distributed/utils.h +67 -0
  204. mlx/include/mlx/dtype.h +116 -0
  205. mlx/include/mlx/dtype_utils.h +119 -0
  206. mlx/include/mlx/einsum.h +23 -0
  207. mlx/include/mlx/event.h +58 -0
  208. mlx/include/mlx/export.h +137 -0
  209. mlx/include/mlx/export_impl.h +99 -0
  210. mlx/include/mlx/fast.h +105 -0
  211. mlx/include/mlx/fast_primitives.h +432 -0
  212. mlx/include/mlx/fence.h +39 -0
  213. mlx/include/mlx/fft.h +237 -0
  214. mlx/include/mlx/graph_utils.h +67 -0
  215. mlx/include/mlx/io/gguf.h +20 -0
  216. mlx/include/mlx/io/load.h +179 -0
  217. mlx/include/mlx/io.h +61 -0
  218. mlx/include/mlx/linalg.h +119 -0
  219. mlx/include/mlx/memory.h +80 -0
  220. mlx/include/mlx/mlx.h +25 -0
  221. mlx/include/mlx/ops.h +1789 -0
  222. mlx/include/mlx/primitives.h +2551 -0
  223. mlx/include/mlx/random.h +283 -0
  224. mlx/include/mlx/scheduler.h +143 -0
  225. mlx/include/mlx/small_vector.h +540 -0
  226. mlx/include/mlx/stream.h +61 -0
  227. mlx/include/mlx/threadpool.h +133 -0
  228. mlx/include/mlx/transforms.h +231 -0
  229. mlx/include/mlx/transforms_impl.h +109 -0
  230. mlx/include/mlx/types/bf16.h +188 -0
  231. mlx/include/mlx/types/complex.h +113 -0
  232. mlx/include/mlx/types/fp16.h +235 -0
  233. mlx/include/mlx/types/half_types.h +58 -0
  234. mlx/include/mlx/types/limits.h +76 -0
  235. mlx/include/mlx/utils.h +212 -0
  236. mlx/include/mlx/version.h +22 -0
  237. mlx/lib/mlx.lib +0 -0
  238. mlx/libopenblas.dll +0 -0
  239. mlx/mlx.dll +0 -0
  240. mlx/py.typed +1 -0
  241. mlx/share/cmake/MLX/FindCUDNN.cmake +177 -0
  242. mlx/share/cmake/MLX/FindNCCL.cmake +54 -0
  243. mlx/share/cmake/MLX/Findnvpl.cmake +3 -0
  244. mlx/share/cmake/MLX/MLXConfig.cmake +66 -0
  245. mlx/share/cmake/MLX/MLXConfigVersion.cmake +65 -0
  246. mlx/share/cmake/MLX/MLXTargets-release.cmake +19 -0
  247. mlx/share/cmake/MLX/MLXTargets.cmake +107 -0
  248. mlx/share/cmake/MLX/extension.cmake +50 -0
  249. mlx/utils.py +326 -0
  250. mlx_cpu-0.32.0.dist-info/METADATA +142 -0
  251. mlx_cpu-0.32.0.dist-info/RECORD +254 -0
  252. mlx_cpu-0.32.0.dist-info/WHEEL +5 -0
  253. mlx_cpu-0.32.0.dist-info/licenses/LICENSE +21 -0
  254. mlx_cpu-0.32.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,482 @@
1
+ // Copyright © 2024-25 Apple Inc.
2
+
3
+ #include "mlx/backend/metal/kernels/steel/attn/nax.h"
4
+ #include "mlx/backend/metal/kernels/steel/attn/params.h"
5
+ #include "mlx/backend/metal/kernels/steel/attn/transforms.h"
6
+ #include "mlx/backend/metal/kernels/steel/utils.h"
7
+
8
+ using namespace mlx::steel;
9
+
10
+ ///////////////////////////////////////////////////////////////////////////////
11
+ // GEMM kernels
12
+ ///////////////////////////////////////////////////////////////////////////////
13
+
14
+ constant bool align_Q [[function_constant(200)]];
15
+ constant bool align_K [[function_constant(201)]];
16
+
17
+ constant bool has_mask [[function_constant(300)]];
18
+ constant bool do_causal [[function_constant(301)]];
19
+ constant bool has_sinks [[function_constant(302)]];
20
+
21
+ template <typename T>
22
+ struct TransformScale {
23
+ T scale;
24
+ METAL_FUNC TransformScale(T scale_) : scale(scale_) {}
25
+
26
+ METAL_FUNC T apply(T x) const {
27
+ return scale * x;
28
+ }
29
+ };
30
+
31
+ struct MaxOp {
32
+ template <typename T>
33
+ METAL_FUNC static constexpr T apply(T x, T y) {
34
+ return metal::max(x, y);
35
+ }
36
+ };
37
+
38
+ struct SumOp {
39
+ template <typename T>
40
+ METAL_FUNC static constexpr T apply(T x, T y) {
41
+ return x + y;
42
+ }
43
+ };
44
+
45
+ struct MulOp {
46
+ template <typename T>
47
+ METAL_FUNC static constexpr T apply(T x, T y) {
48
+ return x * y;
49
+ }
50
+ };
51
+
52
+ struct SubOp {
53
+ template <typename T>
54
+ METAL_FUNC static constexpr T apply(T x, T y) {
55
+ return x - y;
56
+ }
57
+ };
58
+
59
+ struct ExpSubOp {
60
+ template <typename T>
61
+ METAL_FUNC static constexpr T apply(T x, T y) {
62
+ return fast::exp2(x - y);
63
+ }
64
+ };
65
+
66
+ struct DivOp {
67
+ template <typename T>
68
+ METAL_FUNC static constexpr T apply(T x, T y) {
69
+ return x / y;
70
+ }
71
+ };
72
+
73
+ // clang-format off
74
+ template <
75
+ typename T,
76
+ int BQ,
77
+ int BK,
78
+ int BD,
79
+ int WM,
80
+ int WN,
81
+ typename MaskType = float,
82
+ typename AccumType = float>
83
+ [[kernel, max_total_threads_per_threadgroup(WM * WN * 32)]] void attention_nax(
84
+ const device T* Q [[buffer(0)]],
85
+ const device T* K [[buffer(1)]],
86
+ const device T* V [[buffer(2)]],
87
+ device T* O [[buffer(3)]],
88
+ const constant AttnParams* params [[buffer(4)]],
89
+ const constant AttnMaskParams* mask_params [[buffer(5), function_constant(has_mask)]],
90
+ const device MaskType* mask [[buffer(6), function_constant(has_mask)]],
91
+ const device T* sinks [[buffer(7), function_constant(has_sinks)]],
92
+ uint simd_lane_id [[thread_index_in_simdgroup]],
93
+ uint simd_group_id [[simdgroup_index_in_threadgroup]],
94
+ uint3 tid [[threadgroup_position_in_grid]],
95
+ uint3 lid [[thread_position_in_threadgroup]]) { // clang-format on
96
+
97
+ // Pacifying compiler
98
+ (void)lid;
99
+ (void)simd_lane_id;
100
+
101
+ // Move to correct block
102
+ ulong3 tidl{tid.x, tid.y, tid.z};
103
+
104
+ Q += tidl.z * params->Q_strides[0] + // Batch
105
+ tidl.y * params->Q_strides[1] + // Head
106
+ tidl.x * BQ * params->Q_strides[2]; // Sequence
107
+
108
+ ulong kv_head_idx = int(tid.y) / params->gqa_factor;
109
+ K += tidl.z * params->K_strides[0] + // Batch
110
+ kv_head_idx * params->K_strides[1]; // Head
111
+
112
+ V += tidl.z * params->V_strides[0] + // Batch
113
+ kv_head_idx * params->V_strides[1]; // Head
114
+
115
+ O += tidl.z * params->O_strides[0] + // Batch
116
+ tidl.y * params->O_strides[1] + // Head
117
+ tidl.x * BQ * params->O_strides[2]; // Sequence
118
+
119
+ if (has_mask) {
120
+ mask += tidl.z * mask_params->M_strides[0] + // Batch
121
+ tidl.y * mask_params->M_strides[1]; // Head
122
+ }
123
+
124
+ const metal::uniform<float> scale2 =
125
+ make_uniform(params->scale) * make_uniform(1.44269504089f);
126
+
127
+ // Prepare MMA tiles
128
+ constexpr short kU = 16;
129
+
130
+ constexpr int kNWarps = WM * WN;
131
+ static_assert(
132
+ BQ >= (kNWarps * kU) && BQ % (kNWarps * kU) == 0,
133
+ "Each simdgroup must host atleast 1 simdgroup matrix along Q sequence.");
134
+
135
+ // Q seq frags per warp
136
+ constexpr int TQ = BQ / (kNWarps * kU);
137
+ // HeadDim frags (all warps load the same frags)
138
+ constexpr int TD = BD / kU;
139
+ // KV seq frags per warp
140
+ constexpr short TK = BK / kU;
141
+
142
+ static_assert(TQ == 1, "Check TQ");
143
+ using otile_t = NAXTile<AccumType, TQ, TD>;
144
+ otile_t Otile;
145
+
146
+ Otile.clear();
147
+
148
+ // Prepare mma tile offsets
149
+ const short tm = kU * TQ * simd_group_id;
150
+ Q += tm * int(params->Q_strides[2]);
151
+
152
+ const short2 simd_coord = otile_t::NAXFrag_t::get_coord();
153
+ const short sm = simd_coord.y;
154
+ const short sn = simd_coord.x;
155
+
156
+ // Init row reduction variables
157
+ constexpr short kRowsPT = otile_t::kRowsPerThread;
158
+
159
+ metal::vec<AccumType, kRowsPT> max_score;
160
+ metal::vec<AccumType, kRowsPT> sum_score{0};
161
+
162
+ // Init to -Inf
163
+ STEEL_PRAGMA_UNROLL
164
+ for (short i = 0; i < kRowsPT; ++i) {
165
+ max_score[i] = Limits<AccumType>::finite_min;
166
+ }
167
+
168
+ if (has_sinks) {
169
+ STEEL_PRAGMA_UNROLL
170
+ for (short i = 0; i < kRowsPT; ++i) {
171
+ max_score[i] = M_LOG2E_F * static_cast<AccumType>(sinks[tidl.y]);
172
+ sum_score[i] = 1;
173
+ }
174
+ }
175
+
176
+ int kb_lim = params->NK;
177
+ int kb_min_causal = params->NK;
178
+
179
+ if (do_causal) {
180
+ int q_max = (tid.x + 1) * BQ + params->qL_off;
181
+ kb_lim = (q_max + BK - 1) / BK;
182
+ kb_lim = min(params->NK, kb_lim);
183
+
184
+ int q_min = tid.x * BQ + params->qL_off;
185
+ q_min = max(0, q_min);
186
+ kb_min_causal = (q_min / BK);
187
+ }
188
+
189
+ const bool is_last_bq = int(tid.x) == (params->NQ_aligned);
190
+ // const bool is_last_tq = int(simd_group_id) >= (params->qL_rem / UQ);
191
+ const bool is_last_q = is_last_bq;
192
+
193
+ const short lim_rows_q = params->qL_rem - tm;
194
+ const short lim_rows_k = params->kL_rem;
195
+
196
+ // Loop over KV seq length
197
+ for (int kb = 0; kb < kb_lim; kb++) {
198
+ const int is_last_k = (kb == (params->NK_aligned));
199
+
200
+ // Do S = Q @ K.T
201
+ using stile_t = NAXTile<AccumType, TQ, TK>;
202
+ stile_t Stile;
203
+
204
+ Stile.clear();
205
+
206
+ STEEL_PRAGMA_UNROLL
207
+ for (short iq = 0; iq < TQ; iq++) {
208
+ STEEL_PRAGMA_UNROLL
209
+ for (short ik = 0; ik < TK; ik += 2) {
210
+ STEEL_PRAGMA_UNROLL
211
+ for (short id = 0; id < TD; id++) {
212
+ NAXTile<T, 1, 1> Qtile;
213
+ NAXTile<T, 2, 1> Ktile;
214
+
215
+ const int Q_load_off = iq * kU * int(params->Q_strides[2]) + id * kU;
216
+ const int K_load_off = ik * kU * int(params->K_strides[2]) + id * kU;
217
+
218
+ if (!align_Q && is_last_q) {
219
+ Qtile.load_rows(
220
+ Q + Q_load_off,
221
+ int(params->Q_strides[2]),
222
+ lim_rows_q - iq * kU);
223
+ } else {
224
+ Qtile.load(Q + Q_load_off, int(params->Q_strides[2]));
225
+ }
226
+
227
+ if (!align_K && is_last_k) {
228
+ Ktile.load_rows(
229
+ K + K_load_off,
230
+ int(params->K_strides[2]),
231
+ lim_rows_k - ik * kU);
232
+ } else {
233
+ Ktile.load(K + K_load_off, int(params->K_strides[2]));
234
+ }
235
+
236
+ stile_t::NAXFrag_t::mma(
237
+ Stile.frag_at(iq, ik),
238
+ Stile.frag_at(iq, ik + 1),
239
+ Qtile.frag_at(0, 0),
240
+ metal::false_type{},
241
+ Ktile.frag_at(0, 0),
242
+ Ktile.frag_at(1, 0),
243
+ metal::true_type{});
244
+ }
245
+ }
246
+ }
247
+
248
+ // Scale S
249
+ STEEL_PRAGMA_UNROLL
250
+ for (short ii = 0; ii < stile_t::kElemsPerTile; ii++) {
251
+ Stile.elems()[ii] *= float(scale2);
252
+ }
253
+
254
+ // Mask out length sequence
255
+ if (!align_K && is_last_k) {
256
+ constexpr auto neg_inf = Limits<AccumType>::finite_min;
257
+
258
+ STEEL_PRAGMA_UNROLL
259
+ for (short iq = 0; iq < TQ; iq++) {
260
+ STEEL_PRAGMA_UNROLL
261
+ for (short ik = 0; ik < TK; ik++) {
262
+ const short col_pos = ik * kU + sn;
263
+
264
+ thread auto& fg = Stile.frag_at(iq, ik);
265
+
266
+ STEEL_PRAGMA_UNROLL
267
+ for (short ii = 0; ii < stile_t::kFragThrRows; ii++) {
268
+ STEEL_PRAGMA_UNROLL
269
+ for (short jj = 0; jj < stile_t::kFragThrCols; jj++) {
270
+ const auto loc = ii * stile_t::kFragThrCols + jj;
271
+ fg[loc] = ((col_pos + jj) < params->kL_rem) ? fg[loc] : neg_inf;
272
+ }
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ // Mask out if causal
279
+ if (do_causal && kb >= kb_min_causal) {
280
+ constexpr auto neg_inf = Limits<AccumType>::finite_min;
281
+
282
+ const int base_row = tid.x * BQ + params->qL_off + tm;
283
+ const int base_col = kb * BK;
284
+
285
+ STEEL_PRAGMA_UNROLL
286
+ for (short iq = 0; iq < TQ; iq++) {
287
+ STEEL_PRAGMA_UNROLL
288
+ for (short ik = 0; ik < TK; ik++) {
289
+ thread auto& fg = Stile.frag_at(iq, ik);
290
+
291
+ STEEL_PRAGMA_UNROLL
292
+ for (short ii = 0; ii < stile_t::kFragThrRows; ii++) {
293
+ STEEL_PRAGMA_UNROLL
294
+ for (short jj = 0; jj < stile_t::kFragThrCols; jj++) {
295
+ const auto r =
296
+ base_row + iq * kU + ii * stile_t::kFragRowsJump + sm;
297
+ const auto c = base_col + ik * kU + jj + sn;
298
+ const auto loc = ii * stile_t::kFragThrCols + jj;
299
+ fg[loc] = (r < c) ? neg_inf : fg[loc];
300
+ }
301
+ }
302
+ }
303
+ }
304
+ }
305
+
306
+ // Other masking as needed
307
+ if (has_mask) {
308
+ constexpr auto neg_inf = Limits<AccumType>::finite_min;
309
+
310
+ const int base_row = tid.x * BQ + tm;
311
+ const int base_col = kb * BK;
312
+
313
+ constexpr bool is_bool = is_same_v<MaskType, bool>;
314
+ using melem_t = typename metal::conditional_t<is_bool, bool, AccumType>;
315
+ using mtile_t = NAXTile<melem_t, TQ, TK>;
316
+ using mfrag_t = typename mtile_t::frag_type;
317
+
318
+ if (base_row + BQ <= params->qL && base_col + BK <= params->kL) {
319
+ for (short iq = 0; iq < TQ; iq++) {
320
+ STEEL_PRAGMA_UNROLL
321
+ for (short ik = 0; ik < TK; ik++) {
322
+ const int row_pos = base_row + iq * kU;
323
+ const int col_pos = base_col + ik * kU;
324
+
325
+ mfrag_t mfrag;
326
+ mtile_t::NAXFrag_t::load(
327
+ mfrag,
328
+ mask,
329
+ int64_t(mask_params->M_strides[2]),
330
+ Int<1>{},
331
+ row_pos,
332
+ col_pos);
333
+
334
+ thread auto& fg = Stile.frag_at(iq, ik);
335
+
336
+ STEEL_PRAGMA_UNROLL
337
+ for (short jj = 0; jj < mtile_t::kElemsPerFrag; jj++) {
338
+ if constexpr (is_bool) {
339
+ fg[jj] = mfrag[jj] ? fg[jj] : neg_inf;
340
+ } else {
341
+ fg[jj] += M_LOG2E_F * AccumType(mfrag[jj]);
342
+ }
343
+ }
344
+ }
345
+ }
346
+ } else {
347
+ STEEL_PRAGMA_UNROLL
348
+ for (short iq = 0; iq < TQ; iq++) {
349
+ STEEL_PRAGMA_UNROLL
350
+ for (short ik = 0; ik < TK; ik++) {
351
+ const int row_pos = base_row + iq * kU;
352
+ const int col_pos = base_col + ik * kU;
353
+
354
+ mfrag_t mfrag;
355
+ mtile_t::NAXFrag_t::load_safe(
356
+ mfrag,
357
+ mask,
358
+ int64_t(mask_params->M_strides[2]),
359
+ Int<1>{},
360
+ params->qL,
361
+ params->kL,
362
+ row_pos,
363
+ col_pos);
364
+
365
+ thread auto& fg = Stile.frag_at(iq, ik);
366
+
367
+ STEEL_PRAGMA_UNROLL
368
+ for (short jj = 0; jj < mtile_t::kElemsPerFrag; jj++) {
369
+ if constexpr (is_bool) {
370
+ fg[jj] = mfrag[jj] ? fg[jj] : neg_inf;
371
+ } else {
372
+ fg[jj] += M_LOG2E_F * AccumType(mfrag[jj]);
373
+ }
374
+ }
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ // Do softmax
381
+
382
+ // Temp variables
383
+ metal::vec<AccumType, kRowsPT> new_max;
384
+ metal::vec<AccumType, kRowsPT> factor;
385
+ STEEL_PRAGMA_UNROLL
386
+ for (short i = 0; i < kRowsPT; ++i) {
387
+ new_max[i] = max_score[i];
388
+ }
389
+
390
+ // Row max
391
+ Stile.template row_reduce<MaxOp>(new_max);
392
+
393
+ // exp(Si - rowmax(Si))
394
+ Stile.template row_bin_op<ExpSubOp>(new_max);
395
+
396
+ // Factor exp(rowmax(Si) - rowmax(Si-1))
397
+ STEEL_PRAGMA_UNROLL
398
+ for (short i = 0; i < kRowsPT; ++i) {
399
+ factor[i] = fast::exp2(max_score[i] - new_max[i]);
400
+ max_score[i] = new_max[i];
401
+ }
402
+
403
+ // Row Sum
404
+ STEEL_PRAGMA_UNROLL
405
+ for (short i = 0; i < kRowsPT; ++i) {
406
+ sum_score[i] = sum_score[i] * factor[i];
407
+ }
408
+
409
+ Stile.template row_reduce<SumOp>(sum_score);
410
+
411
+ // Update O
412
+ Otile.template row_bin_op<MulOp>(factor);
413
+
414
+ simdgroup_barrier(mem_flags::mem_none);
415
+
416
+ // Do O = P @ V
417
+ STEEL_PRAGMA_UNROLL
418
+ for (short iq = 0; iq < TQ; iq++) {
419
+ STEEL_PRAGMA_UNROLL
420
+ for (short id = 0; id < TD; id += 2) {
421
+ if constexpr (BD == 128) {
422
+ if (id == 4) {
423
+ threadgroup_barrier(mem_flags::mem_none);
424
+ }
425
+ }
426
+
427
+ STEEL_PRAGMA_UNROLL
428
+ for (short ik = 0; ik < TK; ik++) {
429
+ NAXTile<T, 1, 2> Vtile;
430
+
431
+ const int V_load_off = ik * kU * int(params->V_strides[2]) + id * kU;
432
+
433
+ if (!align_K && is_last_k) {
434
+ Vtile.load_rows(
435
+ V + V_load_off,
436
+ int(params->V_strides[2]),
437
+ lim_rows_k - ik * kU);
438
+ } else {
439
+ Vtile.load(V + V_load_off, int(params->V_strides[2]));
440
+ }
441
+
442
+ otile_t::NAXFrag_t::mma(
443
+ Otile.frag_at(iq, id),
444
+ Otile.frag_at(iq, id + 1),
445
+ Stile.frag_at(iq, ik),
446
+ metal::false_type{},
447
+ Vtile.frag_at(0, 0),
448
+ Vtile.frag_at(0, 1),
449
+ metal::false_type{});
450
+ }
451
+ }
452
+ }
453
+
454
+ // Prepare for next iteration
455
+ K += BK * int(params->K_strides[2]);
456
+ V += BK * int(params->V_strides[2]);
457
+ }
458
+
459
+ // Normalize output
460
+
461
+ threadgroup_barrier(mem_flags::mem_none);
462
+
463
+ metal::vec<AccumType, kRowsPT> rcp;
464
+ STEEL_PRAGMA_UNROLL
465
+ for (short i = 0; i < kRowsPT; ++i) {
466
+ rcp[i] = 1.f / sum_score[i];
467
+ }
468
+
469
+ Otile.template row_bin_op<MulOp>(rcp);
470
+
471
+ // Store results
472
+ O += tm * int(params->O_strides[2]);
473
+
474
+ if (!align_Q && is_last_q) {
475
+ if (lim_rows_q <= 0)
476
+ return;
477
+
478
+ Otile.store_rows(O, int(params->O_strides[2]), lim_rows_q);
479
+ } else {
480
+ Otile.store(O, int(params->O_strides[2]));
481
+ }
482
+ }