mlx 1.0.0

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.

Potentially problematic release.


This version of mlx might be problematic. Click here for more details.

Files changed (914) hide show
  1. checksums.yaml +7 -0
  2. data/ext/mlx/CMakeLists.txt +7 -0
  3. data/ext/mlx/Makefile +273 -0
  4. data/ext/mlx/extconf.rb +94 -0
  5. data/ext/mlx/mkmf.log +44 -0
  6. data/ext/mlx/native.bundle +0 -0
  7. data/ext/mlx/native.bundle.dSYM/Contents/Info.plist +20 -0
  8. data/ext/mlx/native.bundle.dSYM/Contents/Resources/DWARF/native.bundle +0 -0
  9. data/ext/mlx/native.bundle.dSYM/Contents/Resources/Relocations/aarch64/native.bundle.yml +5 -0
  10. data/ext/mlx/native.cpp +8027 -0
  11. data/ext/mlx/native.o +0 -0
  12. data/lib/mlx/core.rb +1678 -0
  13. data/lib/mlx/distributed_utils/common.rb +116 -0
  14. data/lib/mlx/distributed_utils/config.rb +600 -0
  15. data/lib/mlx/distributed_utils/launch.rb +490 -0
  16. data/lib/mlx/extension.rb +24 -0
  17. data/lib/mlx/nn/base.rb +388 -0
  18. data/lib/mlx/nn/init.rb +140 -0
  19. data/lib/mlx/nn/layers/activations.rb +336 -0
  20. data/lib/mlx/nn/layers/base.rb +6 -0
  21. data/lib/mlx/nn/layers/containers.rb +20 -0
  22. data/lib/mlx/nn/layers/convolution.rb +120 -0
  23. data/lib/mlx/nn/layers/convolution_transpose.rb +114 -0
  24. data/lib/mlx/nn/layers/distributed.rb +309 -0
  25. data/lib/mlx/nn/layers/dropout.rb +75 -0
  26. data/lib/mlx/nn/layers/embedding.rb +28 -0
  27. data/lib/mlx/nn/layers/linear.rb +79 -0
  28. data/lib/mlx/nn/layers/normalization.rb +216 -0
  29. data/lib/mlx/nn/layers/pooling.rb +167 -0
  30. data/lib/mlx/nn/layers/positional_encoding.rb +126 -0
  31. data/lib/mlx/nn/layers/quantized.rb +215 -0
  32. data/lib/mlx/nn/layers/recurrent.rb +135 -0
  33. data/lib/mlx/nn/layers/transformer.rb +330 -0
  34. data/lib/mlx/nn/layers/upsample.rb +97 -0
  35. data/lib/mlx/nn/layers.rb +18 -0
  36. data/lib/mlx/nn/losses.rb +251 -0
  37. data/lib/mlx/nn/utils.rb +167 -0
  38. data/lib/mlx/nn.rb +12 -0
  39. data/lib/mlx/optimizers/optimizers.rb +808 -0
  40. data/lib/mlx/optimizers/schedulers.rb +62 -0
  41. data/lib/mlx/optimizers.rb +9 -0
  42. data/lib/mlx/utils.rb +171 -0
  43. data/lib/mlx/version +1 -0
  44. data/lib/mlx/version.rb +5 -0
  45. data/lib/mlx.rb +64 -0
  46. data/mlx/.clang-format +87 -0
  47. data/mlx/.git +1 -0
  48. data/mlx/.github/ISSUE_TEMPLATE/bug_report.md +28 -0
  49. data/mlx/.github/actions/build-cuda-release/action.yml +31 -0
  50. data/mlx/.github/actions/build-docs/action.yml +38 -0
  51. data/mlx/.github/actions/build-linux/action.yml +38 -0
  52. data/mlx/.github/actions/build-linux-release/action.yml +42 -0
  53. data/mlx/.github/actions/build-macos/action.yml +80 -0
  54. data/mlx/.github/actions/build-macos-release/action.yml +36 -0
  55. data/mlx/.github/actions/build-windows/action.yml +26 -0
  56. data/mlx/.github/actions/setup-linux/action.yml +93 -0
  57. data/mlx/.github/actions/setup-macos/action.yml +24 -0
  58. data/mlx/.github/actions/setup-windows/action.yml +42 -0
  59. data/mlx/.github/actions/test-linux/action.yml +69 -0
  60. data/mlx/.github/actions/test-windows/action.yml +20 -0
  61. data/mlx/.github/dependabot.yml +6 -0
  62. data/mlx/.github/pull_request_template.md +12 -0
  63. data/mlx/.github/scripts/build-sanitizer-tests.sh +48 -0
  64. data/mlx/.github/scripts/setup+build-cpp-linux-fedora-container.sh +27 -0
  65. data/mlx/.github/workflows/build_and_test.yml +152 -0
  66. data/mlx/.github/workflows/documentation.yml +28 -0
  67. data/mlx/.github/workflows/nightly.yml +104 -0
  68. data/mlx/.github/workflows/release.yml +256 -0
  69. data/mlx/.gitignore +81 -0
  70. data/mlx/.pre-commit-config.yaml +27 -0
  71. data/mlx/ACKNOWLEDGMENTS.md +268 -0
  72. data/mlx/CITATION.cff +24 -0
  73. data/mlx/CMakeLists.txt +437 -0
  74. data/mlx/CODE_OF_CONDUCT.md +132 -0
  75. data/mlx/CONTRIBUTING.md +38 -0
  76. data/mlx/LICENSE +21 -0
  77. data/mlx/MANIFEST.in +6 -0
  78. data/mlx/README.md +121 -0
  79. data/mlx/benchmarks/cpp/CMakeLists.txt +11 -0
  80. data/mlx/benchmarks/cpp/autograd.cpp +39 -0
  81. data/mlx/benchmarks/cpp/compare_devices.cpp +27 -0
  82. data/mlx/benchmarks/cpp/irregular_strides.cpp +201 -0
  83. data/mlx/benchmarks/cpp/single_ops.cpp +288 -0
  84. data/mlx/benchmarks/cpp/time_utils.h +39 -0
  85. data/mlx/benchmarks/numpy/single_ops.py +39 -0
  86. data/mlx/benchmarks/numpy/time_utils.py +20 -0
  87. data/mlx/benchmarks/python/batch_matmul_bench.py +62 -0
  88. data/mlx/benchmarks/python/blas/bench_gemm.py +191 -0
  89. data/mlx/benchmarks/python/blas/bench_gemv.py +220 -0
  90. data/mlx/benchmarks/python/comparative/README.md +15 -0
  91. data/mlx/benchmarks/python/comparative/bench_mlx.py +519 -0
  92. data/mlx/benchmarks/python/comparative/bench_torch.py +482 -0
  93. data/mlx/benchmarks/python/comparative/compare.py +284 -0
  94. data/mlx/benchmarks/python/compile_bench.py +107 -0
  95. data/mlx/benchmarks/python/conv1d_bench.py +123 -0
  96. data/mlx/benchmarks/python/conv2d_bench_cpu.py +127 -0
  97. data/mlx/benchmarks/python/conv2d_train_bench_cpu.py +143 -0
  98. data/mlx/benchmarks/python/conv2d_transpose_bench_cpu.py +129 -0
  99. data/mlx/benchmarks/python/conv3d_bench_cpu.py +110 -0
  100. data/mlx/benchmarks/python/conv3d_train_bench_cpu.py +143 -0
  101. data/mlx/benchmarks/python/conv3d_transpose_bench_cpu.py +116 -0
  102. data/mlx/benchmarks/python/conv_bench.py +135 -0
  103. data/mlx/benchmarks/python/conv_transpose_bench.py +135 -0
  104. data/mlx/benchmarks/python/conv_unaligned_bench.py +107 -0
  105. data/mlx/benchmarks/python/distributed_bench.py +66 -0
  106. data/mlx/benchmarks/python/einsum_bench.py +84 -0
  107. data/mlx/benchmarks/python/fft_bench.py +118 -0
  108. data/mlx/benchmarks/python/gather_bench.py +52 -0
  109. data/mlx/benchmarks/python/gather_mm_bench.py +74 -0
  110. data/mlx/benchmarks/python/gather_qmm_bench.py +84 -0
  111. data/mlx/benchmarks/python/hadamard_bench.py +70 -0
  112. data/mlx/benchmarks/python/large_gemm_bench.py +119 -0
  113. data/mlx/benchmarks/python/layer_norm_bench.py +82 -0
  114. data/mlx/benchmarks/python/masked_scatter.py +212 -0
  115. data/mlx/benchmarks/python/rms_norm_bench.py +63 -0
  116. data/mlx/benchmarks/python/rope_bench.py +35 -0
  117. data/mlx/benchmarks/python/scatter_bench.py +96 -0
  118. data/mlx/benchmarks/python/sdpa_bench.py +223 -0
  119. data/mlx/benchmarks/python/sdpa_vector_bench.py +95 -0
  120. data/mlx/benchmarks/python/single_ops.py +132 -0
  121. data/mlx/benchmarks/python/synchronize_bench.py +55 -0
  122. data/mlx/benchmarks/python/time_utils.py +38 -0
  123. data/mlx/cmake/FindCUDNN.cmake +177 -0
  124. data/mlx/cmake/FindNCCL.cmake +54 -0
  125. data/mlx/cmake/Findnvpl.cmake +3 -0
  126. data/mlx/cmake/extension.cmake +50 -0
  127. data/mlx/docs/.clang-format +2 -0
  128. data/mlx/docs/.gitignore +3 -0
  129. data/mlx/docs/.nojekyll +0 -0
  130. data/mlx/docs/Doxyfile +51 -0
  131. data/mlx/docs/Makefile +18 -0
  132. data/mlx/docs/README.md +54 -0
  133. data/mlx/docs/index.html +1 -0
  134. data/mlx/docs/requirements.txt +5 -0
  135. data/mlx/docs/src/_static/distributed/m3-ultra-mesh-broken.png +0 -0
  136. data/mlx/docs/src/_static/distributed/m3-ultra-mesh.png +0 -0
  137. data/mlx/docs/src/_static/metal_debugger/capture.png +0 -0
  138. data/mlx/docs/src/_static/metal_debugger/schema.png +0 -0
  139. data/mlx/docs/src/_static/mlx_logo.png +0 -0
  140. data/mlx/docs/src/_static/mlx_logo_dark.png +0 -0
  141. data/mlx/docs/src/_static/tp_inference/all-to-sharded-linear.png +0 -0
  142. data/mlx/docs/src/_static/tp_inference/column-row-tp.png +0 -0
  143. data/mlx/docs/src/_static/tp_inference/llama-transformer.png +0 -0
  144. data/mlx/docs/src/_static/tp_inference/sharded-to-all-linear.png +0 -0
  145. data/mlx/docs/src/_templates/module-base-class.rst +33 -0
  146. data/mlx/docs/src/_templates/nn-module-template.rst +20 -0
  147. data/mlx/docs/src/_templates/optimizers-template.rst +20 -0
  148. data/mlx/docs/src/conf.py +99 -0
  149. data/mlx/docs/src/cpp/ops.rst +7 -0
  150. data/mlx/docs/src/dev/custom_metal_kernels.rst +445 -0
  151. data/mlx/docs/src/dev/extensions.rst +811 -0
  152. data/mlx/docs/src/dev/metal_debugger.rst +68 -0
  153. data/mlx/docs/src/dev/metal_logging.rst +40 -0
  154. data/mlx/docs/src/dev/mlx_in_cpp.rst +121 -0
  155. data/mlx/docs/src/examples/data_parallelism.rst +91 -0
  156. data/mlx/docs/src/examples/linear_regression.rst +77 -0
  157. data/mlx/docs/src/examples/llama-inference.rst +382 -0
  158. data/mlx/docs/src/examples/mlp.rst +134 -0
  159. data/mlx/docs/src/examples/tensor_parallelism.rst +239 -0
  160. data/mlx/docs/src/index.rst +96 -0
  161. data/mlx/docs/src/install.rst +340 -0
  162. data/mlx/docs/src/python/array.rst +65 -0
  163. data/mlx/docs/src/python/cuda.rst +9 -0
  164. data/mlx/docs/src/python/data_types.rst +78 -0
  165. data/mlx/docs/src/python/devices_and_streams.rst +21 -0
  166. data/mlx/docs/src/python/distributed.rst +22 -0
  167. data/mlx/docs/src/python/export.rst +14 -0
  168. data/mlx/docs/src/python/fast.rst +16 -0
  169. data/mlx/docs/src/python/fft.rst +24 -0
  170. data/mlx/docs/src/python/linalg.rst +27 -0
  171. data/mlx/docs/src/python/memory_management.rst +16 -0
  172. data/mlx/docs/src/python/metal.rst +12 -0
  173. data/mlx/docs/src/python/nn/distributed.rst +30 -0
  174. data/mlx/docs/src/python/nn/functions.rst +40 -0
  175. data/mlx/docs/src/python/nn/init.rst +45 -0
  176. data/mlx/docs/src/python/nn/layers.rst +74 -0
  177. data/mlx/docs/src/python/nn/losses.rst +25 -0
  178. data/mlx/docs/src/python/nn/module.rst +38 -0
  179. data/mlx/docs/src/python/nn.rst +186 -0
  180. data/mlx/docs/src/python/ops.rst +184 -0
  181. data/mlx/docs/src/python/optimizers/common_optimizers.rst +22 -0
  182. data/mlx/docs/src/python/optimizers/optimizer.rst +23 -0
  183. data/mlx/docs/src/python/optimizers/schedulers.rst +15 -0
  184. data/mlx/docs/src/python/optimizers.rst +78 -0
  185. data/mlx/docs/src/python/random.rst +48 -0
  186. data/mlx/docs/src/python/transforms.rst +22 -0
  187. data/mlx/docs/src/python/tree_utils.rst +23 -0
  188. data/mlx/docs/src/usage/compile.rst +516 -0
  189. data/mlx/docs/src/usage/distributed.rst +572 -0
  190. data/mlx/docs/src/usage/export.rst +288 -0
  191. data/mlx/docs/src/usage/function_transforms.rst +191 -0
  192. data/mlx/docs/src/usage/indexing.rst +194 -0
  193. data/mlx/docs/src/usage/launching_distributed.rst +234 -0
  194. data/mlx/docs/src/usage/lazy_evaluation.rst +144 -0
  195. data/mlx/docs/src/usage/numpy.rst +124 -0
  196. data/mlx/docs/src/usage/quick_start.rst +67 -0
  197. data/mlx/docs/src/usage/saving_and_loading.rst +81 -0
  198. data/mlx/docs/src/usage/unified_memory.rst +78 -0
  199. data/mlx/docs/src/usage/using_streams.rst +18 -0
  200. data/mlx/examples/cmake_project/CMakeLists.txt +22 -0
  201. data/mlx/examples/cmake_project/README.md +26 -0
  202. data/mlx/examples/cmake_project/example.cpp +14 -0
  203. data/mlx/examples/cpp/CMakeLists.txt +12 -0
  204. data/mlx/examples/cpp/distributed.cpp +22 -0
  205. data/mlx/examples/cpp/linear_regression.cpp +54 -0
  206. data/mlx/examples/cpp/logistic_regression.cpp +54 -0
  207. data/mlx/examples/cpp/metal_capture.cpp +31 -0
  208. data/mlx/examples/cpp/timer.h +20 -0
  209. data/mlx/examples/cpp/tutorial.cpp +99 -0
  210. data/mlx/examples/export/CMakeLists.txt +22 -0
  211. data/mlx/examples/export/README.md +49 -0
  212. data/mlx/examples/export/eval_mlp.cpp +25 -0
  213. data/mlx/examples/export/eval_mlp.py +52 -0
  214. data/mlx/examples/export/train_mlp.cpp +35 -0
  215. data/mlx/examples/export/train_mlp.py +76 -0
  216. data/mlx/examples/extensions/CMakeLists.txt +78 -0
  217. data/mlx/examples/extensions/README.md +24 -0
  218. data/mlx/examples/extensions/axpby/axpby.cpp +306 -0
  219. data/mlx/examples/extensions/axpby/axpby.h +90 -0
  220. data/mlx/examples/extensions/axpby/axpby.metal +47 -0
  221. data/mlx/examples/extensions/bindings.cpp +39 -0
  222. data/mlx/examples/extensions/mlx_sample_extensions/__init__.py +5 -0
  223. data/mlx/examples/extensions/pyproject.toml +8 -0
  224. data/mlx/examples/extensions/requirements.txt +4 -0
  225. data/mlx/examples/extensions/setup.py +18 -0
  226. data/mlx/examples/extensions/test.py +12 -0
  227. data/mlx/examples/python/linear_regression.py +46 -0
  228. data/mlx/examples/python/logistic_regression.py +49 -0
  229. data/mlx/examples/python/qqmm.py +117 -0
  230. data/mlx/mlx/3rdparty/.clang-format +2 -0
  231. data/mlx/mlx/3rdparty/pocketfft.h +3581 -0
  232. data/mlx/mlx/CMakeLists.txt +107 -0
  233. data/mlx/mlx/allocator.h +75 -0
  234. data/mlx/mlx/api.h +29 -0
  235. data/mlx/mlx/array.cpp +354 -0
  236. data/mlx/mlx/array.h +647 -0
  237. data/mlx/mlx/backend/common/CMakeLists.txt +9 -0
  238. data/mlx/mlx/backend/common/binary.h +97 -0
  239. data/mlx/mlx/backend/common/broadcasting.cpp +24 -0
  240. data/mlx/mlx/backend/common/broadcasting.h +11 -0
  241. data/mlx/mlx/backend/common/buffer_cache.h +158 -0
  242. data/mlx/mlx/backend/common/common.cpp +305 -0
  243. data/mlx/mlx/backend/common/compiled.cpp +243 -0
  244. data/mlx/mlx/backend/common/compiled.h +77 -0
  245. data/mlx/mlx/backend/common/copy.h +50 -0
  246. data/mlx/mlx/backend/common/hadamard.h +109 -0
  247. data/mlx/mlx/backend/common/load.cpp +57 -0
  248. data/mlx/mlx/backend/common/matmul.h +67 -0
  249. data/mlx/mlx/backend/common/reduce.cpp +154 -0
  250. data/mlx/mlx/backend/common/reduce.h +59 -0
  251. data/mlx/mlx/backend/common/slicing.cpp +71 -0
  252. data/mlx/mlx/backend/common/slicing.h +20 -0
  253. data/mlx/mlx/backend/common/ternary.h +85 -0
  254. data/mlx/mlx/backend/common/unary.h +29 -0
  255. data/mlx/mlx/backend/common/utils.cpp +231 -0
  256. data/mlx/mlx/backend/common/utils.h +205 -0
  257. data/mlx/mlx/backend/cpu/CMakeLists.txt +88 -0
  258. data/mlx/mlx/backend/cpu/arange.h +28 -0
  259. data/mlx/mlx/backend/cpu/arg_reduce.cpp +124 -0
  260. data/mlx/mlx/backend/cpu/binary.cpp +269 -0
  261. data/mlx/mlx/backend/cpu/binary.h +517 -0
  262. data/mlx/mlx/backend/cpu/binary_ops.h +98 -0
  263. data/mlx/mlx/backend/cpu/binary_two.h +166 -0
  264. data/mlx/mlx/backend/cpu/cholesky.cpp +85 -0
  265. data/mlx/mlx/backend/cpu/compiled.cpp +357 -0
  266. data/mlx/mlx/backend/cpu/compiled_preamble.h +12 -0
  267. data/mlx/mlx/backend/cpu/conv.cpp +1351 -0
  268. data/mlx/mlx/backend/cpu/copy.cpp +386 -0
  269. data/mlx/mlx/backend/cpu/copy.h +36 -0
  270. data/mlx/mlx/backend/cpu/device_info.cpp +113 -0
  271. data/mlx/mlx/backend/cpu/device_info.h +28 -0
  272. data/mlx/mlx/backend/cpu/distributed.cpp +103 -0
  273. data/mlx/mlx/backend/cpu/eig.cpp +281 -0
  274. data/mlx/mlx/backend/cpu/eigh.cpp +241 -0
  275. data/mlx/mlx/backend/cpu/encoder.cpp +16 -0
  276. data/mlx/mlx/backend/cpu/encoder.h +67 -0
  277. data/mlx/mlx/backend/cpu/eval.cpp +40 -0
  278. data/mlx/mlx/backend/cpu/eval.h +12 -0
  279. data/mlx/mlx/backend/cpu/fft.cpp +120 -0
  280. data/mlx/mlx/backend/cpu/gemm.h +26 -0
  281. data/mlx/mlx/backend/cpu/gemms/bnns.cpp +214 -0
  282. data/mlx/mlx/backend/cpu/gemms/cblas.cpp +134 -0
  283. data/mlx/mlx/backend/cpu/gemms/simd_bf16.cpp +45 -0
  284. data/mlx/mlx/backend/cpu/gemms/simd_fp16.cpp +45 -0
  285. data/mlx/mlx/backend/cpu/gemms/simd_gemm.h +139 -0
  286. data/mlx/mlx/backend/cpu/hadamard.cpp +121 -0
  287. data/mlx/mlx/backend/cpu/indexing.cpp +854 -0
  288. data/mlx/mlx/backend/cpu/inverse.cpp +160 -0
  289. data/mlx/mlx/backend/cpu/jit_compiler.cpp +166 -0
  290. data/mlx/mlx/backend/cpu/jit_compiler.h +20 -0
  291. data/mlx/mlx/backend/cpu/lapack.h +80 -0
  292. data/mlx/mlx/backend/cpu/logsumexp.cpp +139 -0
  293. data/mlx/mlx/backend/cpu/luf.cpp +120 -0
  294. data/mlx/mlx/backend/cpu/make_compiled_preamble.ps1 +38 -0
  295. data/mlx/mlx/backend/cpu/make_compiled_preamble.sh +41 -0
  296. data/mlx/mlx/backend/cpu/masked_mm.cpp +608 -0
  297. data/mlx/mlx/backend/cpu/matmul.cpp +166 -0
  298. data/mlx/mlx/backend/cpu/primitives.cpp +478 -0
  299. data/mlx/mlx/backend/cpu/qrf.cpp +147 -0
  300. data/mlx/mlx/backend/cpu/quantized.cpp +1370 -0
  301. data/mlx/mlx/backend/cpu/reduce.cpp +587 -0
  302. data/mlx/mlx/backend/cpu/scan.cpp +338 -0
  303. data/mlx/mlx/backend/cpu/select.cpp +95 -0
  304. data/mlx/mlx/backend/cpu/simd/accelerate_fp16_simd.h +56 -0
  305. data/mlx/mlx/backend/cpu/simd/accelerate_simd.h +329 -0
  306. data/mlx/mlx/backend/cpu/simd/base_simd.h +319 -0
  307. data/mlx/mlx/backend/cpu/simd/math.h +193 -0
  308. data/mlx/mlx/backend/cpu/simd/neon_fp16_simd.h +212 -0
  309. data/mlx/mlx/backend/cpu/simd/simd.h +4 -0
  310. data/mlx/mlx/backend/cpu/simd/type.h +11 -0
  311. data/mlx/mlx/backend/cpu/slicing.h +21 -0
  312. data/mlx/mlx/backend/cpu/softmax.cpp +170 -0
  313. data/mlx/mlx/backend/cpu/sort.cpp +481 -0
  314. data/mlx/mlx/backend/cpu/svd.cpp +289 -0
  315. data/mlx/mlx/backend/cpu/ternary.h +154 -0
  316. data/mlx/mlx/backend/cpu/threefry.cpp +31 -0
  317. data/mlx/mlx/backend/cpu/threefry.h +21 -0
  318. data/mlx/mlx/backend/cpu/unary.cpp +238 -0
  319. data/mlx/mlx/backend/cpu/unary.h +281 -0
  320. data/mlx/mlx/backend/cpu/unary_ops.h +175 -0
  321. data/mlx/mlx/backend/cuda/CMakeLists.txt +265 -0
  322. data/mlx/mlx/backend/cuda/allocator.cpp +451 -0
  323. data/mlx/mlx/backend/cuda/allocator.h +94 -0
  324. data/mlx/mlx/backend/cuda/arange.cu +68 -0
  325. data/mlx/mlx/backend/cuda/arg_reduce.cu +189 -0
  326. data/mlx/mlx/backend/cuda/bin2h.cmake +150 -0
  327. data/mlx/mlx/backend/cuda/binary/CMakeLists.txt +21 -0
  328. data/mlx/mlx/backend/cuda/binary/add.cu +7 -0
  329. data/mlx/mlx/backend/cuda/binary/arctan2.cu +7 -0
  330. data/mlx/mlx/backend/cuda/binary/binary.cuh +383 -0
  331. data/mlx/mlx/backend/cuda/binary/bitwise_binary.cu +27 -0
  332. data/mlx/mlx/backend/cuda/binary/divide.cu +7 -0
  333. data/mlx/mlx/backend/cuda/binary/equal.cu +15 -0
  334. data/mlx/mlx/backend/cuda/binary/greater.cu +7 -0
  335. data/mlx/mlx/backend/cuda/binary/greater_equal.cu +7 -0
  336. data/mlx/mlx/backend/cuda/binary/less.cu +7 -0
  337. data/mlx/mlx/backend/cuda/binary/less_equal.cu +7 -0
  338. data/mlx/mlx/backend/cuda/binary/log_add_exp.cu +7 -0
  339. data/mlx/mlx/backend/cuda/binary/logical_and.cu +7 -0
  340. data/mlx/mlx/backend/cuda/binary/logical_or.cu +7 -0
  341. data/mlx/mlx/backend/cuda/binary/maximum.cu +7 -0
  342. data/mlx/mlx/backend/cuda/binary/minimum.cu +7 -0
  343. data/mlx/mlx/backend/cuda/binary/multiply.cu +7 -0
  344. data/mlx/mlx/backend/cuda/binary/not_equal.cu +7 -0
  345. data/mlx/mlx/backend/cuda/binary/power.cu +7 -0
  346. data/mlx/mlx/backend/cuda/binary/remainder.cu +7 -0
  347. data/mlx/mlx/backend/cuda/binary/subtract.cu +7 -0
  348. data/mlx/mlx/backend/cuda/binary_two.cu +412 -0
  349. data/mlx/mlx/backend/cuda/compiled.cpp +357 -0
  350. data/mlx/mlx/backend/cuda/conv/conv.h +126 -0
  351. data/mlx/mlx/backend/cuda/conv/gemm_conv.cu +217 -0
  352. data/mlx/mlx/backend/cuda/conv/gemm_grouped_conv.cu +231 -0
  353. data/mlx/mlx/backend/cuda/conv.cpp +403 -0
  354. data/mlx/mlx/backend/cuda/copy/copy.cuh +55 -0
  355. data/mlx/mlx/backend/cuda/copy/copy_contiguous.cu +88 -0
  356. data/mlx/mlx/backend/cuda/copy/copy_general.cu +171 -0
  357. data/mlx/mlx/backend/cuda/copy/copy_general_dynamic.cu +118 -0
  358. data/mlx/mlx/backend/cuda/copy/copy_general_input.cu +229 -0
  359. data/mlx/mlx/backend/cuda/copy.cu +132 -0
  360. data/mlx/mlx/backend/cuda/cublas_utils.cpp +222 -0
  361. data/mlx/mlx/backend/cuda/cublas_utils.h +95 -0
  362. data/mlx/mlx/backend/cuda/cuda.h +21 -0
  363. data/mlx/mlx/backend/cuda/cuda_utils.h +90 -0
  364. data/mlx/mlx/backend/cuda/cudnn_utils.cpp +133 -0
  365. data/mlx/mlx/backend/cuda/cudnn_utils.h +187 -0
  366. data/mlx/mlx/backend/cuda/custom_kernel.cpp +379 -0
  367. data/mlx/mlx/backend/cuda/cutlass_utils.cuh +46 -0
  368. data/mlx/mlx/backend/cuda/delayload.cpp +80 -0
  369. data/mlx/mlx/backend/cuda/device/atomic_ops.cuh +63 -0
  370. data/mlx/mlx/backend/cuda/device/binary_ops.cuh +300 -0
  371. data/mlx/mlx/backend/cuda/device/cast_op.cuh +118 -0
  372. data/mlx/mlx/backend/cuda/device/complex.cuh +60 -0
  373. data/mlx/mlx/backend/cuda/device/config.h +12 -0
  374. data/mlx/mlx/backend/cuda/device/fp16_math.cuh +96 -0
  375. data/mlx/mlx/backend/cuda/device/gather.cuh +53 -0
  376. data/mlx/mlx/backend/cuda/device/gather_axis.cuh +65 -0
  377. data/mlx/mlx/backend/cuda/device/indexing.cuh +30 -0
  378. data/mlx/mlx/backend/cuda/device/scatter.cuh +68 -0
  379. data/mlx/mlx/backend/cuda/device/scatter_axis.cuh +67 -0
  380. data/mlx/mlx/backend/cuda/device/scatter_ops.cuh +44 -0
  381. data/mlx/mlx/backend/cuda/device/ternary_ops.cuh +13 -0
  382. data/mlx/mlx/backend/cuda/device/unary_ops.cuh +350 -0
  383. data/mlx/mlx/backend/cuda/device/utils.cuh +464 -0
  384. data/mlx/mlx/backend/cuda/device.cpp +522 -0
  385. data/mlx/mlx/backend/cuda/device.h +195 -0
  386. data/mlx/mlx/backend/cuda/device_info.cpp +232 -0
  387. data/mlx/mlx/backend/cuda/distributed.cu +121 -0
  388. data/mlx/mlx/backend/cuda/eval.cpp +66 -0
  389. data/mlx/mlx/backend/cuda/event.cu +415 -0
  390. data/mlx/mlx/backend/cuda/event.h +79 -0
  391. data/mlx/mlx/backend/cuda/fence.cpp +42 -0
  392. data/mlx/mlx/backend/cuda/gemms/cublas_gemm.cpp +233 -0
  393. data/mlx/mlx/backend/cuda/gemms/cublas_gemm.h +114 -0
  394. data/mlx/mlx/backend/cuda/gemms/cublas_gemm_batched_12_0.cpp +77 -0
  395. data/mlx/mlx/backend/cuda/gemms/cublas_gemm_batched_12_9.cu +329 -0
  396. data/mlx/mlx/backend/cuda/gemms/gemv.cu +327 -0
  397. data/mlx/mlx/backend/cuda/gemms/gemv.h +34 -0
  398. data/mlx/mlx/backend/cuda/gemms/grouped_gemm.h +25 -0
  399. data/mlx/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu +358 -0
  400. data/mlx/mlx/backend/cuda/indexing.cpp +434 -0
  401. data/mlx/mlx/backend/cuda/jit_module.cpp +443 -0
  402. data/mlx/mlx/backend/cuda/jit_module.h +120 -0
  403. data/mlx/mlx/backend/cuda/kernel_utils.cu +52 -0
  404. data/mlx/mlx/backend/cuda/kernel_utils.cuh +148 -0
  405. data/mlx/mlx/backend/cuda/layer_norm.cu +417 -0
  406. data/mlx/mlx/backend/cuda/load.cpp +60 -0
  407. data/mlx/mlx/backend/cuda/logsumexp.cu +161 -0
  408. data/mlx/mlx/backend/cuda/lru_cache.h +190 -0
  409. data/mlx/mlx/backend/cuda/matmul.cpp +373 -0
  410. data/mlx/mlx/backend/cuda/no_cuda.cpp +47 -0
  411. data/mlx/mlx/backend/cuda/primitives.cpp +46 -0
  412. data/mlx/mlx/backend/cuda/quantized/affine_quantize.cu +329 -0
  413. data/mlx/mlx/backend/cuda/quantized/convert_fp8.cu +19 -0
  414. data/mlx/mlx/backend/cuda/quantized/cublas_qqmm.cpp +206 -0
  415. data/mlx/mlx/backend/cuda/quantized/cublas_qqmm.h +88 -0
  416. data/mlx/mlx/backend/cuda/quantized/cuda_fp4.h +100 -0
  417. data/mlx/mlx/backend/cuda/quantized/fp_quantize.cu +496 -0
  418. data/mlx/mlx/backend/cuda/quantized/mxfp8_quantize.cuh +32 -0
  419. data/mlx/mlx/backend/cuda/quantized/no_qqmm_impl.cpp +26 -0
  420. data/mlx/mlx/backend/cuda/quantized/nvfp4_quantize.cuh +334 -0
  421. data/mlx/mlx/backend/cuda/quantized/qmv.cu +304 -0
  422. data/mlx/mlx/backend/cuda/quantized/qmv.h +21 -0
  423. data/mlx/mlx/backend/cuda/quantized/qqmm.cpp +158 -0
  424. data/mlx/mlx/backend/cuda/quantized/qqmm_impl.cpp +50 -0
  425. data/mlx/mlx/backend/cuda/quantized/qqmm_impl.h +26 -0
  426. data/mlx/mlx/backend/cuda/quantized/qqmm_utils.cu +227 -0
  427. data/mlx/mlx/backend/cuda/quantized/qqmm_utils.h +30 -0
  428. data/mlx/mlx/backend/cuda/quantized/quantized.cpp +85 -0
  429. data/mlx/mlx/backend/cuda/quantized/quantized.h +53 -0
  430. data/mlx/mlx/backend/cuda/quantized/quantized_utils.cuh +88 -0
  431. data/mlx/mlx/backend/cuda/quantized/quantized_utils.h +50 -0
  432. data/mlx/mlx/backend/cuda/random.cu +202 -0
  433. data/mlx/mlx/backend/cuda/reduce/all_reduce.cu +159 -0
  434. data/mlx/mlx/backend/cuda/reduce/col_reduce.cu +510 -0
  435. data/mlx/mlx/backend/cuda/reduce/init_reduce.cu +50 -0
  436. data/mlx/mlx/backend/cuda/reduce/reduce.cuh +71 -0
  437. data/mlx/mlx/backend/cuda/reduce/reduce_ops.cuh +211 -0
  438. data/mlx/mlx/backend/cuda/reduce/reduce_utils.cuh +145 -0
  439. data/mlx/mlx/backend/cuda/reduce/row_reduce.cu +361 -0
  440. data/mlx/mlx/backend/cuda/reduce.cu +73 -0
  441. data/mlx/mlx/backend/cuda/rms_norm.cu +536 -0
  442. data/mlx/mlx/backend/cuda/rope.cu +429 -0
  443. data/mlx/mlx/backend/cuda/scaled_dot_product_attention.cpp +681 -0
  444. data/mlx/mlx/backend/cuda/scaled_dot_product_attention.cu +796 -0
  445. data/mlx/mlx/backend/cuda/scan.cu +468 -0
  446. data/mlx/mlx/backend/cuda/slicing.cpp +111 -0
  447. data/mlx/mlx/backend/cuda/softmax.cu +162 -0
  448. data/mlx/mlx/backend/cuda/sort.cu +1076 -0
  449. data/mlx/mlx/backend/cuda/steel/defines.cuh +9 -0
  450. data/mlx/mlx/backend/cuda/steel/gemm.cuh +101 -0
  451. data/mlx/mlx/backend/cuda/steel/mma.cuh +117 -0
  452. data/mlx/mlx/backend/cuda/steel/tiles.cuh +450 -0
  453. data/mlx/mlx/backend/cuda/steel/utils.cuh +89 -0
  454. data/mlx/mlx/backend/cuda/ternary.cu +271 -0
  455. data/mlx/mlx/backend/cuda/unary/CMakeLists.txt +34 -0
  456. data/mlx/mlx/backend/cuda/unary/abs.cu +7 -0
  457. data/mlx/mlx/backend/cuda/unary/arccos.cu +7 -0
  458. data/mlx/mlx/backend/cuda/unary/arccosh.cu +7 -0
  459. data/mlx/mlx/backend/cuda/unary/arcsin.cu +7 -0
  460. data/mlx/mlx/backend/cuda/unary/arcsinh.cu +7 -0
  461. data/mlx/mlx/backend/cuda/unary/arctan.cu +7 -0
  462. data/mlx/mlx/backend/cuda/unary/arctanh.cu +7 -0
  463. data/mlx/mlx/backend/cuda/unary/bitwise_invert.cu +7 -0
  464. data/mlx/mlx/backend/cuda/unary/ceil.cu +7 -0
  465. data/mlx/mlx/backend/cuda/unary/conjugate.cu +7 -0
  466. data/mlx/mlx/backend/cuda/unary/cos.cu +7 -0
  467. data/mlx/mlx/backend/cuda/unary/cosh.cu +7 -0
  468. data/mlx/mlx/backend/cuda/unary/erf.cu +7 -0
  469. data/mlx/mlx/backend/cuda/unary/erf_inv.cu +7 -0
  470. data/mlx/mlx/backend/cuda/unary/exp.cu +7 -0
  471. data/mlx/mlx/backend/cuda/unary/expm1.cu +7 -0
  472. data/mlx/mlx/backend/cuda/unary/floor.cu +7 -0
  473. data/mlx/mlx/backend/cuda/unary/imag.cu +7 -0
  474. data/mlx/mlx/backend/cuda/unary/log.cu +21 -0
  475. data/mlx/mlx/backend/cuda/unary/log1p.cu +7 -0
  476. data/mlx/mlx/backend/cuda/unary/logical_not.cu +7 -0
  477. data/mlx/mlx/backend/cuda/unary/negative.cu +7 -0
  478. data/mlx/mlx/backend/cuda/unary/real.cu +7 -0
  479. data/mlx/mlx/backend/cuda/unary/round.cu +18 -0
  480. data/mlx/mlx/backend/cuda/unary/sigmoid.cu +7 -0
  481. data/mlx/mlx/backend/cuda/unary/sign.cu +7 -0
  482. data/mlx/mlx/backend/cuda/unary/sin.cu +7 -0
  483. data/mlx/mlx/backend/cuda/unary/sinh.cu +7 -0
  484. data/mlx/mlx/backend/cuda/unary/sqrt.cu +15 -0
  485. data/mlx/mlx/backend/cuda/unary/square.cu +7 -0
  486. data/mlx/mlx/backend/cuda/unary/tan.cu +7 -0
  487. data/mlx/mlx/backend/cuda/unary/tanh.cu +7 -0
  488. data/mlx/mlx/backend/cuda/unary/unary.cuh +224 -0
  489. data/mlx/mlx/backend/cuda/utils.cpp +116 -0
  490. data/mlx/mlx/backend/cuda/utils.h +49 -0
  491. data/mlx/mlx/backend/cuda/vector_types.cuh +48 -0
  492. data/mlx/mlx/backend/cuda/worker.cpp +79 -0
  493. data/mlx/mlx/backend/cuda/worker.h +55 -0
  494. data/mlx/mlx/backend/gpu/CMakeLists.txt +5 -0
  495. data/mlx/mlx/backend/gpu/copy.cpp +89 -0
  496. data/mlx/mlx/backend/gpu/copy.h +57 -0
  497. data/mlx/mlx/backend/gpu/device_info.h +36 -0
  498. data/mlx/mlx/backend/gpu/eval.h +18 -0
  499. data/mlx/mlx/backend/gpu/primitives.cpp +307 -0
  500. data/mlx/mlx/backend/gpu/slicing.cpp +44 -0
  501. data/mlx/mlx/backend/gpu/slicing.h +36 -0
  502. data/mlx/mlx/backend/metal/CMakeLists.txt +144 -0
  503. data/mlx/mlx/backend/metal/allocator.cpp +279 -0
  504. data/mlx/mlx/backend/metal/allocator.h +79 -0
  505. data/mlx/mlx/backend/metal/binary.cpp +257 -0
  506. data/mlx/mlx/backend/metal/binary.h +33 -0
  507. data/mlx/mlx/backend/metal/compiled.cpp +471 -0
  508. data/mlx/mlx/backend/metal/conv.cpp +1118 -0
  509. data/mlx/mlx/backend/metal/copy.cpp +235 -0
  510. data/mlx/mlx/backend/metal/custom_kernel.cpp +430 -0
  511. data/mlx/mlx/backend/metal/device.cpp +816 -0
  512. data/mlx/mlx/backend/metal/device.h +289 -0
  513. data/mlx/mlx/backend/metal/device_info.cpp +58 -0
  514. data/mlx/mlx/backend/metal/distributed.cpp +38 -0
  515. data/mlx/mlx/backend/metal/eval.cpp +97 -0
  516. data/mlx/mlx/backend/metal/event.cpp +62 -0
  517. data/mlx/mlx/backend/metal/fence.cpp +162 -0
  518. data/mlx/mlx/backend/metal/fft.cpp +807 -0
  519. data/mlx/mlx/backend/metal/hadamard.cpp +198 -0
  520. data/mlx/mlx/backend/metal/indexing.cpp +727 -0
  521. data/mlx/mlx/backend/metal/jit/includes.h +58 -0
  522. data/mlx/mlx/backend/metal/jit/indexing.h +76 -0
  523. data/mlx/mlx/backend/metal/jit_kernels.cpp +1118 -0
  524. data/mlx/mlx/backend/metal/kernels/CMakeLists.txt +193 -0
  525. data/mlx/mlx/backend/metal/kernels/arange.h +9 -0
  526. data/mlx/mlx/backend/metal/kernels/arange.metal +20 -0
  527. data/mlx/mlx/backend/metal/kernels/arg_reduce.metal +182 -0
  528. data/mlx/mlx/backend/metal/kernels/atomic.h +345 -0
  529. data/mlx/mlx/backend/metal/kernels/bf16.h +16 -0
  530. data/mlx/mlx/backend/metal/kernels/bf16_math.h +380 -0
  531. data/mlx/mlx/backend/metal/kernels/binary.h +199 -0
  532. data/mlx/mlx/backend/metal/kernels/binary.metal +109 -0
  533. data/mlx/mlx/backend/metal/kernels/binary_ops.h +330 -0
  534. data/mlx/mlx/backend/metal/kernels/binary_two.h +244 -0
  535. data/mlx/mlx/backend/metal/kernels/binary_two.metal +54 -0
  536. data/mlx/mlx/backend/metal/kernels/cexpf.h +134 -0
  537. data/mlx/mlx/backend/metal/kernels/complex.h +173 -0
  538. data/mlx/mlx/backend/metal/kernels/conv.metal +701 -0
  539. data/mlx/mlx/backend/metal/kernels/copy.h +276 -0
  540. data/mlx/mlx/backend/metal/kernels/copy.metal +75 -0
  541. data/mlx/mlx/backend/metal/kernels/defines.h +24 -0
  542. data/mlx/mlx/backend/metal/kernels/erf.h +69 -0
  543. data/mlx/mlx/backend/metal/kernels/expm1f.h +90 -0
  544. data/mlx/mlx/backend/metal/kernels/fence.metal +52 -0
  545. data/mlx/mlx/backend/metal/kernels/fft/radix.h +328 -0
  546. data/mlx/mlx/backend/metal/kernels/fft/readwrite.h +624 -0
  547. data/mlx/mlx/backend/metal/kernels/fft.h +486 -0
  548. data/mlx/mlx/backend/metal/kernels/fft.metal +67 -0
  549. data/mlx/mlx/backend/metal/kernels/fp4.h +48 -0
  550. data/mlx/mlx/backend/metal/kernels/fp8.h +80 -0
  551. data/mlx/mlx/backend/metal/kernels/fp_quantized.h +1850 -0
  552. data/mlx/mlx/backend/metal/kernels/fp_quantized.metal +153 -0
  553. data/mlx/mlx/backend/metal/kernels/fp_quantized_nax.h +1044 -0
  554. data/mlx/mlx/backend/metal/kernels/fp_quantized_nax.metal +79 -0
  555. data/mlx/mlx/backend/metal/kernels/gemv.metal +868 -0
  556. data/mlx/mlx/backend/metal/kernels/gemv_masked.h +827 -0
  557. data/mlx/mlx/backend/metal/kernels/gemv_masked.metal +76 -0
  558. data/mlx/mlx/backend/metal/kernels/hadamard.h +182 -0
  559. data/mlx/mlx/backend/metal/kernels/indexing/gather.h +51 -0
  560. data/mlx/mlx/backend/metal/kernels/indexing/gather_axis.h +44 -0
  561. data/mlx/mlx/backend/metal/kernels/indexing/gather_front.h +24 -0
  562. data/mlx/mlx/backend/metal/kernels/indexing/indexing.h +23 -0
  563. data/mlx/mlx/backend/metal/kernels/indexing/masked_scatter.h +41 -0
  564. data/mlx/mlx/backend/metal/kernels/indexing/scatter.h +59 -0
  565. data/mlx/mlx/backend/metal/kernels/indexing/scatter_axis.h +52 -0
  566. data/mlx/mlx/backend/metal/kernels/layer_norm.metal +433 -0
  567. data/mlx/mlx/backend/metal/kernels/logging.h +26 -0
  568. data/mlx/mlx/backend/metal/kernels/logsumexp.h +140 -0
  569. data/mlx/mlx/backend/metal/kernels/logsumexp.metal +18 -0
  570. data/mlx/mlx/backend/metal/kernels/quantized.h +2508 -0
  571. data/mlx/mlx/backend/metal/kernels/quantized.metal +144 -0
  572. data/mlx/mlx/backend/metal/kernels/quantized_nax.h +1705 -0
  573. data/mlx/mlx/backend/metal/kernels/quantized_nax.metal +106 -0
  574. data/mlx/mlx/backend/metal/kernels/quantized_utils.h +90 -0
  575. data/mlx/mlx/backend/metal/kernels/random.metal +103 -0
  576. data/mlx/mlx/backend/metal/kernels/reduce.h +5 -0
  577. data/mlx/mlx/backend/metal/kernels/reduce.metal +169 -0
  578. data/mlx/mlx/backend/metal/kernels/reduce_utils.h +6 -0
  579. data/mlx/mlx/backend/metal/kernels/reduction/ops.h +275 -0
  580. data/mlx/mlx/backend/metal/kernels/reduction/reduce_all.h +66 -0
  581. data/mlx/mlx/backend/metal/kernels/reduction/reduce_col.h +398 -0
  582. data/mlx/mlx/backend/metal/kernels/reduction/reduce_init.h +8 -0
  583. data/mlx/mlx/backend/metal/kernels/reduction/reduce_row.h +369 -0
  584. data/mlx/mlx/backend/metal/kernels/rms_norm.metal +391 -0
  585. data/mlx/mlx/backend/metal/kernels/rope.metal +229 -0
  586. data/mlx/mlx/backend/metal/kernels/scaled_dot_product_attention.metal +44 -0
  587. data/mlx/mlx/backend/metal/kernels/scan.h +514 -0
  588. data/mlx/mlx/backend/metal/kernels/scan.metal +109 -0
  589. data/mlx/mlx/backend/metal/kernels/sdpa_vector.h +394 -0
  590. data/mlx/mlx/backend/metal/kernels/softmax.h +190 -0
  591. data/mlx/mlx/backend/metal/kernels/softmax.metal +24 -0
  592. data/mlx/mlx/backend/metal/kernels/sort.h +719 -0
  593. data/mlx/mlx/backend/metal/kernels/sort.metal +80 -0
  594. data/mlx/mlx/backend/metal/kernels/steel/attn/attn.h +296 -0
  595. data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.h +471 -0
  596. data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.metal +27 -0
  597. data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.h +481 -0
  598. data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.metal +28 -0
  599. data/mlx/mlx/backend/metal/kernels/steel/attn/loader.h +264 -0
  600. data/mlx/mlx/backend/metal/kernels/steel/attn/mma.h +750 -0
  601. data/mlx/mlx/backend/metal/kernels/steel/attn/nax.h +1076 -0
  602. data/mlx/mlx/backend/metal/kernels/steel/attn/params.h +44 -0
  603. data/mlx/mlx/backend/metal/kernels/steel/attn/transforms.h +71 -0
  604. data/mlx/mlx/backend/metal/kernels/steel/conv/conv.h +13 -0
  605. data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.h +176 -0
  606. data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.metal +56 -0
  607. data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.h +225 -0
  608. data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.metal +47 -0
  609. data/mlx/mlx/backend/metal/kernels/steel/conv/loader.h +6 -0
  610. data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_l.h +451 -0
  611. data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_n.h +319 -0
  612. data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_general.h +381 -0
  613. data/mlx/mlx/backend/metal/kernels/steel/conv/params.h +62 -0
  614. data/mlx/mlx/backend/metal/kernels/steel/defines.h +7 -0
  615. data/mlx/mlx/backend/metal/kernels/steel/gemm/gemm.h +295 -0
  616. data/mlx/mlx/backend/metal/kernels/steel/gemm/gemm_nax.h +157 -0
  617. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.h +346 -0
  618. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.metal +34 -0
  619. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.h +219 -0
  620. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.metal +30 -0
  621. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.h +459 -0
  622. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.metal +59 -0
  623. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.h +143 -0
  624. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.metal +37 -0
  625. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.h +719 -0
  626. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.metal +76 -0
  627. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.h +266 -0
  628. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.metal +43 -0
  629. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.h +227 -0
  630. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.metal +76 -0
  631. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk_nax.h +152 -0
  632. data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk_nax.metal +30 -0
  633. data/mlx/mlx/backend/metal/kernels/steel/gemm/loader.h +137 -0
  634. data/mlx/mlx/backend/metal/kernels/steel/gemm/mma.h +1146 -0
  635. data/mlx/mlx/backend/metal/kernels/steel/gemm/nax.h +1084 -0
  636. data/mlx/mlx/backend/metal/kernels/steel/gemm/params.h +65 -0
  637. data/mlx/mlx/backend/metal/kernels/steel/gemm/transforms.h +72 -0
  638. data/mlx/mlx/backend/metal/kernels/steel/utils/integral_constant.h +134 -0
  639. data/mlx/mlx/backend/metal/kernels/steel/utils/type_traits.h +55 -0
  640. data/mlx/mlx/backend/metal/kernels/steel/utils.h +42 -0
  641. data/mlx/mlx/backend/metal/kernels/ternary.h +145 -0
  642. data/mlx/mlx/backend/metal/kernels/ternary.metal +48 -0
  643. data/mlx/mlx/backend/metal/kernels/ternary_ops.h +10 -0
  644. data/mlx/mlx/backend/metal/kernels/unary.h +63 -0
  645. data/mlx/mlx/backend/metal/kernels/unary.metal +115 -0
  646. data/mlx/mlx/backend/metal/kernels/unary_ops.h +454 -0
  647. data/mlx/mlx/backend/metal/kernels/utils.h +445 -0
  648. data/mlx/mlx/backend/metal/kernels.h +375 -0
  649. data/mlx/mlx/backend/metal/logsumexp.cpp +95 -0
  650. data/mlx/mlx/backend/metal/make_compiled_preamble.sh +120 -0
  651. data/mlx/mlx/backend/metal/matmul.cpp +2572 -0
  652. data/mlx/mlx/backend/metal/matmul.h +144 -0
  653. data/mlx/mlx/backend/metal/metal.cpp +50 -0
  654. data/mlx/mlx/backend/metal/metal.h +25 -0
  655. data/mlx/mlx/backend/metal/no_metal.cpp +42 -0
  656. data/mlx/mlx/backend/metal/nojit_kernels.cpp +414 -0
  657. data/mlx/mlx/backend/metal/normalization.cpp +433 -0
  658. data/mlx/mlx/backend/metal/primitives.cpp +242 -0
  659. data/mlx/mlx/backend/metal/quantized.cpp +1651 -0
  660. data/mlx/mlx/backend/metal/reduce.cpp +1038 -0
  661. data/mlx/mlx/backend/metal/reduce.h +41 -0
  662. data/mlx/mlx/backend/metal/resident.cpp +100 -0
  663. data/mlx/mlx/backend/metal/resident.h +32 -0
  664. data/mlx/mlx/backend/metal/rope.cpp +165 -0
  665. data/mlx/mlx/backend/metal/scaled_dot_product_attention.cpp +798 -0
  666. data/mlx/mlx/backend/metal/scan.cpp +145 -0
  667. data/mlx/mlx/backend/metal/scan.h +17 -0
  668. data/mlx/mlx/backend/metal/slicing.cpp +99 -0
  669. data/mlx/mlx/backend/metal/softmax.cpp +87 -0
  670. data/mlx/mlx/backend/metal/sort.cpp +368 -0
  671. data/mlx/mlx/backend/metal/ternary.cpp +160 -0
  672. data/mlx/mlx/backend/metal/ternary.h +21 -0
  673. data/mlx/mlx/backend/metal/unary.cpp +161 -0
  674. data/mlx/mlx/backend/metal/unary.h +21 -0
  675. data/mlx/mlx/backend/metal/utils.cpp +77 -0
  676. data/mlx/mlx/backend/metal/utils.h +99 -0
  677. data/mlx/mlx/backend/no_cpu/CMakeLists.txt +7 -0
  678. data/mlx/mlx/backend/no_cpu/compiled.cpp +24 -0
  679. data/mlx/mlx/backend/no_cpu/device_info.cpp +22 -0
  680. data/mlx/mlx/backend/no_cpu/primitives.cpp +146 -0
  681. data/mlx/mlx/backend/no_gpu/CMakeLists.txt +8 -0
  682. data/mlx/mlx/backend/no_gpu/allocator.cpp +134 -0
  683. data/mlx/mlx/backend/no_gpu/apple_memory.h +16 -0
  684. data/mlx/mlx/backend/no_gpu/device_info.cpp +22 -0
  685. data/mlx/mlx/backend/no_gpu/eval.cpp +24 -0
  686. data/mlx/mlx/backend/no_gpu/event.cpp +53 -0
  687. data/mlx/mlx/backend/no_gpu/fence.cpp +54 -0
  688. data/mlx/mlx/backend/no_gpu/linux_memory.h +22 -0
  689. data/mlx/mlx/backend/no_gpu/primitives.cpp +185 -0
  690. data/mlx/mlx/compile.cpp +1243 -0
  691. data/mlx/mlx/compile.h +45 -0
  692. data/mlx/mlx/compile_impl.h +70 -0
  693. data/mlx/mlx/device.cpp +72 -0
  694. data/mlx/mlx/device.h +56 -0
  695. data/mlx/mlx/distributed/CMakeLists.txt +14 -0
  696. data/mlx/mlx/distributed/distributed.cpp +197 -0
  697. data/mlx/mlx/distributed/distributed.h +61 -0
  698. data/mlx/mlx/distributed/distributed_impl.h +59 -0
  699. data/mlx/mlx/distributed/jaccl/CMakeLists.txt +12 -0
  700. data/mlx/mlx/distributed/jaccl/jaccl.cpp +178 -0
  701. data/mlx/mlx/distributed/jaccl/jaccl.h +12 -0
  702. data/mlx/mlx/distributed/jaccl/mesh.cpp +451 -0
  703. data/mlx/mlx/distributed/jaccl/mesh.h +122 -0
  704. data/mlx/mlx/distributed/jaccl/no_jaccl.cpp +20 -0
  705. data/mlx/mlx/distributed/jaccl/ring.cpp +692 -0
  706. data/mlx/mlx/distributed/jaccl/ring.h +178 -0
  707. data/mlx/mlx/distributed/jaccl/utils.cpp +329 -0
  708. data/mlx/mlx/distributed/jaccl/utils.h +342 -0
  709. data/mlx/mlx/distributed/mpi/CMakeLists.txt +5 -0
  710. data/mlx/mlx/distributed/mpi/mpi.cpp +501 -0
  711. data/mlx/mlx/distributed/mpi/mpi.h +12 -0
  712. data/mlx/mlx/distributed/mpi/mpi_declarations.h +28 -0
  713. data/mlx/mlx/distributed/mpi/no_mpi.cpp +20 -0
  714. data/mlx/mlx/distributed/nccl/CMakeLists.txt +26 -0
  715. data/mlx/mlx/distributed/nccl/nccl.cpp +443 -0
  716. data/mlx/mlx/distributed/nccl/nccl.h +12 -0
  717. data/mlx/mlx/distributed/nccl/nccl_stub/CMakeLists.txt +1 -0
  718. data/mlx/mlx/distributed/nccl/nccl_stub/nccl_stubs.cpp +54 -0
  719. data/mlx/mlx/distributed/nccl/no_nccl.cpp +20 -0
  720. data/mlx/mlx/distributed/ops.cpp +186 -0
  721. data/mlx/mlx/distributed/ops.h +57 -0
  722. data/mlx/mlx/distributed/primitives.cpp +95 -0
  723. data/mlx/mlx/distributed/primitives.h +156 -0
  724. data/mlx/mlx/distributed/reduction_ops.h +38 -0
  725. data/mlx/mlx/distributed/ring/CMakeLists.txt +5 -0
  726. data/mlx/mlx/distributed/ring/no_ring.cpp +20 -0
  727. data/mlx/mlx/distributed/ring/ring.cpp +870 -0
  728. data/mlx/mlx/distributed/ring/ring.h +12 -0
  729. data/mlx/mlx/distributed/utils.cpp +206 -0
  730. data/mlx/mlx/distributed/utils.h +67 -0
  731. data/mlx/mlx/dtype.cpp +197 -0
  732. data/mlx/mlx/dtype.h +116 -0
  733. data/mlx/mlx/dtype_utils.cpp +42 -0
  734. data/mlx/mlx/dtype_utils.h +119 -0
  735. data/mlx/mlx/einsum.cpp +941 -0
  736. data/mlx/mlx/einsum.h +23 -0
  737. data/mlx/mlx/event.h +58 -0
  738. data/mlx/mlx/export.cpp +1130 -0
  739. data/mlx/mlx/export.h +137 -0
  740. data/mlx/mlx/export_impl.h +99 -0
  741. data/mlx/mlx/fast.cpp +941 -0
  742. data/mlx/mlx/fast.h +103 -0
  743. data/mlx/mlx/fast_primitives.h +427 -0
  744. data/mlx/mlx/fence.h +39 -0
  745. data/mlx/mlx/fft.cpp +262 -0
  746. data/mlx/mlx/fft.h +159 -0
  747. data/mlx/mlx/graph_utils.cpp +175 -0
  748. data/mlx/mlx/graph_utils.h +67 -0
  749. data/mlx/mlx/io/CMakeLists.txt +25 -0
  750. data/mlx/mlx/io/gguf.cpp +470 -0
  751. data/mlx/mlx/io/gguf.h +20 -0
  752. data/mlx/mlx/io/gguf_quants.cpp +164 -0
  753. data/mlx/mlx/io/load.cpp +397 -0
  754. data/mlx/mlx/io/load.h +175 -0
  755. data/mlx/mlx/io/no_gguf.cpp +20 -0
  756. data/mlx/mlx/io/no_safetensors.cpp +37 -0
  757. data/mlx/mlx/io/safetensors.cpp +234 -0
  758. data/mlx/mlx/io.h +61 -0
  759. data/mlx/mlx/linalg.cpp +708 -0
  760. data/mlx/mlx/linalg.h +115 -0
  761. data/mlx/mlx/memory.h +80 -0
  762. data/mlx/mlx/mlx.h +25 -0
  763. data/mlx/mlx/ops.cpp +6094 -0
  764. data/mlx/mlx/ops.h +1610 -0
  765. data/mlx/mlx/primitives.cpp +5850 -0
  766. data/mlx/mlx/primitives.h +2525 -0
  767. data/mlx/mlx/random.cpp +492 -0
  768. data/mlx/mlx/random.h +283 -0
  769. data/mlx/mlx/scheduler.cpp +73 -0
  770. data/mlx/mlx/scheduler.h +189 -0
  771. data/mlx/mlx/small_vector.h +540 -0
  772. data/mlx/mlx/stream.h +42 -0
  773. data/mlx/mlx/threadpool.h +133 -0
  774. data/mlx/mlx/transforms.cpp +1065 -0
  775. data/mlx/mlx/transforms.h +231 -0
  776. data/mlx/mlx/transforms_impl.h +88 -0
  777. data/mlx/mlx/types/bf16.h +187 -0
  778. data/mlx/mlx/types/complex.h +113 -0
  779. data/mlx/mlx/types/fp16.h +234 -0
  780. data/mlx/mlx/types/half_types.h +58 -0
  781. data/mlx/mlx/types/limits.h +70 -0
  782. data/mlx/mlx/utils.cpp +302 -0
  783. data/mlx/mlx/utils.h +174 -0
  784. data/mlx/mlx/version.cpp +11 -0
  785. data/mlx/mlx/version.h +22 -0
  786. data/mlx/mlx.pc.in +52 -0
  787. data/mlx/pyproject.toml +7 -0
  788. data/mlx/python/mlx/__main__.py +27 -0
  789. data/mlx/python/mlx/_distributed_utils/common.py +135 -0
  790. data/mlx/python/mlx/_distributed_utils/config.py +631 -0
  791. data/mlx/python/mlx/_distributed_utils/launch.py +570 -0
  792. data/mlx/python/mlx/_reprlib_fix.py +16 -0
  793. data/mlx/python/mlx/_stub_patterns.txt +36 -0
  794. data/mlx/python/mlx/extension.py +88 -0
  795. data/mlx/python/mlx/nn/__init__.py +5 -0
  796. data/mlx/python/mlx/nn/init.py +441 -0
  797. data/mlx/python/mlx/nn/layers/__init__.py +105 -0
  798. data/mlx/python/mlx/nn/layers/activations.py +661 -0
  799. data/mlx/python/mlx/nn/layers/base.py +675 -0
  800. data/mlx/python/mlx/nn/layers/containers.py +24 -0
  801. data/mlx/python/mlx/nn/layers/convolution.py +232 -0
  802. data/mlx/python/mlx/nn/layers/convolution_transpose.py +242 -0
  803. data/mlx/python/mlx/nn/layers/distributed.py +601 -0
  804. data/mlx/python/mlx/nn/layers/dropout.py +137 -0
  805. data/mlx/python/mlx/nn/layers/embedding.py +53 -0
  806. data/mlx/python/mlx/nn/layers/linear.py +180 -0
  807. data/mlx/python/mlx/nn/layers/normalization.py +363 -0
  808. data/mlx/python/mlx/nn/layers/pooling.py +398 -0
  809. data/mlx/python/mlx/nn/layers/positional_encoding.py +162 -0
  810. data/mlx/python/mlx/nn/layers/quantized.py +426 -0
  811. data/mlx/python/mlx/nn/layers/recurrent.py +289 -0
  812. data/mlx/python/mlx/nn/layers/transformer.py +354 -0
  813. data/mlx/python/mlx/nn/layers/upsample.py +277 -0
  814. data/mlx/python/mlx/nn/losses.py +610 -0
  815. data/mlx/python/mlx/nn/utils.py +165 -0
  816. data/mlx/python/mlx/optimizers/__init__.py +4 -0
  817. data/mlx/python/mlx/optimizers/optimizers.py +976 -0
  818. data/mlx/python/mlx/optimizers/schedulers.py +158 -0
  819. data/mlx/python/mlx/py.typed +1 -0
  820. data/mlx/python/mlx/utils.py +325 -0
  821. data/mlx/python/src/CMakeLists.txt +96 -0
  822. data/mlx/python/src/array.cpp +1525 -0
  823. data/mlx/python/src/buffer.h +124 -0
  824. data/mlx/python/src/constants.cpp +15 -0
  825. data/mlx/python/src/convert.cpp +504 -0
  826. data/mlx/python/src/convert.h +50 -0
  827. data/mlx/python/src/cuda.cpp +19 -0
  828. data/mlx/python/src/device.cpp +98 -0
  829. data/mlx/python/src/distributed.cpp +352 -0
  830. data/mlx/python/src/export.cpp +356 -0
  831. data/mlx/python/src/fast.cpp +627 -0
  832. data/mlx/python/src/fft.cpp +514 -0
  833. data/mlx/python/src/indexing.cpp +1016 -0
  834. data/mlx/python/src/indexing.h +41 -0
  835. data/mlx/python/src/linalg.cpp +663 -0
  836. data/mlx/python/src/load.cpp +531 -0
  837. data/mlx/python/src/load.h +51 -0
  838. data/mlx/python/src/memory.cpp +125 -0
  839. data/mlx/python/src/metal.cpp +98 -0
  840. data/mlx/python/src/mlx.cpp +51 -0
  841. data/mlx/python/src/mlx_func.cpp +116 -0
  842. data/mlx/python/src/mlx_func.h +31 -0
  843. data/mlx/python/src/ops.cpp +5545 -0
  844. data/mlx/python/src/random.cpp +516 -0
  845. data/mlx/python/src/small_vector.h +76 -0
  846. data/mlx/python/src/stream.cpp +147 -0
  847. data/mlx/python/src/transforms.cpp +1542 -0
  848. data/mlx/python/src/trees.cpp +311 -0
  849. data/mlx/python/src/trees.h +62 -0
  850. data/mlx/python/src/utils.cpp +98 -0
  851. data/mlx/python/src/utils.h +78 -0
  852. data/mlx/python/tests/__main__.py +5 -0
  853. data/mlx/python/tests/cuda_skip.py +62 -0
  854. data/mlx/python/tests/mlx_distributed_tests.py +314 -0
  855. data/mlx/python/tests/mlx_tests.py +116 -0
  856. data/mlx/python/tests/mpi_test_distributed.py +142 -0
  857. data/mlx/python/tests/nccl_test_distributed.py +52 -0
  858. data/mlx/python/tests/ring_test_distributed.py +131 -0
  859. data/mlx/python/tests/test_array.py +2139 -0
  860. data/mlx/python/tests/test_autograd.py +880 -0
  861. data/mlx/python/tests/test_bf16.py +196 -0
  862. data/mlx/python/tests/test_blas.py +1429 -0
  863. data/mlx/python/tests/test_compile.py +1277 -0
  864. data/mlx/python/tests/test_constants.py +41 -0
  865. data/mlx/python/tests/test_conv.py +1198 -0
  866. data/mlx/python/tests/test_conv_transpose.py +810 -0
  867. data/mlx/python/tests/test_device.py +150 -0
  868. data/mlx/python/tests/test_double.py +306 -0
  869. data/mlx/python/tests/test_einsum.py +363 -0
  870. data/mlx/python/tests/test_eval.py +200 -0
  871. data/mlx/python/tests/test_export_import.py +614 -0
  872. data/mlx/python/tests/test_fast.py +923 -0
  873. data/mlx/python/tests/test_fast_sdpa.py +647 -0
  874. data/mlx/python/tests/test_fft.py +323 -0
  875. data/mlx/python/tests/test_graph.py +37 -0
  876. data/mlx/python/tests/test_init.py +139 -0
  877. data/mlx/python/tests/test_linalg.py +621 -0
  878. data/mlx/python/tests/test_load.py +447 -0
  879. data/mlx/python/tests/test_losses.py +427 -0
  880. data/mlx/python/tests/test_memory.py +77 -0
  881. data/mlx/python/tests/test_nn.py +1986 -0
  882. data/mlx/python/tests/test_ops.py +3261 -0
  883. data/mlx/python/tests/test_optimizers.py +584 -0
  884. data/mlx/python/tests/test_quantized.py +1160 -0
  885. data/mlx/python/tests/test_random.py +392 -0
  886. data/mlx/python/tests/test_reduce.py +223 -0
  887. data/mlx/python/tests/test_tree.py +96 -0
  888. data/mlx/python/tests/test_upsample.py +100 -0
  889. data/mlx/python/tests/test_vmap.py +860 -0
  890. data/mlx/setup.py +315 -0
  891. data/mlx/tests/CMakeLists.txt +44 -0
  892. data/mlx/tests/allocator_tests.cpp +41 -0
  893. data/mlx/tests/arg_reduce_tests.cpp +204 -0
  894. data/mlx/tests/array_tests.cpp +663 -0
  895. data/mlx/tests/autograd_tests.cpp +1399 -0
  896. data/mlx/tests/blas_tests.cpp +110 -0
  897. data/mlx/tests/compile_tests.cpp +818 -0
  898. data/mlx/tests/creations_tests.cpp +239 -0
  899. data/mlx/tests/custom_vjp_tests.cpp +55 -0
  900. data/mlx/tests/device_tests.cpp +35 -0
  901. data/mlx/tests/einsum_tests.cpp +85 -0
  902. data/mlx/tests/eval_tests.cpp +93 -0
  903. data/mlx/tests/export_import_tests.cpp +164 -0
  904. data/mlx/tests/fft_tests.cpp +366 -0
  905. data/mlx/tests/gpu_tests.cpp +523 -0
  906. data/mlx/tests/linalg_tests.cpp +639 -0
  907. data/mlx/tests/load_tests.cpp +270 -0
  908. data/mlx/tests/ops_tests.cpp +4159 -0
  909. data/mlx/tests/random_tests.cpp +716 -0
  910. data/mlx/tests/scheduler_tests.cpp +121 -0
  911. data/mlx/tests/tests.cpp +26 -0
  912. data/mlx/tests/utils_tests.cpp +67 -0
  913. data/mlx/tests/vmap_tests.cpp +547 -0
  914. metadata +958 -0
@@ -0,0 +1,1277 @@
1
+ # Copyright © 2023-2024 Apple Inc.
2
+
3
+ import gc
4
+ import inspect
5
+ import io
6
+ import math
7
+ from functools import partial, wraps
8
+ from io import StringIO
9
+
10
+ import mlx.core as mx
11
+ import mlx_tests
12
+ import numpy as np
13
+
14
+
15
+ class TestCompile(mlx_tests.MLXTestCase):
16
+ def test_simple_compile(self):
17
+ def fun(x, y):
18
+ return x + y
19
+
20
+ compiled_fn = mx.compile(fun)
21
+ compiled_fn = mx.compile(fun)
22
+ x = mx.array(1.0)
23
+ y = mx.array(1.0)
24
+ out = compiled_fn(x, y)
25
+ self.assertEqual(out.item(), 2.0)
26
+
27
+ # Try again
28
+ out = compiled_fn(x, y)
29
+ self.assertEqual(out.item(), 2.0)
30
+
31
+ # Change sizes
32
+ x = mx.array([1.0, 2.0])
33
+ out = compiled_fn(x, y)
34
+ self.assertTrue(mx.array_equal(out, mx.array([2.0, 3.0])))
35
+
36
+ y = mx.array([1.0, 2.0])
37
+ out = compiled_fn(x, y)
38
+ self.assertTrue(mx.array_equal(out, mx.array([2.0, 4.0])))
39
+
40
+ # Change types
41
+ x = mx.array([1, 2], mx.int32)
42
+ y = mx.array([1, 2], mx.int32)
43
+ out = compiled_fn(x, y)
44
+ self.assertEqual(out.dtype, mx.int32)
45
+ self.assertTrue(mx.array_equal(out, mx.array([2, 4])))
46
+
47
+ def test_compile_grad(self):
48
+ def loss_fn(x):
49
+ return mx.exp(x).sum()
50
+
51
+ grad_fn = mx.grad(loss_fn)
52
+
53
+ x = mx.array([0.5, -0.5, 1.2])
54
+ dfdx = grad_fn(x)
55
+ compile_grad_fn = mx.compile(grad_fn)
56
+ c_dfdx = grad_fn(x)
57
+
58
+ self.assertTrue(mx.allclose(c_dfdx, dfdx))
59
+
60
+ # Run it again without calling compile
61
+ c_dfdx = compile_grad_fn(x)
62
+ self.assertTrue(mx.allclose(c_dfdx, dfdx))
63
+
64
+ # Run it again with calling compile
65
+ c_dfdx = mx.compile(grad_fn)(x)
66
+ self.assertTrue(mx.allclose(c_dfdx, dfdx))
67
+
68
+ # Value and grad
69
+ def loss_fn(x):
70
+ return mx.exp(x).sum(), mx.sin(x)
71
+
72
+ val_and_grad_fn = mx.value_and_grad(loss_fn)
73
+ (loss, val), dfdx = val_and_grad_fn(x)
74
+ (c_loss, c_val), c_dfdx = mx.compile(val_and_grad_fn)(x)
75
+
76
+ self.assertTrue(mx.allclose(c_dfdx, dfdx))
77
+ self.assertTrue(mx.allclose(c_loss, loss))
78
+ self.assertTrue(mx.allclose(c_val, val))
79
+
80
+ def test_compile_inputs_with_primitives(self):
81
+ x = mx.array([1, 2, 3])
82
+ y = mx.array([1, 2, 3])
83
+ for _ in range(5):
84
+ x = x + y
85
+ y = y + 1
86
+
87
+ def fun(x, y):
88
+ return x * y
89
+
90
+ out = fun(x, y)
91
+
92
+ x = mx.array([1, 2, 3])
93
+ y = mx.array([1, 2, 3])
94
+ for _ in range(5):
95
+ x = x + y
96
+ y = y + 1
97
+
98
+ c_out = mx.compile(fun)(x, y)
99
+ self.assertTrue(mx.array_equal(out, c_out))
100
+
101
+ # Try again
102
+ c_out = mx.compile(fun)(x, y)
103
+ self.assertTrue(mx.array_equal(out, c_out))
104
+
105
+ def test_compile_with_closure(self):
106
+ x = mx.array(1)
107
+
108
+ def closure(y):
109
+ return x + y
110
+
111
+ compiled = mx.compile(closure)
112
+ out = compiled(mx.array(1))
113
+ self.assertEqual(out.item(), 2)
114
+
115
+ # Try again
116
+ out = compiled(mx.array(1))
117
+ self.assertEqual(out.item(), 2)
118
+
119
+ # Change the shape of the enclosed variable
120
+ x = mx.array([1, 2])
121
+ out = compiled(mx.array(1))
122
+
123
+ # We still get the original input (closures are not updated)
124
+ self.assertEqual(out.item(), 2)
125
+
126
+ # Try with a tree of enclosed variables
127
+ x = {"a": mx.array(1), "b": mx.array(2)}
128
+
129
+ def closure(y):
130
+ return x["a"] + y + x["b"]
131
+
132
+ compiled = mx.compile(closure)
133
+ out = compiled(mx.array(1))
134
+ self.assertEqual(out.item(), 4)
135
+
136
+ # Change the shape of one input
137
+ x["a"] = mx.array([4, 5])
138
+ out = compiled(mx.array(1))
139
+ self.assertEqual(out.item(), 4)
140
+
141
+ x["b"] = mx.array([-6, -8])
142
+ out = compiled(mx.array(1))
143
+ self.assertEqual(out.item(), 4)
144
+
145
+ # Enclosed variable is not evaluated yet
146
+ x = mx.array(1)
147
+ x = x + x
148
+
149
+ def closure(y):
150
+ return x + y
151
+
152
+ compiled = mx.compile(closure)
153
+ out = compiled(mx.array(2))
154
+ self.assertEqual(out.item(), 4)
155
+
156
+ # And again
157
+ out = compiled(mx.array(2))
158
+ self.assertEqual(out.item(), 4)
159
+
160
+ def test_function_creates_array(self):
161
+ def fun(x):
162
+ return x + mx.array(1)
163
+
164
+ cfun = mx.compile(fun)
165
+ out = cfun(mx.array(3))
166
+ self.assertEqual(out.item(), 4)
167
+
168
+ # And again
169
+ out = cfun(mx.array(3))
170
+ self.assertEqual(out.item(), 4)
171
+
172
+ def test_enable_disable(self):
173
+ def fun(x):
174
+ y = x + 1
175
+ z = x + 1
176
+ return y + z
177
+
178
+ def count_prims(outputs):
179
+ buf = io.StringIO()
180
+ mx.export_to_dot(buf, outputs)
181
+ buf.seek(0)
182
+ return len([l for l in buf.read().split() if "label" in l])
183
+
184
+ x = mx.array(1.0)
185
+ cfun = mx.compile(fun)
186
+ n_compiled = count_prims(cfun(x))
187
+
188
+ # Check disabled
189
+ mx.disable_compile()
190
+ n_uncompiled = count_prims(cfun(x))
191
+ self.assertTrue(n_compiled < n_uncompiled)
192
+
193
+ # Check renabled
194
+ mx.enable_compile()
195
+ n_enable_compiled = count_prims(cfun(x))
196
+ self.assertEqual(n_compiled, n_enable_compiled)
197
+
198
+ def test_compile_two_input_grad(self):
199
+ def loss(w, x):
200
+ y = x * w
201
+ return (y * mx.exp(y)).sum()
202
+
203
+ x = mx.array([1.0, 0.5, 2.0, -0.5])
204
+ w = mx.array([-1.0, 0.3, 1.0, -0.9])
205
+
206
+ expected_grad = mx.grad(loss)(w, x)
207
+ compiled_grad = mx.compile(mx.grad(loss))(w, x)
208
+ self.assertTrue(mx.allclose(expected_grad, compiled_grad))
209
+
210
+ def test_vmap_compiled(self):
211
+ def simple_unary(x):
212
+ return -mx.exp(x)
213
+
214
+ x = mx.array([[1.0, 2.0], [2.0, 3.0]])
215
+
216
+ expected_out = mx.vmap(simple_unary)(x)
217
+ out = mx.vmap(mx.compile(simple_unary))(x)
218
+ self.assertTrue(mx.allclose(expected_out, out))
219
+
220
+ def simple_binary(x, y):
221
+ return mx.abs(mx.exp(x + y) + y)
222
+
223
+ x = mx.array([[1.0, -3.0], [0.5, -0.5]])
224
+ y = mx.array([[2.0, -1.0], [0.25, -0.25]])
225
+
226
+ expected_out = mx.vmap(simple_binary)(x, y)
227
+ out = mx.vmap(mx.compile(simple_binary))(x, y)
228
+ self.assertTrue(mx.allclose(expected_out, out))
229
+
230
+ expected_out = mx.vmap(simple_binary, in_axes=(0, 1))(x, y)
231
+ out = mx.vmap(mx.compile(simple_binary), in_axes=(0, 1))(x, y)
232
+ self.assertTrue(mx.allclose(expected_out, out))
233
+
234
+ y = mx.array([0.25, -0.25])
235
+ expected_out = mx.vmap(simple_binary, in_axes=(0, None))(x, y)
236
+ out = mx.vmap(mx.compile(simple_binary), in_axes=(0, None))(x, y)
237
+ self.assertTrue(mx.allclose(expected_out, out))
238
+
239
+ def simple_unary_outer(x):
240
+ x = mx.abs(x)
241
+
242
+ @mx.compile
243
+ def simple_unary_inner(z):
244
+ return -mx.exp(x)
245
+
246
+ return simple_unary_inner(x)
247
+
248
+ expected_out = -mx.exp(mx.abs(x))
249
+ out = mx.vmap(simple_unary_outer)(x)
250
+ self.assertTrue(mx.allclose(expected_out, out))
251
+
252
+ def test_vjp_vjp_compiled(self):
253
+ def simple_unary(x):
254
+ return -mx.exp(x)
255
+
256
+ x = mx.array([[1.0, 2.0], [2.0, 3.0]])
257
+ y = mx.array([[1.0, 1.0], [1.0, 1.0]])
258
+
259
+ expected_out, expected_vjp_out = mx.vjp(simple_unary, (x,), (y,))
260
+ out, vjp_out = mx.vjp(mx.compile(simple_unary), (x,), (y,))
261
+ self.assertTrue(mx.allclose(expected_vjp_out[0], vjp_out[0]))
262
+ self.assertTrue(mx.allclose(expected_out[0], out[0]))
263
+
264
+ expected_out, expected_jvp_out = mx.jvp(simple_unary, (x,), (y,))
265
+ out, jvp_out = mx.jvp(mx.compile(simple_unary), (x,), (y,))
266
+ self.assertTrue(mx.allclose(expected_jvp_out[0], jvp_out[0]))
267
+ self.assertTrue(mx.allclose(expected_out[0], out[0]))
268
+
269
+ def simple_binary(x, y):
270
+ return mx.abs(mx.exp(x + y) + y)
271
+
272
+ x = mx.array([[1.0, -3.0], [0.5, -0.5]])
273
+ y = mx.array([[2.0, -1.0], [0.25, -0.25]])
274
+ cotans = mx.ones_like(x)
275
+
276
+ expected_out, expected_vjp_out = mx.vjp(simple_binary, (x, y), (cotans,))
277
+ out, vjp_out = mx.vjp(mx.compile(simple_binary), (x, y), (cotans,))
278
+ self.assertTrue(mx.allclose(expected_out[0], out[0]))
279
+ self.assertTrue(mx.allclose(expected_vjp_out[0], vjp_out[0]))
280
+ self.assertTrue(mx.allclose(expected_vjp_out[1], vjp_out[1]))
281
+
282
+ tans = (mx.ones_like(x), mx.ones_like(y))
283
+ expected_out, expected_jvp_out = mx.jvp(simple_binary, (x, y), tans)
284
+ out, jvp_out = mx.jvp(mx.compile(simple_binary), (x, y), tans)
285
+ self.assertTrue(mx.allclose(expected_jvp_out[0], jvp_out[0]))
286
+ self.assertTrue(mx.allclose(expected_out[0], out[0]))
287
+
288
+ def test_transform_over_eval_compiled(self):
289
+ def outer(x):
290
+ y = mx.exp(mx.abs(x))
291
+ mx.eval(y)
292
+ return y.sum()
293
+
294
+ x = mx.array([2.0, -1.0, 0.5])
295
+ dfdx = mx.grad(outer)(x)
296
+
297
+ @mx.compile
298
+ def simple_unary(x):
299
+ return mx.exp(mx.abs(x))
300
+
301
+ def outer(x):
302
+ y = simple_unary(x)
303
+ mx.eval(y)
304
+ return y.sum()
305
+
306
+ cdfdx = mx.grad(outer)(x)
307
+ self.assertTrue(mx.allclose(dfdx, cdfdx))
308
+
309
+ def test_compile_capture(self):
310
+ # Test update captured state outside compiled function
311
+ state = {"y": mx.array(2)}
312
+
313
+ @partial(mx.compile, inputs=state)
314
+ def test_state(x):
315
+ x = x + state["y"]
316
+ return x
317
+
318
+ test_state(mx.array(1))
319
+ # Check the state is unchanged
320
+ self.assertEqual(state["y"], 2)
321
+
322
+ # Check the updated state is used
323
+ state["y"] = mx.array(3)
324
+ out = test_state(mx.array(1))
325
+ self.assertEqual(out.item(), 4)
326
+
327
+ # Capture list
328
+ state = [mx.array(2)]
329
+
330
+ @partial(mx.compile, inputs=state)
331
+ def test_state(x):
332
+ x = x + state[0]
333
+ return x
334
+
335
+ out = test_state(mx.array(1))
336
+ self.assertEqual(out.item(), 3)
337
+ state[0] = mx.array(3)
338
+ out = test_state(mx.array(1))
339
+ self.assertEqual(out.item(), 4)
340
+
341
+ # Capture tuple of list
342
+ state = ([mx.array(2)],)
343
+
344
+ @partial(mx.compile, inputs=state)
345
+ def test_state(x):
346
+ x = x + state[0][0]
347
+ return x
348
+
349
+ out = test_state(mx.array(1))
350
+ self.assertEqual(out.item(), 3)
351
+ state[0][0] = mx.array(3)
352
+ out = test_state(mx.array(1))
353
+ self.assertEqual(out.item(), 4)
354
+
355
+ # Test state updated inside compiled function
356
+ state = {}
357
+
358
+ @partial(mx.compile, outputs=state)
359
+ def test_state(x):
360
+ state["y"] = x + 3
361
+ return mx.abs(x)
362
+
363
+ test_state(mx.array(-1))
364
+ self.assertEqual(state["y"].item(), 2)
365
+
366
+ # Test state changed inside compiled function
367
+ # triggers recompile
368
+ state = {}
369
+
370
+ @partial(mx.compile, inputs=state, outputs=state)
371
+ def test_state(x):
372
+ y = state.get("y", mx.array(0))
373
+ state["y"] = x + y
374
+ return x + 2 * y
375
+
376
+ test_state(mx.array(1))
377
+ self.assertEqual(state["y"].item(), 1)
378
+ test_state(mx.array(1))
379
+ self.assertEqual(state["y"].item(), 2)
380
+
381
+ def test_compile_rng(self):
382
+ @partial(mx.compile, inputs=mx.random.state, outputs=mx.random.state)
383
+ def fun():
384
+ return mx.random.uniform(shape=(10, 10))
385
+
386
+ self.assertFalse(mx.allclose(fun(), fun(), 1e-2, 1e-2))
387
+
388
+ def test_compile_kwargs(self):
389
+ @mx.compile
390
+ def fun(x, y, z):
391
+ return x + y + z
392
+
393
+ x = mx.array(1)
394
+ y = mx.array(2)
395
+ z = mx.array(3)
396
+ out = fun(x, y=y, z=z)
397
+ self.assertEqual(out.item(), 6)
398
+
399
+ def test_shapeless_compile(self):
400
+ y = 1
401
+
402
+ @partial(mx.compile, shapeless=True)
403
+ def fun(x):
404
+ return x + y
405
+
406
+ x = mx.array([1, 2])
407
+ self.assertTrue(mx.array_equal(fun(x), mx.array([2, 3])))
408
+
409
+ # The function is not recompiled, so the change
410
+ # to y should not be reflected in the output
411
+ y = 2
412
+ x = mx.array([1, 2, 3])
413
+ self.assertTrue(mx.array_equal(fun(x), mx.array([2, 3, 4])))
414
+
415
+ # Type change recompiles
416
+ x = mx.array([1.0, 2.0, 3.0])
417
+ self.assertTrue(mx.array_equal(fun(x), mx.array([3.0, 4.0, 5.0])))
418
+
419
+ # Dim change recompiles
420
+ x = mx.array([[1, 2, 3]])
421
+ self.assertTrue(mx.array_equal(fun(x), mx.array([[3, 4, 5]])))
422
+
423
+ def test_shapeless_compile_with_broadcasts(self):
424
+ x = mx.ones((2, 2))
425
+ y = mx.array([2, 2])
426
+
427
+ def fun(x, y):
428
+ return x * y
429
+
430
+ cfun = mx.compile(fun, shapeless=True)
431
+ self.assertTrue(mx.array_equal(cfun(x, y), fun(x, y)))
432
+ self.assertTrue(mx.array_equal(cfun(y, x), fun(y, x)))
433
+ y = mx.array([[3]])
434
+ self.assertTrue(mx.array_equal(cfun(x, y), fun(x, y)))
435
+ self.assertTrue(mx.array_equal(cfun(y, x), fun(y, x)))
436
+
437
+ def test_shapeless_compile_with_reduction(self):
438
+ # Test shapeless compile with a reduction
439
+ z = 1
440
+
441
+ @partial(mx.compile, shapeless=True)
442
+ def fun(x, y):
443
+ return x + y.sum(0, keepdims=True) + z
444
+
445
+ x = mx.ones((2, 2), mx.int32)
446
+ y = mx.ones((2, 2), mx.int32)
447
+ self.assertTrue(mx.array_equal(fun(x, y), mx.full(shape=(2, 2), vals=4)))
448
+ x = mx.ones((3, 3), mx.int32)
449
+ y = mx.ones((3, 3), mx.int32)
450
+ z = 2
451
+ self.assertTrue(mx.array_equal(fun(x, y), mx.full(shape=(3, 3), vals=5)))
452
+
453
+ x1 = mx.array([[1, 2], [3, 4], [5, 6]])
454
+ x2 = mx.array([[1, 2]])
455
+
456
+ def fun(x):
457
+ return x * x.sum(-1, keepdims=True)
458
+
459
+ cfun = mx.compile(fun, shapeless=True)
460
+ mx.eval(cfun(x1))
461
+ self.assertTrue(mx.array_equal(fun(x2), cfun(x2)))
462
+
463
+ def fun(x):
464
+ return x * x.sum(-1, keepdims=False)
465
+
466
+ cfun = mx.compile(fun, shapeless=True)
467
+ self.assertTrue(mx.array_equal(fun(x2), cfun(x2)))
468
+
469
+ def test_shapeless_compile_unflatten(self):
470
+ x = mx.zeros((1, 1, 4 * 32))
471
+
472
+ def fun(x):
473
+ return mx.unflatten(x, -1, (4, -1))
474
+
475
+ self.assertEqual(mx.compile(fun, shapeless=True)(x).shape, (1, 1, 4, 32))
476
+
477
+ def test_shapeless_compile_gather(self):
478
+ x = mx.zeros((1, 1, 32))
479
+
480
+ def fun(x):
481
+ return x[:, -1, :]
482
+
483
+ self.assertEqual(mx.compile(fun, shapeless=True)(x).shape, (1, 32))
484
+
485
+ def test_shapeless_compile_full_like(self):
486
+ x_shape = (1, 1, 32)
487
+ x = mx.zeros((x_shape))
488
+
489
+ def zeros_fun(x):
490
+ return mx.zeros_like(x)
491
+
492
+ def ones_fun(x):
493
+ return mx.ones_like(x)
494
+
495
+ compiled_zero_like = mx.compile(zeros_fun, shapeless=True)
496
+ compiled_ones_like = mx.compile(ones_fun, shapeless=True)
497
+
498
+ self.assertEqual(compiled_zero_like(x).shape, x_shape)
499
+ self.assertEqual(compiled_ones_like(x).shape, x_shape)
500
+
501
+ y_shape = (2, 2, 16)
502
+ y = mx.zeros(y_shape)
503
+
504
+ self.assertEqual(compiled_zero_like(y).shape, y_shape)
505
+ self.assertEqual(compiled_ones_like(y).shape, y_shape)
506
+
507
+ def test_compile_with_constant(self):
508
+ # Test float
509
+ @partial(mx.compile)
510
+ def fun(x, y):
511
+ return x + y
512
+
513
+ z = fun(mx.array(1.0), 1.0)
514
+ self.assertEqual(z.item(), 2.0)
515
+
516
+ z = fun(mx.array(1.0), 2.0)
517
+ self.assertEqual(z.item(), 3.0)
518
+
519
+ z = fun(mx.array(1.0), y=1.0)
520
+ self.assertEqual(z.item(), 2.0)
521
+
522
+ z = fun(mx.array(1.0), y=3.0)
523
+ self.assertEqual(z.item(), 4.0)
524
+
525
+ # Test tuple
526
+ @partial(mx.compile)
527
+ def fun(x, y=(1, 2)):
528
+ return x + y[0] + y[1]
529
+
530
+ z = fun(mx.array(1))
531
+ self.assertEqual(z.item(), 4)
532
+
533
+ z = fun(mx.array(1), (2, 2))
534
+ self.assertEqual(z.item(), 5)
535
+
536
+ z = fun(mx.array(1), (2, 1))
537
+ self.assertEqual(z.item(), 4)
538
+
539
+ # Test bool
540
+ @partial(mx.compile)
541
+ def fun(x, y):
542
+ if y:
543
+ return x + 1
544
+ else:
545
+ return x + 2
546
+
547
+ z = fun(mx.array(1), True)
548
+ self.assertEqual(z.item(), 2)
549
+
550
+ z = fun(mx.array(1), False)
551
+ self.assertEqual(z.item(), 3)
552
+
553
+ # Test string
554
+ @partial(mx.compile)
555
+ def fun(x, y):
556
+ if y == "one":
557
+ return x + 1
558
+ else:
559
+ return x + 2
560
+
561
+ z = fun(mx.array(1), "one")
562
+ self.assertEqual(z.item(), 2)
563
+
564
+ z = fun(mx.array(1), "two")
565
+ self.assertEqual(z.item(), 3)
566
+
567
+ # Test nested constant
568
+ @partial(mx.compile)
569
+ def fun(x, y):
570
+ if y[0][0] == 1:
571
+ return x + 1
572
+ else:
573
+ return x + 2
574
+
575
+ z = fun(mx.array(1), [[1]])
576
+ self.assertEqual(z.item(), 2)
577
+
578
+ z = fun(mx.array(1), [[0]])
579
+ self.assertEqual(z.item(), 3)
580
+
581
+ @partial(mx.compile)
582
+ def fun(x, a, b):
583
+ for ai in a:
584
+ for bi in b:
585
+ x = bi * x + ai
586
+ return x
587
+
588
+ z = fun(mx.array(1), [1, 1], [2])
589
+ self.assertEqual(z.item(), 7)
590
+
591
+ z = fun(mx.array(1), [1], [1, 2])
592
+ self.assertEqual(z.item(), 5)
593
+
594
+ counter = [0]
595
+
596
+ @partial(mx.compile)
597
+ def fun(x, y):
598
+ counter[0] += 1
599
+ return x + y
600
+
601
+ z = fun(mx.array(1), 1)
602
+ self.assertEqual(z.item(), 2)
603
+
604
+ z = fun(1, mx.array(1))
605
+ self.assertEqual(z.item(), 2)
606
+
607
+ self.assertEqual(counter[0], 2)
608
+
609
+ y = 1.0
610
+
611
+ @mx.compile
612
+ def fun(x, constant):
613
+ return x + y
614
+
615
+ constant1 = "abc"
616
+ out = fun(mx.array(0.0), constant1)
617
+ self.assertEqual(out, mx.array(1.0))
618
+
619
+ # new object, same value, no recompilation
620
+ y = 2.0
621
+ constant2 = "abc".encode("utf-8").decode("utf-8")
622
+ out = fun(mx.array(0.0), constant2)
623
+ self.assertEqual(out, mx.array(1.0))
624
+
625
+ # same object, new value, recompilation
626
+ constant2 = "xyz"
627
+ out = fun(mx.array(0.0), constant2)
628
+ self.assertEqual(out, mx.array(2.0))
629
+
630
+ def test_compile_inf(self):
631
+ @mx.compile
632
+ def fun(x):
633
+ return mx.isinf(x + 2)
634
+
635
+ out = fun(mx.array([0.0]))
636
+ self.assertEqual(out.item(), False)
637
+
638
+ def test_unsupported_input_types(self):
639
+ class MyClass:
640
+ value = 1
641
+
642
+ @mx.compile
643
+ def fun(x, y):
644
+ return x + y.value
645
+
646
+ with self.assertRaises(ValueError):
647
+ out = fun(mx.array(0.0), MyClass())
648
+
649
+ with self.assertRaises(ValueError):
650
+ out = fun(mx.array(0.0), y=MyClass())
651
+
652
+ def test_compile_create_list(self):
653
+ @mx.compile
654
+ def fun():
655
+ return [0.1 * mx.zeros((2,)), 0.1 * mx.zeros((2,))]
656
+
657
+ out = fun()
658
+ mx.eval(out)
659
+
660
+ def test_compile_vjp(self):
661
+ def fun(w):
662
+ w1 = w + w
663
+ w2 = w + w
664
+ return w @ w1 + w2 @ w2
665
+
666
+ def step(w):
667
+ out, grad = mx.vjp(fun, (w,), (mx.array([[1.0, 1.0], [1.0, 1.0]]),))
668
+ return out[0], grad[0]
669
+
670
+ w = mx.zeros((2, 2))
671
+ mx.eval(w)
672
+
673
+ expected = step(w)
674
+ out = mx.compile(step)(w)
675
+ self.assertTrue(mx.allclose(expected[0], out[0]))
676
+ self.assertTrue(mx.allclose(expected[1], out[1]))
677
+
678
+ def fun(w1, w2, x):
679
+ x = x @ w1
680
+ y = x @ w2
681
+ x = x + y * y
682
+ return (x * x).sum()
683
+
684
+ w1 = mx.zeros((4, 4))
685
+ w2 = mx.zeros((4, 4))
686
+ x = mx.zeros((4, 4))
687
+
688
+ def step(w1, w2, x):
689
+ loss, gradient = mx.value_and_grad(fun)(w1, w2, x)
690
+ w1 = w1 + gradient
691
+ return loss, w1
692
+
693
+ mx.eval(x, w1, w2)
694
+ expected = step(w1, w2, x)
695
+ out = mx.compile(step)(w1, w2, x)
696
+
697
+ self.assertTrue(mx.allclose(expected[0], out[0]))
698
+ self.assertTrue(mx.allclose(expected[1], out[1]))
699
+
700
+ def test_shapeless_mean(self):
701
+ def mean(x):
702
+ return mx.mean(x, keepdims=True)
703
+
704
+ cfun = mx.compile(mean)
705
+ out = cfun(mx.ones((5, 5)))
706
+ self.assertTrue(mx.allclose(out, mx.array(1.0)))
707
+
708
+ cmean = mx.compile(mean, shapeless=True)
709
+
710
+ x = mx.ones(2)
711
+ out = cmean(x)
712
+ self.assertTrue(mx.allclose(out, mean(x)))
713
+
714
+ x = mx.ones(4)
715
+ out = cmean(x)
716
+ self.assertTrue(mx.allclose(out, mean(x)))
717
+
718
+ x = mx.ones(7)
719
+ out = cmean(x)
720
+ self.assertTrue(mx.allclose(out, mean(x)))
721
+
722
+ def test_compile_broadcast_only(self):
723
+ def fn(a):
724
+ a = mx.broadcast_to(a, (1,))
725
+ return a + a
726
+
727
+ out = mx.compile(fn)(mx.array(2.0))
728
+ # Make sure repr can be called
729
+ self.assertTrue(repr(out) is not None)
730
+ self.assertTrue(mx.array_equal(out, mx.array([4.0])))
731
+
732
+ def test_compile_with_long_name(self):
733
+ def fn(a, b):
734
+ for _ in range(10):
735
+ a = a - 1.0
736
+ b = b - 1.0
737
+ return a + b
738
+
739
+ out = mx.compile(fn)(mx.array(10.0), mx.array(20.0))
740
+ self.assertEqual(out.item(), 10.0)
741
+
742
+ def test_compile_multi_output(self):
743
+ def fn(x):
744
+ ys = [x]
745
+ for i in range(5):
746
+ ys.append(ys[-1] + x)
747
+ return ys, mx.sum(ys[-1])
748
+
749
+ x = mx.ones(1, dtype=mx.int32)
750
+ y1 = mx.compile(fn)(x)[1]
751
+ y2 = fn(x)[1]
752
+ self.assertEqual(y1.item(), y2.item())
753
+ self.assertEqual(y1.item(), 6)
754
+
755
+ def test_inf_constant(self):
756
+ def fn(x):
757
+ return mx.where(mx.isinf(x), 0, 1)
758
+
759
+ x = mx.array([0, float("inf"), 1], dtype=mx.bfloat16)
760
+ self.assertTrue(mx.array_equal(mx.compile(fn)(x), fn(x)))
761
+
762
+ def test_max_into_equal(self):
763
+ x = mx.random.uniform(shape=(1, 2, 2))
764
+ mx.eval(x)
765
+
766
+ def fn():
767
+ maxes = mx.max(x, axis=(1, 2), keepdims=True)
768
+ return x == maxes
769
+
770
+ out = mx.compile(fn)()
771
+ expected = fn()
772
+ self.assertTrue(mx.array_equal(expected, out))
773
+
774
+ def test_dtypes(self):
775
+ x = mx.array([0, 1, 2, 3])
776
+ dtypes = [mx.bool_, mx.int8, mx.uint8, mx.int16, mx.uint16]
777
+ for dtype in dtypes:
778
+ x = x.astype(dtype)
779
+ mx.eval(x)
780
+
781
+ def fn(x):
782
+ return x * 1 + 0
783
+
784
+ out = mx.compile(fn)(x)
785
+ expected = fn(x)
786
+ self.assertTrue(mx.array_equal(expected, out))
787
+
788
+ def test_compile_without_captured_inputs(self):
789
+ x = mx.array([1, 2, 3]) + 2
790
+
791
+ def fn(a):
792
+ y = x + 1
793
+ return a + y
794
+
795
+ with self.assertRaises(ValueError):
796
+ y = mx.compile(fn)(x)
797
+
798
+ x = mx.array([1.0, 2.0]) + mx.array([1.0, 2.0])
799
+ y = None
800
+
801
+ def fn(x):
802
+ nonlocal y
803
+ if y is None:
804
+ y = mx.array([1.0, 2.0])
805
+
806
+ y = y + x
807
+ return y
808
+
809
+ fn(x)
810
+ with self.assertRaises(ValueError):
811
+ y = mx.compile(fn)(x)
812
+
813
+ def test_compile_dynamic_dims(self):
814
+ a = mx.random.uniform(shape=(2,) * 10)
815
+ b = mx.random.uniform(shape=(2,) * 10)
816
+ a = a.T
817
+ mx.eval(a, b)
818
+
819
+ def fn(a, b):
820
+ return mx.abs(a + b)
821
+
822
+ out = mx.compile(fn)(a, b)
823
+ expected = fn(a, b)
824
+ self.assertTrue(mx.allclose(out, expected))
825
+
826
+ def test_compile_many_inputs(self):
827
+ inputs = [mx.ones((2, 2, 2, 2)) for _ in range(20)]
828
+ inputs[0] = inputs[0].T
829
+
830
+ @mx.compile
831
+ def fun(*inputs):
832
+ x = inputs[0]
833
+ for y in inputs[1:10]:
834
+ x = x + y
835
+ a = inputs[10]
836
+ for b in inputs[11:]:
837
+ a = a + b
838
+ return x + a
839
+
840
+ out = fun(*inputs)
841
+ self.assertTrue(mx.allclose(out, mx.full((2, 2), 20)))
842
+
843
+ @mx.compile
844
+ def fun(arrs):
845
+ for _ in range(6):
846
+ arrs = [x + y for x, y in zip(arrs[::2], arrs[1::2])]
847
+ return arrs[0]
848
+
849
+ arrs = [mx.array([1.0, 2.0]) for _ in range(64)]
850
+ out = fun(arrs)
851
+ self.assertTrue(mx.allclose(out, mx.array([64.0, 128.0])))
852
+
853
+ inputs = [mx.arange(16384).astype(mx.float16) for _ in range(8)]
854
+
855
+ def fun(inputs):
856
+ a = inputs[0] + inputs[1]
857
+ b = inputs[2] + inputs[3]
858
+ c = inputs[4] + inputs[5]
859
+ d = inputs[6] + inputs[7]
860
+ return a * b * c * d
861
+
862
+ out = mx.compile(fun)(inputs)
863
+ expected = fun(inputs)
864
+ self.assertTrue(mx.allclose(out, expected))
865
+
866
+ def test_compile_many_outputs(self):
867
+ @mx.compile
868
+ def fun(arr):
869
+ arrs = [arr] * 64
870
+ first_arrs = None
871
+ for _ in range(6):
872
+ arrs = [x + y for x, y in zip(arrs[::2], arrs[1::2])]
873
+ if first_arrs is None:
874
+ first_arrs = arrs
875
+ return arrs[0], first_arrs
876
+
877
+ out = fun(mx.array([1.0, 2.0]))
878
+ self.assertTrue(mx.allclose(out[0], mx.array([64.0, 128.0])))
879
+
880
+ def test_shapeless_compile_matmul(self):
881
+ a = mx.array([0.0, 1.0, 2.0])
882
+ b = mx.array([0.0, 1.0, 2.0])
883
+
884
+ fun = mx.compile(lambda a, b: a @ b, shapeless=True)
885
+ self.assertTrue(mx.allclose(fun(a, b), a @ b))
886
+
887
+ def test_shapeless_compile_slice_update(self):
888
+ def fun(x):
889
+ x[2] = mx.array([3.0])
890
+ return x
891
+
892
+ cfun = mx.compile(fun, shapeless=True)
893
+
894
+ a = mx.array([0.0, 1.0, 2.0, 3.0])
895
+ self.assertTrue(mx.allclose(cfun(a), fun(a)))
896
+
897
+ a = mx.array([0.0, 1.0, 2.0, 3.0, 4.0])
898
+ self.assertTrue(mx.allclose(cfun(a), fun(a)))
899
+
900
+ def test_shapeless_compile_with_reshape(self):
901
+ def fun(x):
902
+ return x.reshape(x.shape[0] * x.shape[1], -1)
903
+
904
+ compiled_fun = mx.compile(fun, shapeless=True)
905
+
906
+ x = mx.zeros(shape=(2, 3, 4))
907
+ out = compiled_fun(x)
908
+ self.assertEqual(out.shape, (6, 4))
909
+
910
+ x = mx.zeros(shape=(2, 3, 8))
911
+ out = compiled_fun(x)
912
+ self.assertEqual(out.shape, (6, 8))
913
+
914
+ x = mx.zeros(shape=(5, 5, 5))
915
+
916
+ with self.assertRaises(ValueError):
917
+ compiled_fun(x)
918
+
919
+ def test_compile_shapeless_with_broadcast(self):
920
+ a = mx.array(0.0)
921
+ b = mx.ones((2, 2))
922
+
923
+ def fun(a):
924
+ return mx.broadcast_to(a, b.shape)
925
+
926
+ cfun = mx.compile(fun, shapeless=True)
927
+ # Works on the first shape
928
+ cfun(a)
929
+
930
+ # Fails on a different shape
931
+ with self.assertRaises(ValueError):
932
+ cfun(mx.array(0.0).reshape(1, 1, 1))
933
+
934
+ def fun(a, b):
935
+ return mx.broadcast_arrays(a, b)
936
+
937
+ cfun = mx.compile(fun, shapeless=True)
938
+ a, b = cfun(a, b)
939
+ self.assertEqual(a.shape, (2, 2))
940
+ self.assertEqual(b.shape, (2, 2))
941
+
942
+ # Batched matmul
943
+ a = mx.zeros((2, 1, 4, 2))
944
+ b = mx.zeros((3, 2, 5))
945
+
946
+ def fun(a, b):
947
+ return a @ b
948
+
949
+ cfun = mx.compile(fun, shapeless=True)
950
+ out = cfun(a, b)
951
+ self.assertEqual(out.shape, (2, 3, 4, 5))
952
+
953
+ # Shapeless compile should be preserved over vjp, jvp, vmap
954
+ def fun(args):
955
+ return sum(args).sum()
956
+
957
+ a = mx.array(0.0)
958
+ b = mx.ones((2, 2))
959
+
960
+ cfun = mx.compile(mx.grad(fun), shapeless=True)
961
+ out = cfun((a, b))
962
+
963
+ self.assertEqual(out[0].shape, ())
964
+ self.assertEqual(out[1].shape, (2, 2))
965
+
966
+ out = cfun((b, a))
967
+
968
+ self.assertEqual(out[0].shape, (2, 2))
969
+ self.assertEqual(out[1].shape, ())
970
+
971
+ # Shapeless compile should be preserved over vjp, jvp, vmap
972
+ def fun(args):
973
+ return (args[0] @ args[1]).sum()
974
+
975
+ a = mx.zeros((2, 1, 4, 2))
976
+ b = mx.zeros((3, 2, 5))
977
+
978
+ cfun = mx.compile(mx.grad(fun), shapeless=True)
979
+ out = cfun((a, b))
980
+
981
+ self.assertEqual(out[0].shape, (2, 1, 4, 2))
982
+ self.assertEqual(out[1].shape, (3, 2, 5))
983
+
984
+ a = mx.zeros((3, 1, 4, 2))
985
+ b = mx.zeros((2, 2, 5))
986
+
987
+ out = cfun((a, b))
988
+
989
+ self.assertEqual(out[0].shape, (3, 1, 4, 2))
990
+ self.assertEqual(out[1].shape, (2, 2, 5))
991
+
992
+ def test_leaks(self):
993
+ gc.collect()
994
+ if mx.metal.is_available():
995
+ mem_pre = mx.get_active_memory()
996
+ else:
997
+ mem_pre = 0
998
+
999
+ def outer():
1000
+ d = {}
1001
+
1002
+ def f(x):
1003
+ return d["x"]
1004
+
1005
+ d["f"] = mx.compile(f)
1006
+ d["x"] = mx.array([0] * 1000)
1007
+
1008
+ for _ in range(5):
1009
+ outer()
1010
+ gc.collect()
1011
+
1012
+ if mx.metal.is_available():
1013
+ mem_post = mx.get_active_memory()
1014
+ else:
1015
+ mem_post = 0
1016
+
1017
+ self.assertEqual(mem_pre, mem_post)
1018
+
1019
+ def test_double_constant(self):
1020
+ with mx.stream(mx.cpu):
1021
+ x = mx.array(1.0, dtype=mx.float64)
1022
+
1023
+ def fun(x):
1024
+ return (x + math.pi) * 2.0
1025
+
1026
+ y = fun(x).item()
1027
+ y_compiled = mx.compile(fun)(x).item()
1028
+ self.assertEqual(y, y_compiled)
1029
+
1030
+ def test_shared_broadcast(self):
1031
+ def fun(x, y, z):
1032
+ yy = mx.broadcast_to(y, z.shape)
1033
+ return (x + yy * z), yy.sum()
1034
+
1035
+ a = mx.random.normal((10, 10))
1036
+ b = mx.array(0.1)
1037
+ c = mx.random.normal((10, 10))
1038
+ mx.eval(a, b, c)
1039
+ fc = mx.compile(fun)
1040
+ d = fc(a, b, c)
1041
+
1042
+ s = StringIO()
1043
+ mx.export_to_dot(s, a=a, b=b, c=c, d1=d[0], d2=d[1])
1044
+ s.seek(0)
1045
+ s = s.read()
1046
+
1047
+ self.assertTrue("CompiledBroadcastMultiplyAdd" in s)
1048
+ d_hat = fun(a, b, c)
1049
+ self.assertTrue(mx.allclose(d[0], d_hat[0]))
1050
+ self.assertTrue(mx.allclose(d[1], d_hat[1]))
1051
+
1052
+ def test_wrap_compiled(self):
1053
+ @mx.compile
1054
+ def inner():
1055
+ pass
1056
+
1057
+ @wraps(inner)
1058
+ def wrapper():
1059
+ pass
1060
+
1061
+ def test_compiled_preserves_attributes(self):
1062
+ def inner(x: mx.array, y: str):
1063
+ """
1064
+ A useful function.
1065
+ """
1066
+ pass
1067
+
1068
+ c_inner = mx.compile(inner)
1069
+ self.assertEqual(inner.__name__, c_inner.__name__)
1070
+ self.assertEqual(inner.__qualname__, c_inner.__qualname__)
1071
+ self.assertEqual(inner.__doc__, c_inner.__doc__)
1072
+ self.assertEqual(inspect.signature(inner), inspect.signature(c_inner))
1073
+
1074
+ def test_compile_with_none(self):
1075
+ @mx.compile
1076
+ def fun(x, y):
1077
+ if y is None:
1078
+ return mx.abs(x - 2.0)
1079
+ else:
1080
+ return mx.abs(x + y)
1081
+
1082
+ out = fun(mx.array(1.0), None)
1083
+ self.assertEqual(out.item(), 1.0)
1084
+
1085
+ out = fun(mx.array(1.0), mx.array(2.0))
1086
+ self.assertEqual(out.item(), 3.0)
1087
+
1088
+ def test_compile_changing_outputs(self):
1089
+ @mx.compile
1090
+ def fun(x, y):
1091
+ if y is None:
1092
+ return 2 * x
1093
+ elif (
1094
+ isinstance(x, mx.array)
1095
+ and isinstance(y, mx.array)
1096
+ and x.dtype == y.dtype == mx.float32
1097
+ ):
1098
+ return [x + y]
1099
+ elif y.dtype == mx.bool_:
1100
+ return {"a": x, "b": y * x}
1101
+ else:
1102
+ return None
1103
+
1104
+ a = fun(mx.array(1.0), mx.array(2.0))
1105
+ self.assertTrue(isinstance(a, list))
1106
+ self.assertEqual(a[0].item(), 3.0)
1107
+
1108
+ b = fun(mx.array(1.0), mx.array(True))
1109
+ self.assertTrue(isinstance(b, dict))
1110
+ self.assertEqual(b["a"].item(), 1.0)
1111
+ self.assertEqual(b["b"].item(), 1.0)
1112
+
1113
+ c = fun(mx.array(1.0), None)
1114
+ self.assertTrue(isinstance(c, mx.array))
1115
+ self.assertEqual(c.item(), 2.0)
1116
+
1117
+ d = fun(False, mx.array(1.0))
1118
+ self.assertTrue(d is None)
1119
+
1120
+ def test_compile_changing_outputs_with_state(self):
1121
+ state = [mx.array(1.0)]
1122
+
1123
+ @partial(mx.compile, inputs=state, outputs=state)
1124
+ def fun(y):
1125
+ x = state[0]
1126
+ if y.dtype == mx.float32:
1127
+ state[0] = 2 * y
1128
+ return [x, y, x + y]
1129
+ elif y.dtype == mx.int32:
1130
+ state[0] *= 2
1131
+ return x + y
1132
+
1133
+ for i in range(10):
1134
+ fun(mx.array(1.0))
1135
+ fun(mx.array(1))
1136
+
1137
+ self.assertEqual(state[0].item(), 4)
1138
+
1139
+ def test_outputs_changing(self):
1140
+ @mx.compile
1141
+ def fun(x):
1142
+ x = mx.abs(mx.negative(x))
1143
+ y = mx.abs(x)
1144
+ return x, y
1145
+
1146
+ @mx.compile
1147
+ def fun2(x):
1148
+ x = mx.abs(mx.negative(x))
1149
+ y = mx.abs(x)
1150
+ return y
1151
+
1152
+ a, b = fun(mx.array(-1.0))
1153
+ mx.eval(a, b)
1154
+
1155
+ a = fun2(mx.array(-1.0))
1156
+ self.assertEqual(a.item(), 1.0)
1157
+
1158
+ def test_multiple_compile_same_capture(self):
1159
+ def fun(do_compile):
1160
+ t = mx.ones((10,))
1161
+ u = (1.0 - t) * 0.0 + t * 3.0
1162
+
1163
+ o = mx.ones((6,))
1164
+ b = o[:, None] * u
1165
+
1166
+ c = b * mx.ones_like(u)
1167
+
1168
+ a = mx.ones((6,))
1169
+ if do_compile:
1170
+ d = mx.compile(lambda x: x @ b)(a)
1171
+ e = mx.compile(lambda x: x @ c.T)(d)
1172
+ else:
1173
+ d = a @ b
1174
+ e = d @ c.T
1175
+ return e
1176
+
1177
+ out = fun(True)
1178
+ mx.eval(out)
1179
+ expected = fun(False)
1180
+ self.assertTrue(mx.allclose(out, expected))
1181
+
1182
+ def test_compile_types(self):
1183
+ from typing import NamedTuple
1184
+
1185
+ class Vector(tuple):
1186
+ pass
1187
+
1188
+ class State(NamedTuple):
1189
+ a: mx.array
1190
+ b: mx.array
1191
+
1192
+ def transform(x: State):
1193
+ return State(x.a + 10, x.b * 10)
1194
+
1195
+ def transform_tuple(t):
1196
+ return (t[0] + 10, t[1] * 10)
1197
+
1198
+ def transform_vector(t):
1199
+ return Vector([t[0] + 10, t[1] * 10])
1200
+
1201
+ x = State(mx.array(1), mx.array(2))
1202
+
1203
+ compiled_transform = mx.compile(transform)
1204
+ compiled_transform_tuple = mx.compile(transform_tuple)
1205
+ compiled_transform_vector = mx.compile(transform_vector)
1206
+
1207
+ x_batch_tuple = (mx.array([1, 2, 3]), mx.array([4, 5, 6]))
1208
+ out1 = compiled_transform_tuple(x_batch_tuple)
1209
+
1210
+ self.assertTrue(isinstance(out1, tuple))
1211
+ self.assertTrue(mx.array_equal(out1[0], mx.array([11, 12, 13])))
1212
+ self.assertTrue(mx.array_equal(out1[1], mx.array([40, 50, 60])))
1213
+
1214
+ x_batch = State(mx.array([1, 2, 3]), mx.array([4, 5, 6]))
1215
+ out2 = compiled_transform(x_batch)
1216
+ self.assertTrue(isinstance(out2, State))
1217
+ self.assertTrue(mx.array_equal(out2.a, mx.array([11, 12, 13])))
1218
+ self.assertTrue(mx.array_equal(out2.b, mx.array([40, 50, 60])))
1219
+
1220
+ x_batch_vector = Vector([mx.array([1, 2, 3]), mx.array([4, 5, 6])])
1221
+ out3 = compiled_transform_vector(x_batch_vector)
1222
+ self.assertTrue(isinstance(out3, Vector))
1223
+ self.assertTrue(mx.array_equal(out3[0], mx.array([11, 12, 13])))
1224
+ self.assertTrue(mx.array_equal(out3[1], mx.array([40, 50, 60])))
1225
+
1226
+ def test_compile_output_with_siblings(self):
1227
+ @mx.compile
1228
+ def fun(x, y):
1229
+ return mx.divmod(mx.abs(x), mx.abs(y))[0]
1230
+
1231
+ out = fun(mx.array(1.0), mx.array(1.0))
1232
+ self.assertEqual(out.item(), 1.0)
1233
+
1234
+ # Make sure the following compiles without issue
1235
+ def loss_fn(params, x):
1236
+ emb, w = params
1237
+ return mx.fast.layer_norm(emb[x], w, None, 1e-4).sum()
1238
+
1239
+ emb = mx.zeros((10, 32))
1240
+ w = mx.zeros((32,))
1241
+
1242
+ loss_and_grad_fn = mx.value_and_grad(loss_fn)
1243
+
1244
+ x = mx.zeros(shape=(4, 32), dtype=mx.int32)
1245
+ mx.eval(x, emb, w)
1246
+
1247
+ @mx.compile
1248
+ def step(emb, w, x):
1249
+ loss, grads = loss_and_grad_fn((emb, w), x)
1250
+ return loss, grads
1251
+
1252
+ loss, grads = step(emb, w, x)
1253
+ mx.eval(loss, grads)
1254
+
1255
+ def test_compile_donates_input_buffer(self):
1256
+ mx.set_default_device(mx.cpu)
1257
+
1258
+ def fun(x):
1259
+ return mx.sin(x) + 1
1260
+
1261
+ compiled_fn = mx.compile(fun)
1262
+
1263
+ input = mx.arange(16, dtype=mx.float32)
1264
+ mx.eval(input)
1265
+ in_ptr = np.asarray(input, copy=False).__array_interface__["data"][0]
1266
+
1267
+ out = compiled_fn(input)
1268
+ del input # Ensure the reference is dropped
1269
+ mx.eval(out)
1270
+
1271
+ self.assertEqual(
1272
+ np.asarray(out, copy=False).__array_interface__["data"][0], in_ptr
1273
+ )
1274
+
1275
+
1276
+ if __name__ == "__main__":
1277
+ mlx_tests.MLXTestRunner()