feat: native i0 (modified Bessel function) and kaiser window#3156
Open
Vlor999 wants to merge 1 commit intoml-explore:mainfrom
Open
feat: native i0 (modified Bessel function) and kaiser window#3156Vlor999 wants to merge 1 commit intoml-explore:mainfrom
Vlor999 wants to merge 1 commit intoml-explore:mainfrom
Conversation
a09f9d0 to
11b9b39
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces native hardware-accelerated support for the modified Bessel function of the first kind, order zero (
mlx.core.i0), and leverages it to implement the Kaiser window function (mlx.core.kaiser).Implementing
i0natively allowskaiserto be fully compiled and evaluated on Apple Silicon GPUs without relying on slow CPU fallback loops or external libraries.Implementation Details
1. Hardware-Accelerated
i0(Cephes Approximation)Since$I_0(x)$ cannot be computed via simple composition, it is implemented as a core unary primitive using the industry-standard Cephes polynomial approximation (matching NumPy/SciPy):
mlx/backend/cpu/simd/math.h): Implemented vectorized evaluation usingSimd<T, N>and FMA instructions for high-throughput CPU processing.mlx/backend/metal/kernels/i0.h): Implemented a native Metal shader usingmetal::fmaandmetal::precise::exp/sqrtto ensure numerical stability matching the CPU.mlx/primitives.cpp): Implemented the analytic derivative. Sincejvppass using the same two-domain Cephes approximation logic.2. Kaiser Window (
mlx.core.kaiser)mlx/ops.cppas a composite function using the newi0primitive.1.0fto avoid unnecessary array allocations in thesqrtterm. Handles edge cases (M=0,M=1).3. Python Bindings
i0andkaiserinpython/src/ops.cppwith fully typednb::sigand LaTeX docstrings.Test Plan
Added comprehensive tests in
python/tests/test_ops.py:test_i0): Checkedmx.i0againstnumpy.i0across both polynomial domains (|x| <= 3.75and|x| > 3.75).test_i0_cpu_gpu_parity): Verified that forcingmx.stream(mx.cpu)andmx.stream(mx.gpu)yields identical results within1e-5tolerance.test_kaiser_general): Verifiedmx.kaiser(M, beta)againstnp.kaiserincluding edge cases (M=1) and symmetry.All tests pass successfully.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes