GPU-accelerated gamma and lgamma functions for Apple MLX
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_gamma-0.1.1.tar.gz.
File metadata
- Download URL: mlx_gamma-0.1.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a353e73df37429af4844fc6477528b481ef7fa88dfa47c6af24dae25ebc8a1df
|
|
| MD5 |
72acf281f8266cbcfff406898c595da8
|
|
| BLAKE2b-256 |
77dd349e4f3bde62c16af8848f2270145665fad76609260bfa5594b3df3b581e
|
File details
Details for the file mlx_gamma-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mlx_gamma-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b4e9f4b88fe4d72751b018067fe7874345ab30c539385b6356870f094bdb0ec
|
|
| MD5 |
0e35a40dfddb43638a803fdc3892220d
|
|
| BLAKE2b-256 |
227e722c67710e5bf62fd395987b75b5e08da8d3ae9046cc454889b458b247f5
|