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.
- checksums.yaml +7 -0
- data/ext/mlx/CMakeLists.txt +7 -0
- data/ext/mlx/Makefile +273 -0
- data/ext/mlx/extconf.rb +94 -0
- data/ext/mlx/mkmf.log +44 -0
- data/ext/mlx/native.bundle +0 -0
- data/ext/mlx/native.bundle.dSYM/Contents/Info.plist +20 -0
- data/ext/mlx/native.bundle.dSYM/Contents/Resources/DWARF/native.bundle +0 -0
- data/ext/mlx/native.bundle.dSYM/Contents/Resources/Relocations/aarch64/native.bundle.yml +5 -0
- data/ext/mlx/native.cpp +8027 -0
- data/ext/mlx/native.o +0 -0
- data/lib/mlx/core.rb +1678 -0
- data/lib/mlx/distributed_utils/common.rb +116 -0
- data/lib/mlx/distributed_utils/config.rb +600 -0
- data/lib/mlx/distributed_utils/launch.rb +490 -0
- data/lib/mlx/extension.rb +24 -0
- data/lib/mlx/nn/base.rb +388 -0
- data/lib/mlx/nn/init.rb +140 -0
- data/lib/mlx/nn/layers/activations.rb +336 -0
- data/lib/mlx/nn/layers/base.rb +6 -0
- data/lib/mlx/nn/layers/containers.rb +20 -0
- data/lib/mlx/nn/layers/convolution.rb +120 -0
- data/lib/mlx/nn/layers/convolution_transpose.rb +114 -0
- data/lib/mlx/nn/layers/distributed.rb +309 -0
- data/lib/mlx/nn/layers/dropout.rb +75 -0
- data/lib/mlx/nn/layers/embedding.rb +28 -0
- data/lib/mlx/nn/layers/linear.rb +79 -0
- data/lib/mlx/nn/layers/normalization.rb +216 -0
- data/lib/mlx/nn/layers/pooling.rb +167 -0
- data/lib/mlx/nn/layers/positional_encoding.rb +126 -0
- data/lib/mlx/nn/layers/quantized.rb +215 -0
- data/lib/mlx/nn/layers/recurrent.rb +135 -0
- data/lib/mlx/nn/layers/transformer.rb +330 -0
- data/lib/mlx/nn/layers/upsample.rb +97 -0
- data/lib/mlx/nn/layers.rb +18 -0
- data/lib/mlx/nn/losses.rb +251 -0
- data/lib/mlx/nn/utils.rb +167 -0
- data/lib/mlx/nn.rb +12 -0
- data/lib/mlx/optimizers/optimizers.rb +808 -0
- data/lib/mlx/optimizers/schedulers.rb +62 -0
- data/lib/mlx/optimizers.rb +9 -0
- data/lib/mlx/utils.rb +171 -0
- data/lib/mlx/version +1 -0
- data/lib/mlx/version.rb +5 -0
- data/lib/mlx.rb +64 -0
- data/mlx/.clang-format +87 -0
- data/mlx/.git +1 -0
- data/mlx/.github/ISSUE_TEMPLATE/bug_report.md +28 -0
- data/mlx/.github/actions/build-cuda-release/action.yml +31 -0
- data/mlx/.github/actions/build-docs/action.yml +38 -0
- data/mlx/.github/actions/build-linux/action.yml +38 -0
- data/mlx/.github/actions/build-linux-release/action.yml +42 -0
- data/mlx/.github/actions/build-macos/action.yml +80 -0
- data/mlx/.github/actions/build-macos-release/action.yml +36 -0
- data/mlx/.github/actions/build-windows/action.yml +26 -0
- data/mlx/.github/actions/setup-linux/action.yml +93 -0
- data/mlx/.github/actions/setup-macos/action.yml +24 -0
- data/mlx/.github/actions/setup-windows/action.yml +42 -0
- data/mlx/.github/actions/test-linux/action.yml +69 -0
- data/mlx/.github/actions/test-windows/action.yml +20 -0
- data/mlx/.github/dependabot.yml +6 -0
- data/mlx/.github/pull_request_template.md +12 -0
- data/mlx/.github/scripts/build-sanitizer-tests.sh +48 -0
- data/mlx/.github/scripts/setup+build-cpp-linux-fedora-container.sh +27 -0
- data/mlx/.github/workflows/build_and_test.yml +152 -0
- data/mlx/.github/workflows/documentation.yml +28 -0
- data/mlx/.github/workflows/nightly.yml +104 -0
- data/mlx/.github/workflows/release.yml +256 -0
- data/mlx/.gitignore +81 -0
- data/mlx/.pre-commit-config.yaml +27 -0
- data/mlx/ACKNOWLEDGMENTS.md +268 -0
- data/mlx/CITATION.cff +24 -0
- data/mlx/CMakeLists.txt +437 -0
- data/mlx/CODE_OF_CONDUCT.md +132 -0
- data/mlx/CONTRIBUTING.md +38 -0
- data/mlx/LICENSE +21 -0
- data/mlx/MANIFEST.in +6 -0
- data/mlx/README.md +121 -0
- data/mlx/benchmarks/cpp/CMakeLists.txt +11 -0
- data/mlx/benchmarks/cpp/autograd.cpp +39 -0
- data/mlx/benchmarks/cpp/compare_devices.cpp +27 -0
- data/mlx/benchmarks/cpp/irregular_strides.cpp +201 -0
- data/mlx/benchmarks/cpp/single_ops.cpp +288 -0
- data/mlx/benchmarks/cpp/time_utils.h +39 -0
- data/mlx/benchmarks/numpy/single_ops.py +39 -0
- data/mlx/benchmarks/numpy/time_utils.py +20 -0
- data/mlx/benchmarks/python/batch_matmul_bench.py +62 -0
- data/mlx/benchmarks/python/blas/bench_gemm.py +191 -0
- data/mlx/benchmarks/python/blas/bench_gemv.py +220 -0
- data/mlx/benchmarks/python/comparative/README.md +15 -0
- data/mlx/benchmarks/python/comparative/bench_mlx.py +519 -0
- data/mlx/benchmarks/python/comparative/bench_torch.py +482 -0
- data/mlx/benchmarks/python/comparative/compare.py +284 -0
- data/mlx/benchmarks/python/compile_bench.py +107 -0
- data/mlx/benchmarks/python/conv1d_bench.py +123 -0
- data/mlx/benchmarks/python/conv2d_bench_cpu.py +127 -0
- data/mlx/benchmarks/python/conv2d_train_bench_cpu.py +143 -0
- data/mlx/benchmarks/python/conv2d_transpose_bench_cpu.py +129 -0
- data/mlx/benchmarks/python/conv3d_bench_cpu.py +110 -0
- data/mlx/benchmarks/python/conv3d_train_bench_cpu.py +143 -0
- data/mlx/benchmarks/python/conv3d_transpose_bench_cpu.py +116 -0
- data/mlx/benchmarks/python/conv_bench.py +135 -0
- data/mlx/benchmarks/python/conv_transpose_bench.py +135 -0
- data/mlx/benchmarks/python/conv_unaligned_bench.py +107 -0
- data/mlx/benchmarks/python/distributed_bench.py +66 -0
- data/mlx/benchmarks/python/einsum_bench.py +84 -0
- data/mlx/benchmarks/python/fft_bench.py +118 -0
- data/mlx/benchmarks/python/gather_bench.py +52 -0
- data/mlx/benchmarks/python/gather_mm_bench.py +74 -0
- data/mlx/benchmarks/python/gather_qmm_bench.py +84 -0
- data/mlx/benchmarks/python/hadamard_bench.py +70 -0
- data/mlx/benchmarks/python/large_gemm_bench.py +119 -0
- data/mlx/benchmarks/python/layer_norm_bench.py +82 -0
- data/mlx/benchmarks/python/masked_scatter.py +212 -0
- data/mlx/benchmarks/python/rms_norm_bench.py +63 -0
- data/mlx/benchmarks/python/rope_bench.py +35 -0
- data/mlx/benchmarks/python/scatter_bench.py +96 -0
- data/mlx/benchmarks/python/sdpa_bench.py +223 -0
- data/mlx/benchmarks/python/sdpa_vector_bench.py +95 -0
- data/mlx/benchmarks/python/single_ops.py +132 -0
- data/mlx/benchmarks/python/synchronize_bench.py +55 -0
- data/mlx/benchmarks/python/time_utils.py +38 -0
- data/mlx/cmake/FindCUDNN.cmake +177 -0
- data/mlx/cmake/FindNCCL.cmake +54 -0
- data/mlx/cmake/Findnvpl.cmake +3 -0
- data/mlx/cmake/extension.cmake +50 -0
- data/mlx/docs/.clang-format +2 -0
- data/mlx/docs/.gitignore +3 -0
- data/mlx/docs/.nojekyll +0 -0
- data/mlx/docs/Doxyfile +51 -0
- data/mlx/docs/Makefile +18 -0
- data/mlx/docs/README.md +54 -0
- data/mlx/docs/index.html +1 -0
- data/mlx/docs/requirements.txt +5 -0
- data/mlx/docs/src/_static/distributed/m3-ultra-mesh-broken.png +0 -0
- data/mlx/docs/src/_static/distributed/m3-ultra-mesh.png +0 -0
- data/mlx/docs/src/_static/metal_debugger/capture.png +0 -0
- data/mlx/docs/src/_static/metal_debugger/schema.png +0 -0
- data/mlx/docs/src/_static/mlx_logo.png +0 -0
- data/mlx/docs/src/_static/mlx_logo_dark.png +0 -0
- data/mlx/docs/src/_static/tp_inference/all-to-sharded-linear.png +0 -0
- data/mlx/docs/src/_static/tp_inference/column-row-tp.png +0 -0
- data/mlx/docs/src/_static/tp_inference/llama-transformer.png +0 -0
- data/mlx/docs/src/_static/tp_inference/sharded-to-all-linear.png +0 -0
- data/mlx/docs/src/_templates/module-base-class.rst +33 -0
- data/mlx/docs/src/_templates/nn-module-template.rst +20 -0
- data/mlx/docs/src/_templates/optimizers-template.rst +20 -0
- data/mlx/docs/src/conf.py +99 -0
- data/mlx/docs/src/cpp/ops.rst +7 -0
- data/mlx/docs/src/dev/custom_metal_kernels.rst +445 -0
- data/mlx/docs/src/dev/extensions.rst +811 -0
- data/mlx/docs/src/dev/metal_debugger.rst +68 -0
- data/mlx/docs/src/dev/metal_logging.rst +40 -0
- data/mlx/docs/src/dev/mlx_in_cpp.rst +121 -0
- data/mlx/docs/src/examples/data_parallelism.rst +91 -0
- data/mlx/docs/src/examples/linear_regression.rst +77 -0
- data/mlx/docs/src/examples/llama-inference.rst +382 -0
- data/mlx/docs/src/examples/mlp.rst +134 -0
- data/mlx/docs/src/examples/tensor_parallelism.rst +239 -0
- data/mlx/docs/src/index.rst +96 -0
- data/mlx/docs/src/install.rst +340 -0
- data/mlx/docs/src/python/array.rst +65 -0
- data/mlx/docs/src/python/cuda.rst +9 -0
- data/mlx/docs/src/python/data_types.rst +78 -0
- data/mlx/docs/src/python/devices_and_streams.rst +21 -0
- data/mlx/docs/src/python/distributed.rst +22 -0
- data/mlx/docs/src/python/export.rst +14 -0
- data/mlx/docs/src/python/fast.rst +16 -0
- data/mlx/docs/src/python/fft.rst +24 -0
- data/mlx/docs/src/python/linalg.rst +27 -0
- data/mlx/docs/src/python/memory_management.rst +16 -0
- data/mlx/docs/src/python/metal.rst +12 -0
- data/mlx/docs/src/python/nn/distributed.rst +30 -0
- data/mlx/docs/src/python/nn/functions.rst +40 -0
- data/mlx/docs/src/python/nn/init.rst +45 -0
- data/mlx/docs/src/python/nn/layers.rst +74 -0
- data/mlx/docs/src/python/nn/losses.rst +25 -0
- data/mlx/docs/src/python/nn/module.rst +38 -0
- data/mlx/docs/src/python/nn.rst +186 -0
- data/mlx/docs/src/python/ops.rst +184 -0
- data/mlx/docs/src/python/optimizers/common_optimizers.rst +22 -0
- data/mlx/docs/src/python/optimizers/optimizer.rst +23 -0
- data/mlx/docs/src/python/optimizers/schedulers.rst +15 -0
- data/mlx/docs/src/python/optimizers.rst +78 -0
- data/mlx/docs/src/python/random.rst +48 -0
- data/mlx/docs/src/python/transforms.rst +22 -0
- data/mlx/docs/src/python/tree_utils.rst +23 -0
- data/mlx/docs/src/usage/compile.rst +516 -0
- data/mlx/docs/src/usage/distributed.rst +572 -0
- data/mlx/docs/src/usage/export.rst +288 -0
- data/mlx/docs/src/usage/function_transforms.rst +191 -0
- data/mlx/docs/src/usage/indexing.rst +194 -0
- data/mlx/docs/src/usage/launching_distributed.rst +234 -0
- data/mlx/docs/src/usage/lazy_evaluation.rst +144 -0
- data/mlx/docs/src/usage/numpy.rst +124 -0
- data/mlx/docs/src/usage/quick_start.rst +67 -0
- data/mlx/docs/src/usage/saving_and_loading.rst +81 -0
- data/mlx/docs/src/usage/unified_memory.rst +78 -0
- data/mlx/docs/src/usage/using_streams.rst +18 -0
- data/mlx/examples/cmake_project/CMakeLists.txt +22 -0
- data/mlx/examples/cmake_project/README.md +26 -0
- data/mlx/examples/cmake_project/example.cpp +14 -0
- data/mlx/examples/cpp/CMakeLists.txt +12 -0
- data/mlx/examples/cpp/distributed.cpp +22 -0
- data/mlx/examples/cpp/linear_regression.cpp +54 -0
- data/mlx/examples/cpp/logistic_regression.cpp +54 -0
- data/mlx/examples/cpp/metal_capture.cpp +31 -0
- data/mlx/examples/cpp/timer.h +20 -0
- data/mlx/examples/cpp/tutorial.cpp +99 -0
- data/mlx/examples/export/CMakeLists.txt +22 -0
- data/mlx/examples/export/README.md +49 -0
- data/mlx/examples/export/eval_mlp.cpp +25 -0
- data/mlx/examples/export/eval_mlp.py +52 -0
- data/mlx/examples/export/train_mlp.cpp +35 -0
- data/mlx/examples/export/train_mlp.py +76 -0
- data/mlx/examples/extensions/CMakeLists.txt +78 -0
- data/mlx/examples/extensions/README.md +24 -0
- data/mlx/examples/extensions/axpby/axpby.cpp +306 -0
- data/mlx/examples/extensions/axpby/axpby.h +90 -0
- data/mlx/examples/extensions/axpby/axpby.metal +47 -0
- data/mlx/examples/extensions/bindings.cpp +39 -0
- data/mlx/examples/extensions/mlx_sample_extensions/__init__.py +5 -0
- data/mlx/examples/extensions/pyproject.toml +8 -0
- data/mlx/examples/extensions/requirements.txt +4 -0
- data/mlx/examples/extensions/setup.py +18 -0
- data/mlx/examples/extensions/test.py +12 -0
- data/mlx/examples/python/linear_regression.py +46 -0
- data/mlx/examples/python/logistic_regression.py +49 -0
- data/mlx/examples/python/qqmm.py +117 -0
- data/mlx/mlx/3rdparty/.clang-format +2 -0
- data/mlx/mlx/3rdparty/pocketfft.h +3581 -0
- data/mlx/mlx/CMakeLists.txt +107 -0
- data/mlx/mlx/allocator.h +75 -0
- data/mlx/mlx/api.h +29 -0
- data/mlx/mlx/array.cpp +354 -0
- data/mlx/mlx/array.h +647 -0
- data/mlx/mlx/backend/common/CMakeLists.txt +9 -0
- data/mlx/mlx/backend/common/binary.h +97 -0
- data/mlx/mlx/backend/common/broadcasting.cpp +24 -0
- data/mlx/mlx/backend/common/broadcasting.h +11 -0
- data/mlx/mlx/backend/common/buffer_cache.h +158 -0
- data/mlx/mlx/backend/common/common.cpp +305 -0
- data/mlx/mlx/backend/common/compiled.cpp +243 -0
- data/mlx/mlx/backend/common/compiled.h +77 -0
- data/mlx/mlx/backend/common/copy.h +50 -0
- data/mlx/mlx/backend/common/hadamard.h +109 -0
- data/mlx/mlx/backend/common/load.cpp +57 -0
- data/mlx/mlx/backend/common/matmul.h +67 -0
- data/mlx/mlx/backend/common/reduce.cpp +154 -0
- data/mlx/mlx/backend/common/reduce.h +59 -0
- data/mlx/mlx/backend/common/slicing.cpp +71 -0
- data/mlx/mlx/backend/common/slicing.h +20 -0
- data/mlx/mlx/backend/common/ternary.h +85 -0
- data/mlx/mlx/backend/common/unary.h +29 -0
- data/mlx/mlx/backend/common/utils.cpp +231 -0
- data/mlx/mlx/backend/common/utils.h +205 -0
- data/mlx/mlx/backend/cpu/CMakeLists.txt +88 -0
- data/mlx/mlx/backend/cpu/arange.h +28 -0
- data/mlx/mlx/backend/cpu/arg_reduce.cpp +124 -0
- data/mlx/mlx/backend/cpu/binary.cpp +269 -0
- data/mlx/mlx/backend/cpu/binary.h +517 -0
- data/mlx/mlx/backend/cpu/binary_ops.h +98 -0
- data/mlx/mlx/backend/cpu/binary_two.h +166 -0
- data/mlx/mlx/backend/cpu/cholesky.cpp +85 -0
- data/mlx/mlx/backend/cpu/compiled.cpp +357 -0
- data/mlx/mlx/backend/cpu/compiled_preamble.h +12 -0
- data/mlx/mlx/backend/cpu/conv.cpp +1351 -0
- data/mlx/mlx/backend/cpu/copy.cpp +386 -0
- data/mlx/mlx/backend/cpu/copy.h +36 -0
- data/mlx/mlx/backend/cpu/device_info.cpp +113 -0
- data/mlx/mlx/backend/cpu/device_info.h +28 -0
- data/mlx/mlx/backend/cpu/distributed.cpp +103 -0
- data/mlx/mlx/backend/cpu/eig.cpp +281 -0
- data/mlx/mlx/backend/cpu/eigh.cpp +241 -0
- data/mlx/mlx/backend/cpu/encoder.cpp +16 -0
- data/mlx/mlx/backend/cpu/encoder.h +67 -0
- data/mlx/mlx/backend/cpu/eval.cpp +40 -0
- data/mlx/mlx/backend/cpu/eval.h +12 -0
- data/mlx/mlx/backend/cpu/fft.cpp +120 -0
- data/mlx/mlx/backend/cpu/gemm.h +26 -0
- data/mlx/mlx/backend/cpu/gemms/bnns.cpp +214 -0
- data/mlx/mlx/backend/cpu/gemms/cblas.cpp +134 -0
- data/mlx/mlx/backend/cpu/gemms/simd_bf16.cpp +45 -0
- data/mlx/mlx/backend/cpu/gemms/simd_fp16.cpp +45 -0
- data/mlx/mlx/backend/cpu/gemms/simd_gemm.h +139 -0
- data/mlx/mlx/backend/cpu/hadamard.cpp +121 -0
- data/mlx/mlx/backend/cpu/indexing.cpp +854 -0
- data/mlx/mlx/backend/cpu/inverse.cpp +160 -0
- data/mlx/mlx/backend/cpu/jit_compiler.cpp +166 -0
- data/mlx/mlx/backend/cpu/jit_compiler.h +20 -0
- data/mlx/mlx/backend/cpu/lapack.h +80 -0
- data/mlx/mlx/backend/cpu/logsumexp.cpp +139 -0
- data/mlx/mlx/backend/cpu/luf.cpp +120 -0
- data/mlx/mlx/backend/cpu/make_compiled_preamble.ps1 +38 -0
- data/mlx/mlx/backend/cpu/make_compiled_preamble.sh +41 -0
- data/mlx/mlx/backend/cpu/masked_mm.cpp +608 -0
- data/mlx/mlx/backend/cpu/matmul.cpp +166 -0
- data/mlx/mlx/backend/cpu/primitives.cpp +478 -0
- data/mlx/mlx/backend/cpu/qrf.cpp +147 -0
- data/mlx/mlx/backend/cpu/quantized.cpp +1370 -0
- data/mlx/mlx/backend/cpu/reduce.cpp +587 -0
- data/mlx/mlx/backend/cpu/scan.cpp +338 -0
- data/mlx/mlx/backend/cpu/select.cpp +95 -0
- data/mlx/mlx/backend/cpu/simd/accelerate_fp16_simd.h +56 -0
- data/mlx/mlx/backend/cpu/simd/accelerate_simd.h +329 -0
- data/mlx/mlx/backend/cpu/simd/base_simd.h +319 -0
- data/mlx/mlx/backend/cpu/simd/math.h +193 -0
- data/mlx/mlx/backend/cpu/simd/neon_fp16_simd.h +212 -0
- data/mlx/mlx/backend/cpu/simd/simd.h +4 -0
- data/mlx/mlx/backend/cpu/simd/type.h +11 -0
- data/mlx/mlx/backend/cpu/slicing.h +21 -0
- data/mlx/mlx/backend/cpu/softmax.cpp +170 -0
- data/mlx/mlx/backend/cpu/sort.cpp +481 -0
- data/mlx/mlx/backend/cpu/svd.cpp +289 -0
- data/mlx/mlx/backend/cpu/ternary.h +154 -0
- data/mlx/mlx/backend/cpu/threefry.cpp +31 -0
- data/mlx/mlx/backend/cpu/threefry.h +21 -0
- data/mlx/mlx/backend/cpu/unary.cpp +238 -0
- data/mlx/mlx/backend/cpu/unary.h +281 -0
- data/mlx/mlx/backend/cpu/unary_ops.h +175 -0
- data/mlx/mlx/backend/cuda/CMakeLists.txt +265 -0
- data/mlx/mlx/backend/cuda/allocator.cpp +451 -0
- data/mlx/mlx/backend/cuda/allocator.h +94 -0
- data/mlx/mlx/backend/cuda/arange.cu +68 -0
- data/mlx/mlx/backend/cuda/arg_reduce.cu +189 -0
- data/mlx/mlx/backend/cuda/bin2h.cmake +150 -0
- data/mlx/mlx/backend/cuda/binary/CMakeLists.txt +21 -0
- data/mlx/mlx/backend/cuda/binary/add.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/arctan2.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/binary.cuh +383 -0
- data/mlx/mlx/backend/cuda/binary/bitwise_binary.cu +27 -0
- data/mlx/mlx/backend/cuda/binary/divide.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/equal.cu +15 -0
- data/mlx/mlx/backend/cuda/binary/greater.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/greater_equal.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/less.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/less_equal.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/log_add_exp.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/logical_and.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/logical_or.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/maximum.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/minimum.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/multiply.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/not_equal.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/power.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/remainder.cu +7 -0
- data/mlx/mlx/backend/cuda/binary/subtract.cu +7 -0
- data/mlx/mlx/backend/cuda/binary_two.cu +412 -0
- data/mlx/mlx/backend/cuda/compiled.cpp +357 -0
- data/mlx/mlx/backend/cuda/conv/conv.h +126 -0
- data/mlx/mlx/backend/cuda/conv/gemm_conv.cu +217 -0
- data/mlx/mlx/backend/cuda/conv/gemm_grouped_conv.cu +231 -0
- data/mlx/mlx/backend/cuda/conv.cpp +403 -0
- data/mlx/mlx/backend/cuda/copy/copy.cuh +55 -0
- data/mlx/mlx/backend/cuda/copy/copy_contiguous.cu +88 -0
- data/mlx/mlx/backend/cuda/copy/copy_general.cu +171 -0
- data/mlx/mlx/backend/cuda/copy/copy_general_dynamic.cu +118 -0
- data/mlx/mlx/backend/cuda/copy/copy_general_input.cu +229 -0
- data/mlx/mlx/backend/cuda/copy.cu +132 -0
- data/mlx/mlx/backend/cuda/cublas_utils.cpp +222 -0
- data/mlx/mlx/backend/cuda/cublas_utils.h +95 -0
- data/mlx/mlx/backend/cuda/cuda.h +21 -0
- data/mlx/mlx/backend/cuda/cuda_utils.h +90 -0
- data/mlx/mlx/backend/cuda/cudnn_utils.cpp +133 -0
- data/mlx/mlx/backend/cuda/cudnn_utils.h +187 -0
- data/mlx/mlx/backend/cuda/custom_kernel.cpp +379 -0
- data/mlx/mlx/backend/cuda/cutlass_utils.cuh +46 -0
- data/mlx/mlx/backend/cuda/delayload.cpp +80 -0
- data/mlx/mlx/backend/cuda/device/atomic_ops.cuh +63 -0
- data/mlx/mlx/backend/cuda/device/binary_ops.cuh +300 -0
- data/mlx/mlx/backend/cuda/device/cast_op.cuh +118 -0
- data/mlx/mlx/backend/cuda/device/complex.cuh +60 -0
- data/mlx/mlx/backend/cuda/device/config.h +12 -0
- data/mlx/mlx/backend/cuda/device/fp16_math.cuh +96 -0
- data/mlx/mlx/backend/cuda/device/gather.cuh +53 -0
- data/mlx/mlx/backend/cuda/device/gather_axis.cuh +65 -0
- data/mlx/mlx/backend/cuda/device/indexing.cuh +30 -0
- data/mlx/mlx/backend/cuda/device/scatter.cuh +68 -0
- data/mlx/mlx/backend/cuda/device/scatter_axis.cuh +67 -0
- data/mlx/mlx/backend/cuda/device/scatter_ops.cuh +44 -0
- data/mlx/mlx/backend/cuda/device/ternary_ops.cuh +13 -0
- data/mlx/mlx/backend/cuda/device/unary_ops.cuh +350 -0
- data/mlx/mlx/backend/cuda/device/utils.cuh +464 -0
- data/mlx/mlx/backend/cuda/device.cpp +522 -0
- data/mlx/mlx/backend/cuda/device.h +195 -0
- data/mlx/mlx/backend/cuda/device_info.cpp +232 -0
- data/mlx/mlx/backend/cuda/distributed.cu +121 -0
- data/mlx/mlx/backend/cuda/eval.cpp +66 -0
- data/mlx/mlx/backend/cuda/event.cu +415 -0
- data/mlx/mlx/backend/cuda/event.h +79 -0
- data/mlx/mlx/backend/cuda/fence.cpp +42 -0
- data/mlx/mlx/backend/cuda/gemms/cublas_gemm.cpp +233 -0
- data/mlx/mlx/backend/cuda/gemms/cublas_gemm.h +114 -0
- data/mlx/mlx/backend/cuda/gemms/cublas_gemm_batched_12_0.cpp +77 -0
- data/mlx/mlx/backend/cuda/gemms/cublas_gemm_batched_12_9.cu +329 -0
- data/mlx/mlx/backend/cuda/gemms/gemv.cu +327 -0
- data/mlx/mlx/backend/cuda/gemms/gemv.h +34 -0
- data/mlx/mlx/backend/cuda/gemms/grouped_gemm.h +25 -0
- data/mlx/mlx/backend/cuda/gemms/grouped_gemm_unaligned.cu +358 -0
- data/mlx/mlx/backend/cuda/indexing.cpp +434 -0
- data/mlx/mlx/backend/cuda/jit_module.cpp +443 -0
- data/mlx/mlx/backend/cuda/jit_module.h +120 -0
- data/mlx/mlx/backend/cuda/kernel_utils.cu +52 -0
- data/mlx/mlx/backend/cuda/kernel_utils.cuh +148 -0
- data/mlx/mlx/backend/cuda/layer_norm.cu +417 -0
- data/mlx/mlx/backend/cuda/load.cpp +60 -0
- data/mlx/mlx/backend/cuda/logsumexp.cu +161 -0
- data/mlx/mlx/backend/cuda/lru_cache.h +190 -0
- data/mlx/mlx/backend/cuda/matmul.cpp +373 -0
- data/mlx/mlx/backend/cuda/no_cuda.cpp +47 -0
- data/mlx/mlx/backend/cuda/primitives.cpp +46 -0
- data/mlx/mlx/backend/cuda/quantized/affine_quantize.cu +329 -0
- data/mlx/mlx/backend/cuda/quantized/convert_fp8.cu +19 -0
- data/mlx/mlx/backend/cuda/quantized/cublas_qqmm.cpp +206 -0
- data/mlx/mlx/backend/cuda/quantized/cublas_qqmm.h +88 -0
- data/mlx/mlx/backend/cuda/quantized/cuda_fp4.h +100 -0
- data/mlx/mlx/backend/cuda/quantized/fp_quantize.cu +496 -0
- data/mlx/mlx/backend/cuda/quantized/mxfp8_quantize.cuh +32 -0
- data/mlx/mlx/backend/cuda/quantized/no_qqmm_impl.cpp +26 -0
- data/mlx/mlx/backend/cuda/quantized/nvfp4_quantize.cuh +334 -0
- data/mlx/mlx/backend/cuda/quantized/qmv.cu +304 -0
- data/mlx/mlx/backend/cuda/quantized/qmv.h +21 -0
- data/mlx/mlx/backend/cuda/quantized/qqmm.cpp +158 -0
- data/mlx/mlx/backend/cuda/quantized/qqmm_impl.cpp +50 -0
- data/mlx/mlx/backend/cuda/quantized/qqmm_impl.h +26 -0
- data/mlx/mlx/backend/cuda/quantized/qqmm_utils.cu +227 -0
- data/mlx/mlx/backend/cuda/quantized/qqmm_utils.h +30 -0
- data/mlx/mlx/backend/cuda/quantized/quantized.cpp +85 -0
- data/mlx/mlx/backend/cuda/quantized/quantized.h +53 -0
- data/mlx/mlx/backend/cuda/quantized/quantized_utils.cuh +88 -0
- data/mlx/mlx/backend/cuda/quantized/quantized_utils.h +50 -0
- data/mlx/mlx/backend/cuda/random.cu +202 -0
- data/mlx/mlx/backend/cuda/reduce/all_reduce.cu +159 -0
- data/mlx/mlx/backend/cuda/reduce/col_reduce.cu +510 -0
- data/mlx/mlx/backend/cuda/reduce/init_reduce.cu +50 -0
- data/mlx/mlx/backend/cuda/reduce/reduce.cuh +71 -0
- data/mlx/mlx/backend/cuda/reduce/reduce_ops.cuh +211 -0
- data/mlx/mlx/backend/cuda/reduce/reduce_utils.cuh +145 -0
- data/mlx/mlx/backend/cuda/reduce/row_reduce.cu +361 -0
- data/mlx/mlx/backend/cuda/reduce.cu +73 -0
- data/mlx/mlx/backend/cuda/rms_norm.cu +536 -0
- data/mlx/mlx/backend/cuda/rope.cu +429 -0
- data/mlx/mlx/backend/cuda/scaled_dot_product_attention.cpp +681 -0
- data/mlx/mlx/backend/cuda/scaled_dot_product_attention.cu +796 -0
- data/mlx/mlx/backend/cuda/scan.cu +468 -0
- data/mlx/mlx/backend/cuda/slicing.cpp +111 -0
- data/mlx/mlx/backend/cuda/softmax.cu +162 -0
- data/mlx/mlx/backend/cuda/sort.cu +1076 -0
- data/mlx/mlx/backend/cuda/steel/defines.cuh +9 -0
- data/mlx/mlx/backend/cuda/steel/gemm.cuh +101 -0
- data/mlx/mlx/backend/cuda/steel/mma.cuh +117 -0
- data/mlx/mlx/backend/cuda/steel/tiles.cuh +450 -0
- data/mlx/mlx/backend/cuda/steel/utils.cuh +89 -0
- data/mlx/mlx/backend/cuda/ternary.cu +271 -0
- data/mlx/mlx/backend/cuda/unary/CMakeLists.txt +34 -0
- data/mlx/mlx/backend/cuda/unary/abs.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arccos.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arccosh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arcsin.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arcsinh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arctan.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/arctanh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/bitwise_invert.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/ceil.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/conjugate.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/cos.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/cosh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/erf.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/erf_inv.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/exp.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/expm1.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/floor.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/imag.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/log.cu +21 -0
- data/mlx/mlx/backend/cuda/unary/log1p.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/logical_not.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/negative.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/real.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/round.cu +18 -0
- data/mlx/mlx/backend/cuda/unary/sigmoid.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/sign.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/sin.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/sinh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/sqrt.cu +15 -0
- data/mlx/mlx/backend/cuda/unary/square.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/tan.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/tanh.cu +7 -0
- data/mlx/mlx/backend/cuda/unary/unary.cuh +224 -0
- data/mlx/mlx/backend/cuda/utils.cpp +116 -0
- data/mlx/mlx/backend/cuda/utils.h +49 -0
- data/mlx/mlx/backend/cuda/vector_types.cuh +48 -0
- data/mlx/mlx/backend/cuda/worker.cpp +79 -0
- data/mlx/mlx/backend/cuda/worker.h +55 -0
- data/mlx/mlx/backend/gpu/CMakeLists.txt +5 -0
- data/mlx/mlx/backend/gpu/copy.cpp +89 -0
- data/mlx/mlx/backend/gpu/copy.h +57 -0
- data/mlx/mlx/backend/gpu/device_info.h +36 -0
- data/mlx/mlx/backend/gpu/eval.h +18 -0
- data/mlx/mlx/backend/gpu/primitives.cpp +307 -0
- data/mlx/mlx/backend/gpu/slicing.cpp +44 -0
- data/mlx/mlx/backend/gpu/slicing.h +36 -0
- data/mlx/mlx/backend/metal/CMakeLists.txt +144 -0
- data/mlx/mlx/backend/metal/allocator.cpp +279 -0
- data/mlx/mlx/backend/metal/allocator.h +79 -0
- data/mlx/mlx/backend/metal/binary.cpp +257 -0
- data/mlx/mlx/backend/metal/binary.h +33 -0
- data/mlx/mlx/backend/metal/compiled.cpp +471 -0
- data/mlx/mlx/backend/metal/conv.cpp +1118 -0
- data/mlx/mlx/backend/metal/copy.cpp +235 -0
- data/mlx/mlx/backend/metal/custom_kernel.cpp +430 -0
- data/mlx/mlx/backend/metal/device.cpp +816 -0
- data/mlx/mlx/backend/metal/device.h +289 -0
- data/mlx/mlx/backend/metal/device_info.cpp +58 -0
- data/mlx/mlx/backend/metal/distributed.cpp +38 -0
- data/mlx/mlx/backend/metal/eval.cpp +97 -0
- data/mlx/mlx/backend/metal/event.cpp +62 -0
- data/mlx/mlx/backend/metal/fence.cpp +162 -0
- data/mlx/mlx/backend/metal/fft.cpp +807 -0
- data/mlx/mlx/backend/metal/hadamard.cpp +198 -0
- data/mlx/mlx/backend/metal/indexing.cpp +727 -0
- data/mlx/mlx/backend/metal/jit/includes.h +58 -0
- data/mlx/mlx/backend/metal/jit/indexing.h +76 -0
- data/mlx/mlx/backend/metal/jit_kernels.cpp +1118 -0
- data/mlx/mlx/backend/metal/kernels/CMakeLists.txt +193 -0
- data/mlx/mlx/backend/metal/kernels/arange.h +9 -0
- data/mlx/mlx/backend/metal/kernels/arange.metal +20 -0
- data/mlx/mlx/backend/metal/kernels/arg_reduce.metal +182 -0
- data/mlx/mlx/backend/metal/kernels/atomic.h +345 -0
- data/mlx/mlx/backend/metal/kernels/bf16.h +16 -0
- data/mlx/mlx/backend/metal/kernels/bf16_math.h +380 -0
- data/mlx/mlx/backend/metal/kernels/binary.h +199 -0
- data/mlx/mlx/backend/metal/kernels/binary.metal +109 -0
- data/mlx/mlx/backend/metal/kernels/binary_ops.h +330 -0
- data/mlx/mlx/backend/metal/kernels/binary_two.h +244 -0
- data/mlx/mlx/backend/metal/kernels/binary_two.metal +54 -0
- data/mlx/mlx/backend/metal/kernels/cexpf.h +134 -0
- data/mlx/mlx/backend/metal/kernels/complex.h +173 -0
- data/mlx/mlx/backend/metal/kernels/conv.metal +701 -0
- data/mlx/mlx/backend/metal/kernels/copy.h +276 -0
- data/mlx/mlx/backend/metal/kernels/copy.metal +75 -0
- data/mlx/mlx/backend/metal/kernels/defines.h +24 -0
- data/mlx/mlx/backend/metal/kernels/erf.h +69 -0
- data/mlx/mlx/backend/metal/kernels/expm1f.h +90 -0
- data/mlx/mlx/backend/metal/kernels/fence.metal +52 -0
- data/mlx/mlx/backend/metal/kernels/fft/radix.h +328 -0
- data/mlx/mlx/backend/metal/kernels/fft/readwrite.h +624 -0
- data/mlx/mlx/backend/metal/kernels/fft.h +486 -0
- data/mlx/mlx/backend/metal/kernels/fft.metal +67 -0
- data/mlx/mlx/backend/metal/kernels/fp4.h +48 -0
- data/mlx/mlx/backend/metal/kernels/fp8.h +80 -0
- data/mlx/mlx/backend/metal/kernels/fp_quantized.h +1850 -0
- data/mlx/mlx/backend/metal/kernels/fp_quantized.metal +153 -0
- data/mlx/mlx/backend/metal/kernels/fp_quantized_nax.h +1044 -0
- data/mlx/mlx/backend/metal/kernels/fp_quantized_nax.metal +79 -0
- data/mlx/mlx/backend/metal/kernels/gemv.metal +868 -0
- data/mlx/mlx/backend/metal/kernels/gemv_masked.h +827 -0
- data/mlx/mlx/backend/metal/kernels/gemv_masked.metal +76 -0
- data/mlx/mlx/backend/metal/kernels/hadamard.h +182 -0
- data/mlx/mlx/backend/metal/kernels/indexing/gather.h +51 -0
- data/mlx/mlx/backend/metal/kernels/indexing/gather_axis.h +44 -0
- data/mlx/mlx/backend/metal/kernels/indexing/gather_front.h +24 -0
- data/mlx/mlx/backend/metal/kernels/indexing/indexing.h +23 -0
- data/mlx/mlx/backend/metal/kernels/indexing/masked_scatter.h +41 -0
- data/mlx/mlx/backend/metal/kernels/indexing/scatter.h +59 -0
- data/mlx/mlx/backend/metal/kernels/indexing/scatter_axis.h +52 -0
- data/mlx/mlx/backend/metal/kernels/layer_norm.metal +433 -0
- data/mlx/mlx/backend/metal/kernels/logging.h +26 -0
- data/mlx/mlx/backend/metal/kernels/logsumexp.h +140 -0
- data/mlx/mlx/backend/metal/kernels/logsumexp.metal +18 -0
- data/mlx/mlx/backend/metal/kernels/quantized.h +2508 -0
- data/mlx/mlx/backend/metal/kernels/quantized.metal +144 -0
- data/mlx/mlx/backend/metal/kernels/quantized_nax.h +1705 -0
- data/mlx/mlx/backend/metal/kernels/quantized_nax.metal +106 -0
- data/mlx/mlx/backend/metal/kernels/quantized_utils.h +90 -0
- data/mlx/mlx/backend/metal/kernels/random.metal +103 -0
- data/mlx/mlx/backend/metal/kernels/reduce.h +5 -0
- data/mlx/mlx/backend/metal/kernels/reduce.metal +169 -0
- data/mlx/mlx/backend/metal/kernels/reduce_utils.h +6 -0
- data/mlx/mlx/backend/metal/kernels/reduction/ops.h +275 -0
- data/mlx/mlx/backend/metal/kernels/reduction/reduce_all.h +66 -0
- data/mlx/mlx/backend/metal/kernels/reduction/reduce_col.h +398 -0
- data/mlx/mlx/backend/metal/kernels/reduction/reduce_init.h +8 -0
- data/mlx/mlx/backend/metal/kernels/reduction/reduce_row.h +369 -0
- data/mlx/mlx/backend/metal/kernels/rms_norm.metal +391 -0
- data/mlx/mlx/backend/metal/kernels/rope.metal +229 -0
- data/mlx/mlx/backend/metal/kernels/scaled_dot_product_attention.metal +44 -0
- data/mlx/mlx/backend/metal/kernels/scan.h +514 -0
- data/mlx/mlx/backend/metal/kernels/scan.metal +109 -0
- data/mlx/mlx/backend/metal/kernels/sdpa_vector.h +394 -0
- data/mlx/mlx/backend/metal/kernels/softmax.h +190 -0
- data/mlx/mlx/backend/metal/kernels/softmax.metal +24 -0
- data/mlx/mlx/backend/metal/kernels/sort.h +719 -0
- data/mlx/mlx/backend/metal/kernels/sort.metal +80 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/attn.h +296 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.h +471 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.metal +27 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.h +481 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.metal +28 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/loader.h +264 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/mma.h +750 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/nax.h +1076 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/params.h +44 -0
- data/mlx/mlx/backend/metal/kernels/steel/attn/transforms.h +71 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/conv.h +13 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.h +176 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.metal +56 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.h +225 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.metal +47 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/loader.h +6 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_l.h +451 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_n.h +319 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/loaders/loader_general.h +381 -0
- data/mlx/mlx/backend/metal/kernels/steel/conv/params.h +62 -0
- data/mlx/mlx/backend/metal/kernels/steel/defines.h +7 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/gemm.h +295 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/gemm_nax.h +157 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.h +346 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.metal +34 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.h +219 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.metal +30 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.h +459 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.metal +59 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.h +143 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.metal +37 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.h +719 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.metal +76 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.h +266 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.metal +43 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.h +227 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.metal +76 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk_nax.h +152 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk_nax.metal +30 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/loader.h +137 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/mma.h +1146 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/nax.h +1084 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/params.h +65 -0
- data/mlx/mlx/backend/metal/kernels/steel/gemm/transforms.h +72 -0
- data/mlx/mlx/backend/metal/kernels/steel/utils/integral_constant.h +134 -0
- data/mlx/mlx/backend/metal/kernels/steel/utils/type_traits.h +55 -0
- data/mlx/mlx/backend/metal/kernels/steel/utils.h +42 -0
- data/mlx/mlx/backend/metal/kernels/ternary.h +145 -0
- data/mlx/mlx/backend/metal/kernels/ternary.metal +48 -0
- data/mlx/mlx/backend/metal/kernels/ternary_ops.h +10 -0
- data/mlx/mlx/backend/metal/kernels/unary.h +63 -0
- data/mlx/mlx/backend/metal/kernels/unary.metal +115 -0
- data/mlx/mlx/backend/metal/kernels/unary_ops.h +454 -0
- data/mlx/mlx/backend/metal/kernels/utils.h +445 -0
- data/mlx/mlx/backend/metal/kernels.h +375 -0
- data/mlx/mlx/backend/metal/logsumexp.cpp +95 -0
- data/mlx/mlx/backend/metal/make_compiled_preamble.sh +120 -0
- data/mlx/mlx/backend/metal/matmul.cpp +2572 -0
- data/mlx/mlx/backend/metal/matmul.h +144 -0
- data/mlx/mlx/backend/metal/metal.cpp +50 -0
- data/mlx/mlx/backend/metal/metal.h +25 -0
- data/mlx/mlx/backend/metal/no_metal.cpp +42 -0
- data/mlx/mlx/backend/metal/nojit_kernels.cpp +414 -0
- data/mlx/mlx/backend/metal/normalization.cpp +433 -0
- data/mlx/mlx/backend/metal/primitives.cpp +242 -0
- data/mlx/mlx/backend/metal/quantized.cpp +1651 -0
- data/mlx/mlx/backend/metal/reduce.cpp +1038 -0
- data/mlx/mlx/backend/metal/reduce.h +41 -0
- data/mlx/mlx/backend/metal/resident.cpp +100 -0
- data/mlx/mlx/backend/metal/resident.h +32 -0
- data/mlx/mlx/backend/metal/rope.cpp +165 -0
- data/mlx/mlx/backend/metal/scaled_dot_product_attention.cpp +798 -0
- data/mlx/mlx/backend/metal/scan.cpp +145 -0
- data/mlx/mlx/backend/metal/scan.h +17 -0
- data/mlx/mlx/backend/metal/slicing.cpp +99 -0
- data/mlx/mlx/backend/metal/softmax.cpp +87 -0
- data/mlx/mlx/backend/metal/sort.cpp +368 -0
- data/mlx/mlx/backend/metal/ternary.cpp +160 -0
- data/mlx/mlx/backend/metal/ternary.h +21 -0
- data/mlx/mlx/backend/metal/unary.cpp +161 -0
- data/mlx/mlx/backend/metal/unary.h +21 -0
- data/mlx/mlx/backend/metal/utils.cpp +77 -0
- data/mlx/mlx/backend/metal/utils.h +99 -0
- data/mlx/mlx/backend/no_cpu/CMakeLists.txt +7 -0
- data/mlx/mlx/backend/no_cpu/compiled.cpp +24 -0
- data/mlx/mlx/backend/no_cpu/device_info.cpp +22 -0
- data/mlx/mlx/backend/no_cpu/primitives.cpp +146 -0
- data/mlx/mlx/backend/no_gpu/CMakeLists.txt +8 -0
- data/mlx/mlx/backend/no_gpu/allocator.cpp +134 -0
- data/mlx/mlx/backend/no_gpu/apple_memory.h +16 -0
- data/mlx/mlx/backend/no_gpu/device_info.cpp +22 -0
- data/mlx/mlx/backend/no_gpu/eval.cpp +24 -0
- data/mlx/mlx/backend/no_gpu/event.cpp +53 -0
- data/mlx/mlx/backend/no_gpu/fence.cpp +54 -0
- data/mlx/mlx/backend/no_gpu/linux_memory.h +22 -0
- data/mlx/mlx/backend/no_gpu/primitives.cpp +185 -0
- data/mlx/mlx/compile.cpp +1243 -0
- data/mlx/mlx/compile.h +45 -0
- data/mlx/mlx/compile_impl.h +70 -0
- data/mlx/mlx/device.cpp +72 -0
- data/mlx/mlx/device.h +56 -0
- data/mlx/mlx/distributed/CMakeLists.txt +14 -0
- data/mlx/mlx/distributed/distributed.cpp +197 -0
- data/mlx/mlx/distributed/distributed.h +61 -0
- data/mlx/mlx/distributed/distributed_impl.h +59 -0
- data/mlx/mlx/distributed/jaccl/CMakeLists.txt +12 -0
- data/mlx/mlx/distributed/jaccl/jaccl.cpp +178 -0
- data/mlx/mlx/distributed/jaccl/jaccl.h +12 -0
- data/mlx/mlx/distributed/jaccl/mesh.cpp +451 -0
- data/mlx/mlx/distributed/jaccl/mesh.h +122 -0
- data/mlx/mlx/distributed/jaccl/no_jaccl.cpp +20 -0
- data/mlx/mlx/distributed/jaccl/ring.cpp +692 -0
- data/mlx/mlx/distributed/jaccl/ring.h +178 -0
- data/mlx/mlx/distributed/jaccl/utils.cpp +329 -0
- data/mlx/mlx/distributed/jaccl/utils.h +342 -0
- data/mlx/mlx/distributed/mpi/CMakeLists.txt +5 -0
- data/mlx/mlx/distributed/mpi/mpi.cpp +501 -0
- data/mlx/mlx/distributed/mpi/mpi.h +12 -0
- data/mlx/mlx/distributed/mpi/mpi_declarations.h +28 -0
- data/mlx/mlx/distributed/mpi/no_mpi.cpp +20 -0
- data/mlx/mlx/distributed/nccl/CMakeLists.txt +26 -0
- data/mlx/mlx/distributed/nccl/nccl.cpp +443 -0
- data/mlx/mlx/distributed/nccl/nccl.h +12 -0
- data/mlx/mlx/distributed/nccl/nccl_stub/CMakeLists.txt +1 -0
- data/mlx/mlx/distributed/nccl/nccl_stub/nccl_stubs.cpp +54 -0
- data/mlx/mlx/distributed/nccl/no_nccl.cpp +20 -0
- data/mlx/mlx/distributed/ops.cpp +186 -0
- data/mlx/mlx/distributed/ops.h +57 -0
- data/mlx/mlx/distributed/primitives.cpp +95 -0
- data/mlx/mlx/distributed/primitives.h +156 -0
- data/mlx/mlx/distributed/reduction_ops.h +38 -0
- data/mlx/mlx/distributed/ring/CMakeLists.txt +5 -0
- data/mlx/mlx/distributed/ring/no_ring.cpp +20 -0
- data/mlx/mlx/distributed/ring/ring.cpp +870 -0
- data/mlx/mlx/distributed/ring/ring.h +12 -0
- data/mlx/mlx/distributed/utils.cpp +206 -0
- data/mlx/mlx/distributed/utils.h +67 -0
- data/mlx/mlx/dtype.cpp +197 -0
- data/mlx/mlx/dtype.h +116 -0
- data/mlx/mlx/dtype_utils.cpp +42 -0
- data/mlx/mlx/dtype_utils.h +119 -0
- data/mlx/mlx/einsum.cpp +941 -0
- data/mlx/mlx/einsum.h +23 -0
- data/mlx/mlx/event.h +58 -0
- data/mlx/mlx/export.cpp +1130 -0
- data/mlx/mlx/export.h +137 -0
- data/mlx/mlx/export_impl.h +99 -0
- data/mlx/mlx/fast.cpp +941 -0
- data/mlx/mlx/fast.h +103 -0
- data/mlx/mlx/fast_primitives.h +427 -0
- data/mlx/mlx/fence.h +39 -0
- data/mlx/mlx/fft.cpp +262 -0
- data/mlx/mlx/fft.h +159 -0
- data/mlx/mlx/graph_utils.cpp +175 -0
- data/mlx/mlx/graph_utils.h +67 -0
- data/mlx/mlx/io/CMakeLists.txt +25 -0
- data/mlx/mlx/io/gguf.cpp +470 -0
- data/mlx/mlx/io/gguf.h +20 -0
- data/mlx/mlx/io/gguf_quants.cpp +164 -0
- data/mlx/mlx/io/load.cpp +397 -0
- data/mlx/mlx/io/load.h +175 -0
- data/mlx/mlx/io/no_gguf.cpp +20 -0
- data/mlx/mlx/io/no_safetensors.cpp +37 -0
- data/mlx/mlx/io/safetensors.cpp +234 -0
- data/mlx/mlx/io.h +61 -0
- data/mlx/mlx/linalg.cpp +708 -0
- data/mlx/mlx/linalg.h +115 -0
- data/mlx/mlx/memory.h +80 -0
- data/mlx/mlx/mlx.h +25 -0
- data/mlx/mlx/ops.cpp +6094 -0
- data/mlx/mlx/ops.h +1610 -0
- data/mlx/mlx/primitives.cpp +5850 -0
- data/mlx/mlx/primitives.h +2525 -0
- data/mlx/mlx/random.cpp +492 -0
- data/mlx/mlx/random.h +283 -0
- data/mlx/mlx/scheduler.cpp +73 -0
- data/mlx/mlx/scheduler.h +189 -0
- data/mlx/mlx/small_vector.h +540 -0
- data/mlx/mlx/stream.h +42 -0
- data/mlx/mlx/threadpool.h +133 -0
- data/mlx/mlx/transforms.cpp +1065 -0
- data/mlx/mlx/transforms.h +231 -0
- data/mlx/mlx/transforms_impl.h +88 -0
- data/mlx/mlx/types/bf16.h +187 -0
- data/mlx/mlx/types/complex.h +113 -0
- data/mlx/mlx/types/fp16.h +234 -0
- data/mlx/mlx/types/half_types.h +58 -0
- data/mlx/mlx/types/limits.h +70 -0
- data/mlx/mlx/utils.cpp +302 -0
- data/mlx/mlx/utils.h +174 -0
- data/mlx/mlx/version.cpp +11 -0
- data/mlx/mlx/version.h +22 -0
- data/mlx/mlx.pc.in +52 -0
- data/mlx/pyproject.toml +7 -0
- data/mlx/python/mlx/__main__.py +27 -0
- data/mlx/python/mlx/_distributed_utils/common.py +135 -0
- data/mlx/python/mlx/_distributed_utils/config.py +631 -0
- data/mlx/python/mlx/_distributed_utils/launch.py +570 -0
- data/mlx/python/mlx/_reprlib_fix.py +16 -0
- data/mlx/python/mlx/_stub_patterns.txt +36 -0
- data/mlx/python/mlx/extension.py +88 -0
- data/mlx/python/mlx/nn/__init__.py +5 -0
- data/mlx/python/mlx/nn/init.py +441 -0
- data/mlx/python/mlx/nn/layers/__init__.py +105 -0
- data/mlx/python/mlx/nn/layers/activations.py +661 -0
- data/mlx/python/mlx/nn/layers/base.py +675 -0
- data/mlx/python/mlx/nn/layers/containers.py +24 -0
- data/mlx/python/mlx/nn/layers/convolution.py +232 -0
- data/mlx/python/mlx/nn/layers/convolution_transpose.py +242 -0
- data/mlx/python/mlx/nn/layers/distributed.py +601 -0
- data/mlx/python/mlx/nn/layers/dropout.py +137 -0
- data/mlx/python/mlx/nn/layers/embedding.py +53 -0
- data/mlx/python/mlx/nn/layers/linear.py +180 -0
- data/mlx/python/mlx/nn/layers/normalization.py +363 -0
- data/mlx/python/mlx/nn/layers/pooling.py +398 -0
- data/mlx/python/mlx/nn/layers/positional_encoding.py +162 -0
- data/mlx/python/mlx/nn/layers/quantized.py +426 -0
- data/mlx/python/mlx/nn/layers/recurrent.py +289 -0
- data/mlx/python/mlx/nn/layers/transformer.py +354 -0
- data/mlx/python/mlx/nn/layers/upsample.py +277 -0
- data/mlx/python/mlx/nn/losses.py +610 -0
- data/mlx/python/mlx/nn/utils.py +165 -0
- data/mlx/python/mlx/optimizers/__init__.py +4 -0
- data/mlx/python/mlx/optimizers/optimizers.py +976 -0
- data/mlx/python/mlx/optimizers/schedulers.py +158 -0
- data/mlx/python/mlx/py.typed +1 -0
- data/mlx/python/mlx/utils.py +325 -0
- data/mlx/python/src/CMakeLists.txt +96 -0
- data/mlx/python/src/array.cpp +1525 -0
- data/mlx/python/src/buffer.h +124 -0
- data/mlx/python/src/constants.cpp +15 -0
- data/mlx/python/src/convert.cpp +504 -0
- data/mlx/python/src/convert.h +50 -0
- data/mlx/python/src/cuda.cpp +19 -0
- data/mlx/python/src/device.cpp +98 -0
- data/mlx/python/src/distributed.cpp +352 -0
- data/mlx/python/src/export.cpp +356 -0
- data/mlx/python/src/fast.cpp +627 -0
- data/mlx/python/src/fft.cpp +514 -0
- data/mlx/python/src/indexing.cpp +1016 -0
- data/mlx/python/src/indexing.h +41 -0
- data/mlx/python/src/linalg.cpp +663 -0
- data/mlx/python/src/load.cpp +531 -0
- data/mlx/python/src/load.h +51 -0
- data/mlx/python/src/memory.cpp +125 -0
- data/mlx/python/src/metal.cpp +98 -0
- data/mlx/python/src/mlx.cpp +51 -0
- data/mlx/python/src/mlx_func.cpp +116 -0
- data/mlx/python/src/mlx_func.h +31 -0
- data/mlx/python/src/ops.cpp +5545 -0
- data/mlx/python/src/random.cpp +516 -0
- data/mlx/python/src/small_vector.h +76 -0
- data/mlx/python/src/stream.cpp +147 -0
- data/mlx/python/src/transforms.cpp +1542 -0
- data/mlx/python/src/trees.cpp +311 -0
- data/mlx/python/src/trees.h +62 -0
- data/mlx/python/src/utils.cpp +98 -0
- data/mlx/python/src/utils.h +78 -0
- data/mlx/python/tests/__main__.py +5 -0
- data/mlx/python/tests/cuda_skip.py +62 -0
- data/mlx/python/tests/mlx_distributed_tests.py +314 -0
- data/mlx/python/tests/mlx_tests.py +116 -0
- data/mlx/python/tests/mpi_test_distributed.py +142 -0
- data/mlx/python/tests/nccl_test_distributed.py +52 -0
- data/mlx/python/tests/ring_test_distributed.py +131 -0
- data/mlx/python/tests/test_array.py +2139 -0
- data/mlx/python/tests/test_autograd.py +880 -0
- data/mlx/python/tests/test_bf16.py +196 -0
- data/mlx/python/tests/test_blas.py +1429 -0
- data/mlx/python/tests/test_compile.py +1277 -0
- data/mlx/python/tests/test_constants.py +41 -0
- data/mlx/python/tests/test_conv.py +1198 -0
- data/mlx/python/tests/test_conv_transpose.py +810 -0
- data/mlx/python/tests/test_device.py +150 -0
- data/mlx/python/tests/test_double.py +306 -0
- data/mlx/python/tests/test_einsum.py +363 -0
- data/mlx/python/tests/test_eval.py +200 -0
- data/mlx/python/tests/test_export_import.py +614 -0
- data/mlx/python/tests/test_fast.py +923 -0
- data/mlx/python/tests/test_fast_sdpa.py +647 -0
- data/mlx/python/tests/test_fft.py +323 -0
- data/mlx/python/tests/test_graph.py +37 -0
- data/mlx/python/tests/test_init.py +139 -0
- data/mlx/python/tests/test_linalg.py +621 -0
- data/mlx/python/tests/test_load.py +447 -0
- data/mlx/python/tests/test_losses.py +427 -0
- data/mlx/python/tests/test_memory.py +77 -0
- data/mlx/python/tests/test_nn.py +1986 -0
- data/mlx/python/tests/test_ops.py +3261 -0
- data/mlx/python/tests/test_optimizers.py +584 -0
- data/mlx/python/tests/test_quantized.py +1160 -0
- data/mlx/python/tests/test_random.py +392 -0
- data/mlx/python/tests/test_reduce.py +223 -0
- data/mlx/python/tests/test_tree.py +96 -0
- data/mlx/python/tests/test_upsample.py +100 -0
- data/mlx/python/tests/test_vmap.py +860 -0
- data/mlx/setup.py +315 -0
- data/mlx/tests/CMakeLists.txt +44 -0
- data/mlx/tests/allocator_tests.cpp +41 -0
- data/mlx/tests/arg_reduce_tests.cpp +204 -0
- data/mlx/tests/array_tests.cpp +663 -0
- data/mlx/tests/autograd_tests.cpp +1399 -0
- data/mlx/tests/blas_tests.cpp +110 -0
- data/mlx/tests/compile_tests.cpp +818 -0
- data/mlx/tests/creations_tests.cpp +239 -0
- data/mlx/tests/custom_vjp_tests.cpp +55 -0
- data/mlx/tests/device_tests.cpp +35 -0
- data/mlx/tests/einsum_tests.cpp +85 -0
- data/mlx/tests/eval_tests.cpp +93 -0
- data/mlx/tests/export_import_tests.cpp +164 -0
- data/mlx/tests/fft_tests.cpp +366 -0
- data/mlx/tests/gpu_tests.cpp +523 -0
- data/mlx/tests/linalg_tests.cpp +639 -0
- data/mlx/tests/load_tests.cpp +270 -0
- data/mlx/tests/ops_tests.cpp +4159 -0
- data/mlx/tests/random_tests.cpp +716 -0
- data/mlx/tests/scheduler_tests.cpp +121 -0
- data/mlx/tests/tests.cpp +26 -0
- data/mlx/tests/utils_tests.cpp +67 -0
- data/mlx/tests/vmap_tests.cpp +547 -0
- metadata +958 -0
|
@@ -0,0 +1,2139 @@
|
|
|
1
|
+
# Copyright © 2023-2024 Apple Inc.
|
|
2
|
+
|
|
3
|
+
import gc
|
|
4
|
+
import operator
|
|
5
|
+
import os
|
|
6
|
+
import pickle
|
|
7
|
+
import platform
|
|
8
|
+
import sys
|
|
9
|
+
import unittest
|
|
10
|
+
import weakref
|
|
11
|
+
from copy import copy, deepcopy
|
|
12
|
+
from itertools import permutations
|
|
13
|
+
|
|
14
|
+
if platform.system() == "Windows":
|
|
15
|
+
import psutil
|
|
16
|
+
else:
|
|
17
|
+
import resource
|
|
18
|
+
|
|
19
|
+
import mlx.core as mx
|
|
20
|
+
import mlx_tests
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
import tensorflow as tf
|
|
25
|
+
|
|
26
|
+
has_tf = True
|
|
27
|
+
except ImportError as e:
|
|
28
|
+
has_tf = False
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TestVersion(mlx_tests.MLXTestCase):
|
|
32
|
+
def test_version(self):
|
|
33
|
+
v = mx.__version__
|
|
34
|
+
vnums = v.split(".")
|
|
35
|
+
self.assertGreaterEqual(len(vnums), 3)
|
|
36
|
+
v = ".".join(str(int(vn)) for vn in vnums[:3])
|
|
37
|
+
self.assertEqual(v, mx.__version__[: len(v)])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TestDtypes(mlx_tests.MLXTestCase):
|
|
41
|
+
def test_dtypes(self):
|
|
42
|
+
self.assertEqual(mx.bool_.size, 1)
|
|
43
|
+
self.assertEqual(mx.uint8.size, 1)
|
|
44
|
+
self.assertEqual(mx.uint16.size, 2)
|
|
45
|
+
self.assertEqual(mx.uint32.size, 4)
|
|
46
|
+
self.assertEqual(mx.uint64.size, 8)
|
|
47
|
+
self.assertEqual(mx.int8.size, 1)
|
|
48
|
+
self.assertEqual(mx.int16.size, 2)
|
|
49
|
+
self.assertEqual(mx.int32.size, 4)
|
|
50
|
+
self.assertEqual(mx.int64.size, 8)
|
|
51
|
+
self.assertEqual(mx.float16.size, 2)
|
|
52
|
+
self.assertEqual(mx.float32.size, 4)
|
|
53
|
+
self.assertEqual(mx.bfloat16.size, 2)
|
|
54
|
+
self.assertEqual(mx.complex64.size, 8)
|
|
55
|
+
|
|
56
|
+
self.assertEqual(str(mx.bool_), "mlx.core.bool")
|
|
57
|
+
self.assertEqual(str(mx.uint8), "mlx.core.uint8")
|
|
58
|
+
self.assertEqual(str(mx.uint16), "mlx.core.uint16")
|
|
59
|
+
self.assertEqual(str(mx.uint32), "mlx.core.uint32")
|
|
60
|
+
self.assertEqual(str(mx.uint64), "mlx.core.uint64")
|
|
61
|
+
self.assertEqual(str(mx.int8), "mlx.core.int8")
|
|
62
|
+
self.assertEqual(str(mx.int16), "mlx.core.int16")
|
|
63
|
+
self.assertEqual(str(mx.int32), "mlx.core.int32")
|
|
64
|
+
self.assertEqual(str(mx.int64), "mlx.core.int64")
|
|
65
|
+
self.assertEqual(str(mx.float16), "mlx.core.float16")
|
|
66
|
+
self.assertEqual(str(mx.float32), "mlx.core.float32")
|
|
67
|
+
self.assertEqual(str(mx.bfloat16), "mlx.core.bfloat16")
|
|
68
|
+
self.assertEqual(str(mx.complex64), "mlx.core.complex64")
|
|
69
|
+
|
|
70
|
+
def test_scalar_conversion(self):
|
|
71
|
+
dtypes = [
|
|
72
|
+
"uint8",
|
|
73
|
+
"uint16",
|
|
74
|
+
"uint32",
|
|
75
|
+
"uint64",
|
|
76
|
+
"int8",
|
|
77
|
+
"int16",
|
|
78
|
+
"int32",
|
|
79
|
+
"int64",
|
|
80
|
+
"float16",
|
|
81
|
+
"float32",
|
|
82
|
+
"complex64",
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
for dtype in dtypes:
|
|
86
|
+
with self.subTest(dtype=dtype):
|
|
87
|
+
x = np.array(2, dtype=getattr(np, dtype))
|
|
88
|
+
y = np.min(x)
|
|
89
|
+
|
|
90
|
+
self.assertEqual(x.dtype, y.dtype)
|
|
91
|
+
self.assertTupleEqual(x.shape, y.shape)
|
|
92
|
+
|
|
93
|
+
z = mx.array(y)
|
|
94
|
+
self.assertEqual(np.array(z), x)
|
|
95
|
+
self.assertEqual(np.array(z), y)
|
|
96
|
+
self.assertEqual(z.dtype, getattr(mx, dtype))
|
|
97
|
+
self.assertListEqual(list(z.shape), list(x.shape))
|
|
98
|
+
self.assertListEqual(list(z.shape), list(y.shape))
|
|
99
|
+
|
|
100
|
+
def test_finfo(self):
|
|
101
|
+
with self.assertRaises(ValueError):
|
|
102
|
+
mx.finfo(mx.int32)
|
|
103
|
+
|
|
104
|
+
self.assertEqual(mx.finfo(mx.float32).min, np.finfo(np.float32).min)
|
|
105
|
+
self.assertEqual(mx.finfo(mx.float32).max, np.finfo(np.float32).max)
|
|
106
|
+
self.assertEqual(mx.finfo(mx.float32).eps, np.finfo(np.float32).eps)
|
|
107
|
+
self.assertEqual(mx.finfo(mx.float32).dtype, mx.float32)
|
|
108
|
+
|
|
109
|
+
self.assertEqual(mx.finfo(mx.float16).min, np.finfo(np.float16).min)
|
|
110
|
+
self.assertEqual(mx.finfo(mx.float16).max, np.finfo(np.float16).max)
|
|
111
|
+
self.assertEqual(mx.finfo(mx.float16).eps, np.finfo(np.float16).eps)
|
|
112
|
+
self.assertEqual(mx.finfo(mx.float16).dtype, mx.float16)
|
|
113
|
+
|
|
114
|
+
def test_iinfo(self):
|
|
115
|
+
with self.assertRaises(ValueError):
|
|
116
|
+
mx.iinfo(mx.float32)
|
|
117
|
+
|
|
118
|
+
self.assertEqual(mx.iinfo(mx.int32).min, np.iinfo(np.int32).min)
|
|
119
|
+
self.assertEqual(mx.iinfo(mx.int32).max, np.iinfo(np.int32).max)
|
|
120
|
+
self.assertEqual(mx.iinfo(mx.int32).dtype, mx.int32)
|
|
121
|
+
|
|
122
|
+
self.assertEqual(mx.iinfo(mx.uint32).min, np.iinfo(np.uint32).min)
|
|
123
|
+
self.assertEqual(mx.iinfo(mx.uint32).max, np.iinfo(np.uint32).max)
|
|
124
|
+
self.assertEqual(mx.iinfo(mx.int8).dtype, mx.int8)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class TestEquality(mlx_tests.MLXTestCase):
|
|
128
|
+
def test_array_eq_array(self):
|
|
129
|
+
a = mx.array([1, 2, 3])
|
|
130
|
+
b = mx.array([1, 2, 3])
|
|
131
|
+
c = mx.array([1, 2, 4])
|
|
132
|
+
self.assertTrue(mx.all(a == b))
|
|
133
|
+
self.assertFalse(mx.all(a == c))
|
|
134
|
+
|
|
135
|
+
def test_array_eq_scalar(self):
|
|
136
|
+
a = mx.array([1, 2, 3])
|
|
137
|
+
b = 1
|
|
138
|
+
c = 4
|
|
139
|
+
d = 2.5
|
|
140
|
+
e = mx.array([1, 2.5, 3.25])
|
|
141
|
+
self.assertTrue(mx.any(a == b))
|
|
142
|
+
self.assertFalse(mx.all(a == c))
|
|
143
|
+
self.assertFalse(mx.all(a == d))
|
|
144
|
+
self.assertTrue(mx.any(a == e))
|
|
145
|
+
|
|
146
|
+
def test_list_equals_array(self):
|
|
147
|
+
a = mx.array([1, 2, 3])
|
|
148
|
+
b = [1, 2, 3]
|
|
149
|
+
c = [1, 2, 4]
|
|
150
|
+
|
|
151
|
+
# mlx array equality returns false if is compared with any kind of
|
|
152
|
+
# object which is not an mlx array
|
|
153
|
+
self.assertFalse(a == b)
|
|
154
|
+
self.assertFalse(a == c)
|
|
155
|
+
|
|
156
|
+
def test_tuple_equals_array(self):
|
|
157
|
+
a = mx.array([1, 2, 3])
|
|
158
|
+
b = (1, 2, 3)
|
|
159
|
+
c = (1, 2, 4)
|
|
160
|
+
|
|
161
|
+
# mlx array equality returns false if is compared with any kind of
|
|
162
|
+
# object which is not an mlx array
|
|
163
|
+
self.assertFalse(a == b)
|
|
164
|
+
self.assertFalse(a == c)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class TestInequality(mlx_tests.MLXTestCase):
|
|
168
|
+
def test_array_ne_array(self):
|
|
169
|
+
a = mx.array([1, 2, 3])
|
|
170
|
+
b = mx.array([1, 2, 3])
|
|
171
|
+
c = mx.array([1, 2, 4])
|
|
172
|
+
self.assertFalse(mx.any(a != b))
|
|
173
|
+
self.assertTrue(mx.any(a != c))
|
|
174
|
+
|
|
175
|
+
def test_array_ne_scalar(self):
|
|
176
|
+
a = mx.array([1, 2, 3])
|
|
177
|
+
b = 1
|
|
178
|
+
c = 4
|
|
179
|
+
d = 1.5
|
|
180
|
+
e = 2.5
|
|
181
|
+
f = mx.array([1, 2.5, 3.25])
|
|
182
|
+
self.assertFalse(mx.all(a != b))
|
|
183
|
+
self.assertTrue(mx.any(a != c))
|
|
184
|
+
self.assertTrue(mx.any(a != d))
|
|
185
|
+
self.assertTrue(mx.any(a != e))
|
|
186
|
+
self.assertFalse(mx.all(a != f))
|
|
187
|
+
|
|
188
|
+
def test_list_not_equals_array(self):
|
|
189
|
+
a = mx.array([1, 2, 3])
|
|
190
|
+
b = [1, 2, 3]
|
|
191
|
+
c = [1, 2, 4]
|
|
192
|
+
|
|
193
|
+
# mlx array inequality returns true if is compared with any kind of
|
|
194
|
+
# object which is not an mlx array
|
|
195
|
+
self.assertTrue(a != b)
|
|
196
|
+
self.assertTrue(a != c)
|
|
197
|
+
|
|
198
|
+
def test_dlx_device_type(self):
|
|
199
|
+
a = mx.array([1, 2, 3])
|
|
200
|
+
device_type, device_id = a.__dlpack_device__()
|
|
201
|
+
self.assertIn(device_type, [1, 8, 13])
|
|
202
|
+
self.assertEqual(device_id, 0)
|
|
203
|
+
|
|
204
|
+
if device_type == 8:
|
|
205
|
+
# Additional check if Metal is supposed to be available
|
|
206
|
+
self.assertTrue(mx.metal.is_available())
|
|
207
|
+
elif device_type == 1:
|
|
208
|
+
# Additional check if CPU is the fallback
|
|
209
|
+
self.assertFalse(mx.metal.is_available())
|
|
210
|
+
|
|
211
|
+
def test_tuple_not_equals_array(self):
|
|
212
|
+
a = mx.array([1, 2, 3])
|
|
213
|
+
b = (1, 2, 3)
|
|
214
|
+
c = (1, 2, 4)
|
|
215
|
+
|
|
216
|
+
# mlx array inequality returns true if is compared with any kind of
|
|
217
|
+
# object which is not an mlx array
|
|
218
|
+
self.assertTrue(a != b)
|
|
219
|
+
self.assertTrue(a != c)
|
|
220
|
+
|
|
221
|
+
def test_obj_inequality_array(self):
|
|
222
|
+
str_ = "hello"
|
|
223
|
+
a = mx.array([1, 2, 3])
|
|
224
|
+
lst_ = [1, 2, 3]
|
|
225
|
+
tpl_ = (1, 2, 3)
|
|
226
|
+
|
|
227
|
+
# check if object comparison(</>/<=/>=) with mlx array should throw an exception
|
|
228
|
+
# if not, the tests will fail
|
|
229
|
+
with self.assertRaises(ValueError):
|
|
230
|
+
a < str_
|
|
231
|
+
with self.assertRaises(ValueError):
|
|
232
|
+
a > str_
|
|
233
|
+
with self.assertRaises(ValueError):
|
|
234
|
+
a <= str_
|
|
235
|
+
with self.assertRaises(ValueError):
|
|
236
|
+
a >= str_
|
|
237
|
+
with self.assertRaises(ValueError):
|
|
238
|
+
a < lst_
|
|
239
|
+
with self.assertRaises(ValueError):
|
|
240
|
+
a > lst_
|
|
241
|
+
with self.assertRaises(ValueError):
|
|
242
|
+
a <= lst_
|
|
243
|
+
with self.assertRaises(ValueError):
|
|
244
|
+
a >= lst_
|
|
245
|
+
with self.assertRaises(ValueError):
|
|
246
|
+
a < tpl_
|
|
247
|
+
with self.assertRaises(ValueError):
|
|
248
|
+
a > tpl_
|
|
249
|
+
with self.assertRaises(ValueError):
|
|
250
|
+
a <= tpl_
|
|
251
|
+
with self.assertRaises(ValueError):
|
|
252
|
+
a >= tpl_
|
|
253
|
+
|
|
254
|
+
def test_invalid_op_on_array(self):
|
|
255
|
+
str_ = "hello"
|
|
256
|
+
a = mx.array([1, 2.5, 3.25])
|
|
257
|
+
lst_ = [1, 2.1, 3.25]
|
|
258
|
+
tpl_ = (1, 2.5, 3.25)
|
|
259
|
+
|
|
260
|
+
with self.assertRaises(ValueError):
|
|
261
|
+
a * str_
|
|
262
|
+
with self.assertRaises(ValueError):
|
|
263
|
+
a *= str_
|
|
264
|
+
with self.assertRaises(ValueError):
|
|
265
|
+
a /= lst_
|
|
266
|
+
with self.assertRaises(ValueError):
|
|
267
|
+
a // lst_
|
|
268
|
+
with self.assertRaises(ValueError):
|
|
269
|
+
a % lst_
|
|
270
|
+
with self.assertRaises(ValueError):
|
|
271
|
+
a**tpl_
|
|
272
|
+
with self.assertRaises(ValueError):
|
|
273
|
+
a & tpl_
|
|
274
|
+
with self.assertRaises(ValueError):
|
|
275
|
+
a | str_
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class TestArray(mlx_tests.MLXTestCase):
|
|
279
|
+
def test_array_basics(self):
|
|
280
|
+
x = mx.array(1)
|
|
281
|
+
self.assertEqual(x.size, 1)
|
|
282
|
+
self.assertEqual(x.ndim, 0)
|
|
283
|
+
self.assertEqual(x.itemsize, 4)
|
|
284
|
+
self.assertEqual(x.nbytes, 4)
|
|
285
|
+
self.assertEqual(x.shape, ())
|
|
286
|
+
self.assertEqual(x.dtype, mx.int32)
|
|
287
|
+
self.assertEqual(x.item(), 1)
|
|
288
|
+
self.assertTrue(isinstance(x.item(), int))
|
|
289
|
+
|
|
290
|
+
with self.assertRaises(TypeError):
|
|
291
|
+
len(x)
|
|
292
|
+
|
|
293
|
+
x = mx.array(1, mx.uint32)
|
|
294
|
+
self.assertEqual(x.item(), 1)
|
|
295
|
+
self.assertTrue(isinstance(x.item(), int))
|
|
296
|
+
|
|
297
|
+
x = mx.array(1, mx.int64)
|
|
298
|
+
self.assertEqual(x.item(), 1)
|
|
299
|
+
self.assertTrue(isinstance(x.item(), int))
|
|
300
|
+
|
|
301
|
+
x = mx.array(1, mx.bfloat16)
|
|
302
|
+
self.assertEqual(x.item(), 1.0)
|
|
303
|
+
|
|
304
|
+
x = mx.array(1.0)
|
|
305
|
+
self.assertEqual(x.size, 1)
|
|
306
|
+
self.assertEqual(x.ndim, 0)
|
|
307
|
+
self.assertEqual(x.shape, ())
|
|
308
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
309
|
+
self.assertEqual(x.item(), 1.0)
|
|
310
|
+
self.assertTrue(isinstance(x.item(), float))
|
|
311
|
+
|
|
312
|
+
x = mx.array(False)
|
|
313
|
+
self.assertEqual(x.size, 1)
|
|
314
|
+
self.assertEqual(x.ndim, 0)
|
|
315
|
+
self.assertEqual(x.shape, ())
|
|
316
|
+
self.assertEqual(x.dtype, mx.bool_)
|
|
317
|
+
self.assertEqual(x.item(), False)
|
|
318
|
+
self.assertTrue(isinstance(x.item(), bool))
|
|
319
|
+
|
|
320
|
+
x = mx.array(complex(1, 1))
|
|
321
|
+
self.assertEqual(x.ndim, 0)
|
|
322
|
+
self.assertEqual(x.shape, ())
|
|
323
|
+
self.assertEqual(x.dtype, mx.complex64)
|
|
324
|
+
self.assertEqual(x.item(), complex(1, 1))
|
|
325
|
+
self.assertTrue(isinstance(x.item(), complex))
|
|
326
|
+
|
|
327
|
+
x = mx.array([True, False, True])
|
|
328
|
+
self.assertEqual(x.dtype, mx.bool_)
|
|
329
|
+
self.assertEqual(x.ndim, 1)
|
|
330
|
+
self.assertEqual(x.shape, (3,))
|
|
331
|
+
self.assertEqual(len(x), 3)
|
|
332
|
+
|
|
333
|
+
x = mx.array([True, False, True], mx.float32)
|
|
334
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
335
|
+
|
|
336
|
+
x = mx.array([0, 1, 2])
|
|
337
|
+
self.assertEqual(x.dtype, mx.int32)
|
|
338
|
+
self.assertEqual(x.ndim, 1)
|
|
339
|
+
self.assertEqual(x.shape, (3,))
|
|
340
|
+
|
|
341
|
+
x = mx.array([0, 1, 2], mx.float32)
|
|
342
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
343
|
+
|
|
344
|
+
x = mx.array([0.0, 1.0, 2.0])
|
|
345
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
346
|
+
self.assertEqual(x.ndim, 1)
|
|
347
|
+
self.assertEqual(x.shape, (3,))
|
|
348
|
+
|
|
349
|
+
x = mx.array([1j, 1 + 0j])
|
|
350
|
+
self.assertEqual(x.dtype, mx.complex64)
|
|
351
|
+
self.assertEqual(x.ndim, 1)
|
|
352
|
+
self.assertEqual(x.shape, (2,))
|
|
353
|
+
|
|
354
|
+
# From tuple
|
|
355
|
+
x = mx.array((1, 2, 3), mx.int32)
|
|
356
|
+
self.assertEqual(x.dtype, mx.int32)
|
|
357
|
+
self.assertEqual(x.tolist(), [1, 2, 3])
|
|
358
|
+
|
|
359
|
+
def test_bool_conversion(self):
|
|
360
|
+
x = mx.array(True)
|
|
361
|
+
self.assertTrue(x)
|
|
362
|
+
x = mx.array(False)
|
|
363
|
+
self.assertFalse(x)
|
|
364
|
+
x = mx.array(1.0)
|
|
365
|
+
self.assertTrue(x)
|
|
366
|
+
x = mx.array(0.0)
|
|
367
|
+
self.assertFalse(x)
|
|
368
|
+
|
|
369
|
+
def test_int_type(self):
|
|
370
|
+
x = mx.array(1)
|
|
371
|
+
self.assertTrue(x.dtype == mx.int32)
|
|
372
|
+
x = mx.array(2**32 - 1)
|
|
373
|
+
self.assertTrue(x.dtype == mx.int64)
|
|
374
|
+
x = mx.array(2**40)
|
|
375
|
+
self.assertTrue(x.dtype == mx.int64)
|
|
376
|
+
x = mx.array(2**32 - 1, dtype=mx.uint32)
|
|
377
|
+
self.assertTrue(x.dtype == mx.uint32)
|
|
378
|
+
x = mx.array([1, 2], dtype=mx.int64) + 0x80000000
|
|
379
|
+
self.assertTrue(x.dtype == mx.int64)
|
|
380
|
+
|
|
381
|
+
def test_construction_from_lists(self):
|
|
382
|
+
x = mx.array([])
|
|
383
|
+
self.assertEqual(x.size, 0)
|
|
384
|
+
self.assertEqual(x.shape, (0,))
|
|
385
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
386
|
+
|
|
387
|
+
x = mx.array([[], [], []])
|
|
388
|
+
self.assertEqual(x.size, 0)
|
|
389
|
+
self.assertEqual(x.shape, (3, 0))
|
|
390
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
391
|
+
|
|
392
|
+
x = mx.array([[[], []], [[], []], [[], []]])
|
|
393
|
+
self.assertEqual(x.size, 0)
|
|
394
|
+
self.assertEqual(x.shape, (3, 2, 0))
|
|
395
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
396
|
+
|
|
397
|
+
# Check failure cases
|
|
398
|
+
with self.assertRaises(ValueError):
|
|
399
|
+
x = mx.array([[[], []], [[]], [[], []]])
|
|
400
|
+
|
|
401
|
+
with self.assertRaises(ValueError):
|
|
402
|
+
x = mx.array([[[], []], [[1.0, 2.0], []], [[], []]])
|
|
403
|
+
|
|
404
|
+
with self.assertRaises(ValueError):
|
|
405
|
+
x = mx.array([[0, 1], [[0, 1], 1]])
|
|
406
|
+
|
|
407
|
+
with self.assertRaises(ValueError):
|
|
408
|
+
x = mx.array([[0, 1], ["hello", 1]])
|
|
409
|
+
|
|
410
|
+
x = mx.array([True, False, 3])
|
|
411
|
+
self.assertEqual(x.dtype, mx.int32)
|
|
412
|
+
|
|
413
|
+
x = mx.array([True, False, 3, 4.0])
|
|
414
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
415
|
+
|
|
416
|
+
x = mx.array([[True, False], [1, 3], [2, 4.0]])
|
|
417
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
418
|
+
|
|
419
|
+
x = mx.array([[1.0, 2.0], [0.0, 3.9]], mx.bool_)
|
|
420
|
+
self.assertEqual(x.dtype, mx.bool_)
|
|
421
|
+
self.assertTrue(mx.array_equal(x, mx.array([[True, True], [False, True]])))
|
|
422
|
+
|
|
423
|
+
x = mx.array([[1.0, 2.0], [0.0, 3.9]], mx.int32)
|
|
424
|
+
self.assertTrue(mx.array_equal(x, mx.array([[1, 2], [0, 3]])))
|
|
425
|
+
|
|
426
|
+
x = mx.array([1 + 0j, 2j, True, 0], mx.complex64)
|
|
427
|
+
self.assertEqual(x.tolist(), [1 + 0j, 2j, 1 + 0j, 0j])
|
|
428
|
+
|
|
429
|
+
xnp = np.array([0, 4294967295], dtype=np.uint32)
|
|
430
|
+
x = mx.array([0, 4294967295], dtype=mx.uint32)
|
|
431
|
+
self.assertTrue(np.array_equal(x, xnp))
|
|
432
|
+
|
|
433
|
+
xnp = np.array([0, 4294967295], dtype=np.float32)
|
|
434
|
+
x = mx.array([0, 4294967295], dtype=mx.float32)
|
|
435
|
+
self.assertTrue(np.array_equal(x, xnp))
|
|
436
|
+
|
|
437
|
+
def test_double_keeps_precision(self):
|
|
438
|
+
x = 39.14223403241
|
|
439
|
+
out = mx.array(x, dtype=mx.float64).item()
|
|
440
|
+
self.assertEqual(out, x)
|
|
441
|
+
|
|
442
|
+
out = mx.array([x], dtype=mx.float64).item()
|
|
443
|
+
self.assertEqual(out, x)
|
|
444
|
+
|
|
445
|
+
def test_construction_from_lists_of_mlx_arrays(self):
|
|
446
|
+
dtypes = [
|
|
447
|
+
mx.bool_,
|
|
448
|
+
mx.uint8,
|
|
449
|
+
mx.uint16,
|
|
450
|
+
mx.uint32,
|
|
451
|
+
mx.uint64,
|
|
452
|
+
mx.int8,
|
|
453
|
+
mx.int16,
|
|
454
|
+
mx.int32,
|
|
455
|
+
mx.int64,
|
|
456
|
+
mx.float16,
|
|
457
|
+
mx.float32,
|
|
458
|
+
mx.bfloat16,
|
|
459
|
+
mx.complex64,
|
|
460
|
+
]
|
|
461
|
+
for x_t, y_t in permutations(dtypes, 2):
|
|
462
|
+
# check type promotion and numeric correctness
|
|
463
|
+
x, y = mx.array([1.0], x_t), mx.array([2.0], y_t)
|
|
464
|
+
z = mx.array([x, y])
|
|
465
|
+
expected = mx.stack([x, y], axis=0)
|
|
466
|
+
self.assertEqualArray(z, expected)
|
|
467
|
+
|
|
468
|
+
# check heterogeneous construction with mlx arrays and python primitive types
|
|
469
|
+
x, y = mx.array([True], x_t), mx.array([False], y_t)
|
|
470
|
+
z = mx.array([[x, [2.0]], [[3.0], y]])
|
|
471
|
+
expected = mx.array([[[x.item()], [2.0]], [[3.0], [y.item()]]], z.dtype)
|
|
472
|
+
self.assertEqualArray(z, expected)
|
|
473
|
+
|
|
474
|
+
# check when create from an array which does not contain memory to the raw data
|
|
475
|
+
x = mx.array([1.0]).astype(mx.bfloat16) # x does not hold raw data
|
|
476
|
+
for y_t in dtypes:
|
|
477
|
+
y = mx.array([2.0], y_t)
|
|
478
|
+
z = mx.array([x, y])
|
|
479
|
+
expected = mx.stack([x, y], axis=0)
|
|
480
|
+
self.assertEqualArray(z, expected)
|
|
481
|
+
|
|
482
|
+
# shape check from `stack()`
|
|
483
|
+
with self.assertRaises(ValueError) as e:
|
|
484
|
+
mx.array([x, 1.0])
|
|
485
|
+
self.assertEqual(
|
|
486
|
+
str(e.exception), "Initialization encountered non-uniform length."
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
# shape check from `validate_shape`
|
|
490
|
+
with self.assertRaises(ValueError) as e:
|
|
491
|
+
mx.array([1.0, x])
|
|
492
|
+
self.assertEqual(
|
|
493
|
+
str(e.exception), "Initialization encountered non-uniform length."
|
|
494
|
+
)
|
|
495
|
+
|
|
496
|
+
# check that `[mx.array, ...]` retains the `mx.array` in the graph
|
|
497
|
+
def f(x):
|
|
498
|
+
y = mx.array([x, mx.array([2.0])])
|
|
499
|
+
return (2 * y).sum()
|
|
500
|
+
|
|
501
|
+
x = mx.array([1.0])
|
|
502
|
+
dfdx = mx.grad(f)
|
|
503
|
+
self.assertEqual(dfdx(x).item(), 2.0)
|
|
504
|
+
|
|
505
|
+
def test_init_from_array(self):
|
|
506
|
+
x = mx.array(3.0)
|
|
507
|
+
y = mx.array(x)
|
|
508
|
+
|
|
509
|
+
self.assertTrue(mx.array_equal(x, y))
|
|
510
|
+
|
|
511
|
+
y = mx.array(x, mx.int32)
|
|
512
|
+
self.assertEqual(y.dtype, mx.int32)
|
|
513
|
+
self.assertEqual(y.item(), 3)
|
|
514
|
+
|
|
515
|
+
y = mx.array(x, mx.bool_)
|
|
516
|
+
self.assertEqual(y.dtype, mx.bool_)
|
|
517
|
+
self.assertEqual(y.item(), True)
|
|
518
|
+
|
|
519
|
+
y = mx.array(x, mx.complex64)
|
|
520
|
+
self.assertEqual(y.dtype, mx.complex64)
|
|
521
|
+
self.assertEqual(y.item(), 3.0 + 0j)
|
|
522
|
+
|
|
523
|
+
def test_array_repr(self):
|
|
524
|
+
x = mx.array(True)
|
|
525
|
+
self.assertEqual(str(x), "array(True, dtype=bool)")
|
|
526
|
+
x = mx.array(1)
|
|
527
|
+
self.assertEqual(str(x), "array(1, dtype=int32)")
|
|
528
|
+
x = mx.array(1.0)
|
|
529
|
+
self.assertEqual(str(x), "array(1, dtype=float32)")
|
|
530
|
+
|
|
531
|
+
x = mx.array([1, 0, 1])
|
|
532
|
+
self.assertEqual(str(x), "array([1, 0, 1], dtype=int32)")
|
|
533
|
+
|
|
534
|
+
x = mx.array([1] * 6)
|
|
535
|
+
expected = "array([1, 1, 1, 1, 1, 1], dtype=int32)"
|
|
536
|
+
self.assertEqual(str(x), expected)
|
|
537
|
+
|
|
538
|
+
x = mx.array([1] * 7)
|
|
539
|
+
expected = "array([1, 1, 1, ..., 1, 1, 1], dtype=int32)"
|
|
540
|
+
self.assertEqual(str(x), expected)
|
|
541
|
+
|
|
542
|
+
x = mx.array([[1, 2], [1, 2], [1, 2]])
|
|
543
|
+
expected = "array([[1, 2],\n [1, 2],\n [1, 2]], dtype=int32)"
|
|
544
|
+
self.assertEqual(str(x), expected)
|
|
545
|
+
|
|
546
|
+
x = mx.array([[[1, 2], [1, 2]], [[1, 2], [1, 2]]])
|
|
547
|
+
expected = (
|
|
548
|
+
"array([[[1, 2],\n"
|
|
549
|
+
" [1, 2]],\n"
|
|
550
|
+
" [[1, 2],\n"
|
|
551
|
+
" [1, 2]]], dtype=int32)"
|
|
552
|
+
)
|
|
553
|
+
self.assertEqual(str(x), expected)
|
|
554
|
+
|
|
555
|
+
x = mx.array([[1, 2]] * 6)
|
|
556
|
+
expected = (
|
|
557
|
+
"array([[1, 2],\n"
|
|
558
|
+
" [1, 2],\n"
|
|
559
|
+
" [1, 2],\n"
|
|
560
|
+
" [1, 2],\n"
|
|
561
|
+
" [1, 2],\n"
|
|
562
|
+
" [1, 2]], dtype=int32)"
|
|
563
|
+
)
|
|
564
|
+
self.assertEqual(str(x), expected)
|
|
565
|
+
x = mx.array([[1, 2]] * 7)
|
|
566
|
+
expected = (
|
|
567
|
+
"array([[1, 2],\n"
|
|
568
|
+
" [1, 2],\n"
|
|
569
|
+
" [1, 2],\n"
|
|
570
|
+
" ...,\n"
|
|
571
|
+
" [1, 2],\n"
|
|
572
|
+
" [1, 2],\n"
|
|
573
|
+
" [1, 2]], dtype=int32)"
|
|
574
|
+
)
|
|
575
|
+
self.assertEqual(str(x), expected)
|
|
576
|
+
|
|
577
|
+
x = mx.array([1], dtype=mx.int8)
|
|
578
|
+
expected = "array([1], dtype=int8)"
|
|
579
|
+
self.assertEqual(str(x), expected)
|
|
580
|
+
x = mx.array([1], dtype=mx.int16)
|
|
581
|
+
expected = "array([1], dtype=int16)"
|
|
582
|
+
self.assertEqual(str(x), expected)
|
|
583
|
+
x = mx.array([1], dtype=mx.uint8)
|
|
584
|
+
expected = "array([1], dtype=uint8)"
|
|
585
|
+
self.assertEqual(str(x), expected)
|
|
586
|
+
|
|
587
|
+
# Fp16 is not supported in all platforms
|
|
588
|
+
x = mx.array([1.2], dtype=mx.float16)
|
|
589
|
+
expected = "array([1.2002], dtype=float16)"
|
|
590
|
+
self.assertEqual(str(x), expected)
|
|
591
|
+
|
|
592
|
+
x = mx.array([1 + 1j], dtype=mx.complex64)
|
|
593
|
+
expected = "array([1+1j], dtype=complex64)"
|
|
594
|
+
self.assertEqual(str(x), expected)
|
|
595
|
+
x = mx.array([1 - 1j], dtype=mx.complex64)
|
|
596
|
+
expected = "array([1-1j], dtype=complex64)"
|
|
597
|
+
|
|
598
|
+
x = mx.array([1 + 1j], dtype=mx.complex64)
|
|
599
|
+
expected = "array([1+1j], dtype=complex64)"
|
|
600
|
+
self.assertEqual(str(x), expected)
|
|
601
|
+
x = mx.array([1 - 1j], dtype=mx.complex64)
|
|
602
|
+
expected = "array([1-1j], dtype=complex64)"
|
|
603
|
+
|
|
604
|
+
def test_array_to_list(self):
|
|
605
|
+
types = [mx.bool_, mx.uint32, mx.int32, mx.int64, mx.float32]
|
|
606
|
+
for t in types:
|
|
607
|
+
x = mx.array(1, t)
|
|
608
|
+
self.assertEqual(x.tolist(), 1)
|
|
609
|
+
|
|
610
|
+
vals = [1, 2, 3, 4]
|
|
611
|
+
x = mx.array(vals)
|
|
612
|
+
self.assertEqual(x.tolist(), vals)
|
|
613
|
+
|
|
614
|
+
vals = [[1, 2], [3, 4]]
|
|
615
|
+
x = mx.array(vals)
|
|
616
|
+
self.assertEqual(x.tolist(), vals)
|
|
617
|
+
|
|
618
|
+
vals = [[1, 0], [0, 1]]
|
|
619
|
+
x = mx.array(vals, mx.bool_)
|
|
620
|
+
self.assertEqual(x.tolist(), vals)
|
|
621
|
+
|
|
622
|
+
vals = [[1.5, 2.5], [3.5, 4.5]]
|
|
623
|
+
x = mx.array(vals)
|
|
624
|
+
self.assertEqual(x.tolist(), vals)
|
|
625
|
+
|
|
626
|
+
vals = [[[0.5, 1.5], [2.5, 3.5]], [[4.5, 5.5], [6.5, 7.5]]]
|
|
627
|
+
x = mx.array(vals)
|
|
628
|
+
self.assertEqual(x.tolist(), vals)
|
|
629
|
+
|
|
630
|
+
# Empty arrays
|
|
631
|
+
vals = []
|
|
632
|
+
x = mx.array(vals)
|
|
633
|
+
self.assertEqual(x.tolist(), vals)
|
|
634
|
+
|
|
635
|
+
vals = [[], []]
|
|
636
|
+
x = mx.array(vals)
|
|
637
|
+
self.assertEqual(x.tolist(), vals)
|
|
638
|
+
|
|
639
|
+
# Complex arrays
|
|
640
|
+
vals = [0.5 + 0j, 1.5 + 1j, 2.5 + 0j, 3.5 + 1j]
|
|
641
|
+
x = mx.array(vals)
|
|
642
|
+
self.assertEqual(x.tolist(), vals)
|
|
643
|
+
|
|
644
|
+
# Half types
|
|
645
|
+
vals = [1.0, 2.0, 3.0, 4.0, 5.0]
|
|
646
|
+
x = mx.array(vals, dtype=mx.float16)
|
|
647
|
+
self.assertEqual(x.tolist(), vals)
|
|
648
|
+
|
|
649
|
+
x = mx.array(vals, dtype=mx.bfloat16)
|
|
650
|
+
self.assertEqual(x.tolist(), vals)
|
|
651
|
+
|
|
652
|
+
def test_array_np_conversion(self):
|
|
653
|
+
# Shape test
|
|
654
|
+
a = np.array([])
|
|
655
|
+
x = mx.array(a)
|
|
656
|
+
self.assertEqual(x.size, 0)
|
|
657
|
+
self.assertEqual(x.shape, (0,))
|
|
658
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
659
|
+
|
|
660
|
+
a = np.array([[], [], []])
|
|
661
|
+
x = mx.array(a)
|
|
662
|
+
self.assertEqual(x.size, 0)
|
|
663
|
+
self.assertEqual(x.shape, (3, 0))
|
|
664
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
665
|
+
|
|
666
|
+
a = np.array([[[], []], [[], []], [[], []]])
|
|
667
|
+
x = mx.array(a)
|
|
668
|
+
self.assertEqual(x.size, 0)
|
|
669
|
+
self.assertEqual(x.shape, (3, 2, 0))
|
|
670
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
671
|
+
|
|
672
|
+
# Content test
|
|
673
|
+
a = 2.0 * np.ones((3, 5, 4))
|
|
674
|
+
x = mx.array(a)
|
|
675
|
+
self.assertEqual(x.dtype, mx.float32)
|
|
676
|
+
self.assertEqual(x.ndim, 3)
|
|
677
|
+
self.assertEqual(x.shape, (3, 5, 4))
|
|
678
|
+
|
|
679
|
+
y = np.asarray(x)
|
|
680
|
+
self.assertTrue(np.allclose(a, y))
|
|
681
|
+
|
|
682
|
+
a = np.array(3, dtype=np.int32)
|
|
683
|
+
x = mx.array(a)
|
|
684
|
+
self.assertEqual(x.dtype, mx.int32)
|
|
685
|
+
self.assertEqual(x.ndim, 0)
|
|
686
|
+
self.assertEqual(x.shape, ())
|
|
687
|
+
self.assertEqual(x.item(), 3)
|
|
688
|
+
|
|
689
|
+
# mlx to numpy test
|
|
690
|
+
x = mx.array([True, False, True])
|
|
691
|
+
y = np.asarray(x)
|
|
692
|
+
self.assertEqual(y.dtype, np.bool_)
|
|
693
|
+
self.assertEqual(y.ndim, 1)
|
|
694
|
+
self.assertEqual(y.shape, (3,))
|
|
695
|
+
self.assertEqual(y[0], True)
|
|
696
|
+
self.assertEqual(y[1], False)
|
|
697
|
+
self.assertEqual(y[2], True)
|
|
698
|
+
|
|
699
|
+
# complex64 mx <-> np
|
|
700
|
+
cvals = [0j, 1, 1 + 1j]
|
|
701
|
+
x = np.array(cvals)
|
|
702
|
+
y = mx.array(x)
|
|
703
|
+
self.assertEqual(y.dtype, mx.complex64)
|
|
704
|
+
self.assertEqual(y.shape, (3,))
|
|
705
|
+
self.assertEqual(y.tolist(), cvals)
|
|
706
|
+
|
|
707
|
+
y = mx.array([0j, 1, 1 + 1j])
|
|
708
|
+
x = np.asarray(y)
|
|
709
|
+
self.assertEqual(x.dtype, np.complex64)
|
|
710
|
+
self.assertEqual(x.shape, (3,))
|
|
711
|
+
self.assertEqual(x.tolist(), cvals)
|
|
712
|
+
|
|
713
|
+
def test_array_np_dtype_conversion(self):
|
|
714
|
+
dtypes_list = [
|
|
715
|
+
(mx.bool_, np.bool_),
|
|
716
|
+
(mx.uint8, np.uint8),
|
|
717
|
+
(mx.uint16, np.uint16),
|
|
718
|
+
(mx.uint32, np.uint32),
|
|
719
|
+
(mx.uint64, np.uint64),
|
|
720
|
+
(mx.int8, np.int8),
|
|
721
|
+
(mx.int16, np.int16),
|
|
722
|
+
(mx.int32, np.int32),
|
|
723
|
+
(mx.int64, np.int64),
|
|
724
|
+
(mx.float16, np.float16),
|
|
725
|
+
(mx.float32, np.float32),
|
|
726
|
+
(mx.complex64, np.complex64),
|
|
727
|
+
]
|
|
728
|
+
|
|
729
|
+
for mlx_dtype, np_dtype in dtypes_list:
|
|
730
|
+
a_npy = np.random.uniform(low=0, high=100, size=(32,)).astype(np_dtype)
|
|
731
|
+
a_mlx = mx.array(a_npy)
|
|
732
|
+
|
|
733
|
+
self.assertEqual(a_mlx.dtype, mlx_dtype)
|
|
734
|
+
self.assertTrue(np.allclose(a_mlx, a_npy))
|
|
735
|
+
|
|
736
|
+
b_mlx = mx.random.uniform(
|
|
737
|
+
low=0,
|
|
738
|
+
high=10,
|
|
739
|
+
shape=(32,),
|
|
740
|
+
).astype(mlx_dtype)
|
|
741
|
+
b_npy = np.array(b_mlx)
|
|
742
|
+
|
|
743
|
+
self.assertEqual(b_npy.dtype, np_dtype)
|
|
744
|
+
|
|
745
|
+
def test_array_from_noncontiguous_np(self):
|
|
746
|
+
for t in [np.int8, np.int32, np.float16, np.float32, np.complex64]:
|
|
747
|
+
np_arr = np.random.uniform(size=(10, 10)).astype(np.complex64)
|
|
748
|
+
np_arr = np_arr.T
|
|
749
|
+
mx_arr = mx.array(np_arr)
|
|
750
|
+
self.assertTrue(mx.array_equal(np_arr, mx_arr))
|
|
751
|
+
|
|
752
|
+
def test_array_np_shape_dim_check(self):
|
|
753
|
+
a_npy = np.empty(2**31, dtype=np.bool_)
|
|
754
|
+
with self.assertRaises(ValueError) as e:
|
|
755
|
+
mx.array(a_npy)
|
|
756
|
+
self.assertEqual(
|
|
757
|
+
str(e.exception), "Shape dimension falls outside supported `int` range."
|
|
758
|
+
)
|
|
759
|
+
|
|
760
|
+
def test_dtype_promotion(self):
|
|
761
|
+
dtypes_list = [
|
|
762
|
+
(mx.bool_, np.bool_),
|
|
763
|
+
(mx.uint8, np.uint8),
|
|
764
|
+
(mx.uint16, np.uint16),
|
|
765
|
+
(mx.uint32, np.uint32),
|
|
766
|
+
(mx.uint64, np.uint64),
|
|
767
|
+
(mx.int8, np.int8),
|
|
768
|
+
(mx.int16, np.int16),
|
|
769
|
+
(mx.int32, np.int32),
|
|
770
|
+
(mx.int64, np.int64),
|
|
771
|
+
(mx.float32, np.float32),
|
|
772
|
+
]
|
|
773
|
+
|
|
774
|
+
promotion_pairs = permutations(dtypes_list, 2)
|
|
775
|
+
|
|
776
|
+
for (mlx_dt_1, np_dt_1), (mlx_dt_2, np_dt_2) in promotion_pairs:
|
|
777
|
+
with self.subTest(dtype1=np_dt_1, dtype2=np_dt_2):
|
|
778
|
+
a_npy = np.ones((3,), dtype=np_dt_1)
|
|
779
|
+
b_npy = np.ones((3,), dtype=np_dt_2)
|
|
780
|
+
|
|
781
|
+
c_npy = a_npy + b_npy
|
|
782
|
+
|
|
783
|
+
a_mlx = mx.ones((3,), dtype=mlx_dt_1)
|
|
784
|
+
b_mlx = mx.ones((3,), dtype=mlx_dt_2)
|
|
785
|
+
|
|
786
|
+
c_mlx = a_mlx + b_mlx
|
|
787
|
+
|
|
788
|
+
self.assertEqual(c_mlx.dtype, mx.array(c_npy).dtype)
|
|
789
|
+
|
|
790
|
+
a_mlx = mx.ones((3,), dtype=mx.float16)
|
|
791
|
+
b_mlx = mx.ones((3,), dtype=mx.float32)
|
|
792
|
+
c_mlx = a_mlx + b_mlx
|
|
793
|
+
|
|
794
|
+
self.assertEqual(c_mlx.dtype, mx.float32)
|
|
795
|
+
|
|
796
|
+
b_mlx = mx.ones((3,), dtype=mx.int32)
|
|
797
|
+
c_mlx = a_mlx + b_mlx
|
|
798
|
+
|
|
799
|
+
self.assertEqual(c_mlx.dtype, mx.float16)
|
|
800
|
+
|
|
801
|
+
def test_dtype_python_scalar_promotion(self):
|
|
802
|
+
tests = [
|
|
803
|
+
(mx.bool_, operator.mul, False, mx.bool_),
|
|
804
|
+
(mx.bool_, operator.mul, 0, mx.int32),
|
|
805
|
+
(mx.bool_, operator.mul, 1.0, mx.float32),
|
|
806
|
+
(mx.int8, operator.mul, False, mx.int8),
|
|
807
|
+
(mx.int8, operator.mul, 0, mx.int8),
|
|
808
|
+
(mx.int8, operator.mul, 1.0, mx.float32),
|
|
809
|
+
(mx.int16, operator.mul, False, mx.int16),
|
|
810
|
+
(mx.int16, operator.mul, 0, mx.int16),
|
|
811
|
+
(mx.int16, operator.mul, 1.0, mx.float32),
|
|
812
|
+
(mx.int32, operator.mul, False, mx.int32),
|
|
813
|
+
(mx.int32, operator.mul, 0, mx.int32),
|
|
814
|
+
(mx.int32, operator.mul, 1.0, mx.float32),
|
|
815
|
+
(mx.int64, operator.mul, False, mx.int64),
|
|
816
|
+
(mx.int64, operator.mul, 0, mx.int64),
|
|
817
|
+
(mx.int64, operator.mul, 1.0, mx.float32),
|
|
818
|
+
(mx.uint8, operator.mul, False, mx.uint8),
|
|
819
|
+
(mx.uint8, operator.mul, 0, mx.uint8),
|
|
820
|
+
(mx.uint8, operator.mul, 1.0, mx.float32),
|
|
821
|
+
(mx.uint16, operator.mul, False, mx.uint16),
|
|
822
|
+
(mx.uint16, operator.mul, 0, mx.uint16),
|
|
823
|
+
(mx.uint16, operator.mul, 1.0, mx.float32),
|
|
824
|
+
(mx.uint32, operator.mul, False, mx.uint32),
|
|
825
|
+
(mx.uint32, operator.mul, 0, mx.uint32),
|
|
826
|
+
(mx.uint32, operator.mul, 1.0, mx.float32),
|
|
827
|
+
(mx.uint64, operator.mul, False, mx.uint64),
|
|
828
|
+
(mx.uint64, operator.mul, 0, mx.uint64),
|
|
829
|
+
(mx.uint64, operator.mul, 1.0, mx.float32),
|
|
830
|
+
(mx.float32, operator.mul, False, mx.float32),
|
|
831
|
+
(mx.float32, operator.mul, 0, mx.float32),
|
|
832
|
+
(mx.float32, operator.mul, 1.0, mx.float32),
|
|
833
|
+
(mx.float16, operator.mul, False, mx.float16),
|
|
834
|
+
(mx.float16, operator.mul, 0, mx.float16),
|
|
835
|
+
(mx.float16, operator.mul, 1.0, mx.float16),
|
|
836
|
+
]
|
|
837
|
+
|
|
838
|
+
for dtype_in, f, v, dtype_out in tests:
|
|
839
|
+
x = mx.array(0, dtype_in)
|
|
840
|
+
y = f(x, v)
|
|
841
|
+
self.assertEqual(y.dtype, dtype_out)
|
|
842
|
+
|
|
843
|
+
def test_array_comparison(self):
|
|
844
|
+
a = mx.array([0.0, 1.0, 5.0])
|
|
845
|
+
b = mx.array([-1.0, 2.0, 5.0])
|
|
846
|
+
|
|
847
|
+
self.assertEqual((a < b).tolist(), [False, True, False])
|
|
848
|
+
self.assertEqual((a <= b).tolist(), [False, True, True])
|
|
849
|
+
self.assertEqual((a > b).tolist(), [True, False, False])
|
|
850
|
+
self.assertEqual((a >= b).tolist(), [True, False, True])
|
|
851
|
+
|
|
852
|
+
self.assertEqual((a < 5).tolist(), [True, True, False])
|
|
853
|
+
self.assertEqual((5 < a).tolist(), [False, False, False])
|
|
854
|
+
self.assertEqual((5 <= a).tolist(), [False, False, True])
|
|
855
|
+
self.assertEqual((a > 1).tolist(), [False, False, True])
|
|
856
|
+
self.assertEqual((a >= 1).tolist(), [False, True, True])
|
|
857
|
+
|
|
858
|
+
def test_array_neg(self):
|
|
859
|
+
a = mx.array([-1.0, 4.0, 0.0])
|
|
860
|
+
|
|
861
|
+
self.assertEqual((-a).tolist(), [1.0, -4.0, 0.0])
|
|
862
|
+
|
|
863
|
+
def test_array_type_cast(self):
|
|
864
|
+
a = mx.array([0.1, 2.3, -1.3])
|
|
865
|
+
b = [0, 2, -1]
|
|
866
|
+
|
|
867
|
+
self.assertEqual(a.astype(mx.int32).tolist(), b)
|
|
868
|
+
self.assertEqual(a.astype(mx.int32).dtype, mx.int32)
|
|
869
|
+
|
|
870
|
+
b = mx.array(b).astype(mx.float32)
|
|
871
|
+
self.assertEqual(b.dtype, mx.float32)
|
|
872
|
+
|
|
873
|
+
def test_array_iteration(self):
|
|
874
|
+
a = mx.array([0, 1, 2])
|
|
875
|
+
|
|
876
|
+
for i, x in enumerate(a):
|
|
877
|
+
self.assertEqual(x.item(), i)
|
|
878
|
+
|
|
879
|
+
a = mx.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
|
|
880
|
+
x, y, z = a
|
|
881
|
+
self.assertEqual(x.tolist(), [1.0, 2.0])
|
|
882
|
+
self.assertEqual(y.tolist(), [3.0, 4.0])
|
|
883
|
+
self.assertEqual(z.tolist(), [5.0, 6.0])
|
|
884
|
+
|
|
885
|
+
def test_array_pickle(self):
|
|
886
|
+
dtypes = [
|
|
887
|
+
mx.int8,
|
|
888
|
+
mx.int16,
|
|
889
|
+
mx.int32,
|
|
890
|
+
mx.int64,
|
|
891
|
+
mx.uint8,
|
|
892
|
+
mx.uint16,
|
|
893
|
+
mx.uint32,
|
|
894
|
+
mx.uint64,
|
|
895
|
+
mx.float16,
|
|
896
|
+
mx.float32,
|
|
897
|
+
mx.bfloat16,
|
|
898
|
+
mx.complex64,
|
|
899
|
+
]
|
|
900
|
+
|
|
901
|
+
for dtype in dtypes:
|
|
902
|
+
x = mx.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=dtype)
|
|
903
|
+
state = pickle.dumps(x)
|
|
904
|
+
y = pickle.loads(state)
|
|
905
|
+
self.assertEqualArray(y, x)
|
|
906
|
+
|
|
907
|
+
def test_array_copy(self):
|
|
908
|
+
dtypes = [
|
|
909
|
+
mx.int8,
|
|
910
|
+
mx.int16,
|
|
911
|
+
mx.int32,
|
|
912
|
+
mx.int64,
|
|
913
|
+
mx.uint8,
|
|
914
|
+
mx.uint16,
|
|
915
|
+
mx.uint32,
|
|
916
|
+
mx.uint64,
|
|
917
|
+
mx.float16,
|
|
918
|
+
mx.float32,
|
|
919
|
+
mx.bfloat16,
|
|
920
|
+
mx.complex64,
|
|
921
|
+
]
|
|
922
|
+
|
|
923
|
+
for copy_function in [copy, deepcopy]:
|
|
924
|
+
for dtype in dtypes:
|
|
925
|
+
x = mx.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype=dtype)
|
|
926
|
+
y = copy_function(x)
|
|
927
|
+
self.assertEqualArray(y, x)
|
|
928
|
+
|
|
929
|
+
y -= 1
|
|
930
|
+
self.assertEqualArray(y, x - 1)
|
|
931
|
+
|
|
932
|
+
def test_indexing(self):
|
|
933
|
+
# Only ellipsis is a no-op
|
|
934
|
+
a_mlx = mx.array([1])[...]
|
|
935
|
+
self.assertEqual(a_mlx.shape, (1,))
|
|
936
|
+
self.assertEqual(a_mlx.item(), 1)
|
|
937
|
+
|
|
938
|
+
# Basic content check, slice indexing
|
|
939
|
+
a_npy = np.arange(64, dtype=np.float32)
|
|
940
|
+
a_mlx = mx.array(a_npy)
|
|
941
|
+
a_sliced_mlx = a_mlx[2:50:4]
|
|
942
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
943
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[2:50:4]))
|
|
944
|
+
|
|
945
|
+
# Basic content check, mlx array indexing
|
|
946
|
+
a_npy = np.arange(64, dtype=np.int32)
|
|
947
|
+
a_npy = a_npy.reshape((8, 8))
|
|
948
|
+
a_mlx = mx.array(a_npy)
|
|
949
|
+
idx_npy = np.array([0, 1, 2, 7, 5], dtype=np.uint32)
|
|
950
|
+
idx_mlx = mx.array(idx_npy)
|
|
951
|
+
a_sliced_mlx = a_mlx[idx_mlx]
|
|
952
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
953
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[idx_npy]))
|
|
954
|
+
|
|
955
|
+
# Basic content check, int indexing
|
|
956
|
+
a_sliced_mlx = a_mlx[5]
|
|
957
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
958
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[5]))
|
|
959
|
+
self.assertEqual(len(a_sliced_npy.shape), len(a_npy[5].shape))
|
|
960
|
+
self.assertEqual(len(a_sliced_npy.shape), 1)
|
|
961
|
+
self.assertEqual(a_sliced_npy.shape[0], a_npy[5].shape[0])
|
|
962
|
+
|
|
963
|
+
# Basic content check, negative indexing
|
|
964
|
+
a_sliced_mlx = a_mlx[-1]
|
|
965
|
+
self.assertTrue(np.array_equal(a_sliced_mlx, a_npy[-1]))
|
|
966
|
+
|
|
967
|
+
# Basic content check, empty index
|
|
968
|
+
a_sliced_mlx = a_mlx[()]
|
|
969
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
970
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[()]))
|
|
971
|
+
|
|
972
|
+
# Basic content check, new axis
|
|
973
|
+
a_sliced_mlx = a_mlx[None]
|
|
974
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
975
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[None]))
|
|
976
|
+
|
|
977
|
+
a_sliced_mlx = a_mlx[:, None]
|
|
978
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
979
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, None]))
|
|
980
|
+
|
|
981
|
+
# Multi dim indexing, all ints
|
|
982
|
+
self.assertEqual(a_mlx[0, 0].item(), 0)
|
|
983
|
+
self.assertEqual(a_mlx[0, 0].ndim, 0)
|
|
984
|
+
|
|
985
|
+
# Multi dim indexing, all slices
|
|
986
|
+
a_sliced_mlx = a_mlx[2:4, 5:]
|
|
987
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
988
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[2:4, 5:]))
|
|
989
|
+
|
|
990
|
+
a_sliced_mlx = a_mlx[:, 0:5]
|
|
991
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
992
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, 0:5]))
|
|
993
|
+
|
|
994
|
+
# Slicing, strides
|
|
995
|
+
a_sliced_mlx = a_mlx[:, ::2]
|
|
996
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
997
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, ::2]))
|
|
998
|
+
|
|
999
|
+
# Slicing, -ve index
|
|
1000
|
+
a_sliced_mlx = a_mlx[-2:, :-1]
|
|
1001
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1002
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[-2:, :-1]))
|
|
1003
|
+
|
|
1004
|
+
# Slicing, start > end
|
|
1005
|
+
a_sliced_mlx = a_mlx[8:3]
|
|
1006
|
+
self.assertEqual(a_sliced_mlx.size, 0)
|
|
1007
|
+
|
|
1008
|
+
# Slicing, Clipping past the end
|
|
1009
|
+
a_sliced_mlx = a_mlx[7:10]
|
|
1010
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1011
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[7:10]))
|
|
1012
|
+
|
|
1013
|
+
# Multi dim indexing, int and slices
|
|
1014
|
+
a_sliced_mlx = a_mlx[0, :5]
|
|
1015
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1016
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[0, :5]))
|
|
1017
|
+
|
|
1018
|
+
a_sliced_mlx = a_mlx[:, -1]
|
|
1019
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1020
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, -1]))
|
|
1021
|
+
|
|
1022
|
+
# Multi dim indexing, int and array
|
|
1023
|
+
a_sliced_mlx = a_mlx[idx_mlx, 0]
|
|
1024
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1025
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[idx_npy, 0]))
|
|
1026
|
+
|
|
1027
|
+
# Multi dim indexing, array and slices
|
|
1028
|
+
a_sliced_mlx = a_mlx[idx_mlx, :5]
|
|
1029
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1030
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[idx_npy, :5]))
|
|
1031
|
+
|
|
1032
|
+
a_sliced_mlx = a_mlx[:, idx_mlx]
|
|
1033
|
+
a_sliced_npy = np.asarray(a_sliced_mlx)
|
|
1034
|
+
self.assertTrue(np.array_equal(a_sliced_npy, a_npy[:, idx_npy]))
|
|
1035
|
+
|
|
1036
|
+
# Multi dim indexing with multiple arrays
|
|
1037
|
+
def check_slices(arr_np, *idx_np):
|
|
1038
|
+
arr_mlx = mx.array(arr_np)
|
|
1039
|
+
idx_mlx = [
|
|
1040
|
+
mx.array(idx) if isinstance(idx, np.ndarray) else idx for idx in idx_np
|
|
1041
|
+
]
|
|
1042
|
+
slice_mlx = arr_mlx[tuple(idx_mlx)]
|
|
1043
|
+
self.assertTrue(
|
|
1044
|
+
np.array_equal(arr_np[tuple(idx_np)], arr_mlx[tuple(idx_mlx)])
|
|
1045
|
+
)
|
|
1046
|
+
|
|
1047
|
+
a_np = np.arange(16).reshape(4, 4)
|
|
1048
|
+
check_slices(a_np, np.array([0, 1, 2, 3]), np.array([0, 1, 2, 3]))
|
|
1049
|
+
check_slices(a_np, np.array([0, 1, 2, 3]), np.array([1, 0, 3, 3]))
|
|
1050
|
+
check_slices(a_np, np.array([[0, 1]]), np.array([[0], [1], [3]]))
|
|
1051
|
+
|
|
1052
|
+
a_np = np.arange(64).reshape(2, 4, 2, 4)
|
|
1053
|
+
check_slices(a_np, 0, np.array([0, 1, 2]))
|
|
1054
|
+
check_slices(a_np, slice(0, 1), np.array([0, 1, 2]))
|
|
1055
|
+
check_slices(
|
|
1056
|
+
a_np, slice(0, 1), np.array([0, 1, 2]), slice(None), slice(0, 4, 2)
|
|
1057
|
+
)
|
|
1058
|
+
check_slices(
|
|
1059
|
+
a_np, slice(0, 1), np.array([0, 1, 2]), slice(None), np.array([1, 2, 0])
|
|
1060
|
+
)
|
|
1061
|
+
check_slices(a_np, slice(0, 1), np.array([0, 1, 2]), 1, np.array([1, 2, 0]))
|
|
1062
|
+
check_slices(
|
|
1063
|
+
a_np, slice(0, 1), np.array([0, 1, 2]), np.array([1, 0, 0]), slice(0, 1)
|
|
1064
|
+
)
|
|
1065
|
+
check_slices(
|
|
1066
|
+
a_np,
|
|
1067
|
+
slice(0, 1),
|
|
1068
|
+
np.array([[0], [1], [2]]),
|
|
1069
|
+
np.array([[1, 0, 0]]),
|
|
1070
|
+
slice(0, 1),
|
|
1071
|
+
)
|
|
1072
|
+
check_slices(
|
|
1073
|
+
a_np,
|
|
1074
|
+
slice(0, 2),
|
|
1075
|
+
np.array([[0], [1], [2]]),
|
|
1076
|
+
slice(0, 2),
|
|
1077
|
+
np.array([[1, 0, 0]]),
|
|
1078
|
+
)
|
|
1079
|
+
for p in permutations([slice(None), slice(None), 0, np.array([1, 0])]):
|
|
1080
|
+
check_slices(a_np, *p)
|
|
1081
|
+
for p in permutations(
|
|
1082
|
+
[slice(None), slice(None), 0, np.array([1, 0]), None, None]
|
|
1083
|
+
):
|
|
1084
|
+
check_slices(a_np, *p)
|
|
1085
|
+
for p in permutations([0, np.array([1, 0]), None, Ellipsis, slice(None)]):
|
|
1086
|
+
check_slices(a_np, *p)
|
|
1087
|
+
|
|
1088
|
+
# Non-contiguous arrays in slicing
|
|
1089
|
+
a_mlx = mx.reshape(mx.arange(128), (16, 8))
|
|
1090
|
+
a_mlx = a_mlx[::2, :]
|
|
1091
|
+
a_np = np.array(a_mlx)
|
|
1092
|
+
idx_np = np.arange(8)[::2]
|
|
1093
|
+
idx_mlx = mx.arange(8)[::2]
|
|
1094
|
+
self.assertTrue(
|
|
1095
|
+
np.array_equal(a_np[idx_np, idx_np], np.array(a_mlx[idx_mlx, idx_mlx]))
|
|
1096
|
+
)
|
|
1097
|
+
|
|
1098
|
+
# Slicing with negative indices and integer
|
|
1099
|
+
a_np = np.arange(10).reshape(5, 2)
|
|
1100
|
+
a_mlx = mx.array(a_np)
|
|
1101
|
+
self.assertTrue(np.array_equal(a_np[2:-1, 0], np.array(a_mlx[2:-1, 0])))
|
|
1102
|
+
|
|
1103
|
+
def test_indexing_grad(self):
|
|
1104
|
+
x = mx.array([[1, 2], [3, 4]]).astype(mx.float32)
|
|
1105
|
+
ind = mx.array([0, 1, 0]).astype(mx.float32)
|
|
1106
|
+
|
|
1107
|
+
def index_fn(x, ind):
|
|
1108
|
+
return x[ind.astype(mx.int32)].sum()
|
|
1109
|
+
|
|
1110
|
+
grad_x, grad_ind = mx.grad(index_fn, argnums=(0, 1))(x, ind)
|
|
1111
|
+
expected = mx.array([[2, 2], [1, 1]])
|
|
1112
|
+
|
|
1113
|
+
self.assertTrue(mx.array_equal(grad_x, expected))
|
|
1114
|
+
self.assertTrue(mx.array_equal(grad_ind, mx.zeros(ind.shape)))
|
|
1115
|
+
|
|
1116
|
+
def test_setitem(self):
|
|
1117
|
+
a = mx.array(0)
|
|
1118
|
+
a[None] = 1
|
|
1119
|
+
self.assertEqual(a.item(), 1)
|
|
1120
|
+
|
|
1121
|
+
a = mx.array([1, 2, 3])
|
|
1122
|
+
a[0] = 2
|
|
1123
|
+
self.assertEqual(a.tolist(), [2, 2, 3])
|
|
1124
|
+
|
|
1125
|
+
a[-1] = 2
|
|
1126
|
+
self.assertEqual(a.tolist(), [2, 2, 2])
|
|
1127
|
+
|
|
1128
|
+
a[0] = mx.array([[[1]]])
|
|
1129
|
+
self.assertEqual(a.tolist(), [1, 2, 2])
|
|
1130
|
+
|
|
1131
|
+
a[:] = 0
|
|
1132
|
+
self.assertEqual(a.tolist(), [0, 0, 0])
|
|
1133
|
+
|
|
1134
|
+
a[None] = 1
|
|
1135
|
+
self.assertEqual(a.tolist(), [1, 1, 1])
|
|
1136
|
+
|
|
1137
|
+
a[0:1] = 2
|
|
1138
|
+
self.assertEqual(a.tolist(), [2, 1, 1])
|
|
1139
|
+
|
|
1140
|
+
a[0:2] = 3
|
|
1141
|
+
self.assertEqual(a.tolist(), [3, 3, 1])
|
|
1142
|
+
|
|
1143
|
+
a[0:3] = 4
|
|
1144
|
+
self.assertEqual(a.tolist(), [4, 4, 4])
|
|
1145
|
+
|
|
1146
|
+
a[0:1] = mx.array(0)
|
|
1147
|
+
self.assertEqual(a.tolist(), [0, 4, 4])
|
|
1148
|
+
|
|
1149
|
+
a[0:1] = mx.array([1])
|
|
1150
|
+
self.assertEqual(a.tolist(), [1, 4, 4])
|
|
1151
|
+
|
|
1152
|
+
with self.assertRaises(ValueError):
|
|
1153
|
+
a[0:1] = mx.array([2, 3])
|
|
1154
|
+
|
|
1155
|
+
a[0:2] = mx.array([2, 2])
|
|
1156
|
+
self.assertEqual(a.tolist(), [2, 2, 4])
|
|
1157
|
+
|
|
1158
|
+
a[:] = mx.array([[[[1, 1, 1]]]])
|
|
1159
|
+
self.assertEqual(a.tolist(), [1, 1, 1])
|
|
1160
|
+
|
|
1161
|
+
# Array slices
|
|
1162
|
+
def check_slices(arr_np, update_np, *idx_np):
|
|
1163
|
+
arr_mlx = mx.array(arr_np)
|
|
1164
|
+
update_mlx = mx.array(update_np)
|
|
1165
|
+
idx_mlx = [
|
|
1166
|
+
mx.array(idx) if isinstance(idx, np.ndarray) else idx for idx in idx_np
|
|
1167
|
+
]
|
|
1168
|
+
if len(idx_np) > 1:
|
|
1169
|
+
idx_np = tuple(idx_np)
|
|
1170
|
+
idx_mlx = tuple(idx_mlx)
|
|
1171
|
+
else:
|
|
1172
|
+
idx_np = idx_np[0]
|
|
1173
|
+
idx_mlx = idx_mlx[0]
|
|
1174
|
+
arr_np[idx_np] = update_np
|
|
1175
|
+
arr_mlx[idx_mlx] = update_mlx
|
|
1176
|
+
self.assertTrue(np.array_equal(arr_np, arr_mlx))
|
|
1177
|
+
|
|
1178
|
+
check_slices(np.zeros((3, 3)), 1, 0)
|
|
1179
|
+
check_slices(np.zeros((3, 3)), 1, -1)
|
|
1180
|
+
check_slices(np.zeros((3, 3)), 1, slice(0, 2))
|
|
1181
|
+
check_slices(np.zeros((3, 3)), np.array([[0, 1, 2], [3, 4, 5]]), slice(0, 2))
|
|
1182
|
+
|
|
1183
|
+
with self.assertRaises(ValueError):
|
|
1184
|
+
a = mx.array(0)
|
|
1185
|
+
a[0] = mx.array(1)
|
|
1186
|
+
|
|
1187
|
+
check_slices(np.zeros((3, 3)), 1, np.array([0, 1, 2]))
|
|
1188
|
+
check_slices(np.zeros((3, 3)), np.array(3), np.array([0, 1, 2]))
|
|
1189
|
+
check_slices(np.zeros((3, 3)), np.array([3]), np.array([0, 1, 2]))
|
|
1190
|
+
check_slices(np.zeros((3, 3)), np.array([3]), np.array([0, 1]))
|
|
1191
|
+
check_slices(np.zeros((3, 2)), np.array([[3, 3], [4, 4]]), np.array([0, 1]))
|
|
1192
|
+
check_slices(np.zeros((3, 2)), np.array([[3, 3], [4, 4]]), np.array([0, 1]))
|
|
1193
|
+
check_slices(
|
|
1194
|
+
np.zeros((3, 2)), np.array([[3, 3], [4, 4], [5, 5]]), np.array([0, 2, 1])
|
|
1195
|
+
)
|
|
1196
|
+
|
|
1197
|
+
# Multiple slices
|
|
1198
|
+
a = mx.array(0)
|
|
1199
|
+
a[None, None] = 1
|
|
1200
|
+
self.assertEqual(a.item(), 1)
|
|
1201
|
+
|
|
1202
|
+
a[None, None] = mx.array(2)
|
|
1203
|
+
self.assertEqual(a.item(), 2)
|
|
1204
|
+
|
|
1205
|
+
a[None, None] = mx.array([[[3]]])
|
|
1206
|
+
self.assertEqual(a.item(), 3)
|
|
1207
|
+
|
|
1208
|
+
a[()] = 4
|
|
1209
|
+
self.assertEqual(a.item(), 4)
|
|
1210
|
+
|
|
1211
|
+
a_np = np.zeros((2, 3, 4, 5))
|
|
1212
|
+
check_slices(a_np, 1, np.array([0, 0]), slice(0, 2), slice(0, 3), 4)
|
|
1213
|
+
check_slices(
|
|
1214
|
+
a_np,
|
|
1215
|
+
np.arange(10).reshape(2, 5),
|
|
1216
|
+
np.array([0, 0]),
|
|
1217
|
+
np.array([0, 1]),
|
|
1218
|
+
np.array([2, 3]),
|
|
1219
|
+
)
|
|
1220
|
+
check_slices(
|
|
1221
|
+
a_np,
|
|
1222
|
+
np.array([[3], [4]]),
|
|
1223
|
+
np.array([0, 0]),
|
|
1224
|
+
np.array([0, 1]),
|
|
1225
|
+
np.array([2, 3]),
|
|
1226
|
+
)
|
|
1227
|
+
check_slices(
|
|
1228
|
+
a_np, np.arange(5), np.array([0, 0]), np.array([0, 1]), np.array([2, 3])
|
|
1229
|
+
)
|
|
1230
|
+
check_slices(np.zeros(5), np.arange(2), None, None, np.array([2, 3]))
|
|
1231
|
+
check_slices(
|
|
1232
|
+
np.zeros((4, 3, 4)),
|
|
1233
|
+
np.arange(3),
|
|
1234
|
+
np.array([2, 3]),
|
|
1235
|
+
slice(0, 3),
|
|
1236
|
+
np.array([2, 3]),
|
|
1237
|
+
)
|
|
1238
|
+
|
|
1239
|
+
with self.assertRaises(ValueError):
|
|
1240
|
+
a = mx.zeros((4, 3, 4))
|
|
1241
|
+
a[mx.array([2, 3]), None, mx.array([2, 3])] = mx.arange(2)
|
|
1242
|
+
|
|
1243
|
+
with self.assertRaises(ValueError):
|
|
1244
|
+
a = mx.zeros((4, 3, 4))
|
|
1245
|
+
a[mx.array([2, 3]), None, mx.array([2, 3])] = mx.arange(3)
|
|
1246
|
+
|
|
1247
|
+
check_slices(np.zeros((4, 3, 4)), 1, np.array([2, 3]), None, np.array([2, 1]))
|
|
1248
|
+
check_slices(
|
|
1249
|
+
np.zeros((4, 3, 4)), np.arange(4), np.array([2, 3]), None, np.array([2, 1])
|
|
1250
|
+
)
|
|
1251
|
+
check_slices(
|
|
1252
|
+
np.zeros((4, 3, 4)),
|
|
1253
|
+
np.arange(2 * 4).reshape(2, 1, 4),
|
|
1254
|
+
np.array([2, 3]),
|
|
1255
|
+
None,
|
|
1256
|
+
np.array([2, 1]),
|
|
1257
|
+
)
|
|
1258
|
+
|
|
1259
|
+
check_slices(np.zeros((4, 4)), 1, slice(0, 2), slice(0, 2))
|
|
1260
|
+
check_slices(np.zeros((4, 4)), np.arange(2), slice(0, 2), slice(0, 2))
|
|
1261
|
+
check_slices(
|
|
1262
|
+
np.zeros((4, 4)), np.arange(2).reshape(2, 1), slice(0, 2), slice(0, 2)
|
|
1263
|
+
)
|
|
1264
|
+
check_slices(
|
|
1265
|
+
np.zeros((4, 4)), np.arange(4).reshape(2, 2), slice(0, 2), slice(0, 2)
|
|
1266
|
+
)
|
|
1267
|
+
|
|
1268
|
+
with self.assertRaises(ValueError):
|
|
1269
|
+
a = mx.zeros((2, 2, 2))
|
|
1270
|
+
a[..., ...] = 1
|
|
1271
|
+
|
|
1272
|
+
with self.assertRaises(ValueError):
|
|
1273
|
+
a = mx.zeros((2, 2, 2, 2, 2))
|
|
1274
|
+
a[0, ..., 0, ..., 0] = 1
|
|
1275
|
+
|
|
1276
|
+
with self.assertRaises(ValueError):
|
|
1277
|
+
a = mx.zeros((2, 2))
|
|
1278
|
+
a[0, 0, 0] = 1
|
|
1279
|
+
|
|
1280
|
+
with self.assertRaises(ValueError):
|
|
1281
|
+
a = mx.zeros((5, 4, 3))
|
|
1282
|
+
a[:, 0] = mx.ones((5, 1, 3))
|
|
1283
|
+
|
|
1284
|
+
check_slices(np.zeros((2, 2, 2, 2)), 1, None, Ellipsis, None)
|
|
1285
|
+
check_slices(
|
|
1286
|
+
np.zeros((2, 2, 2, 2)), 1, np.array([0, 1]), Ellipsis, np.array([0, 1])
|
|
1287
|
+
)
|
|
1288
|
+
check_slices(
|
|
1289
|
+
np.zeros((2, 2, 2, 2)),
|
|
1290
|
+
np.arange(2 * 2 * 2).reshape(2, 2, 2),
|
|
1291
|
+
np.array([0, 1]),
|
|
1292
|
+
Ellipsis,
|
|
1293
|
+
np.array([0, 1]),
|
|
1294
|
+
)
|
|
1295
|
+
|
|
1296
|
+
# Check slice assign with negative indices works
|
|
1297
|
+
a = mx.zeros((5, 5), mx.int32)
|
|
1298
|
+
a[2:-2, 2:-2] = 4
|
|
1299
|
+
self.assertEqual(a[2, 2].item(), 4)
|
|
1300
|
+
|
|
1301
|
+
# Check slice array slice
|
|
1302
|
+
check_slices(
|
|
1303
|
+
np.zeros((5, 4, 4)),
|
|
1304
|
+
np.arange(4 * 2 * 3).reshape(4, 2, 3),
|
|
1305
|
+
slice(0, 4),
|
|
1306
|
+
np.array([1, 3]),
|
|
1307
|
+
slice(None, -1),
|
|
1308
|
+
)
|
|
1309
|
+
check_slices(
|
|
1310
|
+
np.zeros((5, 4, 4)),
|
|
1311
|
+
np.arange(4 * 2 * 2).reshape(4, 2, 2),
|
|
1312
|
+
slice(0, 4),
|
|
1313
|
+
np.array([1, 3]),
|
|
1314
|
+
slice(0, 4, 2),
|
|
1315
|
+
)
|
|
1316
|
+
|
|
1317
|
+
check_slices(
|
|
1318
|
+
np.zeros((1, 10, 4)),
|
|
1319
|
+
np.arange(2 * 4).reshape(1, 2, 4),
|
|
1320
|
+
slice(None, None, None),
|
|
1321
|
+
np.array([1, 3]),
|
|
1322
|
+
)
|
|
1323
|
+
|
|
1324
|
+
check_slices(
|
|
1325
|
+
np.zeros((3, 4, 5, 3)),
|
|
1326
|
+
np.arange(2 * 4 * 3 * 3).reshape(2, 4, 3, 3),
|
|
1327
|
+
np.array([2, 1]),
|
|
1328
|
+
slice(None, None, None),
|
|
1329
|
+
slice(None, None, 2),
|
|
1330
|
+
slice(None, None, None),
|
|
1331
|
+
)
|
|
1332
|
+
|
|
1333
|
+
check_slices(
|
|
1334
|
+
np.zeros((3, 4, 5, 3)),
|
|
1335
|
+
np.arange(2 * 4 * 3 * 3).reshape(2, 4, 3, 3),
|
|
1336
|
+
np.array([2, 1]),
|
|
1337
|
+
slice(None, None, None),
|
|
1338
|
+
slice(None, None, 2),
|
|
1339
|
+
)
|
|
1340
|
+
|
|
1341
|
+
check_slices(np.zeros((5, 4, 3)), np.ones((5, 3)), slice(None), 0)
|
|
1342
|
+
|
|
1343
|
+
check_slices(np.zeros((5, 4, 3)), np.ones((5, 1, 3)), slice(None), slice(0, 1))
|
|
1344
|
+
check_slices(
|
|
1345
|
+
np.ones((3, 4, 4, 4)), np.zeros((4, 4)), 0, slice(0, 4), 3, slice(0, 4)
|
|
1346
|
+
)
|
|
1347
|
+
|
|
1348
|
+
x = mx.zeros((2, 3, 4, 5, 3))
|
|
1349
|
+
x[..., 0] = 1.0
|
|
1350
|
+
self.assertTrue(mx.array_equal(x[..., 0], mx.ones((2, 3, 4, 5))))
|
|
1351
|
+
|
|
1352
|
+
x = mx.zeros((2, 3, 4, 5, 3))
|
|
1353
|
+
x[:, 0] = 1.0
|
|
1354
|
+
self.assertTrue(mx.array_equal(x[:, 0], mx.ones((2, 4, 5, 3))))
|
|
1355
|
+
|
|
1356
|
+
x = mx.zeros((2, 2, 2, 2, 2, 2))
|
|
1357
|
+
x[0, 0] = 1
|
|
1358
|
+
self.assertTrue(mx.array_equal(x[0, 0], mx.ones((2, 2, 2, 2))))
|
|
1359
|
+
|
|
1360
|
+
a = mx.zeros((2, 2, 2))
|
|
1361
|
+
with self.assertRaises(ValueError):
|
|
1362
|
+
a[:, None, :] = mx.ones((2, 2, 2))
|
|
1363
|
+
|
|
1364
|
+
# Ok, doesn't throw
|
|
1365
|
+
a[:, None, :] = mx.ones((2, 1, 2, 2))
|
|
1366
|
+
a[:, None, :] = mx.ones((2, 2))
|
|
1367
|
+
a[:, None, 0] = mx.ones((2,))
|
|
1368
|
+
a[:, None, 0] = mx.ones((1, 2))
|
|
1369
|
+
|
|
1370
|
+
def test_array_at(self):
|
|
1371
|
+
a = mx.array(1)
|
|
1372
|
+
with self.assertRaises(ValueError):
|
|
1373
|
+
a.at.add(1)
|
|
1374
|
+
|
|
1375
|
+
a = a.at[None].add(1)
|
|
1376
|
+
self.assertEqual(a.item(), 2)
|
|
1377
|
+
|
|
1378
|
+
a = mx.array([0, 1, 2])
|
|
1379
|
+
a = a.at[1].add(2)
|
|
1380
|
+
self.assertEqual(a.tolist(), [0, 3, 2])
|
|
1381
|
+
|
|
1382
|
+
a = a.at[mx.array([0, 0, 0, 0])].add(1)
|
|
1383
|
+
self.assertEqual(a.tolist(), [4, 3, 2])
|
|
1384
|
+
|
|
1385
|
+
a = mx.zeros((10, 10))
|
|
1386
|
+
a = a.at[0].add(mx.arange(10))
|
|
1387
|
+
self.assertEqual(a[0].tolist(), list(range(10)))
|
|
1388
|
+
|
|
1389
|
+
a = mx.zeros((10, 10))
|
|
1390
|
+
index_x = mx.array([0, 2, 3, 7])
|
|
1391
|
+
index_y = mx.array([3, 3, 1, 2])
|
|
1392
|
+
u = mx.random.uniform(shape=(4,))
|
|
1393
|
+
a = a.at[index_x, index_y].add(u)
|
|
1394
|
+
self.assertTrue(mx.allclose(a.sum(), u.sum()))
|
|
1395
|
+
self.assertEqualArray(a.sum(), u.sum(), atol=1e-6, rtol=1e-5)
|
|
1396
|
+
self.assertEqual(a[index_x, index_y].tolist(), u.tolist())
|
|
1397
|
+
|
|
1398
|
+
# Test all array.at ops
|
|
1399
|
+
a = mx.random.uniform(shape=(10, 5, 2))
|
|
1400
|
+
idx_x = mx.array([0, 4])
|
|
1401
|
+
update = mx.ones((2, 5))
|
|
1402
|
+
a[idx_x, :, 0] = 0
|
|
1403
|
+
a = a.at[idx_x, :, 0].add(update)
|
|
1404
|
+
self.assertEqualArray(a[idx_x, :, 0], update)
|
|
1405
|
+
a = a.at[idx_x, :, 0].subtract(update)
|
|
1406
|
+
self.assertEqualArray(a[idx_x, :, 0], mx.zeros_like(update))
|
|
1407
|
+
a = a.at[idx_x, :, 0].add(2 * update)
|
|
1408
|
+
self.assertEqualArray(a[idx_x, :, 0], 2 * update)
|
|
1409
|
+
a = a.at[idx_x, :, 0].multiply(2 * update)
|
|
1410
|
+
self.assertEqualArray(a[idx_x, :, 0], 4 * update)
|
|
1411
|
+
a = a.at[idx_x, :, 0].divide(3 * update)
|
|
1412
|
+
self.assertEqualArray(a[idx_x, :, 0], (4 / 3) * update)
|
|
1413
|
+
a[idx_x, :, 0] = 5
|
|
1414
|
+
update = mx.arange(10).reshape(2, 5)
|
|
1415
|
+
a = a.at[idx_x, :, 0].maximum(update)
|
|
1416
|
+
self.assertEqualArray(a[idx_x, :, 0], mx.maximum(a[idx_x, :, 0], update))
|
|
1417
|
+
a[idx_x, :, 0] = 5
|
|
1418
|
+
a = a.at[idx_x, :, 0].minimum(update)
|
|
1419
|
+
self.assertEqualArray(a[idx_x, :, 0], mx.minimum(a[idx_x, :, 0], update))
|
|
1420
|
+
|
|
1421
|
+
update = mx.array([1.0, 2.0])[None, None, None]
|
|
1422
|
+
src = mx.array([1.0, 2.0])[None, :]
|
|
1423
|
+
src = src.at[0:1].add(update)
|
|
1424
|
+
self.assertTrue(mx.array_equal(src, mx.array([[2.0, 4.0]])))
|
|
1425
|
+
|
|
1426
|
+
def test_slice_negative_step(self):
|
|
1427
|
+
a_np = np.arange(20)
|
|
1428
|
+
a_mx = mx.array(a_np)
|
|
1429
|
+
|
|
1430
|
+
# Basic negative slice
|
|
1431
|
+
b_np = a_np[::-1]
|
|
1432
|
+
b_mx = a_mx[::-1]
|
|
1433
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1434
|
+
|
|
1435
|
+
# Bounds negative slice
|
|
1436
|
+
b_np = a_np[-3:3:-1]
|
|
1437
|
+
b_mx = a_mx[-3:3:-1]
|
|
1438
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1439
|
+
|
|
1440
|
+
# Bounds negative slice
|
|
1441
|
+
b_np = a_np[25:-50:-1]
|
|
1442
|
+
b_mx = a_mx[25:-50:-1]
|
|
1443
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1444
|
+
|
|
1445
|
+
# Jumping negative slice
|
|
1446
|
+
b_np = a_np[::-3]
|
|
1447
|
+
b_mx = a_mx[::-3]
|
|
1448
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1449
|
+
|
|
1450
|
+
# Bounds and negative slice
|
|
1451
|
+
b_np = a_np[-3:3:-3]
|
|
1452
|
+
b_mx = a_mx[-3:3:-3]
|
|
1453
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1454
|
+
|
|
1455
|
+
# Bounds and negative slice
|
|
1456
|
+
b_np = a_np[25:-50:-3]
|
|
1457
|
+
b_mx = a_mx[25:-50:-3]
|
|
1458
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1459
|
+
|
|
1460
|
+
# Negative slice and ascending bounds
|
|
1461
|
+
b_np = a_np[0:20:-3]
|
|
1462
|
+
b_mx = a_mx[0:20:-3]
|
|
1463
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1464
|
+
|
|
1465
|
+
# Multi-dim negative slices
|
|
1466
|
+
a_np = np.arange(3 * 6 * 4).reshape(3, 6, 4)
|
|
1467
|
+
a_mx = mx.array(a_np)
|
|
1468
|
+
|
|
1469
|
+
# Flip each dim
|
|
1470
|
+
b_np = a_np[..., ::-1]
|
|
1471
|
+
b_mx = a_mx[..., ::-1]
|
|
1472
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1473
|
+
|
|
1474
|
+
b_np = a_np[:, ::-1, :]
|
|
1475
|
+
b_mx = a_mx[:, ::-1, :]
|
|
1476
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1477
|
+
|
|
1478
|
+
b_np = a_np[::-1, ...]
|
|
1479
|
+
b_mx = a_mx[::-1, ...]
|
|
1480
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1481
|
+
|
|
1482
|
+
# Flip pairs of dims
|
|
1483
|
+
b_np = a_np[::-1, 1:5:2, ::-2]
|
|
1484
|
+
b_mx = a_mx[::-1, 1:5:2, ::-2]
|
|
1485
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1486
|
+
|
|
1487
|
+
b_np = a_np[::-1, ::-2, 1:5:2]
|
|
1488
|
+
b_mx = a_mx[::-1, ::-2, 1:5:2]
|
|
1489
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1490
|
+
|
|
1491
|
+
# Flip all dims
|
|
1492
|
+
b_np = a_np[::-1, ::-3, ::-2]
|
|
1493
|
+
b_mx = a_mx[::-1, ::-3, ::-2]
|
|
1494
|
+
self.assertTrue(np.array_equal(b_np, b_mx))
|
|
1495
|
+
|
|
1496
|
+
def test_api(self):
|
|
1497
|
+
x = mx.array(np.random.rand(10, 10, 10))
|
|
1498
|
+
ops = [
|
|
1499
|
+
("reshape", (100, -1)),
|
|
1500
|
+
"square",
|
|
1501
|
+
"sqrt",
|
|
1502
|
+
"rsqrt",
|
|
1503
|
+
"reciprocal",
|
|
1504
|
+
"exp",
|
|
1505
|
+
"log",
|
|
1506
|
+
"sin",
|
|
1507
|
+
"cos",
|
|
1508
|
+
"log1p",
|
|
1509
|
+
"abs",
|
|
1510
|
+
"log10",
|
|
1511
|
+
"log2",
|
|
1512
|
+
"conj",
|
|
1513
|
+
("all", 1),
|
|
1514
|
+
("any", 1),
|
|
1515
|
+
("transpose", (0, 2, 1)),
|
|
1516
|
+
("sum", 1),
|
|
1517
|
+
("prod", 1),
|
|
1518
|
+
("min", 1),
|
|
1519
|
+
("max", 1),
|
|
1520
|
+
("logcumsumexp", 1),
|
|
1521
|
+
("logsumexp", 1),
|
|
1522
|
+
("mean", 1),
|
|
1523
|
+
("var", 1),
|
|
1524
|
+
("argmin", 1),
|
|
1525
|
+
("argmax", 1),
|
|
1526
|
+
("cummax", 1),
|
|
1527
|
+
("cummin", 1),
|
|
1528
|
+
("cumprod", 1),
|
|
1529
|
+
("cumsum", 1),
|
|
1530
|
+
("diagonal", 0, 0, 1),
|
|
1531
|
+
("flatten", 0, -1),
|
|
1532
|
+
("moveaxis", 1, 2),
|
|
1533
|
+
("round", 2),
|
|
1534
|
+
("std", 1, True, 0),
|
|
1535
|
+
("swapaxes", 1, 2),
|
|
1536
|
+
]
|
|
1537
|
+
for op in ops:
|
|
1538
|
+
if isinstance(op, tuple):
|
|
1539
|
+
op, *args = op
|
|
1540
|
+
else:
|
|
1541
|
+
args = tuple()
|
|
1542
|
+
y1 = getattr(mx, op)(x, *args)
|
|
1543
|
+
y2 = getattr(x, op)(*args)
|
|
1544
|
+
self.assertEqual(y1.dtype, y2.dtype)
|
|
1545
|
+
self.assertEqual(y1.shape, y2.shape)
|
|
1546
|
+
self.assertTrue(mx.array_equal(y1, y2))
|
|
1547
|
+
|
|
1548
|
+
y1 = mx.split(x, 2)
|
|
1549
|
+
y2 = x.split(2)
|
|
1550
|
+
self.assertEqual(len(y1), 2)
|
|
1551
|
+
self.assertEqual(len(y1), len(y2))
|
|
1552
|
+
self.assertTrue(mx.array_equal(y1[0], y2[0]))
|
|
1553
|
+
self.assertTrue(mx.array_equal(y1[1], y2[1]))
|
|
1554
|
+
x = mx.array(np.random.rand(10, 10, 1))
|
|
1555
|
+
y1 = mx.squeeze(x, axis=2)
|
|
1556
|
+
y2 = x.squeeze(axis=2)
|
|
1557
|
+
self.assertEqual(y1.shape, y2.shape)
|
|
1558
|
+
self.assertTrue(mx.array_equal(y1, y2))
|
|
1559
|
+
|
|
1560
|
+
def test_memoryless_copy(self):
|
|
1561
|
+
a_mx = mx.ones((2, 2))
|
|
1562
|
+
b_mx = mx.broadcast_to(a_mx, (5, 2, 2))
|
|
1563
|
+
|
|
1564
|
+
# Make np arrays without copy
|
|
1565
|
+
a_np = np.array(a_mx, copy=False)
|
|
1566
|
+
b_np = np.array(b_mx, copy=False)
|
|
1567
|
+
|
|
1568
|
+
# Check that we get read-only array that does not own the underlying data
|
|
1569
|
+
self.assertFalse(a_np.flags.owndata)
|
|
1570
|
+
self.assertTrue(a_np.flags.writeable)
|
|
1571
|
+
|
|
1572
|
+
# Check contents
|
|
1573
|
+
self.assertTrue(np.array_equal(np.ones((2, 2), dtype=np.float32), a_np))
|
|
1574
|
+
self.assertTrue(np.array_equal(np.ones((5, 2, 2), dtype=np.float32), b_np))
|
|
1575
|
+
|
|
1576
|
+
# Check strides
|
|
1577
|
+
self.assertSequenceEqual(b_np.strides, (0, 8, 4))
|
|
1578
|
+
|
|
1579
|
+
def test_np_array_conversion_copies_by_default(self):
|
|
1580
|
+
a_mx = mx.ones((2, 2))
|
|
1581
|
+
a_np = np.array(a_mx)
|
|
1582
|
+
self.assertTrue(a_np.flags.owndata)
|
|
1583
|
+
self.assertTrue(a_np.flags.writeable)
|
|
1584
|
+
|
|
1585
|
+
def test_buffer_protocol(self):
|
|
1586
|
+
dtypes_list = [
|
|
1587
|
+
(mx.bool_, np.bool_, None),
|
|
1588
|
+
(mx.uint8, np.uint8, np.iinfo),
|
|
1589
|
+
(mx.uint16, np.uint16, np.iinfo),
|
|
1590
|
+
(mx.uint32, np.uint32, np.iinfo),
|
|
1591
|
+
(mx.uint64, np.uint64, np.iinfo),
|
|
1592
|
+
(mx.int8, np.int8, np.iinfo),
|
|
1593
|
+
(mx.int16, np.int16, np.iinfo),
|
|
1594
|
+
(mx.int32, np.int32, np.iinfo),
|
|
1595
|
+
(mx.int64, np.int64, np.iinfo),
|
|
1596
|
+
(mx.float16, np.float16, np.finfo),
|
|
1597
|
+
(mx.float32, np.float32, np.finfo),
|
|
1598
|
+
(mx.complex64, np.complex64, np.finfo),
|
|
1599
|
+
]
|
|
1600
|
+
|
|
1601
|
+
for mlx_dtype, np_dtype, info_fn in dtypes_list:
|
|
1602
|
+
a_np = np.random.uniform(low=0, high=100, size=(3, 4)).astype(np_dtype)
|
|
1603
|
+
if info_fn is not None:
|
|
1604
|
+
info = info_fn(np_dtype)
|
|
1605
|
+
a_np[0, 0] = info.min
|
|
1606
|
+
a_np[0, 1] = info.max
|
|
1607
|
+
a_mx = mx.array(a_np)
|
|
1608
|
+
for f in [lambda x: x, lambda x: x.T]:
|
|
1609
|
+
mv_mx = memoryview(f(a_mx))
|
|
1610
|
+
mv_np = memoryview(f(a_np))
|
|
1611
|
+
self.assertEqual(mv_mx.strides, mv_np.strides, f"{mlx_dtype}{np_dtype}")
|
|
1612
|
+
self.assertEqual(mv_mx.shape, mv_np.shape, f"{mlx_dtype}{np_dtype}")
|
|
1613
|
+
# correct buffer format for 8 byte (unsigned) 'long long' is Q/q, see
|
|
1614
|
+
# https://docs.python.org/3.10/library/struct.html#format-characters
|
|
1615
|
+
# numpy returns L/l, as 'long' is equivalent to 'long long' on 64bit machines, so q and l are equivalent
|
|
1616
|
+
# see https://github.com/pybind/pybind11/issues/1908
|
|
1617
|
+
if np_dtype == np.uint64:
|
|
1618
|
+
self.assertEqual(mv_mx.format, "Q", f"{mlx_dtype}{np_dtype}")
|
|
1619
|
+
elif np_dtype == np.int64:
|
|
1620
|
+
self.assertEqual(mv_mx.format, "q", f"{mlx_dtype}{np_dtype}")
|
|
1621
|
+
# for windows long is 32bit and numpy returns L/l.
|
|
1622
|
+
elif np_dtype == np.uint32 and platform.system() == "Windows":
|
|
1623
|
+
self.assertEqual(mv_mx.format, "I", f"{mlx_dtype}{np_dtype}")
|
|
1624
|
+
elif np_dtype == np.int32 and platform.system() == "Windows":
|
|
1625
|
+
self.assertEqual(mv_mx.format, "i", f"{mlx_dtype}{np_dtype}")
|
|
1626
|
+
else:
|
|
1627
|
+
self.assertEqual(
|
|
1628
|
+
mv_mx.format, mv_np.format, f"{mlx_dtype}{np_dtype}"
|
|
1629
|
+
)
|
|
1630
|
+
self.assertFalse(mv_mx.readonly)
|
|
1631
|
+
back_to_npy = np.array(mv_mx, copy=False)
|
|
1632
|
+
self.assertEqualArray(
|
|
1633
|
+
back_to_npy,
|
|
1634
|
+
f(a_np),
|
|
1635
|
+
atol=0,
|
|
1636
|
+
rtol=0,
|
|
1637
|
+
)
|
|
1638
|
+
|
|
1639
|
+
# extra test for bfloat16, which is not numpy convertible
|
|
1640
|
+
a_mx = mx.random.uniform(low=0, high=100, shape=(3, 4), dtype=mx.bfloat16)
|
|
1641
|
+
mv_mx = memoryview(a_mx)
|
|
1642
|
+
self.assertEqual(mv_mx.strides, (8, 2))
|
|
1643
|
+
self.assertEqual(mv_mx.shape, (3, 4))
|
|
1644
|
+
self.assertEqual(mv_mx.format, "B")
|
|
1645
|
+
with self.assertRaises(RuntimeError) as cm:
|
|
1646
|
+
np.array(a_mx)
|
|
1647
|
+
e = cm.exception
|
|
1648
|
+
self.assertTrue("Item size 2 for PEP 3118 buffer format string" in str(e))
|
|
1649
|
+
|
|
1650
|
+
# Test buffer protocol with non-arrays ie bytes
|
|
1651
|
+
a = ord("a") * 257 + mx.arange(10).astype(mx.int16)
|
|
1652
|
+
ab = bytes(a)
|
|
1653
|
+
self.assertEqual(len(ab), 20)
|
|
1654
|
+
if sys.byteorder == "little":
|
|
1655
|
+
self.assertEqual(b"aaaaaaaaaa", ab[1::2])
|
|
1656
|
+
self.assertEqual(b"abcdefghij", ab[::2])
|
|
1657
|
+
else:
|
|
1658
|
+
self.assertEqual(b"aaaaaaaaaa", ab[::2])
|
|
1659
|
+
self.assertEqual(b"abcdefghij", ab[1::2])
|
|
1660
|
+
|
|
1661
|
+
def test_buffer_protocol_ref_counting(self):
|
|
1662
|
+
a = mx.arange(3)
|
|
1663
|
+
wr = weakref.ref(a)
|
|
1664
|
+
self.assertIsNotNone(wr())
|
|
1665
|
+
mv = memoryview(a)
|
|
1666
|
+
a = None
|
|
1667
|
+
self.assertIsNotNone(wr())
|
|
1668
|
+
mv = None
|
|
1669
|
+
self.assertIsNone(wr())
|
|
1670
|
+
|
|
1671
|
+
def test_array_view_ref_counting(self):
|
|
1672
|
+
a = mx.arange(3)
|
|
1673
|
+
wr = weakref.ref(a)
|
|
1674
|
+
self.assertIsNotNone(wr())
|
|
1675
|
+
a_np = np.array(a, copy=False)
|
|
1676
|
+
a = None
|
|
1677
|
+
self.assertIsNotNone(wr())
|
|
1678
|
+
a_np = None
|
|
1679
|
+
self.assertIsNone(wr())
|
|
1680
|
+
|
|
1681
|
+
@unittest.skipIf(not has_tf, "requires TensorFlow")
|
|
1682
|
+
def test_buffer_protocol_tf(self):
|
|
1683
|
+
dtypes_list = [
|
|
1684
|
+
(
|
|
1685
|
+
mx.bool_,
|
|
1686
|
+
tf.bool,
|
|
1687
|
+
np.bool_,
|
|
1688
|
+
),
|
|
1689
|
+
(
|
|
1690
|
+
mx.uint8,
|
|
1691
|
+
tf.uint8,
|
|
1692
|
+
np.uint8,
|
|
1693
|
+
),
|
|
1694
|
+
(
|
|
1695
|
+
mx.uint16,
|
|
1696
|
+
tf.uint16,
|
|
1697
|
+
np.uint16,
|
|
1698
|
+
),
|
|
1699
|
+
(
|
|
1700
|
+
mx.uint32,
|
|
1701
|
+
tf.uint32,
|
|
1702
|
+
np.uint32,
|
|
1703
|
+
),
|
|
1704
|
+
(mx.uint64, tf.uint64, np.uint64),
|
|
1705
|
+
(mx.int8, tf.int8, np.int8),
|
|
1706
|
+
(mx.int16, tf.int16, np.int16),
|
|
1707
|
+
(mx.int32, tf.int32, np.int32),
|
|
1708
|
+
(mx.int64, tf.int64, np.int64),
|
|
1709
|
+
(mx.float16, tf.float16, np.float16),
|
|
1710
|
+
(mx.float32, tf.float32, np.float32),
|
|
1711
|
+
(
|
|
1712
|
+
mx.complex64,
|
|
1713
|
+
tf.complex64,
|
|
1714
|
+
np.complex64,
|
|
1715
|
+
),
|
|
1716
|
+
]
|
|
1717
|
+
|
|
1718
|
+
for mlx_dtype, tf_dtype, np_dtype in dtypes_list:
|
|
1719
|
+
a_np = np.random.uniform(low=0, high=100, size=(3, 4)).astype(np_dtype)
|
|
1720
|
+
a_tf = tf.constant(a_np, dtype=tf_dtype)
|
|
1721
|
+
a_mx = mx.array(np.array(a_tf))
|
|
1722
|
+
for f in [
|
|
1723
|
+
lambda x: x,
|
|
1724
|
+
lambda x: tf.transpose(x) if isinstance(x, tf.Tensor) else x.T,
|
|
1725
|
+
]:
|
|
1726
|
+
mv_mx = memoryview(f(a_mx))
|
|
1727
|
+
mv_tf = memoryview(f(a_tf))
|
|
1728
|
+
if (mv_mx.c_contiguous and mv_tf.c_contiguous) or (
|
|
1729
|
+
mv_mx.f_contiguous and mv_tf.f_contiguous
|
|
1730
|
+
):
|
|
1731
|
+
self.assertEqual(
|
|
1732
|
+
mv_mx.strides, mv_tf.strides, f"{mlx_dtype}{tf_dtype}"
|
|
1733
|
+
)
|
|
1734
|
+
self.assertEqual(mv_mx.shape, mv_tf.shape, f"{mlx_dtype}{tf_dtype}")
|
|
1735
|
+
self.assertFalse(mv_mx.readonly)
|
|
1736
|
+
back_to_npy = np.array(mv_mx)
|
|
1737
|
+
self.assertEqualArray(
|
|
1738
|
+
back_to_npy,
|
|
1739
|
+
f(a_tf),
|
|
1740
|
+
atol=0,
|
|
1741
|
+
rtol=0,
|
|
1742
|
+
)
|
|
1743
|
+
|
|
1744
|
+
def test_logical_overloads(self):
|
|
1745
|
+
with self.assertRaises(ValueError):
|
|
1746
|
+
mx.array(1.0) & mx.array(1)
|
|
1747
|
+
with self.assertRaises(ValueError):
|
|
1748
|
+
mx.array(1.0) | mx.array(1)
|
|
1749
|
+
|
|
1750
|
+
self.assertEqual((mx.array(True) & True).item(), True)
|
|
1751
|
+
self.assertEqual((mx.array(True) & False).item(), False)
|
|
1752
|
+
self.assertEqual((mx.array(True) | False).item(), True)
|
|
1753
|
+
self.assertEqual((mx.array(False) | False).item(), False)
|
|
1754
|
+
self.assertEqual((~mx.array(False)).item(), True)
|
|
1755
|
+
self.assertEqual((mx.array(False) ^ True).item(), True)
|
|
1756
|
+
|
|
1757
|
+
def test_inplace(self):
|
|
1758
|
+
iops = [
|
|
1759
|
+
"__iadd__",
|
|
1760
|
+
"__isub__",
|
|
1761
|
+
"__imul__",
|
|
1762
|
+
"__ifloordiv__",
|
|
1763
|
+
"__imod__",
|
|
1764
|
+
"__ipow__",
|
|
1765
|
+
"__ixor__",
|
|
1766
|
+
]
|
|
1767
|
+
|
|
1768
|
+
for op in iops:
|
|
1769
|
+
a = mx.array([1, 2, 3])
|
|
1770
|
+
a_np = np.array(a)
|
|
1771
|
+
b = a
|
|
1772
|
+
b = getattr(a, op)(3)
|
|
1773
|
+
self.assertTrue(mx.array_equal(a, b))
|
|
1774
|
+
out_np = getattr(a_np, op)(3)
|
|
1775
|
+
self.assertTrue(np.array_equal(out_np, a))
|
|
1776
|
+
|
|
1777
|
+
with self.assertRaises(ValueError):
|
|
1778
|
+
a = mx.array([1])
|
|
1779
|
+
a /= 1
|
|
1780
|
+
|
|
1781
|
+
a = mx.array([2.0])
|
|
1782
|
+
b = a
|
|
1783
|
+
b /= 2
|
|
1784
|
+
self.assertEqual(b.item(), 1.0)
|
|
1785
|
+
self.assertEqual(b.item(), a.item())
|
|
1786
|
+
|
|
1787
|
+
a = mx.array(True)
|
|
1788
|
+
b = a
|
|
1789
|
+
b &= False
|
|
1790
|
+
self.assertEqual(b.item(), False)
|
|
1791
|
+
self.assertEqual(b.item(), a.item())
|
|
1792
|
+
|
|
1793
|
+
a = mx.array(False)
|
|
1794
|
+
b = a
|
|
1795
|
+
b |= True
|
|
1796
|
+
self.assertEqual(b.item(), True)
|
|
1797
|
+
self.assertEqual(b.item(), a.item())
|
|
1798
|
+
|
|
1799
|
+
# In-place matmul on its own
|
|
1800
|
+
a = mx.array([[1.0, 2.0], [3.0, 4.0]])
|
|
1801
|
+
b = a
|
|
1802
|
+
b @= a
|
|
1803
|
+
self.assertTrue(mx.array_equal(a, b))
|
|
1804
|
+
|
|
1805
|
+
a = mx.array(False)
|
|
1806
|
+
a ^= True
|
|
1807
|
+
self.assertEqual(a.item(), True)
|
|
1808
|
+
|
|
1809
|
+
def test_inplace_preserves_ids(self):
|
|
1810
|
+
a = mx.array([1.0])
|
|
1811
|
+
orig_id = id(a)
|
|
1812
|
+
a += mx.array(2.0)
|
|
1813
|
+
self.assertEqual(id(a), orig_id)
|
|
1814
|
+
|
|
1815
|
+
a[0] = 2.0
|
|
1816
|
+
self.assertEqual(id(a), orig_id)
|
|
1817
|
+
|
|
1818
|
+
a -= mx.array(3.0)
|
|
1819
|
+
self.assertEqual(id(a), orig_id)
|
|
1820
|
+
|
|
1821
|
+
a *= mx.array(3.0)
|
|
1822
|
+
self.assertEqual(id(a), orig_id)
|
|
1823
|
+
|
|
1824
|
+
def test_load_from_pickled_np(self):
|
|
1825
|
+
a = np.array([1, 2, 3], dtype=np.int32)
|
|
1826
|
+
b = pickle.loads(pickle.dumps(a))
|
|
1827
|
+
self.assertTrue(mx.array_equal(mx.array(a), mx.array(b)))
|
|
1828
|
+
|
|
1829
|
+
a = np.array([1.0, 2.0, 3.0], dtype=np.float16)
|
|
1830
|
+
b = pickle.loads(pickle.dumps(a))
|
|
1831
|
+
self.assertTrue(mx.array_equal(mx.array(a), mx.array(b)))
|
|
1832
|
+
|
|
1833
|
+
def test_multi_output_leak(self):
|
|
1834
|
+
def fun():
|
|
1835
|
+
a = mx.zeros((2**20))
|
|
1836
|
+
mx.eval(a)
|
|
1837
|
+
b, c = mx.divmod(a, a)
|
|
1838
|
+
del b, c
|
|
1839
|
+
|
|
1840
|
+
fun()
|
|
1841
|
+
mx.synchronize()
|
|
1842
|
+
peak_1 = mx.get_peak_memory()
|
|
1843
|
+
fun()
|
|
1844
|
+
mx.synchronize()
|
|
1845
|
+
peak_2 = mx.get_peak_memory()
|
|
1846
|
+
self.assertEqual(peak_1, peak_2)
|
|
1847
|
+
|
|
1848
|
+
def fun():
|
|
1849
|
+
a = mx.array([1.0, 2.0, 3.0, 4.0])
|
|
1850
|
+
b, _ = mx.divmod(a, a)
|
|
1851
|
+
return mx.log(b)
|
|
1852
|
+
|
|
1853
|
+
fun()
|
|
1854
|
+
mx.synchronize()
|
|
1855
|
+
peak_1 = mx.get_peak_memory()
|
|
1856
|
+
fun()
|
|
1857
|
+
mx.synchronize()
|
|
1858
|
+
peak_2 = mx.get_peak_memory()
|
|
1859
|
+
self.assertEqual(peak_1, peak_2)
|
|
1860
|
+
|
|
1861
|
+
def test_add_numpy(self):
|
|
1862
|
+
x = mx.array(1)
|
|
1863
|
+
y = np.array(2, dtype=np.int32)
|
|
1864
|
+
z = x + y
|
|
1865
|
+
self.assertEqual(z.dtype, mx.int32)
|
|
1866
|
+
self.assertEqual(z.item(), 3)
|
|
1867
|
+
|
|
1868
|
+
def test_dlpack(self):
|
|
1869
|
+
x = mx.array(1, dtype=mx.int32)
|
|
1870
|
+
y = np.from_dlpack(x)
|
|
1871
|
+
self.assertTrue(mx.array_equal(y, x))
|
|
1872
|
+
|
|
1873
|
+
x = mx.array([[1.0, 2.0], [3.0, 4.0]])
|
|
1874
|
+
y = np.from_dlpack(x)
|
|
1875
|
+
self.assertTrue(mx.array_equal(y, x))
|
|
1876
|
+
|
|
1877
|
+
x = mx.arange(16).reshape(4, 4)
|
|
1878
|
+
x = x[::2, ::2]
|
|
1879
|
+
y = np.from_dlpack(x)
|
|
1880
|
+
self.assertTrue(mx.array_equal(y, x))
|
|
1881
|
+
|
|
1882
|
+
def test_getitem_with_list(self):
|
|
1883
|
+
a = mx.array([1, 2, 3, 4, 5])
|
|
1884
|
+
idx = [0, 2, 4]
|
|
1885
|
+
self.assertTrue(np.array_equal(a[idx], np.array(a)[idx]))
|
|
1886
|
+
|
|
1887
|
+
a = mx.array([[1, 2], [3, 4], [5, 6]])
|
|
1888
|
+
idx = [0, 2]
|
|
1889
|
+
self.assertTrue(np.array_equal(a[idx], np.array(a)[idx]))
|
|
1890
|
+
|
|
1891
|
+
a = mx.arange(10).reshape(5, 2)
|
|
1892
|
+
idx = [0, 2, 4]
|
|
1893
|
+
self.assertTrue(np.array_equal(a[idx], np.array(a)[idx]))
|
|
1894
|
+
|
|
1895
|
+
idx = [0, 2]
|
|
1896
|
+
a = mx.arange(16).reshape(4, 4)
|
|
1897
|
+
anp = np.array(a)
|
|
1898
|
+
self.assertTrue(np.array_equal(a[idx, 0], anp[idx, 0]))
|
|
1899
|
+
self.assertTrue(np.array_equal(a[idx, :], anp[idx, :]))
|
|
1900
|
+
self.assertTrue(np.array_equal(a[0, idx], anp[0, idx]))
|
|
1901
|
+
self.assertTrue(np.array_equal(a[:, idx], anp[:, idx]))
|
|
1902
|
+
|
|
1903
|
+
def test_setitem_with_list(self):
|
|
1904
|
+
a = mx.array([1, 2, 3, 4, 5])
|
|
1905
|
+
anp = np.array(a)
|
|
1906
|
+
idx = [0, 2, 4]
|
|
1907
|
+
a[idx] = 3
|
|
1908
|
+
anp[idx] = 3
|
|
1909
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1910
|
+
|
|
1911
|
+
a = mx.array([[1, 2], [3, 4], [5, 6]])
|
|
1912
|
+
idx = [0, 2]
|
|
1913
|
+
anp = np.array(a)
|
|
1914
|
+
a[idx] = 3
|
|
1915
|
+
anp[idx] = 3
|
|
1916
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1917
|
+
|
|
1918
|
+
a = mx.arange(10).reshape(5, 2)
|
|
1919
|
+
idx = [0, 2, 4]
|
|
1920
|
+
anp = np.array(a)
|
|
1921
|
+
a[idx] = 3
|
|
1922
|
+
anp[idx] = 3
|
|
1923
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1924
|
+
|
|
1925
|
+
idx = [0, 2]
|
|
1926
|
+
a = mx.arange(16).reshape(4, 4)
|
|
1927
|
+
anp = np.array(a)
|
|
1928
|
+
a[idx, 0] = 1
|
|
1929
|
+
anp[idx, 0] = 1
|
|
1930
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1931
|
+
|
|
1932
|
+
a[idx, :] = 2
|
|
1933
|
+
anp[idx, :] = 2
|
|
1934
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1935
|
+
|
|
1936
|
+
a[0, idx] = 3
|
|
1937
|
+
anp[0, idx] = 3
|
|
1938
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1939
|
+
|
|
1940
|
+
a[:, idx] = 4
|
|
1941
|
+
anp[:, idx] = 4
|
|
1942
|
+
self.assertTrue(np.array_equal(a, anp))
|
|
1943
|
+
|
|
1944
|
+
def test_setitem_with_boolean_mask(self):
|
|
1945
|
+
# Python list mask
|
|
1946
|
+
a = mx.array([1.0, 2.0, 3.0])
|
|
1947
|
+
mask = [True, False, True]
|
|
1948
|
+
src = mx.array([5.0, 6.0])
|
|
1949
|
+
expected = mx.array([5.0, 2.0, 6.0])
|
|
1950
|
+
a[mask] = src
|
|
1951
|
+
self.assertTrue(mx.array_equal(a, expected))
|
|
1952
|
+
|
|
1953
|
+
# mx.array scalar mask
|
|
1954
|
+
a = mx.array([1.0, 2.0, 3.0])
|
|
1955
|
+
mask = mx.array(True)
|
|
1956
|
+
expected = mx.array([5.0, 5.0, 5.0])
|
|
1957
|
+
a[mask] = 5.0
|
|
1958
|
+
self.assertTrue(mx.array_equal(a, expected))
|
|
1959
|
+
|
|
1960
|
+
# scalar mask
|
|
1961
|
+
a = mx.array([1.0, 2.0, 3.0])
|
|
1962
|
+
mask = True
|
|
1963
|
+
expected = mx.array([5.0, 5.0, 5.0])
|
|
1964
|
+
a[mask] = 5.0
|
|
1965
|
+
self.assertTrue(mx.array_equal(a, expected))
|
|
1966
|
+
|
|
1967
|
+
mask_np = np.zeros((1, 10, 10), dtype=bool)
|
|
1968
|
+
with self.assertRaises(ValueError):
|
|
1969
|
+
mx.arange(1000).reshape(10, 10, 10)[mask_np] = 0
|
|
1970
|
+
|
|
1971
|
+
mask_np = np.zeros((10, 10, 1), dtype=bool)
|
|
1972
|
+
with self.assertRaises(ValueError):
|
|
1973
|
+
mx.arange(1000).reshape(10, 10, 10)[mask_np] = 0
|
|
1974
|
+
|
|
1975
|
+
def test_array_namespace(self):
|
|
1976
|
+
a = mx.array(1.0)
|
|
1977
|
+
api = a.__array_namespace__()
|
|
1978
|
+
self.assertTrue(hasattr(api, "array"))
|
|
1979
|
+
self.assertTrue(hasattr(api, "add"))
|
|
1980
|
+
|
|
1981
|
+
def test_array_namespace_asarray(self):
|
|
1982
|
+
xp = mx.array(1.0).__array_namespace__()
|
|
1983
|
+
self.assertTrue(hasattr(xp, "asarray"))
|
|
1984
|
+
|
|
1985
|
+
arr = xp.asarray([1, 2, 3])
|
|
1986
|
+
self.assertEqual(arr.tolist(), [1, 2, 3])
|
|
1987
|
+
|
|
1988
|
+
arr_f32 = xp.asarray([1, 2, 3], dtype=mx.float32)
|
|
1989
|
+
self.assertEqual(arr_f32.dtype, mx.float32)
|
|
1990
|
+
|
|
1991
|
+
existing = mx.array([4, 5, 6])
|
|
1992
|
+
arr_pass = xp.asarray(existing)
|
|
1993
|
+
self.assertEqual(arr_pass.tolist(), [4, 5, 6])
|
|
1994
|
+
|
|
1995
|
+
def test_asarray(self):
|
|
1996
|
+
# List inputs
|
|
1997
|
+
self.assertEqual(mx.asarray([1, 2, 3]).tolist(), [1, 2, 3])
|
|
1998
|
+
self.assertEqual(mx.asarray([[1, 2], [3, 4]]).tolist(), [[1, 2], [3, 4]])
|
|
1999
|
+
|
|
2000
|
+
# Tuple inputs
|
|
2001
|
+
self.assertEqual(mx.asarray((1, 2, 3)).tolist(), [1, 2, 3])
|
|
2002
|
+
self.assertEqual(mx.asarray(((1, 2), (3, 4))).tolist(), [[1, 2], [3, 4]])
|
|
2003
|
+
|
|
2004
|
+
# Mixed nesting
|
|
2005
|
+
self.assertEqual(mx.asarray([(1, 2), (3, 4)]).tolist(), [[1, 2], [3, 4]])
|
|
2006
|
+
self.assertEqual(mx.asarray(([1, 2], [3, 4])).tolist(), [[1, 2], [3, 4]])
|
|
2007
|
+
|
|
2008
|
+
# Scalar inputs
|
|
2009
|
+
self.assertEqual(mx.asarray(42).item(), 42)
|
|
2010
|
+
self.assertEqual(mx.asarray(3.14).item(), 3.140000104904175)
|
|
2011
|
+
self.assertEqual(mx.asarray(True).item(), True)
|
|
2012
|
+
self.assertEqual(mx.asarray(1 + 2j).item(), (1 + 2j))
|
|
2013
|
+
|
|
2014
|
+
# MLX array inputs
|
|
2015
|
+
arr = mx.array([1, 2, 3])
|
|
2016
|
+
self.assertEqual(mx.asarray(arr).tolist(), [1, 2, 3])
|
|
2017
|
+
|
|
2018
|
+
arr_int = mx.array([1, 2, 3], dtype=mx.int32)
|
|
2019
|
+
arr_float = mx.asarray(arr_int, dtype=mx.float32)
|
|
2020
|
+
self.assertEqual(arr_float.dtype, mx.float32)
|
|
2021
|
+
self.assertEqual(arr_float.tolist(), [1.0, 2.0, 3.0])
|
|
2022
|
+
|
|
2023
|
+
# NumPy array inputs
|
|
2024
|
+
np_arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
|
|
2025
|
+
mx_arr = mx.asarray(np_arr)
|
|
2026
|
+
self.assertEqual(mx_arr.tolist(), [1.0, 2.0, 3.0])
|
|
2027
|
+
self.assertEqual(mx_arr.dtype, mx.float32)
|
|
2028
|
+
|
|
2029
|
+
# dtype parameter
|
|
2030
|
+
self.assertEqual(mx.asarray([1, 2, 3], dtype=mx.float32).dtype, mx.float32)
|
|
2031
|
+
self.assertEqual(mx.asarray(42, dtype=mx.float16).dtype, mx.float16)
|
|
2032
|
+
|
|
2033
|
+
def test_to_scalar(self):
|
|
2034
|
+
a = mx.array(1)
|
|
2035
|
+
self.assertEqual(int(a), 1)
|
|
2036
|
+
self.assertEqual(float(a), 1)
|
|
2037
|
+
|
|
2038
|
+
a = mx.array(1.5)
|
|
2039
|
+
self.assertEqual(float(a), 1.5)
|
|
2040
|
+
self.assertEqual(int(a), 1)
|
|
2041
|
+
|
|
2042
|
+
a = mx.zeros((2, 1))
|
|
2043
|
+
with self.assertRaises(ValueError):
|
|
2044
|
+
float(a)
|
|
2045
|
+
with self.assertRaises(ValueError):
|
|
2046
|
+
int(a)
|
|
2047
|
+
|
|
2048
|
+
def test_format(self):
|
|
2049
|
+
a = mx.arange(3)
|
|
2050
|
+
self.assertEqual(f"{a[0]:.2f}", "0.00")
|
|
2051
|
+
|
|
2052
|
+
b = mx.array(0.35487)
|
|
2053
|
+
self.assertEqual(f"{b:.1f}", "0.4")
|
|
2054
|
+
|
|
2055
|
+
with self.assertRaises(TypeError):
|
|
2056
|
+
s = f"{a:.2f}"
|
|
2057
|
+
|
|
2058
|
+
a = mx.array([1, 2, 3])
|
|
2059
|
+
self.assertEqual(f"{a}", "array([1, 2, 3], dtype=int32)")
|
|
2060
|
+
|
|
2061
|
+
def test_deep_graphs(self):
|
|
2062
|
+
# The following tests should simply run cleanly without a segfault or
|
|
2063
|
+
# crash due to exceeding recursion depth limits.
|
|
2064
|
+
|
|
2065
|
+
# Deep graph destroyed without eval
|
|
2066
|
+
x = mx.array([1.0, 2.0])
|
|
2067
|
+
for _ in range(100_000):
|
|
2068
|
+
x = mx.sin(x)
|
|
2069
|
+
del x
|
|
2070
|
+
|
|
2071
|
+
# Duplicate input deep graph destroyed without eval
|
|
2072
|
+
x = mx.array([1.0, 2.0])
|
|
2073
|
+
for _ in range(100_000):
|
|
2074
|
+
x = x + x
|
|
2075
|
+
|
|
2076
|
+
# Deep graph with siblings destroyed without eval
|
|
2077
|
+
x = mx.array([1, 2])
|
|
2078
|
+
for _ in range(100_000):
|
|
2079
|
+
x = mx.concatenate(mx.split(x, 2))
|
|
2080
|
+
del x
|
|
2081
|
+
|
|
2082
|
+
# Deep graph with eval
|
|
2083
|
+
x = mx.array([1.0, 2.0])
|
|
2084
|
+
for _ in range(100_000):
|
|
2085
|
+
x = mx.sin(x)
|
|
2086
|
+
mx.eval(x)
|
|
2087
|
+
|
|
2088
|
+
def test_siblings_without_eval(self):
|
|
2089
|
+
def get_mem():
|
|
2090
|
+
if platform.system() == "Windows":
|
|
2091
|
+
process = psutil.Process(os.getpid())
|
|
2092
|
+
return process.memory_info().peak_wset
|
|
2093
|
+
else:
|
|
2094
|
+
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
|
2095
|
+
|
|
2096
|
+
key = mx.array([1, 2])
|
|
2097
|
+
|
|
2098
|
+
def t():
|
|
2099
|
+
a, b = mx.split(key, 2)
|
|
2100
|
+
a = mx.reshape(a, [])
|
|
2101
|
+
b = mx.reshape(b, [])
|
|
2102
|
+
return b
|
|
2103
|
+
|
|
2104
|
+
mx.synchronize()
|
|
2105
|
+
t()
|
|
2106
|
+
gc.collect()
|
|
2107
|
+
expected = get_mem()
|
|
2108
|
+
for _ in range(100):
|
|
2109
|
+
t()
|
|
2110
|
+
used = get_mem()
|
|
2111
|
+
self.assertEqual(expected, used)
|
|
2112
|
+
|
|
2113
|
+
def test_scalar_integer_conversion_overflow(self):
|
|
2114
|
+
y = mx.array(2000000000, dtype=mx.int32)
|
|
2115
|
+
x = 3000000000
|
|
2116
|
+
with self.assertRaises(ValueError):
|
|
2117
|
+
y + x
|
|
2118
|
+
with self.assertRaises(ValueError):
|
|
2119
|
+
mx.add(y, x)
|
|
2120
|
+
|
|
2121
|
+
def test_real_imag(self):
|
|
2122
|
+
x = mx.array([1.0])
|
|
2123
|
+
self.assertEqual(x.real.item(), 1.0)
|
|
2124
|
+
self.assertEqual(x.imag.item(), 0.0)
|
|
2125
|
+
|
|
2126
|
+
x = mx.array([1.0 + 1.0j])
|
|
2127
|
+
self.assertEqual(x.imag.item(), 1.0)
|
|
2128
|
+
self.assertEqual(x.real.item(), 1.0)
|
|
2129
|
+
|
|
2130
|
+
def test_large_indices(self):
|
|
2131
|
+
x = mx.array([0, 1, 2])
|
|
2132
|
+
with self.assertRaises(ValueError):
|
|
2133
|
+
x[: 2**32]
|
|
2134
|
+
with self.assertRaises(ValueError):
|
|
2135
|
+
x[2**32]
|
|
2136
|
+
|
|
2137
|
+
|
|
2138
|
+
if __name__ == "__main__":
|
|
2139
|
+
mlx_tests.MLXTestRunner()
|