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,1986 @@
|
|
|
1
|
+
# Copyright © 2023-2024 Apple Inc.
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import tempfile
|
|
5
|
+
import unittest
|
|
6
|
+
|
|
7
|
+
import mlx.core as mx
|
|
8
|
+
import mlx.nn as nn
|
|
9
|
+
import mlx_tests
|
|
10
|
+
import numpy as np
|
|
11
|
+
from mlx.utils import tree_flatten, tree_map, tree_reduce
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class TestBase(mlx_tests.MLXTestCase):
|
|
15
|
+
def test_module_utilities(self):
|
|
16
|
+
m = nn.Sequential(
|
|
17
|
+
nn.Sequential(nn.Linear(2, 10), nn.relu),
|
|
18
|
+
nn.Sequential(nn.Linear(10, 10), nn.ReLU()),
|
|
19
|
+
nn.Linear(10, 1),
|
|
20
|
+
mx.sigmoid,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
children = m.children()
|
|
24
|
+
self.assertTrue(isinstance(children, dict))
|
|
25
|
+
self.assertEqual(len(children), 1)
|
|
26
|
+
self.assertTrue(isinstance(children["layers"], list))
|
|
27
|
+
self.assertEqual(len(children["layers"]), 4)
|
|
28
|
+
self.assertEqual(children["layers"][3], {})
|
|
29
|
+
flat_children = tree_flatten(children, is_leaf=nn.Module.is_module)
|
|
30
|
+
self.assertEqual(len(flat_children), 3)
|
|
31
|
+
|
|
32
|
+
leaves = tree_flatten(m.leaf_modules(), is_leaf=nn.Module.is_module)
|
|
33
|
+
self.assertEqual(len(leaves), 4)
|
|
34
|
+
self.assertEqual(leaves[0][0], "layers.0.layers.0")
|
|
35
|
+
self.assertEqual(leaves[1][0], "layers.1.layers.0")
|
|
36
|
+
self.assertEqual(leaves[2][0], "layers.1.layers.1")
|
|
37
|
+
self.assertEqual(leaves[3][0], "layers.2")
|
|
38
|
+
self.assertTrue(leaves[0][1] is m.layers[0].layers[0])
|
|
39
|
+
self.assertTrue(leaves[1][1] is m.layers[1].layers[0])
|
|
40
|
+
self.assertTrue(leaves[2][1] is m.layers[1].layers[1])
|
|
41
|
+
self.assertTrue(leaves[3][1] is m.layers[2])
|
|
42
|
+
|
|
43
|
+
m.eval()
|
|
44
|
+
|
|
45
|
+
def assert_not_training(k, m):
|
|
46
|
+
self.assertFalse(m.training)
|
|
47
|
+
|
|
48
|
+
m.apply_to_modules(assert_not_training)
|
|
49
|
+
|
|
50
|
+
m.train()
|
|
51
|
+
|
|
52
|
+
def assert_training(k, m):
|
|
53
|
+
self.assertTrue(m.training)
|
|
54
|
+
|
|
55
|
+
m.apply_to_modules(assert_training)
|
|
56
|
+
|
|
57
|
+
def test_module_attributes(self):
|
|
58
|
+
class Model(nn.Module):
|
|
59
|
+
def __init__(self):
|
|
60
|
+
super().__init__()
|
|
61
|
+
self.val = None
|
|
62
|
+
self.initialize()
|
|
63
|
+
|
|
64
|
+
def initialize(self):
|
|
65
|
+
self.val = mx.array(1.0)
|
|
66
|
+
|
|
67
|
+
model = Model()
|
|
68
|
+
self.assertTrue(mx.array_equal(model.val, mx.array(1.0)))
|
|
69
|
+
|
|
70
|
+
model.val = None
|
|
71
|
+
self.assertEqual(model.val, None)
|
|
72
|
+
|
|
73
|
+
model.val = mx.array([3])
|
|
74
|
+
self.assertEqual(model.val.item(), 3)
|
|
75
|
+
|
|
76
|
+
def test_model_with_dict(self):
|
|
77
|
+
class DictModule(nn.Module):
|
|
78
|
+
def __init__(self):
|
|
79
|
+
super().__init__()
|
|
80
|
+
self.weights = {"w1": mx.zeros((2, 2)), "w2": mx.ones((2, 2))}
|
|
81
|
+
|
|
82
|
+
model = DictModule()
|
|
83
|
+
params = tree_flatten(model.parameters(), destination={})
|
|
84
|
+
self.assertEqual(len(params), 2)
|
|
85
|
+
self.assertTrue(mx.array_equal(params["weights.w1"], mx.zeros((2, 2))))
|
|
86
|
+
self.assertTrue(mx.array_equal(params["weights.w2"], mx.ones((2, 2))))
|
|
87
|
+
|
|
88
|
+
def test_save_npz_weights(self):
|
|
89
|
+
def make_model():
|
|
90
|
+
return nn.Sequential(nn.Linear(2, 2), nn.ReLU(), nn.Linear(2, 2))
|
|
91
|
+
|
|
92
|
+
m = make_model()
|
|
93
|
+
tdir = tempfile.TemporaryDirectory()
|
|
94
|
+
npz_file = os.path.join(tdir.name, "model.npz")
|
|
95
|
+
m.save_weights(npz_file)
|
|
96
|
+
m_load = make_model()
|
|
97
|
+
m_load.load_weights(npz_file)
|
|
98
|
+
|
|
99
|
+
# Eval before cleanup so model file is unlocked.
|
|
100
|
+
mx.eval(m_load.state)
|
|
101
|
+
tdir.cleanup()
|
|
102
|
+
|
|
103
|
+
eq_tree = tree_map(mx.array_equal, m.parameters(), m_load.parameters())
|
|
104
|
+
self.assertTrue(all(tree_flatten(eq_tree)))
|
|
105
|
+
|
|
106
|
+
def test_save_safetensors_weights(self):
|
|
107
|
+
def make_model():
|
|
108
|
+
return nn.Sequential(nn.Linear(2, 2), nn.ReLU(), nn.Linear(2, 2), nn.ReLU())
|
|
109
|
+
|
|
110
|
+
m = make_model()
|
|
111
|
+
tdir = tempfile.TemporaryDirectory()
|
|
112
|
+
safetensors_file = os.path.join(tdir.name, "model.safetensors")
|
|
113
|
+
m.save_weights(safetensors_file)
|
|
114
|
+
m_load = make_model()
|
|
115
|
+
m_load.load_weights(safetensors_file)
|
|
116
|
+
|
|
117
|
+
# Eval before cleanup so model file is unlocked.
|
|
118
|
+
mx.eval(m_load.state)
|
|
119
|
+
tdir.cleanup()
|
|
120
|
+
|
|
121
|
+
eq_tree = tree_map(mx.array_equal, m.parameters(), m_load.parameters())
|
|
122
|
+
self.assertTrue(all(tree_flatten(eq_tree)))
|
|
123
|
+
|
|
124
|
+
def test_load_from_weights(self):
|
|
125
|
+
m = nn.Linear(2, 2)
|
|
126
|
+
|
|
127
|
+
# Too few weights
|
|
128
|
+
weights = [("weight", mx.ones((2, 2)))]
|
|
129
|
+
with self.assertRaises(ValueError):
|
|
130
|
+
m.load_weights(weights)
|
|
131
|
+
|
|
132
|
+
m.load_weights(weights, strict=False)
|
|
133
|
+
self.assertTrue(mx.array_equal(m.weight, weights[0][1]))
|
|
134
|
+
|
|
135
|
+
# Wrong name
|
|
136
|
+
with self.assertRaises(ValueError):
|
|
137
|
+
m.load_weights([("weihgt", mx.ones((2, 2)))])
|
|
138
|
+
|
|
139
|
+
# Ok
|
|
140
|
+
m.load_weights([("weihgt", mx.ones((2, 2)))], strict=False)
|
|
141
|
+
|
|
142
|
+
# Too many weights
|
|
143
|
+
with self.assertRaises(ValueError):
|
|
144
|
+
m.load_weights(
|
|
145
|
+
[
|
|
146
|
+
("weight", mx.ones((2, 2))),
|
|
147
|
+
("bias", mx.ones((2,))),
|
|
148
|
+
("bias2", mx.ones((2,))),
|
|
149
|
+
]
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Wrong shape
|
|
153
|
+
with self.assertRaises(ValueError):
|
|
154
|
+
m.load_weights(
|
|
155
|
+
[
|
|
156
|
+
("weight", mx.ones((2, 2))),
|
|
157
|
+
("bias", mx.ones((2, 1))),
|
|
158
|
+
]
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
# Wrong type
|
|
162
|
+
with self.assertRaises(ValueError):
|
|
163
|
+
m.load_weights(
|
|
164
|
+
[
|
|
165
|
+
("weight", mx.ones((2, 2))),
|
|
166
|
+
("bias", 3),
|
|
167
|
+
]
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# Empty weights is ok if strict is false
|
|
171
|
+
m.load_weights([], strict=False)
|
|
172
|
+
|
|
173
|
+
def test_module_state(self):
|
|
174
|
+
m = nn.Linear(10, 1)
|
|
175
|
+
m.state["hello"] = "world"
|
|
176
|
+
self.assertEqual(m.state["hello"], "world")
|
|
177
|
+
|
|
178
|
+
def test_chaining(self):
|
|
179
|
+
m = nn.Sequential(nn.Linear(2, 2), nn.ReLU(), nn.Linear(2, 1))
|
|
180
|
+
pre_freeze_num_params = len(m.parameters())
|
|
181
|
+
m.freeze().unfreeze()
|
|
182
|
+
self.assertEqual(len(m.parameters()), pre_freeze_num_params)
|
|
183
|
+
params_dict = m.parameters()
|
|
184
|
+
|
|
185
|
+
self.assertFalse(m.update(params_dict).eval()._training)
|
|
186
|
+
self.assertTrue(m.train()._training)
|
|
187
|
+
|
|
188
|
+
def test_quantize(self):
|
|
189
|
+
m = nn.Sequential(nn.Embedding(5, 256), nn.ReLU(), nn.Linear(256, 256))
|
|
190
|
+
nn.quantize(m)
|
|
191
|
+
self.assertTrue(isinstance(m.layers[0], nn.QuantizedEmbedding))
|
|
192
|
+
self.assertTrue(isinstance(m.layers[1], nn.ReLU))
|
|
193
|
+
self.assertTrue(isinstance(m.layers[2], nn.QuantizedLinear))
|
|
194
|
+
|
|
195
|
+
m = nn.Sequential(nn.Embedding(5, 256), nn.ReLU(), nn.Linear(256, 256))
|
|
196
|
+
nn.quantize(m, class_predicate=lambda _, m: isinstance(m, nn.Linear))
|
|
197
|
+
self.assertTrue(isinstance(m.layers[0], nn.Embedding))
|
|
198
|
+
self.assertTrue(isinstance(m.layers[1], nn.ReLU))
|
|
199
|
+
self.assertTrue(isinstance(m.layers[2], nn.QuantizedLinear))
|
|
200
|
+
|
|
201
|
+
nn.quantize(m, group_size=32, mode="mxfp4")
|
|
202
|
+
self.assertTrue(isinstance(m.layers[0], nn.QuantizedEmbedding))
|
|
203
|
+
self.assertTrue(isinstance(m.layers[1], nn.ReLU))
|
|
204
|
+
self.assertTrue(isinstance(m.layers[2], nn.QuantizedLinear))
|
|
205
|
+
self.assertTrue(isinstance(m.layers[2].scales, mx.array))
|
|
206
|
+
|
|
207
|
+
m = nn.Sequential(
|
|
208
|
+
nn.Embedding(5, 256), nn.ReLU(), nn.Linear(256, 256, bias=False)
|
|
209
|
+
)
|
|
210
|
+
nn.quantize(
|
|
211
|
+
m,
|
|
212
|
+
group_size=32,
|
|
213
|
+
mode="mxfp8",
|
|
214
|
+
quantize_input=True,
|
|
215
|
+
class_predicate=lambda path, module: isinstance(module, nn.Linear),
|
|
216
|
+
)
|
|
217
|
+
self.assertTrue(isinstance(m.layers[0], nn.Embedding))
|
|
218
|
+
self.assertTrue(isinstance(m.layers[1], nn.ReLU))
|
|
219
|
+
self.assertTrue(isinstance(m.layers[2], nn.QQLinear))
|
|
220
|
+
|
|
221
|
+
# Check that Embedding does not support quantize_input
|
|
222
|
+
m = nn.Sequential(
|
|
223
|
+
nn.Embedding(5, 256), nn.ReLU(), nn.Linear(256, 256, bias=False)
|
|
224
|
+
)
|
|
225
|
+
with self.assertRaises(ValueError) as context:
|
|
226
|
+
nn.quantize(m, group_size=32, mode="mxfp8", quantize_input=True)
|
|
227
|
+
|
|
228
|
+
def test_quantize_freeze(self):
|
|
229
|
+
lin = nn.Linear(512, 512)
|
|
230
|
+
qlin = lin.to_quantized()
|
|
231
|
+
qlin.unfreeze(keys=["scales"])
|
|
232
|
+
size = tree_reduce(lambda acc, p: acc + p.size, qlin.trainable_parameters(), 0)
|
|
233
|
+
self.assertTrue(size > 0)
|
|
234
|
+
|
|
235
|
+
def test_quantized_sharded_linear_construction(self):
|
|
236
|
+
input_dims, output_dims = 1536, 1024
|
|
237
|
+
for bits in [2, 3, 4, 5, 6, 8]:
|
|
238
|
+
lin = nn.Linear(input_dims, output_dims)
|
|
239
|
+
qlin = lin.to_quantized(bits=bits)
|
|
240
|
+
|
|
241
|
+
slin1 = nn.QuantizedAllToShardedLinear.from_quantized_linear(qlin)
|
|
242
|
+
self.assertEqual(slin1.weight.shape, qlin.weight.shape)
|
|
243
|
+
|
|
244
|
+
slin2 = nn.QuantizedShardedToAllLinear.from_quantized_linear(qlin)
|
|
245
|
+
self.assertEqual(slin2.weight.shape, qlin.weight.shape)
|
|
246
|
+
|
|
247
|
+
def test_grad_of_module(self):
|
|
248
|
+
class Model(nn.Module):
|
|
249
|
+
def __init__(self):
|
|
250
|
+
super().__init__()
|
|
251
|
+
self.m1 = nn.Linear(3, 3)
|
|
252
|
+
|
|
253
|
+
model = Model()
|
|
254
|
+
|
|
255
|
+
def loss_fn(model):
|
|
256
|
+
return model.m1(x).sum()
|
|
257
|
+
|
|
258
|
+
x = mx.zeros((3,))
|
|
259
|
+
mx.grad(loss_fn)(model)
|
|
260
|
+
|
|
261
|
+
def test_update(self):
|
|
262
|
+
m = nn.Sequential(nn.Linear(3, 3), nn.Linear(3, 3))
|
|
263
|
+
|
|
264
|
+
# Updating non-existent parameters
|
|
265
|
+
with self.assertRaises(ValueError):
|
|
266
|
+
updates = {"layers": [{"value": 0}]}
|
|
267
|
+
m.update(updates)
|
|
268
|
+
|
|
269
|
+
with self.assertRaises(ValueError):
|
|
270
|
+
updates = {"layers": ["hello"]}
|
|
271
|
+
m.update(updates)
|
|
272
|
+
|
|
273
|
+
# Wronge type
|
|
274
|
+
with self.assertRaises(ValueError):
|
|
275
|
+
updates = {"layers": [{"weight": "hi"}]}
|
|
276
|
+
m.update(updates)
|
|
277
|
+
|
|
278
|
+
def test_update_modules(self):
|
|
279
|
+
m = nn.Sequential(nn.Linear(3, 3), nn.Linear(3, 3))
|
|
280
|
+
|
|
281
|
+
# Updating non-existent modules should not be allowed by default
|
|
282
|
+
with self.assertRaises(ValueError):
|
|
283
|
+
m = m.update_modules({"values": [0, 1]})
|
|
284
|
+
|
|
285
|
+
# Update wrong types
|
|
286
|
+
with self.assertRaises(ValueError):
|
|
287
|
+
m = m.update_modules({"layers": [0, 1]})
|
|
288
|
+
|
|
289
|
+
class MyModule(nn.Module):
|
|
290
|
+
def __init__(self):
|
|
291
|
+
super().__init__()
|
|
292
|
+
self.test = mx.array(1.0)
|
|
293
|
+
self.list = [mx.array(1.0), mx.array(2.0)]
|
|
294
|
+
|
|
295
|
+
m = MyModule()
|
|
296
|
+
with self.assertRaises(ValueError):
|
|
297
|
+
m = m.update_modules({"test": "hi"})
|
|
298
|
+
with self.assertRaises(ValueError):
|
|
299
|
+
m = m.update_modules({"list": ["hi"]})
|
|
300
|
+
|
|
301
|
+
# Allow updating a strict subset
|
|
302
|
+
m = nn.Sequential(nn.Linear(3, 3), nn.Linear(3, 3))
|
|
303
|
+
m.update_modules({"layers": [{}, nn.Linear(3, 4)]})
|
|
304
|
+
self.assertEqual(m.layers[1].weight.shape, (4, 3))
|
|
305
|
+
|
|
306
|
+
# Using leaf_modules in the update should always work
|
|
307
|
+
class MyModel(nn.Module):
|
|
308
|
+
def __init__(self):
|
|
309
|
+
super().__init__()
|
|
310
|
+
self.stuff = [nn.Linear(2, 2), 0, nn.Linear(2, 2)]
|
|
311
|
+
self.more_stuff = {"hi": nn.Linear(2, 2), "bye": 0}
|
|
312
|
+
|
|
313
|
+
m = MyModel()
|
|
314
|
+
m.update_modules(m.leaf_modules())
|
|
315
|
+
|
|
316
|
+
def test_parameter_deletion(self):
|
|
317
|
+
m = nn.Linear(32, 32)
|
|
318
|
+
del m.weight
|
|
319
|
+
self.assertFalse(hasattr(m, "weight"))
|
|
320
|
+
|
|
321
|
+
def test_circular_leaks(self):
|
|
322
|
+
y = mx.random.uniform(1)
|
|
323
|
+
mx.eval(y)
|
|
324
|
+
|
|
325
|
+
def make_and_update():
|
|
326
|
+
model = nn.Linear(1024, 512)
|
|
327
|
+
mx.eval(model.parameters())
|
|
328
|
+
leaves = {}
|
|
329
|
+
model.update_modules(leaves)
|
|
330
|
+
|
|
331
|
+
mx.synchronize()
|
|
332
|
+
pre = mx.get_active_memory()
|
|
333
|
+
make_and_update()
|
|
334
|
+
mx.synchronize()
|
|
335
|
+
post = mx.get_active_memory()
|
|
336
|
+
self.assertEqual(pre, post)
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
class TestLayers(mlx_tests.MLXTestCase):
|
|
340
|
+
def test_identity(self):
|
|
341
|
+
inputs = mx.zeros((10, 4))
|
|
342
|
+
layer = nn.Identity()
|
|
343
|
+
outputs = layer(inputs)
|
|
344
|
+
self.assertEqual(inputs.shape, outputs.shape)
|
|
345
|
+
|
|
346
|
+
def test_linear(self):
|
|
347
|
+
inputs = mx.zeros((10, 4))
|
|
348
|
+
layer = nn.Linear(input_dims=4, output_dims=8)
|
|
349
|
+
outputs = layer(inputs)
|
|
350
|
+
self.assertEqual(outputs.shape, (10, 8))
|
|
351
|
+
|
|
352
|
+
def test_bilinear(self):
|
|
353
|
+
inputs1 = mx.zeros((10, 2))
|
|
354
|
+
inputs2 = mx.zeros((10, 4))
|
|
355
|
+
layer = nn.Bilinear(input1_dims=2, input2_dims=4, output_dims=6)
|
|
356
|
+
outputs = layer(inputs1, inputs2)
|
|
357
|
+
self.assertEqual(outputs.shape, (10, 6))
|
|
358
|
+
|
|
359
|
+
def test_group_norm(self):
|
|
360
|
+
x = mx.arange(100, dtype=mx.float32)
|
|
361
|
+
x = x.reshape(1, 10, 10, 1)
|
|
362
|
+
x = mx.broadcast_to(x, (2, 10, 10, 4))
|
|
363
|
+
x = mx.concatenate([x, 0.5 * x], axis=-1)
|
|
364
|
+
|
|
365
|
+
# Group norm in groups last mode
|
|
366
|
+
g = nn.GroupNorm(2, 8)
|
|
367
|
+
y = g(x)
|
|
368
|
+
means = y.reshape(2, -1, 2).mean(axis=1)
|
|
369
|
+
var = y.reshape(2, -1, 2).var(axis=1)
|
|
370
|
+
self.assertTrue(np.allclose(means, np.zeros_like(means), atol=1e-6))
|
|
371
|
+
self.assertTrue(np.allclose(var, np.ones_like(var), atol=1e-6))
|
|
372
|
+
g.weight = g.weight * 2
|
|
373
|
+
g.bias = g.bias + 3
|
|
374
|
+
y = g(x)
|
|
375
|
+
means = y.reshape(2, -1, 2).mean(axis=1)
|
|
376
|
+
var = y.reshape(2, -1, 2).var(axis=1)
|
|
377
|
+
self.assertTrue(np.allclose(means, 3 * np.ones_like(means), atol=1e-6))
|
|
378
|
+
self.assertTrue(np.allclose(var, 4 * np.ones_like(var), atol=1e-6))
|
|
379
|
+
|
|
380
|
+
# Group norm in groups first mode
|
|
381
|
+
g = nn.GroupNorm(2, 8, pytorch_compatible=True)
|
|
382
|
+
y = g(x)
|
|
383
|
+
means = y.reshape(2, -1, 2, 4).mean(axis=(1, -1))
|
|
384
|
+
var = y.reshape(2, -1, 2, 4).var(axis=(1, -1))
|
|
385
|
+
self.assertTrue(np.allclose(means, np.zeros_like(means), atol=1e-6))
|
|
386
|
+
self.assertTrue(np.allclose(var, np.ones_like(var), atol=1e-6))
|
|
387
|
+
g.weight = g.weight * 2
|
|
388
|
+
g.bias = g.bias + 3
|
|
389
|
+
y = g(x)
|
|
390
|
+
means = y.reshape(2, -1, 2, 4).mean(axis=(1, -1))
|
|
391
|
+
var = y.reshape(2, -1, 2, 4).var(axis=(1, -1))
|
|
392
|
+
self.assertTrue(np.allclose(means, 3 * np.ones_like(means), atol=1e-6))
|
|
393
|
+
self.assertTrue(np.allclose(var, 4 * np.ones_like(var), atol=1e-6))
|
|
394
|
+
|
|
395
|
+
def test_instance_norm(self):
|
|
396
|
+
# Test InstanceNorm1d
|
|
397
|
+
x = mx.array(
|
|
398
|
+
[
|
|
399
|
+
[
|
|
400
|
+
[-0.0119524, 1.1263, 2.02223],
|
|
401
|
+
[-0.500331, 0.517899, -1.21143],
|
|
402
|
+
[1.12958, -0.21413, -2.48738],
|
|
403
|
+
[1.39955, 0.891329, 1.63289],
|
|
404
|
+
],
|
|
405
|
+
[
|
|
406
|
+
[0.241417, -0.619157, -0.77484],
|
|
407
|
+
[-1.42512, 0.970817, -1.31352],
|
|
408
|
+
[2.739, -1.2506, 1.56844],
|
|
409
|
+
[-1.23175, 0.32756, 1.13969],
|
|
410
|
+
],
|
|
411
|
+
]
|
|
412
|
+
)
|
|
413
|
+
inorm = nn.InstanceNorm(dims=3)
|
|
414
|
+
y = inorm(x)
|
|
415
|
+
expected_y = [
|
|
416
|
+
[
|
|
417
|
+
[-0.657082, 1.07593, 1.0712],
|
|
418
|
+
[-1.27879, -0.123074, -0.632505],
|
|
419
|
+
[0.796101, -1.56572, -1.30476],
|
|
420
|
+
[1.13978, 0.612862, 0.866067],
|
|
421
|
+
],
|
|
422
|
+
[
|
|
423
|
+
[0.0964426, -0.557906, -0.759885],
|
|
424
|
+
[-0.904772, 1.30444, -1.20013],
|
|
425
|
+
[1.59693, -1.29752, 1.15521],
|
|
426
|
+
[-0.7886, 0.550987, 0.804807],
|
|
427
|
+
],
|
|
428
|
+
]
|
|
429
|
+
self.assertTrue(x.shape == y.shape)
|
|
430
|
+
self.assertTrue(np.allclose(y, expected_y, atol=1e-5))
|
|
431
|
+
# Test InstanceNorm2d
|
|
432
|
+
x = mx.array(
|
|
433
|
+
[
|
|
434
|
+
[
|
|
435
|
+
[
|
|
436
|
+
[-0.458824, 0.483254, -0.58611],
|
|
437
|
+
[-0.447996, -0.176577, -0.622545],
|
|
438
|
+
[0.0486988, -0.0611224, 1.8845],
|
|
439
|
+
],
|
|
440
|
+
[
|
|
441
|
+
[1.13049, 0.345315, -0.926389],
|
|
442
|
+
[0.301795, 0.99207, -0.184927],
|
|
443
|
+
[-2.23876, -0.758631, -1.12639],
|
|
444
|
+
],
|
|
445
|
+
[
|
|
446
|
+
[0.0986325, -1.82973, -0.241765],
|
|
447
|
+
[-1.25257, 0.154442, -0.556204],
|
|
448
|
+
[-0.329399, -0.319107, 0.830584],
|
|
449
|
+
],
|
|
450
|
+
],
|
|
451
|
+
[
|
|
452
|
+
[
|
|
453
|
+
[1.04407, 0.073752, 0.407081],
|
|
454
|
+
[0.0800776, 1.2513, 1.20627],
|
|
455
|
+
[0.782321, -0.444367, 0.563132],
|
|
456
|
+
],
|
|
457
|
+
[
|
|
458
|
+
[0.671423, -1.21689, -1.88979],
|
|
459
|
+
[-0.110299, -1.42248, 1.17838],
|
|
460
|
+
[0.159905, 0.516452, -0.539121],
|
|
461
|
+
],
|
|
462
|
+
[
|
|
463
|
+
[0.810252, 1.50456, 1.08659],
|
|
464
|
+
[0.182597, 0.0576239, 0.973883],
|
|
465
|
+
[-0.0621687, 0.184253, 0.784216],
|
|
466
|
+
],
|
|
467
|
+
],
|
|
468
|
+
]
|
|
469
|
+
)
|
|
470
|
+
inorm = nn.InstanceNorm(dims=3)
|
|
471
|
+
y = inorm(x)
|
|
472
|
+
expected_y = [
|
|
473
|
+
[
|
|
474
|
+
[
|
|
475
|
+
[-0.120422, 0.801503, -0.463983],
|
|
476
|
+
[-0.108465, -0.0608611, -0.504602],
|
|
477
|
+
[0.440008, 0.090032, 2.29032],
|
|
478
|
+
],
|
|
479
|
+
[
|
|
480
|
+
[1.63457, 0.621224, -0.843335],
|
|
481
|
+
[0.719488, 1.4665, -0.0167344],
|
|
482
|
+
[-2.08591, -0.821575, -1.0663],
|
|
483
|
+
],
|
|
484
|
+
[
|
|
485
|
+
[0.495147, -2.22145, -0.0800989],
|
|
486
|
+
[-0.996913, 0.371763, -0.430643],
|
|
487
|
+
[0.022495, -0.24714, 1.11538],
|
|
488
|
+
],
|
|
489
|
+
],
|
|
490
|
+
[
|
|
491
|
+
[
|
|
492
|
+
[1.5975, 0.0190292, -0.0123306],
|
|
493
|
+
[-0.776381, 1.28291, 0.817237],
|
|
494
|
+
[0.952927, -0.537076, 0.149652],
|
|
495
|
+
],
|
|
496
|
+
[
|
|
497
|
+
[0.679836, -1.36624, -2.39651],
|
|
498
|
+
[-1.24519, -1.5869, 0.788287],
|
|
499
|
+
[-0.579802, 0.494186, -0.994499],
|
|
500
|
+
],
|
|
501
|
+
[
|
|
502
|
+
[1.02171, 1.55474, 0.693008],
|
|
503
|
+
[-0.523922, 0.00171862, 0.576016],
|
|
504
|
+
[-1.12667, 0.137632, 0.37914],
|
|
505
|
+
],
|
|
506
|
+
],
|
|
507
|
+
]
|
|
508
|
+
self.assertTrue(x.shape == y.shape)
|
|
509
|
+
self.assertTrue(np.allclose(y, expected_y, atol=1e-5))
|
|
510
|
+
# # Test InstanceNorm3d
|
|
511
|
+
x = mx.array(
|
|
512
|
+
[
|
|
513
|
+
[
|
|
514
|
+
[
|
|
515
|
+
[[0.777621, 0.528145, -1.56133], [-2.1722, 0.128192, 0.153862]],
|
|
516
|
+
[
|
|
517
|
+
[-1.41317, 0.476288, -1.20411],
|
|
518
|
+
[0.284446, -0.649858, 0.152112],
|
|
519
|
+
],
|
|
520
|
+
],
|
|
521
|
+
[
|
|
522
|
+
[[0.11, -0.12431, 1.18768], [-0.837743, 1.93502, 0.00236324]],
|
|
523
|
+
[
|
|
524
|
+
[-2.40205, -1.25873, -2.04243],
|
|
525
|
+
[0.336682, -0.261986, 1.54289],
|
|
526
|
+
],
|
|
527
|
+
],
|
|
528
|
+
[
|
|
529
|
+
[
|
|
530
|
+
[0.789185, -1.63747, 0.67917],
|
|
531
|
+
[-1.42998, -1.73247, -0.402572],
|
|
532
|
+
],
|
|
533
|
+
[
|
|
534
|
+
[-0.459489, -2.15559, -0.249959],
|
|
535
|
+
[0.0298199, 0.10275, -0.821897],
|
|
536
|
+
],
|
|
537
|
+
],
|
|
538
|
+
],
|
|
539
|
+
[
|
|
540
|
+
[
|
|
541
|
+
[
|
|
542
|
+
[-2.12354, 0.643973, 0.72391],
|
|
543
|
+
[0.317797, -0.682916, 0.016364],
|
|
544
|
+
],
|
|
545
|
+
[
|
|
546
|
+
[-0.146628, -0.987925, 0.573199],
|
|
547
|
+
[0.0329215, 1.54086, 0.213092],
|
|
548
|
+
],
|
|
549
|
+
],
|
|
550
|
+
[
|
|
551
|
+
[
|
|
552
|
+
[-1.55784, 0.71179, -0.0678402],
|
|
553
|
+
[2.41031, -0.290786, 0.00449439],
|
|
554
|
+
],
|
|
555
|
+
[
|
|
556
|
+
[0.226341, 0.057712, -1.58342],
|
|
557
|
+
[0.265387, -0.742304, 1.28133],
|
|
558
|
+
],
|
|
559
|
+
],
|
|
560
|
+
[
|
|
561
|
+
[
|
|
562
|
+
[0.990317, -0.399875, -0.357647],
|
|
563
|
+
[0.475161, -1.10479, -1.07389],
|
|
564
|
+
],
|
|
565
|
+
[
|
|
566
|
+
[-1.37804, 1.40097, 0.141618],
|
|
567
|
+
[-0.501041, 0.0723374, -0.386141],
|
|
568
|
+
],
|
|
569
|
+
],
|
|
570
|
+
],
|
|
571
|
+
]
|
|
572
|
+
)
|
|
573
|
+
inorm = nn.InstanceNorm(dims=3)
|
|
574
|
+
y = inorm(x)
|
|
575
|
+
expected_y = [
|
|
576
|
+
[
|
|
577
|
+
[
|
|
578
|
+
[[1.23593, 0.821849, -1.30944], [-1.54739, 0.462867, 0.357126]],
|
|
579
|
+
[[-0.831204, 0.775304, -0.962338], [0.770588, -0.23548, 0.355425]],
|
|
580
|
+
],
|
|
581
|
+
[
|
|
582
|
+
[[0.605988, 0.236231, 1.36163], [-0.288258, 2.0846, 0.209922]],
|
|
583
|
+
[[-1.76427, -0.78198, -1.77689], [0.819875, 0.112659, 1.70677]],
|
|
584
|
+
],
|
|
585
|
+
[
|
|
586
|
+
[[1.24684, -1.12192, 0.867539], [-0.847068, -1.20719, -0.183531]],
|
|
587
|
+
[
|
|
588
|
+
[0.0686449, -1.58697, -0.0352458],
|
|
589
|
+
[0.530334, 0.440032, -0.590967],
|
|
590
|
+
],
|
|
591
|
+
],
|
|
592
|
+
],
|
|
593
|
+
[
|
|
594
|
+
[
|
|
595
|
+
[[-1.75315, 0.733967, 1.04349], [0.343736, -0.822472, 0.080661]],
|
|
596
|
+
[[-0.0551618, -1.18025, 0.838402], [0.0990544, 1.78602, 0.348368]],
|
|
597
|
+
],
|
|
598
|
+
[
|
|
599
|
+
[[-1.26726, 0.813517, -0.033924], [2.14101, -0.362504, 0.0645089]],
|
|
600
|
+
[[0.265184, 0.0462839, -2.09632], [0.298721, -0.892134, 1.80203]],
|
|
601
|
+
],
|
|
602
|
+
[
|
|
603
|
+
[[0.921369, -0.490465, -0.428293], [0.478897, -1.31732, -1.40296]],
|
|
604
|
+
[[-1.11283, 1.62192, 0.251107], [-0.35957, 0.0634394, -0.467067]],
|
|
605
|
+
],
|
|
606
|
+
],
|
|
607
|
+
]
|
|
608
|
+
self.assertTrue(x.shape == y.shape)
|
|
609
|
+
self.assertTrue(np.allclose(y, expected_y, atol=1e-5))
|
|
610
|
+
# Test repr
|
|
611
|
+
self.assertTrue(str(inorm) == "InstanceNorm(3, eps=1e-05, affine=False)")
|
|
612
|
+
|
|
613
|
+
def test_batch_norm(self):
|
|
614
|
+
mx.random.seed(42)
|
|
615
|
+
x = mx.random.normal((5, 4), dtype=mx.float32)
|
|
616
|
+
|
|
617
|
+
# Batch norm
|
|
618
|
+
bn = nn.BatchNorm(num_features=4, affine=True)
|
|
619
|
+
self.assertTrue(mx.allclose(bn.running_mean, mx.zeros_like(bn.running_mean)))
|
|
620
|
+
self.assertTrue(mx.allclose(bn.running_var, mx.ones_like(bn.running_var)))
|
|
621
|
+
y = bn(x)
|
|
622
|
+
expected_y = mx.array(
|
|
623
|
+
[
|
|
624
|
+
[-0.439520, 1.647328, -0.955515, 1.966031],
|
|
625
|
+
[-1.726690, -1.449826, -0.234026, -0.723364],
|
|
626
|
+
[0.938414, -0.349603, -0.354470, -0.175369],
|
|
627
|
+
[0.305006, 0.234914, -0.393017, -0.459385],
|
|
628
|
+
[0.922789, -0.082813, 1.937028, -0.607913],
|
|
629
|
+
],
|
|
630
|
+
)
|
|
631
|
+
expected_mean = mx.array([0.008929, 0.005680, -0.016092, 0.027778])
|
|
632
|
+
expected_var = mx.array([0.928435, 1.00455, 1.04117, 0.94258])
|
|
633
|
+
self.assertTrue(x.shape == y.shape)
|
|
634
|
+
self.assertTrue(mx.allclose(y, expected_y, atol=1e-5))
|
|
635
|
+
self.assertTrue(mx.allclose(bn.running_mean, expected_mean, atol=1e-5))
|
|
636
|
+
self.assertTrue(mx.allclose(bn.running_var, expected_var, atol=1e-5))
|
|
637
|
+
|
|
638
|
+
# test eval mode
|
|
639
|
+
bn.eval()
|
|
640
|
+
y = bn(x)
|
|
641
|
+
expected_y = mx.array(
|
|
642
|
+
[
|
|
643
|
+
[-0.15984, 1.73159, -1.25456, 1.57891],
|
|
644
|
+
[-0.872193, -1.4281, -0.414439, -0.228678],
|
|
645
|
+
[0.602743, -0.30566, -0.554687, 0.139639],
|
|
646
|
+
[0.252199, 0.29066, -0.599572, -0.0512532],
|
|
647
|
+
[0.594096, -0.0334829, 2.11359, -0.151081],
|
|
648
|
+
]
|
|
649
|
+
)
|
|
650
|
+
|
|
651
|
+
self.assertTrue(x.shape == y.shape)
|
|
652
|
+
self.assertTrue(mx.allclose(y, expected_y, atol=1e-5))
|
|
653
|
+
|
|
654
|
+
# test_no_affine
|
|
655
|
+
bn = nn.BatchNorm(num_features=4, affine=False)
|
|
656
|
+
y = bn(x)
|
|
657
|
+
expected_y = mx.array(
|
|
658
|
+
[
|
|
659
|
+
[-0.439520, 1.647328, -0.955515, 1.966031],
|
|
660
|
+
[-1.726690, -1.449826, -0.234026, -0.723364],
|
|
661
|
+
[0.938414, -0.349603, -0.354470, -0.175369],
|
|
662
|
+
[0.305006, 0.234914, -0.393017, -0.459385],
|
|
663
|
+
[0.922789, -0.082813, 1.937028, -0.607913],
|
|
664
|
+
]
|
|
665
|
+
)
|
|
666
|
+
self.assertTrue(x.shape == y.shape)
|
|
667
|
+
self.assertTrue(mx.allclose(y, expected_y, atol=1e-5))
|
|
668
|
+
|
|
669
|
+
# test with 3D input
|
|
670
|
+
mx.random.seed(42)
|
|
671
|
+
N = 2
|
|
672
|
+
L = 4
|
|
673
|
+
C = 5
|
|
674
|
+
x = mx.random.normal((N, L, C), dtype=mx.float32)
|
|
675
|
+
|
|
676
|
+
# Batch norm
|
|
677
|
+
bn = nn.BatchNorm(num_features=C, affine=True)
|
|
678
|
+
self.assertTrue(mx.allclose(bn.running_mean, mx.zeros_like(bn.running_mean)))
|
|
679
|
+
self.assertTrue(mx.allclose(bn.running_var, mx.ones_like(bn.running_var)))
|
|
680
|
+
y = bn(x)
|
|
681
|
+
self.assertTrue(x.shape == y.shape)
|
|
682
|
+
expected_y = mx.array(
|
|
683
|
+
[
|
|
684
|
+
[
|
|
685
|
+
[-0.335754, 0.342054, 1.02653, 0.628588, -1.63899],
|
|
686
|
+
[1.92092, 0.432319, 0.343043, 1.95489, 1.0696],
|
|
687
|
+
[-0.853748, 1.3661, 0.868569, 0.0199196, -0.887284],
|
|
688
|
+
[0.459206, -0.684822, -0.706354, -0.271531, 0.566341],
|
|
689
|
+
],
|
|
690
|
+
[
|
|
691
|
+
[-0.921179, 0.684951, -0.77466, -0.490372, -0.247032],
|
|
692
|
+
[1.10839, -2.13179, 0.628924, -1.62639, -0.539708],
|
|
693
|
+
[-0.348943, 0.412194, -2.03818, 0.524972, 1.64568],
|
|
694
|
+
[-1.02889, -0.421, 0.652127, -0.740079, 0.0313996],
|
|
695
|
+
],
|
|
696
|
+
]
|
|
697
|
+
)
|
|
698
|
+
self.assertTrue(mx.allclose(y, expected_y, atol=1e-5))
|
|
699
|
+
expected_mean = mx.array(
|
|
700
|
+
[[[0.00207845, -5.3259e-05, 0.04755, -0.0697296, 0.0236228]]]
|
|
701
|
+
)
|
|
702
|
+
expected_var = mx.array([[[0.968415, 1.05322, 0.96913, 0.932305, 0.967224]]])
|
|
703
|
+
self.assertTrue(mx.allclose(bn.running_mean, expected_mean, atol=1e-5))
|
|
704
|
+
self.assertTrue(mx.allclose(bn.running_var, expected_var, atol=1e-5))
|
|
705
|
+
|
|
706
|
+
x = mx.random.normal((N, L, C, L, C), dtype=mx.float32)
|
|
707
|
+
with self.assertRaises(ValueError):
|
|
708
|
+
y = bn(x)
|
|
709
|
+
|
|
710
|
+
# Check that the running stats are in the param dictionary
|
|
711
|
+
bn_parameters = bn.parameters()
|
|
712
|
+
self.assertIn("running_mean", bn_parameters)
|
|
713
|
+
self.assertIn("running_var", bn_parameters)
|
|
714
|
+
self.assertIn("weight", bn_parameters)
|
|
715
|
+
self.assertIn("bias", bn_parameters)
|
|
716
|
+
|
|
717
|
+
bn_trainable = bn.trainable_parameters()
|
|
718
|
+
self.assertNotIn("running_mean", bn_trainable)
|
|
719
|
+
self.assertNotIn("running_var", bn_trainable)
|
|
720
|
+
self.assertIn("weight", bn_trainable)
|
|
721
|
+
self.assertIn("bias", bn_trainable)
|
|
722
|
+
|
|
723
|
+
bn.unfreeze()
|
|
724
|
+
bn_trainable = bn.trainable_parameters()
|
|
725
|
+
self.assertNotIn("running_mean", bn_trainable)
|
|
726
|
+
self.assertNotIn("running_var", bn_trainable)
|
|
727
|
+
self.assertIn("weight", bn_trainable)
|
|
728
|
+
self.assertIn("bias", bn_trainable)
|
|
729
|
+
|
|
730
|
+
def test_batch_norm_stats(self):
|
|
731
|
+
batch_size = 2
|
|
732
|
+
num_features = 4
|
|
733
|
+
h = 3
|
|
734
|
+
w = 3
|
|
735
|
+
momentum = 0.1
|
|
736
|
+
|
|
737
|
+
batch_norm = nn.BatchNorm(num_features)
|
|
738
|
+
|
|
739
|
+
batch_norm.train()
|
|
740
|
+
running_mean = batch_norm.running_mean
|
|
741
|
+
running_var = batch_norm.running_var
|
|
742
|
+
|
|
743
|
+
data = mx.random.normal((batch_size, num_features))
|
|
744
|
+
|
|
745
|
+
normalized_data = batch_norm(data)
|
|
746
|
+
means = mx.mean(data, axis=0)
|
|
747
|
+
variances = mx.var(data, axis=0)
|
|
748
|
+
running_mean = (1 - momentum) * running_mean + momentum * means
|
|
749
|
+
running_var = (1 - momentum) * running_var + momentum * variances
|
|
750
|
+
self.assertTrue(mx.allclose(batch_norm.running_mean, running_mean, atol=1e-5))
|
|
751
|
+
self.assertTrue(mx.allclose(batch_norm.running_var, running_var, atol=1e-5))
|
|
752
|
+
|
|
753
|
+
batch_norm = nn.BatchNorm(num_features)
|
|
754
|
+
|
|
755
|
+
batch_norm.train()
|
|
756
|
+
running_mean = batch_norm.running_mean
|
|
757
|
+
running_var = batch_norm.running_var
|
|
758
|
+
data = mx.random.normal((batch_size, h, w, num_features))
|
|
759
|
+
|
|
760
|
+
normalized_data = batch_norm(data)
|
|
761
|
+
means = mx.mean(data, axis=(0, 1, 2))
|
|
762
|
+
variances = mx.var(data, axis=(0, 1, 2))
|
|
763
|
+
running_mean = (1 - momentum) * running_mean + momentum * means
|
|
764
|
+
running_var = (1 - momentum) * running_var + momentum * variances
|
|
765
|
+
self.assertTrue(mx.allclose(batch_norm.running_mean, running_mean, atol=1e-5))
|
|
766
|
+
self.assertTrue(mx.allclose(batch_norm.running_var, running_var, atol=1e-5))
|
|
767
|
+
|
|
768
|
+
self.assertEqual(batch_norm.running_mean.shape, running_mean.shape)
|
|
769
|
+
self.assertEqual(batch_norm.running_var.shape, running_var.shape)
|
|
770
|
+
|
|
771
|
+
def test_conv1d(self):
|
|
772
|
+
N = 5
|
|
773
|
+
L = 12
|
|
774
|
+
ks = 3
|
|
775
|
+
C_in = 2
|
|
776
|
+
C_out = 4
|
|
777
|
+
x = mx.ones((N, L, C_in))
|
|
778
|
+
c = nn.Conv1d(in_channels=C_in, out_channels=C_out, kernel_size=ks)
|
|
779
|
+
c.weight = mx.ones_like(c.weight)
|
|
780
|
+
y = c(x)
|
|
781
|
+
self.assertEqual(y.shape, (N, L - ks + 1, C_out))
|
|
782
|
+
self.assertTrue(mx.allclose(y, mx.full(y.shape, ks * C_in, mx.float32)))
|
|
783
|
+
|
|
784
|
+
c = nn.Conv1d(in_channels=C_in, out_channels=C_out, kernel_size=ks, stride=2)
|
|
785
|
+
y = c(x)
|
|
786
|
+
self.assertEqual(y.shape, (N, (L - ks + 1) // 2, C_out))
|
|
787
|
+
self.assertTrue("bias" in c.parameters())
|
|
788
|
+
|
|
789
|
+
dil = 2
|
|
790
|
+
c = nn.Conv1d(
|
|
791
|
+
in_channels=C_in, out_channels=C_out, kernel_size=ks, dilation=dil
|
|
792
|
+
)
|
|
793
|
+
y = c(x)
|
|
794
|
+
self.assertEqual(y.shape, (N, L - (ks - 1) * dil, C_out))
|
|
795
|
+
|
|
796
|
+
c = nn.Conv1d(in_channels=C_in, out_channels=C_out, kernel_size=ks, bias=False)
|
|
797
|
+
self.assertTrue("bias" not in c.parameters())
|
|
798
|
+
|
|
799
|
+
groups = C_in
|
|
800
|
+
c = nn.Conv1d(
|
|
801
|
+
in_channels=C_in, out_channels=C_out, kernel_size=ks, groups=groups
|
|
802
|
+
)
|
|
803
|
+
y = c(x)
|
|
804
|
+
self.assertEqual(c.weight.shape, (C_out, ks, C_in // groups))
|
|
805
|
+
self.assertEqual(y.shape, (N, L - ks + 1, C_out))
|
|
806
|
+
|
|
807
|
+
def test_conv2d(self):
|
|
808
|
+
x = mx.ones((4, 8, 8, 3))
|
|
809
|
+
c = nn.Conv2d(3, 1, 8)
|
|
810
|
+
y = c(x)
|
|
811
|
+
self.assertEqual(y.shape, (4, 1, 1, 1))
|
|
812
|
+
c.weight = mx.ones_like(c.weight) / 8 / 8 / 3
|
|
813
|
+
y = c(x)
|
|
814
|
+
self.assertTrue(np.allclose(y[:, 0, 0, 0], x.mean(axis=(1, 2, 3))))
|
|
815
|
+
|
|
816
|
+
# 3x3 conv no padding stride 1
|
|
817
|
+
c = nn.Conv2d(3, 8, 3)
|
|
818
|
+
y = c(x)
|
|
819
|
+
self.assertEqual(y.shape, (4, 6, 6, 8))
|
|
820
|
+
self.assertLess(mx.abs(y - c.weight.sum((1, 2, 3))).max(), 1e-4)
|
|
821
|
+
|
|
822
|
+
# 3x3 conv padding 1 stride 1
|
|
823
|
+
c = nn.Conv2d(3, 8, 3, padding=1)
|
|
824
|
+
y = c(x)
|
|
825
|
+
self.assertEqual(y.shape, (4, 8, 8, 8))
|
|
826
|
+
self.assertLess(mx.abs(y[:, 1:7, 1:7] - c.weight.sum((1, 2, 3))).max(), 1e-4)
|
|
827
|
+
self.assertLess(
|
|
828
|
+
mx.abs(y[:, 0, 0] - c.weight[:, 1:, 1:].sum(axis=(1, 2, 3))).max(),
|
|
829
|
+
1e-4,
|
|
830
|
+
)
|
|
831
|
+
self.assertLess(
|
|
832
|
+
mx.abs(y[:, 7, 7] - c.weight[:, :-1, :-1].sum(axis=(1, 2, 3))).max(),
|
|
833
|
+
1e-4,
|
|
834
|
+
)
|
|
835
|
+
self.assertLess(
|
|
836
|
+
mx.abs(y[:, 1:7, 7] - c.weight[:, :, :-1].sum(axis=(1, 2, 3))).max(),
|
|
837
|
+
1e-4,
|
|
838
|
+
)
|
|
839
|
+
self.assertLess(
|
|
840
|
+
mx.abs(y[:, 7, 1:7] - c.weight[:, :-1, :].sum(axis=(1, 2, 3))).max(),
|
|
841
|
+
1e-4,
|
|
842
|
+
)
|
|
843
|
+
|
|
844
|
+
# 3x3 conv no padding stride 2
|
|
845
|
+
c = nn.Conv2d(3, 8, 3, padding=0, stride=2)
|
|
846
|
+
y = c(x)
|
|
847
|
+
self.assertEqual(y.shape, (4, 3, 3, 8))
|
|
848
|
+
self.assertLess(mx.abs(y - c.weight.sum((1, 2, 3))).max(), 1e-4)
|
|
849
|
+
|
|
850
|
+
c = nn.Conv2d(3, 8, 3, dilation=2)
|
|
851
|
+
y = c(x)
|
|
852
|
+
self.assertEqual(y.shape, (4, 4, 4, 8))
|
|
853
|
+
self.assertLess(mx.abs(y - c.weight.sum((1, 2, 3))).max(), 1e-4)
|
|
854
|
+
|
|
855
|
+
# 3x3 conv groups > 1
|
|
856
|
+
x = mx.ones((4, 7, 7, 4))
|
|
857
|
+
c = nn.Conv2d(4, 8, 3, padding=1, stride=1, groups=2)
|
|
858
|
+
y = c(x)
|
|
859
|
+
self.assertEqual(y.shape, (4, 7, 7, 8))
|
|
860
|
+
|
|
861
|
+
def test_sequential(self):
|
|
862
|
+
x = mx.ones((10, 2))
|
|
863
|
+
m = nn.Sequential(nn.Linear(2, 10), nn.ReLU(), nn.Linear(10, 1))
|
|
864
|
+
y = m(x)
|
|
865
|
+
self.assertEqual(y.shape, (10, 1))
|
|
866
|
+
params = m.parameters()
|
|
867
|
+
self.assertTrue("layers" in params)
|
|
868
|
+
self.assertEqual(len(params["layers"]), 3)
|
|
869
|
+
self.assertTrue("weight" in params["layers"][0])
|
|
870
|
+
self.assertEqual(len(params["layers"][1]), 0)
|
|
871
|
+
self.assertTrue("weight" in params["layers"][2])
|
|
872
|
+
|
|
873
|
+
m.layers[1] = nn.relu
|
|
874
|
+
y2 = m(x)
|
|
875
|
+
self.assertTrue(mx.array_equal(y, y2))
|
|
876
|
+
|
|
877
|
+
def test_gelu(self):
|
|
878
|
+
inputs = [1.15286231, -0.81037411, 0.35816911, 0.77484438, 0.66276414]
|
|
879
|
+
|
|
880
|
+
# From: jax.nn.gelu(np.array(inputs), approximate=False)
|
|
881
|
+
expected = np.array(
|
|
882
|
+
[1.0093501, -0.16925684, 0.22918941, 0.60498625, 0.49459383]
|
|
883
|
+
)
|
|
884
|
+
# From: jax.nn.gelu(np.array(inputs), approximate=True)
|
|
885
|
+
expected_approx = np.array(
|
|
886
|
+
[1.0091482, -0.1693441, 0.22918446, 0.60491, 0.4945476]
|
|
887
|
+
)
|
|
888
|
+
|
|
889
|
+
out = nn.GELU()(mx.array(inputs))
|
|
890
|
+
self.assertTrue(np.allclose(out, expected))
|
|
891
|
+
|
|
892
|
+
# Test the precise/tanh approximation
|
|
893
|
+
out_approx = nn.GELU(approx="precise")(mx.array(inputs))
|
|
894
|
+
out_approx_tanh = nn.GELU(approx="tanh")(mx.array(inputs))
|
|
895
|
+
self.assertTrue(np.allclose(out_approx, expected_approx))
|
|
896
|
+
self.assertTrue(np.allclose(out_approx_tanh, expected_approx))
|
|
897
|
+
self.assertTrue(np.allclose(out_approx, out_approx_tanh))
|
|
898
|
+
|
|
899
|
+
# Crudely check the approximations
|
|
900
|
+
x = mx.arange(-6.0, 6.0, 12 / 100)
|
|
901
|
+
y = nn.gelu(x)
|
|
902
|
+
y_hat1 = nn.gelu_approx(x)
|
|
903
|
+
y_hat2 = nn.gelu_fast_approx(x)
|
|
904
|
+
self.assertLess(mx.abs(y - y_hat1).max(), 0.0005)
|
|
905
|
+
self.assertLess(mx.abs(y - y_hat2).max(), 0.025)
|
|
906
|
+
|
|
907
|
+
def test_sin_pe(self):
|
|
908
|
+
m = nn.SinusoidalPositionalEncoding(16, min_freq=0.01)
|
|
909
|
+
x = mx.arange(10)
|
|
910
|
+
y = m(x)
|
|
911
|
+
|
|
912
|
+
self.assertEqual(y.shape, (10, 16))
|
|
913
|
+
similarities = y @ y.T
|
|
914
|
+
self.assertLess(
|
|
915
|
+
mx.abs(similarities[mx.arange(10), mx.arange(10)] - 1).max(), 1e-5
|
|
916
|
+
)
|
|
917
|
+
|
|
918
|
+
def test_sigmoid(self):
|
|
919
|
+
x = mx.array([1.0, 0.0, -1.0])
|
|
920
|
+
y1 = mx.sigmoid(x)
|
|
921
|
+
y2 = nn.activations.sigmoid(x)
|
|
922
|
+
y3 = nn.Sigmoid()(x)
|
|
923
|
+
|
|
924
|
+
self.assertEqualArray(y1, y2, atol=0, rtol=0)
|
|
925
|
+
self.assertEqualArray(y1, y3, atol=0, rtol=0)
|
|
926
|
+
|
|
927
|
+
def test_relu(self):
|
|
928
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
929
|
+
y = nn.relu(x)
|
|
930
|
+
self.assertTrue(mx.array_equal(y, mx.array([1.0, 0.0, 0.0])))
|
|
931
|
+
self.assertEqual(y.shape, (3,))
|
|
932
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
933
|
+
|
|
934
|
+
def test_leaky_relu(self):
|
|
935
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
936
|
+
y = nn.leaky_relu(x)
|
|
937
|
+
self.assertTrue(mx.array_equal(y, mx.array([1.0, -0.01, 0.0])))
|
|
938
|
+
self.assertEqual(y.shape, (3,))
|
|
939
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
940
|
+
|
|
941
|
+
y = nn.LeakyReLU(negative_slope=0.1)(x)
|
|
942
|
+
self.assertTrue(mx.array_equal(y, mx.array([1.0, -0.1, 0.0])))
|
|
943
|
+
self.assertEqual(y.shape, (3,))
|
|
944
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
945
|
+
|
|
946
|
+
def test_elu(self):
|
|
947
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
948
|
+
y = nn.elu(x)
|
|
949
|
+
epsilon = 1e-4
|
|
950
|
+
expected_y = mx.array([1.0, -0.6321, 0.0])
|
|
951
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
952
|
+
self.assertEqual(y.shape, (3,))
|
|
953
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
954
|
+
|
|
955
|
+
y = nn.ELU(alpha=1.1)(x)
|
|
956
|
+
epsilon = 1e-4
|
|
957
|
+
expected_y = mx.array([1.0, -0.6953, 0.0])
|
|
958
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
959
|
+
self.assertEqual(y.shape, (3,))
|
|
960
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
961
|
+
|
|
962
|
+
def test_relu6(self):
|
|
963
|
+
x = mx.array([1.0, -1.0, 0.0, 7.0, -7.0])
|
|
964
|
+
y = nn.relu6(x)
|
|
965
|
+
self.assertTrue(mx.array_equal(y, mx.array([1.0, 0.0, 0.0, 6.0, 0.0])))
|
|
966
|
+
self.assertEqual(y.shape, (5,))
|
|
967
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
968
|
+
|
|
969
|
+
def test_softmax(self):
|
|
970
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
971
|
+
y = nn.softmax(x)
|
|
972
|
+
epsilon = 1e-4
|
|
973
|
+
expected_y = mx.array([0.6652, 0.0900, 0.2447])
|
|
974
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
975
|
+
self.assertEqual(y.shape, (3,))
|
|
976
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
977
|
+
|
|
978
|
+
def test_softmin(self):
|
|
979
|
+
x = mx.array([1.0, 2.0, 3.0])
|
|
980
|
+
y = nn.softmin(x)
|
|
981
|
+
epsilon = 1e-4
|
|
982
|
+
expected_y = mx.array([0.6652, 0.2447, 0.0900])
|
|
983
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
984
|
+
self.assertEqual(y.shape, (3,))
|
|
985
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
986
|
+
|
|
987
|
+
def test_softplus(self):
|
|
988
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
989
|
+
y = nn.softplus(x)
|
|
990
|
+
epsilon = 1e-4
|
|
991
|
+
expected_y = mx.array([1.3133, 0.3133, 0.6931])
|
|
992
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
993
|
+
self.assertEqual(y.shape, (3,))
|
|
994
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
995
|
+
|
|
996
|
+
def test_softsign(self):
|
|
997
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
998
|
+
y = nn.softsign(x)
|
|
999
|
+
epsilon = 1e-4
|
|
1000
|
+
expected_y = mx.array([0.5, -0.5, 0.0])
|
|
1001
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1002
|
+
self.assertEqual(y.shape, (3,))
|
|
1003
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1004
|
+
|
|
1005
|
+
def test_softshrink(self):
|
|
1006
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
1007
|
+
y = nn.softshrink(x)
|
|
1008
|
+
epsilon = 1e-4
|
|
1009
|
+
expected_y = mx.array([0.5, -0.5, 0.0])
|
|
1010
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1011
|
+
self.assertEqual(y.shape, (3,))
|
|
1012
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1013
|
+
|
|
1014
|
+
y = nn.Softshrink(lambd=0.7)(x)
|
|
1015
|
+
expected_y = mx.array([0.3, -0.3, 0.0])
|
|
1016
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1017
|
+
self.assertEqual(y.shape, (3,))
|
|
1018
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1019
|
+
|
|
1020
|
+
def test_celu(self):
|
|
1021
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
1022
|
+
y = nn.celu(x)
|
|
1023
|
+
epsilon = 1e-4
|
|
1024
|
+
expected_y = mx.array([1.0, -0.6321, 0.0])
|
|
1025
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1026
|
+
self.assertEqual(y.shape, (3,))
|
|
1027
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1028
|
+
|
|
1029
|
+
y = nn.CELU(alpha=1.1)(x)
|
|
1030
|
+
expected_y = mx.array([1.0, -0.6568, 0.0])
|
|
1031
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1032
|
+
self.assertEqual(y.shape, (3,))
|
|
1033
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1034
|
+
|
|
1035
|
+
def test_log_softmax(self):
|
|
1036
|
+
x = mx.array([1.0, 2.0, 3.0])
|
|
1037
|
+
y = nn.log_softmax(x)
|
|
1038
|
+
epsilon = 1e-4
|
|
1039
|
+
expected_y = mx.array([-2.4076, -1.4076, -0.4076])
|
|
1040
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1041
|
+
self.assertEqual(y.shape, (3,))
|
|
1042
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1043
|
+
|
|
1044
|
+
def test_log_sigmoid(self):
|
|
1045
|
+
x = mx.array([1.0, -1.0, 0.0])
|
|
1046
|
+
y = nn.log_sigmoid(x)
|
|
1047
|
+
epsilon = 1e-4
|
|
1048
|
+
expected_y = mx.array([-0.3133, -1.3133, -0.6931])
|
|
1049
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1050
|
+
self.assertEqual(y.shape, (3,))
|
|
1051
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1052
|
+
|
|
1053
|
+
def test_prelu(self):
|
|
1054
|
+
self.assertEqualArray(
|
|
1055
|
+
nn.PReLU()(mx.array([1.0, -1.0, 0.0, 0.5])),
|
|
1056
|
+
mx.array([1.0, -0.25, 0.0, 0.5]),
|
|
1057
|
+
)
|
|
1058
|
+
|
|
1059
|
+
def test_mish(self):
|
|
1060
|
+
self.assertEqualArray(
|
|
1061
|
+
nn.Mish()(mx.array([1.0, -1.0, 0.0, 0.5])),
|
|
1062
|
+
mx.array([0.8651, -0.3034, 0.0000, 0.3752]),
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
def test_hardswish(self):
|
|
1066
|
+
x = mx.array([-3.0, -1.5, 0.0, 1.5, 3.0])
|
|
1067
|
+
y = nn.hardswish(x)
|
|
1068
|
+
epsilon = 1e-4
|
|
1069
|
+
expected_y = mx.array([0.0, -0.375, 0.0, 1.125, 3.0])
|
|
1070
|
+
self.assertTrue(mx.all(mx.abs(y - expected_y) < epsilon))
|
|
1071
|
+
self.assertEqual(y.shape, (5,))
|
|
1072
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1073
|
+
|
|
1074
|
+
def test_glu(self):
|
|
1075
|
+
x = mx.array([[[1.0, 2.0, 3.0, 4.0]]], dtype=mx.float32)
|
|
1076
|
+
y = mx.array([[[0.952574, 1.96403]]], dtype=mx.float32)
|
|
1077
|
+
out = nn.glu(x)
|
|
1078
|
+
self.assertEqualArray(out, y)
|
|
1079
|
+
|
|
1080
|
+
def test_hard_tanh(self):
|
|
1081
|
+
x = mx.array([1.0, -2.0, 0.0, 0.5, 2.0])
|
|
1082
|
+
y = nn.hard_tanh(x)
|
|
1083
|
+
expected_y = mx.array([1.0, -1.0, 0.0, 0.5, 1.0])
|
|
1084
|
+
self.assertTrue(mx.array_equal(y, expected_y))
|
|
1085
|
+
self.assertEqual(y.shape, (5,))
|
|
1086
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1087
|
+
|
|
1088
|
+
def test_hard_shrink(self):
|
|
1089
|
+
x = mx.array([1.0, -0.5, 0.0, 0.5, -1.5])
|
|
1090
|
+
y = nn.hard_shrink(x)
|
|
1091
|
+
expected_y = mx.array([1.0, 0.0, 0.0, 0.0, -1.5])
|
|
1092
|
+
self.assertTrue(mx.array_equal(y, expected_y))
|
|
1093
|
+
self.assertEqual(y.shape, (5,))
|
|
1094
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1095
|
+
|
|
1096
|
+
y = nn.hard_shrink(x, lambd=0.1)
|
|
1097
|
+
expected_y = mx.array([1.0, -0.5, 0.0, 0.5, -1.5])
|
|
1098
|
+
self.assertTrue(mx.array_equal(y, expected_y))
|
|
1099
|
+
self.assertEqual(y.shape, (5,))
|
|
1100
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1101
|
+
|
|
1102
|
+
def test_rope(self):
|
|
1103
|
+
for kwargs in [{}, {"traditional": False}, {"base": 10000}, {"scale": 0.25}]:
|
|
1104
|
+
rope = nn.RoPE(4, **kwargs)
|
|
1105
|
+
shape = (1, 3, 4)
|
|
1106
|
+
x = mx.random.uniform(shape=shape)
|
|
1107
|
+
y = rope(x)
|
|
1108
|
+
self.assertEqual(y.shape, shape)
|
|
1109
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1110
|
+
|
|
1111
|
+
y = rope(x, offset=3)
|
|
1112
|
+
self.assertEqual(y.shape, shape)
|
|
1113
|
+
|
|
1114
|
+
y = rope(x.astype(mx.float16))
|
|
1115
|
+
self.assertEqual(y.dtype, mx.float16)
|
|
1116
|
+
|
|
1117
|
+
def test_alibi(self):
|
|
1118
|
+
alibi = nn.ALiBi()
|
|
1119
|
+
shape = (1, 8, 20, 20)
|
|
1120
|
+
x = mx.random.uniform(shape=shape)
|
|
1121
|
+
y = alibi(x)
|
|
1122
|
+
self.assertEqual(y.shape, shape)
|
|
1123
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1124
|
+
|
|
1125
|
+
y = alibi(x.astype(mx.float16))
|
|
1126
|
+
self.assertEqual(y.dtype, mx.float16)
|
|
1127
|
+
|
|
1128
|
+
def test_dropout(self):
|
|
1129
|
+
x = mx.ones((2, 4))
|
|
1130
|
+
y = nn.Dropout(0.5)(x)
|
|
1131
|
+
self.assertEqual(y.shape, x.shape)
|
|
1132
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1133
|
+
|
|
1134
|
+
x = mx.ones((2, 4), dtype=mx.bfloat16)
|
|
1135
|
+
y = nn.Dropout(0.5)(x)
|
|
1136
|
+
self.assertEqual(y.shape, x.shape)
|
|
1137
|
+
self.assertEqual(y.dtype, mx.bfloat16)
|
|
1138
|
+
|
|
1139
|
+
x = mx.ones((2, 4), dtype=mx.float16)
|
|
1140
|
+
y = nn.Dropout(0.5)(x)
|
|
1141
|
+
self.assertEqual(y.shape, x.shape)
|
|
1142
|
+
self.assertEqual(y.dtype, mx.float16)
|
|
1143
|
+
|
|
1144
|
+
def test_dropout2d(self):
|
|
1145
|
+
x = mx.ones((2, 4, 4, 4))
|
|
1146
|
+
y = nn.Dropout2d(0.5)(x)
|
|
1147
|
+
self.assertEqual(y.shape, x.shape)
|
|
1148
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1149
|
+
|
|
1150
|
+
x = mx.ones((2, 4, 4, 4), dtype=mx.bfloat16)
|
|
1151
|
+
y = nn.Dropout2d(0.5)(x)
|
|
1152
|
+
self.assertEqual(y.shape, x.shape)
|
|
1153
|
+
self.assertEqual(y.dtype, mx.bfloat16)
|
|
1154
|
+
|
|
1155
|
+
x = mx.ones((2, 4, 4, 4), dtype=mx.float16)
|
|
1156
|
+
y = nn.Dropout2d(0.5)(x)
|
|
1157
|
+
self.assertEqual(y.shape, x.shape)
|
|
1158
|
+
self.assertEqual(y.dtype, mx.float16)
|
|
1159
|
+
|
|
1160
|
+
def test_dropout3d(self):
|
|
1161
|
+
x = mx.ones((2, 4, 4, 4, 4))
|
|
1162
|
+
y = nn.Dropout3d(0.5)(x)
|
|
1163
|
+
self.assertEqual(y.shape, x.shape)
|
|
1164
|
+
self.assertEqual(y.dtype, mx.float32)
|
|
1165
|
+
|
|
1166
|
+
x = mx.ones((2, 4, 4, 4, 4), dtype=mx.bfloat16)
|
|
1167
|
+
y = nn.Dropout3d(0.5)(x)
|
|
1168
|
+
self.assertEqual(y.shape, x.shape)
|
|
1169
|
+
self.assertEqual(y.dtype, mx.bfloat16)
|
|
1170
|
+
|
|
1171
|
+
x = mx.ones((2, 4, 4, 4, 4), dtype=mx.float16)
|
|
1172
|
+
y = nn.Dropout3d(0.5)(x)
|
|
1173
|
+
self.assertEqual(y.shape, x.shape)
|
|
1174
|
+
self.assertEqual(y.dtype, mx.float16)
|
|
1175
|
+
|
|
1176
|
+
def test_upsample(self):
|
|
1177
|
+
b, h, w, c = 1, 2, 2, 1
|
|
1178
|
+
scale_factor = 2
|
|
1179
|
+
upsample_nearest = nn.Upsample(
|
|
1180
|
+
scale_factor=scale_factor, mode="nearest", align_corners=True
|
|
1181
|
+
)
|
|
1182
|
+
upsample_bilinear = nn.Upsample(
|
|
1183
|
+
scale_factor=scale_factor, mode="linear", align_corners=True
|
|
1184
|
+
)
|
|
1185
|
+
upsample_nearest = nn.Upsample(
|
|
1186
|
+
scale_factor=scale_factor, mode="nearest", align_corners=True
|
|
1187
|
+
)
|
|
1188
|
+
upsample_bilinear_no_align_corners = nn.Upsample(
|
|
1189
|
+
scale_factor=scale_factor, mode="linear", align_corners=False
|
|
1190
|
+
)
|
|
1191
|
+
upsample_nearest_no_align_corners = nn.Upsample(
|
|
1192
|
+
scale_factor=scale_factor, mode="nearest", align_corners=False
|
|
1193
|
+
)
|
|
1194
|
+
# Test single feature map, align corners
|
|
1195
|
+
x = mx.arange(b * h * w * c).reshape((b, c, h, w)).transpose((0, 2, 3, 1))
|
|
1196
|
+
expected_nearest = mx.array(
|
|
1197
|
+
[[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]]]
|
|
1198
|
+
).transpose((0, 2, 3, 1))
|
|
1199
|
+
expected_bilinear = mx.array(
|
|
1200
|
+
[
|
|
1201
|
+
[
|
|
1202
|
+
[
|
|
1203
|
+
[0, 0.333333, 0.666667, 1],
|
|
1204
|
+
[0.666667, 1, 1.33333, 1.66667],
|
|
1205
|
+
[1.33333, 1.66667, 2, 2.33333],
|
|
1206
|
+
[2, 2.33333, 2.66667, 3],
|
|
1207
|
+
]
|
|
1208
|
+
]
|
|
1209
|
+
]
|
|
1210
|
+
).transpose((0, 2, 3, 1))
|
|
1211
|
+
# Test single feature map, no align corners
|
|
1212
|
+
x = (
|
|
1213
|
+
mx.arange(1, b * h * w * c + 1)
|
|
1214
|
+
.reshape((b, c, h, w))
|
|
1215
|
+
.transpose((0, 2, 3, 1))
|
|
1216
|
+
)
|
|
1217
|
+
expected_bilinear_no_align_corners = mx.array(
|
|
1218
|
+
[
|
|
1219
|
+
[
|
|
1220
|
+
[
|
|
1221
|
+
[1.0000, 1.2500, 1.7500, 2.0000],
|
|
1222
|
+
[1.5000, 1.7500, 2.2500, 2.5000],
|
|
1223
|
+
[2.5000, 2.7500, 3.2500, 3.5000],
|
|
1224
|
+
[3.0000, 3.2500, 3.7500, 4.0000],
|
|
1225
|
+
]
|
|
1226
|
+
]
|
|
1227
|
+
]
|
|
1228
|
+
).transpose((0, 2, 3, 1))
|
|
1229
|
+
expected_nearest_no_align_corners = mx.array(
|
|
1230
|
+
[[[[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]]]
|
|
1231
|
+
).transpose((0, 2, 3, 1))
|
|
1232
|
+
self.assertTrue(
|
|
1233
|
+
np.allclose(
|
|
1234
|
+
upsample_nearest_no_align_corners(x), expected_nearest_no_align_corners
|
|
1235
|
+
)
|
|
1236
|
+
)
|
|
1237
|
+
self.assertTrue(
|
|
1238
|
+
np.allclose(
|
|
1239
|
+
upsample_bilinear_no_align_corners(x),
|
|
1240
|
+
expected_bilinear_no_align_corners,
|
|
1241
|
+
)
|
|
1242
|
+
)
|
|
1243
|
+
|
|
1244
|
+
# Test a more complex batch
|
|
1245
|
+
b, h, w, c = 2, 3, 3, 2
|
|
1246
|
+
scale_factor = 2
|
|
1247
|
+
x = mx.arange((b * h * w * c)).reshape((b, c, h, w)).transpose((0, 2, 3, 1))
|
|
1248
|
+
|
|
1249
|
+
upsample_nearest = nn.Upsample(
|
|
1250
|
+
scale_factor=scale_factor, mode="nearest", align_corners=True
|
|
1251
|
+
)
|
|
1252
|
+
upsample_bilinear = nn.Upsample(
|
|
1253
|
+
scale_factor=scale_factor, mode="linear", align_corners=True
|
|
1254
|
+
)
|
|
1255
|
+
|
|
1256
|
+
expected_nearest = mx.array(
|
|
1257
|
+
[
|
|
1258
|
+
[
|
|
1259
|
+
[
|
|
1260
|
+
[0.0, 0.0, 1.0, 1.0, 2.0, 2.0],
|
|
1261
|
+
[0.0, 0.0, 1.0, 1.0, 2.0, 2.0],
|
|
1262
|
+
[3.0, 3.0, 4.0, 4.0, 5.0, 5.0],
|
|
1263
|
+
[3.0, 3.0, 4.0, 4.0, 5.0, 5.0],
|
|
1264
|
+
[6.0, 6.0, 7.0, 7.0, 8.0, 8.0],
|
|
1265
|
+
[6.0, 6.0, 7.0, 7.0, 8.0, 8.0],
|
|
1266
|
+
],
|
|
1267
|
+
[
|
|
1268
|
+
[9.0, 9.0, 10.0, 10.0, 11.0, 11.0],
|
|
1269
|
+
[9.0, 9.0, 10.0, 10.0, 11.0, 11.0],
|
|
1270
|
+
[12.0, 12.0, 13.0, 13.0, 14.0, 14.0],
|
|
1271
|
+
[12.0, 12.0, 13.0, 13.0, 14.0, 14.0],
|
|
1272
|
+
[15.0, 15.0, 16.0, 16.0, 17.0, 17.0],
|
|
1273
|
+
[15.0, 15.0, 16.0, 16.0, 17.0, 17.0],
|
|
1274
|
+
],
|
|
1275
|
+
],
|
|
1276
|
+
[
|
|
1277
|
+
[
|
|
1278
|
+
[18.0, 18.0, 19.0, 19.0, 20.0, 20.0],
|
|
1279
|
+
[18.0, 18.0, 19.0, 19.0, 20.0, 20.0],
|
|
1280
|
+
[21.0, 21.0, 22.0, 22.0, 23.0, 23.0],
|
|
1281
|
+
[21.0, 21.0, 22.0, 22.0, 23.0, 23.0],
|
|
1282
|
+
[24.0, 24.0, 25.0, 25.0, 26.0, 26.0],
|
|
1283
|
+
[24.0, 24.0, 25.0, 25.0, 26.0, 26.0],
|
|
1284
|
+
],
|
|
1285
|
+
[
|
|
1286
|
+
[27.0, 27.0, 28.0, 28.0, 29.0, 29.0],
|
|
1287
|
+
[27.0, 27.0, 28.0, 28.0, 29.0, 29.0],
|
|
1288
|
+
[30.0, 30.0, 31.0, 31.0, 32.0, 32.0],
|
|
1289
|
+
[30.0, 30.0, 31.0, 31.0, 32.0, 32.0],
|
|
1290
|
+
[33.0, 33.0, 34.0, 34.0, 35.0, 35.0],
|
|
1291
|
+
[33.0, 33.0, 34.0, 34.0, 35.0, 35.0],
|
|
1292
|
+
],
|
|
1293
|
+
],
|
|
1294
|
+
]
|
|
1295
|
+
).transpose((0, 2, 3, 1))
|
|
1296
|
+
expected_bilinear = mx.array(
|
|
1297
|
+
[
|
|
1298
|
+
[
|
|
1299
|
+
[
|
|
1300
|
+
[0.0, 0.4, 0.8, 1.2, 1.6, 2.0],
|
|
1301
|
+
[1.2, 1.6, 2.0, 2.4, 2.8, 3.2],
|
|
1302
|
+
[2.4, 2.8, 3.2, 3.6, 4.0, 4.4],
|
|
1303
|
+
[3.6, 4.0, 4.4, 4.8, 5.2, 5.6],
|
|
1304
|
+
[4.8, 5.2, 5.6, 6.0, 6.4, 6.8],
|
|
1305
|
+
[6.0, 6.4, 6.8, 7.2, 7.6, 8.0],
|
|
1306
|
+
],
|
|
1307
|
+
[
|
|
1308
|
+
[9.0, 9.4, 9.8, 10.2, 10.6, 11.0],
|
|
1309
|
+
[10.2, 10.6, 11.0, 11.4, 11.8, 12.2],
|
|
1310
|
+
[11.4, 11.8, 12.2, 12.6, 13.0, 13.4],
|
|
1311
|
+
[12.6, 13.0, 13.4, 13.8, 14.2, 14.6],
|
|
1312
|
+
[13.8, 14.2, 14.6, 15.0, 15.4, 15.8],
|
|
1313
|
+
[15.0, 15.4, 15.8, 16.2, 16.6, 17.0],
|
|
1314
|
+
],
|
|
1315
|
+
],
|
|
1316
|
+
[
|
|
1317
|
+
[
|
|
1318
|
+
[18.0, 18.4, 18.8, 19.2, 19.6, 20.0],
|
|
1319
|
+
[19.2, 19.6, 20.0, 20.4, 20.8, 21.2],
|
|
1320
|
+
[20.4, 20.8, 21.2, 21.6, 22.0, 22.4],
|
|
1321
|
+
[21.6, 22.0, 22.4, 22.8, 23.2, 23.6],
|
|
1322
|
+
[22.8, 23.2, 23.6, 24.0, 24.4, 24.8],
|
|
1323
|
+
[24.0, 24.4, 24.8, 25.2, 25.6, 26.0],
|
|
1324
|
+
],
|
|
1325
|
+
[
|
|
1326
|
+
[27.0, 27.4, 27.8, 28.2, 28.6, 29.0],
|
|
1327
|
+
[28.2, 28.6, 29.0, 29.4, 29.8, 30.2],
|
|
1328
|
+
[29.4, 29.8, 30.2, 30.6, 31.0, 31.4],
|
|
1329
|
+
[30.6, 31.0, 31.4, 31.8, 32.2, 32.6],
|
|
1330
|
+
[31.8, 32.2, 32.6, 33.0, 33.4, 33.8],
|
|
1331
|
+
[33.0, 33.4, 33.8, 34.2, 34.6, 35.0],
|
|
1332
|
+
],
|
|
1333
|
+
],
|
|
1334
|
+
]
|
|
1335
|
+
).transpose((0, 2, 3, 1))
|
|
1336
|
+
self.assertTrue(np.allclose(upsample_nearest(x), expected_nearest))
|
|
1337
|
+
self.assertTrue(np.allclose(upsample_bilinear(x), expected_bilinear))
|
|
1338
|
+
|
|
1339
|
+
# Test different height and width scale_factor
|
|
1340
|
+
b, h, w, c = 1, 2, 2, 2
|
|
1341
|
+
x = mx.arange(b * h * w * c).reshape((b, c, h, w)).transpose((0, 2, 3, 1))
|
|
1342
|
+
upsample_nearest = nn.Upsample(
|
|
1343
|
+
scale_factor=(2, 3), mode="nearest", align_corners=True
|
|
1344
|
+
)
|
|
1345
|
+
upsample_bilinear = nn.Upsample(
|
|
1346
|
+
scale_factor=(2, 3), mode="linear", align_corners=True
|
|
1347
|
+
)
|
|
1348
|
+
|
|
1349
|
+
expected_nearest = mx.array(
|
|
1350
|
+
[
|
|
1351
|
+
[
|
|
1352
|
+
[
|
|
1353
|
+
[0, 0, 0, 1, 1, 1],
|
|
1354
|
+
[0, 0, 0, 1, 1, 1],
|
|
1355
|
+
[2, 2, 2, 3, 3, 3],
|
|
1356
|
+
[2, 2, 2, 3, 3, 3],
|
|
1357
|
+
],
|
|
1358
|
+
[
|
|
1359
|
+
[4, 4, 4, 5, 5, 5],
|
|
1360
|
+
[4, 4, 4, 5, 5, 5],
|
|
1361
|
+
[6, 6, 6, 7, 7, 7],
|
|
1362
|
+
[6, 6, 6, 7, 7, 7],
|
|
1363
|
+
],
|
|
1364
|
+
]
|
|
1365
|
+
]
|
|
1366
|
+
).transpose((0, 2, 3, 1))
|
|
1367
|
+
expected_bilinear = mx.array(
|
|
1368
|
+
[
|
|
1369
|
+
[
|
|
1370
|
+
[
|
|
1371
|
+
[0, 0.2, 0.4, 0.6, 0.8, 1],
|
|
1372
|
+
[0.666667, 0.866667, 1.06667, 1.26667, 1.46667, 1.66667],
|
|
1373
|
+
[1.33333, 1.53333, 1.73333, 1.93333, 2.13333, 2.33333],
|
|
1374
|
+
[2, 2.2, 2.4, 2.6, 2.8, 3],
|
|
1375
|
+
],
|
|
1376
|
+
[
|
|
1377
|
+
[4, 4.2, 4.4, 4.6, 4.8, 5],
|
|
1378
|
+
[4.66667, 4.86667, 5.06667, 5.26667, 5.46667, 5.66667],
|
|
1379
|
+
[5.33333, 5.53333, 5.73333, 5.93333, 6.13333, 6.33333],
|
|
1380
|
+
[6, 6.2, 6.4, 6.6, 6.8, 7],
|
|
1381
|
+
],
|
|
1382
|
+
]
|
|
1383
|
+
]
|
|
1384
|
+
).transpose((0, 2, 3, 1))
|
|
1385
|
+
self.assertTrue(np.allclose(upsample_nearest(x), expected_nearest))
|
|
1386
|
+
self.assertTrue(np.allclose(upsample_bilinear(x), expected_bilinear))
|
|
1387
|
+
|
|
1388
|
+
# Test repr
|
|
1389
|
+
self.assertEqual(
|
|
1390
|
+
str(nn.Upsample(scale_factor=2)),
|
|
1391
|
+
"Upsample(scale_factor=2.0, mode='nearest', align_corners=False)",
|
|
1392
|
+
)
|
|
1393
|
+
self.assertEqual(
|
|
1394
|
+
str(nn.Upsample(scale_factor=(2, 3))),
|
|
1395
|
+
"Upsample(scale_factor=(2.0, 3.0), mode='nearest', align_corners=False)",
|
|
1396
|
+
)
|
|
1397
|
+
|
|
1398
|
+
def test_pooling(self):
|
|
1399
|
+
# Test 1d pooling
|
|
1400
|
+
x = mx.array(
|
|
1401
|
+
[
|
|
1402
|
+
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]],
|
|
1403
|
+
[[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]],
|
|
1404
|
+
]
|
|
1405
|
+
)
|
|
1406
|
+
expected_max_pool_output_no_padding_stride_1 = [
|
|
1407
|
+
[[3.0, 4.0, 5.0], [6.0, 7.0, 8.0], [9.0, 10.0, 11.0]],
|
|
1408
|
+
[[15.0, 16.0, 17.0], [18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],
|
|
1409
|
+
]
|
|
1410
|
+
expected_max_pool_output_no_padding_stride_2 = [
|
|
1411
|
+
[[3.0, 4.0, 5.0], [9.0, 10.0, 11.0]],
|
|
1412
|
+
[[15.0, 16.0, 17.0], [21.0, 22.0, 23.0]],
|
|
1413
|
+
]
|
|
1414
|
+
expected_max_pool_output_padding_1_stride_2 = [
|
|
1415
|
+
[[0.0, 1.0, 2.0], [6.0, 7.0, 8.0], [9.0, 10.0, 11.0]],
|
|
1416
|
+
[[12.0, 13.0, 14.0], [18.0, 19.0, 20.0], [21.0, 22.0, 23.0]],
|
|
1417
|
+
]
|
|
1418
|
+
expected_max_pool_output_padding_1_stride_2_kernel_3 = [
|
|
1419
|
+
[[3.0, 4.0, 5.0], [9.0, 10.0, 11.0]],
|
|
1420
|
+
[[15.0, 16.0, 17.0], [21.0, 22.0, 23.0]],
|
|
1421
|
+
]
|
|
1422
|
+
expected_avg_pool_output_no_padding_stride_1 = [
|
|
1423
|
+
[
|
|
1424
|
+
[1.5000, 2.5000, 3.5000],
|
|
1425
|
+
[4.5000, 5.5000, 6.5000],
|
|
1426
|
+
[7.5000, 8.5000, 9.5000],
|
|
1427
|
+
],
|
|
1428
|
+
[
|
|
1429
|
+
[13.5000, 14.5000, 15.5000],
|
|
1430
|
+
[16.5000, 17.5000, 18.5000],
|
|
1431
|
+
[19.5000, 20.5000, 21.5000],
|
|
1432
|
+
],
|
|
1433
|
+
]
|
|
1434
|
+
expected_avg_pool_output_no_padding_stride_2 = [
|
|
1435
|
+
[[1.5000, 2.5000, 3.5000], [7.5000, 8.5000, 9.5000]],
|
|
1436
|
+
[[13.5000, 14.5000, 15.5000], [19.5000, 20.5000, 21.5000]],
|
|
1437
|
+
]
|
|
1438
|
+
expected_avg_pool_output_padding_1_stride_2 = [
|
|
1439
|
+
[
|
|
1440
|
+
[0.0000, 0.5000, 1.0000],
|
|
1441
|
+
[4.5000, 5.5000, 6.5000],
|
|
1442
|
+
[4.5000, 5.0000, 5.5000],
|
|
1443
|
+
],
|
|
1444
|
+
[
|
|
1445
|
+
[6.0000, 6.5000, 7.0000],
|
|
1446
|
+
[16.5000, 17.5000, 18.5000],
|
|
1447
|
+
[10.5000, 11.0000, 11.5000],
|
|
1448
|
+
],
|
|
1449
|
+
]
|
|
1450
|
+
expected_avg_pool_output_padding_1_kernel_3 = [
|
|
1451
|
+
[[1, 1.66667, 2.33333], [6, 7, 8]],
|
|
1452
|
+
[[9, 9.66667, 10.3333], [18, 19, 20]],
|
|
1453
|
+
]
|
|
1454
|
+
self.assertTrue(
|
|
1455
|
+
np.array_equal(
|
|
1456
|
+
nn.MaxPool1d(kernel_size=2, stride=1, padding=0)(x),
|
|
1457
|
+
expected_max_pool_output_no_padding_stride_1,
|
|
1458
|
+
)
|
|
1459
|
+
)
|
|
1460
|
+
self.assertTrue(
|
|
1461
|
+
np.array_equal(
|
|
1462
|
+
nn.MaxPool1d(kernel_size=2, stride=2, padding=0)(x),
|
|
1463
|
+
expected_max_pool_output_no_padding_stride_2,
|
|
1464
|
+
)
|
|
1465
|
+
)
|
|
1466
|
+
self.assertTrue(
|
|
1467
|
+
np.array_equal(
|
|
1468
|
+
nn.MaxPool1d(kernel_size=2, stride=2, padding=1)(x),
|
|
1469
|
+
expected_max_pool_output_padding_1_stride_2,
|
|
1470
|
+
)
|
|
1471
|
+
)
|
|
1472
|
+
self.assertTrue(
|
|
1473
|
+
np.array_equal(
|
|
1474
|
+
nn.MaxPool1d(kernel_size=3, stride=2, padding=1)(x),
|
|
1475
|
+
expected_max_pool_output_padding_1_stride_2_kernel_3,
|
|
1476
|
+
)
|
|
1477
|
+
)
|
|
1478
|
+
self.assertTrue(
|
|
1479
|
+
np.allclose(
|
|
1480
|
+
nn.AvgPool1d(kernel_size=2, stride=1, padding=0)(x),
|
|
1481
|
+
expected_avg_pool_output_no_padding_stride_1,
|
|
1482
|
+
)
|
|
1483
|
+
)
|
|
1484
|
+
self.assertTrue(
|
|
1485
|
+
np.allclose(
|
|
1486
|
+
nn.AvgPool1d(kernel_size=2, stride=2, padding=0)(x),
|
|
1487
|
+
expected_avg_pool_output_no_padding_stride_2,
|
|
1488
|
+
)
|
|
1489
|
+
)
|
|
1490
|
+
self.assertTrue(
|
|
1491
|
+
np.allclose(
|
|
1492
|
+
nn.AvgPool1d(kernel_size=2, stride=2, padding=1)(x),
|
|
1493
|
+
expected_avg_pool_output_padding_1_stride_2,
|
|
1494
|
+
)
|
|
1495
|
+
)
|
|
1496
|
+
self.assertTrue(
|
|
1497
|
+
np.allclose(
|
|
1498
|
+
nn.AvgPool1d(kernel_size=3, stride=2, padding=1)(x),
|
|
1499
|
+
expected_avg_pool_output_padding_1_kernel_3,
|
|
1500
|
+
)
|
|
1501
|
+
)
|
|
1502
|
+
# Test 2d pooling
|
|
1503
|
+
x = mx.array(
|
|
1504
|
+
[
|
|
1505
|
+
[
|
|
1506
|
+
[[0, 16], [1, 17], [2, 18], [3, 19]],
|
|
1507
|
+
[[4, 20], [5, 21], [6, 22], [7, 23]],
|
|
1508
|
+
[[8, 24], [9, 25], [10, 26], [11, 27]],
|
|
1509
|
+
[[12, 28], [13, 29], [14, 30], [15, 31]],
|
|
1510
|
+
]
|
|
1511
|
+
]
|
|
1512
|
+
)
|
|
1513
|
+
expected_max_pool_output_no_padding_stride_1 = [
|
|
1514
|
+
[
|
|
1515
|
+
[[5, 21], [6, 22], [7, 23]],
|
|
1516
|
+
[[9, 25], [10, 26], [11, 27]],
|
|
1517
|
+
[[13, 29], [14, 30], [15, 31]],
|
|
1518
|
+
]
|
|
1519
|
+
]
|
|
1520
|
+
expected_max_pool_output_no_padding_stride_2 = [
|
|
1521
|
+
[[[5, 21], [7, 23]], [[13, 29], [15, 31]]]
|
|
1522
|
+
]
|
|
1523
|
+
expected_max_pool_output_padding_1 = [
|
|
1524
|
+
[
|
|
1525
|
+
[[0, 16], [2, 18], [3, 19]],
|
|
1526
|
+
[[8, 24], [10, 26], [11, 27]],
|
|
1527
|
+
[[12, 28], [14, 30], [15, 31]],
|
|
1528
|
+
]
|
|
1529
|
+
]
|
|
1530
|
+
expected_mean_pool_output_no_padding_stride_1 = [
|
|
1531
|
+
[
|
|
1532
|
+
[[2.5000, 18.5000], [3.5000, 19.5000], [4.5000, 20.5000]],
|
|
1533
|
+
[[6.5000, 22.5000], [7.5000, 23.5000], [8.5000, 24.5000]],
|
|
1534
|
+
[[10.5000, 26.5000], [11.5000, 27.5000], [12.5000, 28.5000]],
|
|
1535
|
+
]
|
|
1536
|
+
]
|
|
1537
|
+
expected_mean_pool_output_no_padding_stride_2 = [
|
|
1538
|
+
[
|
|
1539
|
+
[[2.5000, 18.5000], [4.5000, 20.5000]],
|
|
1540
|
+
[[10.5000, 26.5000], [12.5000, 28.5000]],
|
|
1541
|
+
]
|
|
1542
|
+
]
|
|
1543
|
+
expected_mean_pool_output_padding_1 = [
|
|
1544
|
+
[
|
|
1545
|
+
[[0.0000, 4.0000], [0.7500, 8.7500], [0.7500, 4.7500]],
|
|
1546
|
+
[[3.0000, 11.0000], [7.5000, 23.5000], [4.5000, 12.5000]],
|
|
1547
|
+
[[3.0000, 7.0000], [6.7500, 14.7500], [3.7500, 7.7500]],
|
|
1548
|
+
]
|
|
1549
|
+
]
|
|
1550
|
+
self.assertTrue(
|
|
1551
|
+
np.array_equal(
|
|
1552
|
+
nn.MaxPool2d(kernel_size=2, stride=1, padding=0)(x),
|
|
1553
|
+
expected_max_pool_output_no_padding_stride_1,
|
|
1554
|
+
)
|
|
1555
|
+
)
|
|
1556
|
+
self.assertTrue(
|
|
1557
|
+
np.array_equal(
|
|
1558
|
+
nn.MaxPool2d(kernel_size=2, stride=2, padding=0)(x),
|
|
1559
|
+
expected_max_pool_output_no_padding_stride_2,
|
|
1560
|
+
)
|
|
1561
|
+
)
|
|
1562
|
+
self.assertTrue(
|
|
1563
|
+
np.array_equal(
|
|
1564
|
+
nn.MaxPool2d(kernel_size=2, stride=2, padding=1)(x),
|
|
1565
|
+
expected_max_pool_output_padding_1,
|
|
1566
|
+
)
|
|
1567
|
+
)
|
|
1568
|
+
# Average pooling
|
|
1569
|
+
self.assertTrue(
|
|
1570
|
+
np.allclose(
|
|
1571
|
+
nn.AvgPool2d(kernel_size=2, stride=1, padding=0)(x),
|
|
1572
|
+
expected_mean_pool_output_no_padding_stride_1,
|
|
1573
|
+
)
|
|
1574
|
+
)
|
|
1575
|
+
self.assertTrue(
|
|
1576
|
+
np.array_equal(
|
|
1577
|
+
nn.AvgPool2d(kernel_size=2, stride=2, padding=0)(x),
|
|
1578
|
+
expected_mean_pool_output_no_padding_stride_2,
|
|
1579
|
+
)
|
|
1580
|
+
)
|
|
1581
|
+
self.assertTrue(
|
|
1582
|
+
np.array_equal(
|
|
1583
|
+
nn.AvgPool2d(kernel_size=2, stride=2, padding=1)(x),
|
|
1584
|
+
expected_mean_pool_output_padding_1,
|
|
1585
|
+
)
|
|
1586
|
+
)
|
|
1587
|
+
# Test multiple batches
|
|
1588
|
+
x = mx.array(
|
|
1589
|
+
[
|
|
1590
|
+
[
|
|
1591
|
+
[[0, 1], [2, 3], [4, 5], [6, 7]],
|
|
1592
|
+
[[8, 9], [10, 11], [12, 13], [14, 15]],
|
|
1593
|
+
[[16, 17], [18, 19], [20, 21], [22, 23]],
|
|
1594
|
+
[[24, 25], [26, 27], [28, 29], [30, 31]],
|
|
1595
|
+
],
|
|
1596
|
+
[
|
|
1597
|
+
[[32, 33], [34, 35], [36, 37], [38, 39]],
|
|
1598
|
+
[[40, 41], [42, 43], [44, 45], [46, 47]],
|
|
1599
|
+
[[48, 49], [50, 51], [52, 53], [54, 55]],
|
|
1600
|
+
[[56, 57], [58, 59], [60, 61], [62, 63]],
|
|
1601
|
+
],
|
|
1602
|
+
]
|
|
1603
|
+
)
|
|
1604
|
+
expected_max_pool_output = [
|
|
1605
|
+
[[[10.0, 11.0], [14.0, 15.0]], [[26.0, 27.0], [30.0, 31.0]]],
|
|
1606
|
+
[[[42.0, 43.0], [46.0, 47.0]], [[58.0, 59.0], [62.0, 63.0]]],
|
|
1607
|
+
]
|
|
1608
|
+
expected_avg_pool_output = [
|
|
1609
|
+
[[[2.22222, 2.66667], [5.33333, 6]], [[11.3333, 12], [20, 21]]],
|
|
1610
|
+
[[[16.4444, 16.8889], [26.6667, 27.3333]], [[32.6667, 33.3333], [52, 53]]],
|
|
1611
|
+
]
|
|
1612
|
+
self.assertTrue(
|
|
1613
|
+
np.array_equal(
|
|
1614
|
+
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)(x),
|
|
1615
|
+
expected_max_pool_output,
|
|
1616
|
+
)
|
|
1617
|
+
)
|
|
1618
|
+
self.assertTrue(
|
|
1619
|
+
np.allclose(
|
|
1620
|
+
nn.AvgPool2d(kernel_size=3, stride=2, padding=1)(x),
|
|
1621
|
+
expected_avg_pool_output,
|
|
1622
|
+
)
|
|
1623
|
+
)
|
|
1624
|
+
# Test irregular kernel (2, 4), stride (3, 1) and padding (1, 2)
|
|
1625
|
+
x = mx.array(
|
|
1626
|
+
[
|
|
1627
|
+
[
|
|
1628
|
+
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]],
|
|
1629
|
+
[[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]],
|
|
1630
|
+
[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]],
|
|
1631
|
+
[[36, 37, 38], [39, 40, 41], [42, 43, 44], [45, 46, 47]],
|
|
1632
|
+
],
|
|
1633
|
+
[
|
|
1634
|
+
[[48, 49, 50], [51, 52, 53], [54, 55, 56], [57, 58, 59]],
|
|
1635
|
+
[[60, 61, 62], [63, 64, 65], [66, 67, 68], [69, 70, 71]],
|
|
1636
|
+
[[72, 73, 74], [75, 76, 77], [78, 79, 80], [81, 82, 83]],
|
|
1637
|
+
[[84, 85, 86], [87, 88, 89], [90, 91, 92], [93, 94, 95]],
|
|
1638
|
+
],
|
|
1639
|
+
]
|
|
1640
|
+
)
|
|
1641
|
+
expected_irregular_max_pool_output = [
|
|
1642
|
+
[
|
|
1643
|
+
[
|
|
1644
|
+
[3.0, 4.0, 5.0],
|
|
1645
|
+
[6.0, 7.0, 8.0],
|
|
1646
|
+
[9.0, 10.0, 11.0],
|
|
1647
|
+
[9.0, 10.0, 11.0],
|
|
1648
|
+
[9.0, 10.0, 11.0],
|
|
1649
|
+
],
|
|
1650
|
+
[
|
|
1651
|
+
[39.0, 40.0, 41.0],
|
|
1652
|
+
[42.0, 43.0, 44.0],
|
|
1653
|
+
[45.0, 46.0, 47.0],
|
|
1654
|
+
[45.0, 46.0, 47.0],
|
|
1655
|
+
[45.0, 46.0, 47.0],
|
|
1656
|
+
],
|
|
1657
|
+
],
|
|
1658
|
+
[
|
|
1659
|
+
[
|
|
1660
|
+
[51.0, 52.0, 53.0],
|
|
1661
|
+
[54.0, 55.0, 56.0],
|
|
1662
|
+
[57.0, 58.0, 59.0],
|
|
1663
|
+
[57.0, 58.0, 59.0],
|
|
1664
|
+
[57.0, 58.0, 59.0],
|
|
1665
|
+
],
|
|
1666
|
+
[
|
|
1667
|
+
[87.0, 88.0, 89.0],
|
|
1668
|
+
[90.0, 91.0, 92.0],
|
|
1669
|
+
[93.0, 94.0, 95.0],
|
|
1670
|
+
[93.0, 94.0, 95.0],
|
|
1671
|
+
[93.0, 94.0, 95.0],
|
|
1672
|
+
],
|
|
1673
|
+
],
|
|
1674
|
+
]
|
|
1675
|
+
expected_irregular_average_pool_output = [
|
|
1676
|
+
[
|
|
1677
|
+
[
|
|
1678
|
+
[0.3750, 0.6250, 0.8750],
|
|
1679
|
+
[1.1250, 1.5000, 1.8750],
|
|
1680
|
+
[2.2500, 2.7500, 3.2500],
|
|
1681
|
+
[2.2500, 2.6250, 3.0000],
|
|
1682
|
+
[1.8750, 2.1250, 2.3750],
|
|
1683
|
+
],
|
|
1684
|
+
[
|
|
1685
|
+
[15.7500, 16.2500, 16.7500],
|
|
1686
|
+
[24.7500, 25.5000, 26.2500],
|
|
1687
|
+
[34.5000, 35.5000, 36.5000],
|
|
1688
|
+
[27.0000, 27.7500, 28.5000],
|
|
1689
|
+
[18.7500, 19.2500, 19.7500],
|
|
1690
|
+
],
|
|
1691
|
+
],
|
|
1692
|
+
[
|
|
1693
|
+
[
|
|
1694
|
+
[12.3750, 12.6250, 12.8750],
|
|
1695
|
+
[19.1250, 19.5000, 19.8750],
|
|
1696
|
+
[26.2500, 26.7500, 27.2500],
|
|
1697
|
+
[20.2500, 20.6250, 21.0000],
|
|
1698
|
+
[13.8750, 14.1250, 14.3750],
|
|
1699
|
+
],
|
|
1700
|
+
[
|
|
1701
|
+
[39.7500, 40.2500, 40.7500],
|
|
1702
|
+
[60.7500, 61.5000, 62.2500],
|
|
1703
|
+
[82.5000, 83.5000, 84.5000],
|
|
1704
|
+
[63.0000, 63.7500, 64.5000],
|
|
1705
|
+
[42.7500, 43.2500, 43.7500],
|
|
1706
|
+
],
|
|
1707
|
+
],
|
|
1708
|
+
]
|
|
1709
|
+
self.assertTrue(
|
|
1710
|
+
np.array_equal(
|
|
1711
|
+
nn.MaxPool2d(kernel_size=(2, 4), stride=(3, 1), padding=(1, 2))(x),
|
|
1712
|
+
expected_irregular_max_pool_output,
|
|
1713
|
+
)
|
|
1714
|
+
)
|
|
1715
|
+
self.assertTrue(
|
|
1716
|
+
np.allclose(
|
|
1717
|
+
nn.AvgPool2d(kernel_size=(2, 4), stride=(3, 1), padding=(1, 2))(x),
|
|
1718
|
+
expected_irregular_average_pool_output,
|
|
1719
|
+
)
|
|
1720
|
+
)
|
|
1721
|
+
# Test repr
|
|
1722
|
+
self.assertEqual(
|
|
1723
|
+
str(nn.MaxPool1d(kernel_size=3, padding=2)),
|
|
1724
|
+
"MaxPool1d(kernel_size=(3,), stride=(3,), padding=(2,))",
|
|
1725
|
+
)
|
|
1726
|
+
self.assertEqual(
|
|
1727
|
+
str(nn.AvgPool1d(kernel_size=2, stride=3)),
|
|
1728
|
+
"AvgPool1d(kernel_size=(2,), stride=(3,), padding=(0,))",
|
|
1729
|
+
)
|
|
1730
|
+
self.assertEqual(
|
|
1731
|
+
str(nn.MaxPool2d(kernel_size=3, stride=2, padding=1)),
|
|
1732
|
+
"MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))",
|
|
1733
|
+
)
|
|
1734
|
+
self.assertEqual(
|
|
1735
|
+
str(nn.AvgPool2d(kernel_size=(1, 2), stride=2, padding=(1, 2))),
|
|
1736
|
+
"AvgPool2d(kernel_size=(1, 2), stride=(2, 2), padding=(1, 2))",
|
|
1737
|
+
)
|
|
1738
|
+
# Test 3d pooling
|
|
1739
|
+
x = mx.array(
|
|
1740
|
+
[
|
|
1741
|
+
[
|
|
1742
|
+
[
|
|
1743
|
+
[[0, 1, 2], [3, 4, 5], [6, 7, 8]],
|
|
1744
|
+
[[9, 10, 11], [12, 13, 14], [15, 16, 17]],
|
|
1745
|
+
[[18, 19, 20], [21, 22, 23], [24, 25, 26]],
|
|
1746
|
+
],
|
|
1747
|
+
[
|
|
1748
|
+
[[27, 28, 29], [30, 31, 32], [33, 34, 35]],
|
|
1749
|
+
[[36, 37, 38], [39, 40, 41], [42, 43, 44]],
|
|
1750
|
+
[[45, 46, 47], [48, 49, 50], [51, 52, 53]],
|
|
1751
|
+
],
|
|
1752
|
+
]
|
|
1753
|
+
]
|
|
1754
|
+
)
|
|
1755
|
+
expected_max_pool_output_no_padding_stride_1 = [
|
|
1756
|
+
[[[[39, 40, 41], [42, 43, 44]], [[48, 49, 50], [51, 52, 53]]]]
|
|
1757
|
+
]
|
|
1758
|
+
|
|
1759
|
+
expected_max_pool_output_no_padding_stride_2 = [[[[[39, 40, 41]]]]]
|
|
1760
|
+
expected_max_pool_output_padding_1 = [
|
|
1761
|
+
[
|
|
1762
|
+
[[[0, 1, 2], [6, 7, 8]], [[18, 19, 20], [24, 25, 26]]],
|
|
1763
|
+
[[[27, 28, 29], [33, 34, 35]], [[45, 46, 47], [51, 52, 53]]],
|
|
1764
|
+
]
|
|
1765
|
+
]
|
|
1766
|
+
expected_irregular_max_pool_output = [
|
|
1767
|
+
[
|
|
1768
|
+
[[[9, 10, 11], [12, 13, 14], [15, 16, 17]]],
|
|
1769
|
+
[[[36, 37, 38], [39, 40, 41], [42, 43, 44]]],
|
|
1770
|
+
]
|
|
1771
|
+
]
|
|
1772
|
+
|
|
1773
|
+
self.assertTrue(
|
|
1774
|
+
np.array_equal(
|
|
1775
|
+
nn.MaxPool3d(kernel_size=2, stride=1, padding=0)(x),
|
|
1776
|
+
expected_max_pool_output_no_padding_stride_1,
|
|
1777
|
+
)
|
|
1778
|
+
)
|
|
1779
|
+
self.assertTrue(
|
|
1780
|
+
np.array_equal(
|
|
1781
|
+
nn.MaxPool3d(kernel_size=2, stride=2, padding=0)(x),
|
|
1782
|
+
expected_max_pool_output_no_padding_stride_2,
|
|
1783
|
+
)
|
|
1784
|
+
)
|
|
1785
|
+
self.assertTrue(
|
|
1786
|
+
np.array_equal(
|
|
1787
|
+
nn.MaxPool3d(kernel_size=2, stride=2, padding=1)(x),
|
|
1788
|
+
expected_max_pool_output_padding_1,
|
|
1789
|
+
)
|
|
1790
|
+
)
|
|
1791
|
+
self.assertTrue(
|
|
1792
|
+
np.array_equal(
|
|
1793
|
+
nn.MaxPool3d(kernel_size=(1, 2, 1), stride=(1, 2, 1))(x),
|
|
1794
|
+
expected_irregular_max_pool_output,
|
|
1795
|
+
)
|
|
1796
|
+
)
|
|
1797
|
+
self.assertEqual(
|
|
1798
|
+
str(nn.MaxPool3d(kernel_size=3, stride=3, padding=2)),
|
|
1799
|
+
"MaxPool3d(kernel_size=(3, 3, 3), stride=(3, 3, 3), padding=(2, 2, 2))",
|
|
1800
|
+
)
|
|
1801
|
+
|
|
1802
|
+
expected_avg_pool_output_no_padding_stride_1 = [
|
|
1803
|
+
[
|
|
1804
|
+
[
|
|
1805
|
+
[[19.5, 20.5, 21.5], [22.5, 23.5, 24.5]],
|
|
1806
|
+
[[28.5, 29.5, 30.5], [31.5, 32.5, 33.5]],
|
|
1807
|
+
]
|
|
1808
|
+
]
|
|
1809
|
+
]
|
|
1810
|
+
|
|
1811
|
+
expected_avg_pool_output_no_padding_stride_2 = [[[[[19.5, 20.5, 21.5]]]]]
|
|
1812
|
+
expected_avg_pool_output_padding_1 = [
|
|
1813
|
+
[
|
|
1814
|
+
[
|
|
1815
|
+
[[0, 0.125, 0.25], [1.125, 1.375, 1.625]],
|
|
1816
|
+
[[3.375, 3.625, 3.875], [9, 9.5, 10]],
|
|
1817
|
+
],
|
|
1818
|
+
[
|
|
1819
|
+
[[3.375, 3.5, 3.625], [7.875, 8.125, 8.375]],
|
|
1820
|
+
[[10.125, 10.375, 10.625], [22.5, 23, 23.5]],
|
|
1821
|
+
],
|
|
1822
|
+
]
|
|
1823
|
+
]
|
|
1824
|
+
expected_irregular_avg_pool_output = [
|
|
1825
|
+
[
|
|
1826
|
+
[[[4.5, 5.5, 6.5], [7.5, 8.5, 9.5], [10.5, 11.5, 12.5]]],
|
|
1827
|
+
[[[31.5, 32.5, 33.5], [34.5, 35.5, 36.5], [37.5, 38.5, 39.5]]],
|
|
1828
|
+
]
|
|
1829
|
+
]
|
|
1830
|
+
|
|
1831
|
+
self.assertTrue(
|
|
1832
|
+
np.array_equal(
|
|
1833
|
+
nn.AvgPool3d(kernel_size=2, stride=1, padding=0)(x),
|
|
1834
|
+
expected_avg_pool_output_no_padding_stride_1,
|
|
1835
|
+
)
|
|
1836
|
+
)
|
|
1837
|
+
self.assertTrue(
|
|
1838
|
+
np.array_equal(
|
|
1839
|
+
nn.AvgPool3d(kernel_size=2, stride=2, padding=0)(x),
|
|
1840
|
+
expected_avg_pool_output_no_padding_stride_2,
|
|
1841
|
+
)
|
|
1842
|
+
)
|
|
1843
|
+
self.assertTrue(
|
|
1844
|
+
np.array_equal(
|
|
1845
|
+
nn.AvgPool3d(kernel_size=2, stride=2, padding=1)(x),
|
|
1846
|
+
expected_avg_pool_output_padding_1,
|
|
1847
|
+
)
|
|
1848
|
+
)
|
|
1849
|
+
self.assertTrue(
|
|
1850
|
+
np.array_equal(
|
|
1851
|
+
nn.AvgPool3d(kernel_size=(1, 2, 1), stride=(1, 2, 1))(x),
|
|
1852
|
+
expected_irregular_avg_pool_output,
|
|
1853
|
+
)
|
|
1854
|
+
)
|
|
1855
|
+
self.assertEqual(
|
|
1856
|
+
str(nn.AvgPool3d(kernel_size=3, stride=3, padding=2)),
|
|
1857
|
+
"AvgPool3d(kernel_size=(3, 3, 3), stride=(3, 3, 3), padding=(2, 2, 2))",
|
|
1858
|
+
)
|
|
1859
|
+
|
|
1860
|
+
def test_set_dtype(self):
|
|
1861
|
+
def assert_dtype(layer, dtype):
|
|
1862
|
+
for k, v in tree_flatten(layer.parameters()):
|
|
1863
|
+
self.assertEqual(v.dtype, dtype, f"dtype mismatch for {k}")
|
|
1864
|
+
|
|
1865
|
+
layer = nn.Linear(input_dims=4, output_dims=8, bias=True)
|
|
1866
|
+
assert_dtype(layer, mx.float32)
|
|
1867
|
+
|
|
1868
|
+
layer.set_dtype(mx.bfloat16)
|
|
1869
|
+
assert_dtype(layer, mx.bfloat16)
|
|
1870
|
+
|
|
1871
|
+
layer.set_dtype(mx.float32, lambda x: False)
|
|
1872
|
+
assert_dtype(layer, mx.bfloat16)
|
|
1873
|
+
|
|
1874
|
+
layer.set_dtype(mx.int32, lambda x: True)
|
|
1875
|
+
assert_dtype(layer, mx.int32)
|
|
1876
|
+
|
|
1877
|
+
layer.set_dtype(mx.int64, predicate=None)
|
|
1878
|
+
assert_dtype(layer, mx.int64)
|
|
1879
|
+
|
|
1880
|
+
layer.set_dtype(mx.int16, lambda x: mx.issubdtype(x, mx.integer))
|
|
1881
|
+
assert_dtype(layer, mx.int16)
|
|
1882
|
+
|
|
1883
|
+
def test_rnn(self):
|
|
1884
|
+
layer = nn.RNN(input_size=5, hidden_size=12, bias=True)
|
|
1885
|
+
inp = mx.random.normal((2, 25, 5))
|
|
1886
|
+
|
|
1887
|
+
h_out = layer(inp)
|
|
1888
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1889
|
+
|
|
1890
|
+
layer = nn.RNN(
|
|
1891
|
+
5,
|
|
1892
|
+
12,
|
|
1893
|
+
bias=False,
|
|
1894
|
+
nonlinearity=lambda x: mx.maximum(0, x),
|
|
1895
|
+
)
|
|
1896
|
+
|
|
1897
|
+
h_out = layer(inp)
|
|
1898
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1899
|
+
|
|
1900
|
+
with self.assertRaises(ValueError):
|
|
1901
|
+
nn.RNN(5, 12, nonlinearity="tanh")
|
|
1902
|
+
|
|
1903
|
+
inp = mx.random.normal((44, 5))
|
|
1904
|
+
h_out = layer(inp)
|
|
1905
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1906
|
+
|
|
1907
|
+
h_out = layer(inp, hidden=h_out[-1, :])
|
|
1908
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1909
|
+
|
|
1910
|
+
def test_gru(self):
|
|
1911
|
+
layer = nn.GRU(5, 12, bias=True)
|
|
1912
|
+
inp = mx.random.normal((2, 25, 5))
|
|
1913
|
+
|
|
1914
|
+
h_out = layer(inp)
|
|
1915
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1916
|
+
|
|
1917
|
+
h_out = layer(inp, hidden=h_out[:, -1, :])
|
|
1918
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1919
|
+
|
|
1920
|
+
inp = mx.random.normal((44, 5))
|
|
1921
|
+
h_out = layer(inp)
|
|
1922
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1923
|
+
|
|
1924
|
+
h_out = layer(inp, h_out[-1, :])
|
|
1925
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1926
|
+
|
|
1927
|
+
def test_lstm(self):
|
|
1928
|
+
layer = nn.LSTM(5, 12)
|
|
1929
|
+
inp = mx.random.normal((2, 25, 5))
|
|
1930
|
+
|
|
1931
|
+
h_out, c_out = layer(inp)
|
|
1932
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1933
|
+
self.assertEqual(c_out.shape, (2, 25, 12))
|
|
1934
|
+
|
|
1935
|
+
h_out, c_out = layer(inp, hidden=h_out[:, -1, :], cell=c_out[:, -1, :])
|
|
1936
|
+
self.assertEqual(h_out.shape, (2, 25, 12))
|
|
1937
|
+
self.assertEqual(c_out.shape, (2, 25, 12))
|
|
1938
|
+
|
|
1939
|
+
inp = mx.random.normal((44, 5))
|
|
1940
|
+
h_out, c_out = layer(inp)
|
|
1941
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1942
|
+
self.assertEqual(c_out.shape, (44, 12))
|
|
1943
|
+
|
|
1944
|
+
inp = mx.random.normal((44, 5))
|
|
1945
|
+
h_out, c_out = layer(inp, hidden=h_out[-1, :], cell=c_out[-1, :])
|
|
1946
|
+
self.assertEqual(h_out.shape, (44, 12))
|
|
1947
|
+
self.assertEqual(c_out.shape, (44, 12))
|
|
1948
|
+
|
|
1949
|
+
def test_quantized_embedding(self):
|
|
1950
|
+
emb = nn.Embedding(32, 256)
|
|
1951
|
+
qemb = nn.QuantizedEmbedding.from_embedding(emb, bits=8)
|
|
1952
|
+
x = mx.array([2, 6, 9, 3, 0, 3])
|
|
1953
|
+
y = emb(x)
|
|
1954
|
+
yq = qemb(x)
|
|
1955
|
+
self.assertLess((y - yq).abs().max(), qemb.scales.max())
|
|
1956
|
+
|
|
1957
|
+
x = mx.random.uniform(shape=(2, 256))
|
|
1958
|
+
y = emb.as_linear(x)
|
|
1959
|
+
yq = qemb.as_linear(x)
|
|
1960
|
+
|
|
1961
|
+
def cosine(a, b):
|
|
1962
|
+
ab = (a * b).sum(-1)
|
|
1963
|
+
aa = mx.linalg.norm(a, axis=-1)
|
|
1964
|
+
bb = mx.linalg.norm(b, axis=-1)
|
|
1965
|
+
return ab / aa / bb
|
|
1966
|
+
|
|
1967
|
+
self.assertGreater(cosine(y, yq).min(), 0.99)
|
|
1968
|
+
|
|
1969
|
+
def test_causal_mask(self):
|
|
1970
|
+
mask = nn.MultiHeadAttention.create_additive_causal_mask(4, mx.float16)
|
|
1971
|
+
self.assertFalse(mx.any(mx.isnan(mask)))
|
|
1972
|
+
self.assertTrue(mask[0, -1].item() < 0)
|
|
1973
|
+
|
|
1974
|
+
mask = nn.MultiHeadAttention.create_additive_causal_mask(4, mx.bfloat16)
|
|
1975
|
+
self.assertFalse(mx.any(mx.isnan(mask)))
|
|
1976
|
+
self.assertTrue(mask[0, -1].item() < 0)
|
|
1977
|
+
|
|
1978
|
+
def test_attention(self):
|
|
1979
|
+
attn = nn.MultiHeadAttention(32, 4)
|
|
1980
|
+
x = mx.random.normal(shape=(2, 5, 32))
|
|
1981
|
+
out = attn(x, x, x)
|
|
1982
|
+
self.assertEqual(out.shape, x.shape)
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
if __name__ == "__main__":
|
|
1986
|
+
mlx_tests.MLXTestRunner()
|