Skip to main content

mlx-gamma

GPU-accelerated gamma and lgamma functions for Apple MLX.

MLX is the only major ML framework missing gamma/lgamma support (mlx#2030). PyTorch, JAX, and CuPy all provide these. mlx-gamma fills that gap with pure-MLX vectorized implementations that run entirely on the Apple GPU.

Installation

pip install mlx-gamma

Or from source:

git clone https://github.com/akaiHuang/mlx-gamma.git
cd mlx-gamma
pip install -e ".[dev]"

Quick start

import mlx.core as mx
from mlx_gamma import lgamma, gamma, digamma, beta

x = mx.array([1.0, 2.0, 3.0, 5.0, 10.0, 100.0])

lgamma(x)   # log-gamma
gamma(x)    # gamma function (with sign handling)
digamma(x)  # psi function, d/dx ln Gamma(x)

beta(mx.array([2.0, 3.0]), mx.array([3.0, 4.0]))  # Beta function

Functions

Function Description Domain
lgamma(x) Log of absolute value of Gamma(x) x != 0, -1, -2, ...
gamma(x) Gamma function via exp(lgamma(x)) with sign x != 0, -1, -2, ...
digamma(x) Psi function (logarithmic derivative of Gamma) x != 0, -1, -2, ...
beta(a, b) Beta function B(a,b) = Gamma(a)Gamma(b)/Gamma(a+b) a, b > 0

Algorithm details

  • lgamma: Lanczos approximation (g=7, 9 coefficients) for x >= 0.5; reflection formula for x < 0.5. Accurate to ~13 decimal digits in float64, ~6 in float32.
  • gamma: exp(lgamma(x)) with sign correction for negative arguments.
  • digamma: Asymptotic expansion for x >= 7; recurrence relation to shift small arguments upward; reflection formula for x < 0.
  • beta: Computed via exp(lgamma(a) + lgamma(b) - lgamma(a+b)) for numerical stability.

Benchmarks

python benchmark.py

Measured on Apple M1 Max, MLX 0.31.1, SciPy 1.16.2, float32, 50 timed runs per cell with median wall-clock time (warmup excluded). GPU dispatch overhead dominates small arrays; the crossover where mlx-gamma starts beating scipy is around N = 50k--100k.

N lgamma gamma digamma
1,000 0.02x 0.03x 0.02x
10,000 0.21x 0.50x 0.22x
100,000 2.33x 3.59x 1.66x
1,000,000 6.41x 10.37x 3.67x

Numbers are scipy_time / mlx_time (>1 means mlx-gamma is faster). Full timings, beta-function results, accuracy table, and methodology in benchmark_results.md.

Running tests

pip install -e ".[dev]"
pytest tests/ -v

License

MIT -- see LICENSE.

Author

Sheng-Kai Huang (akai@fawstudio.com)

References

  • MLX issue: apple/mlx#2030
  • Lanczos, C. (1964). "A Precision Approximation of the Gamma Function." SIAM J. Numer. Anal. 1: 86--96.
  • Pugh, G. R. (2004). "An Analysis of the Lanczos Gamma Approximation." PhD thesis, University of British Columbia.

Metadata

Release files for mlx-gamma 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlx-gamma 0.1.1
File Size Uploaded Provenance Actions
mlx_gamma-0.1.1.tar.gz 7.7 kB Without provenance Details for mlx_gamma-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-gamma 0.1.1
File Interpreter ABI Platform Actions
mlx_gamma-0.1.1-py3-none-any.whl Python 3 none any Details for mlx_gamma-0.1.1-py3-none-any.whl

Total release size: 14.5 kB

Release files / mlx_gamma-0.1.1.tar.gz

Download URL mlx_gamma-0.1.1.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a353e73df37429af4844fc6477528b481ef7fa88dfa47c6af24dae25ebc8a1df
BLAKE2b-256 checksum
How to use checksums
77dd349e4f3bde62c16af8848f2270145665fad76609260bfa5594b3df3b581e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / mlx_gamma-0.1.1-py3-none-any.whl

Download URL mlx_gamma-0.1.1-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b4e9f4b88fe4d72751b018067fe7874345ab30c539385b6356870f094bdb0ec
BLAKE2b-256 checksum
How to use checksums
227e722c67710e5bf62fd395987b75b5e08da8d3ae9046cc454889b458b247f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for mlx-gamma 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page