Skip to main content

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

Compares against scipy.special.gammaln on CPU. Typical results on M1 Max for 1M elements:

Function scipy (CPU) mlx-gamma (GPU) Speedup
lgamma ~12 ms ~0.8 ms ~15x

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mlx_gamma-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mlx_gamma-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file mlx_gamma-0.1.0.tar.gz.

File metadata

  • Download URL: mlx_gamma-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for mlx_gamma-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a57653cd41947cac97ec25ae123b7fdd3b2173c314c481f95d53a47f21d700de
MD5 6c2ced2bfb868e6810564b1cb96958bf
BLAKE2b-256 d0ab5cda25891a0247670148672f2f8426f46bbcd575b9978d3523982ebe8166

See more details on using hashes here.

File details

Details for the file mlx_gamma-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mlx_gamma-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for mlx_gamma-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2aafb28b3861fe918faffa01e3b5251e294995393c353db6c25e3aed862e4b88
MD5 4bb964cc0f441f042ad95ca2cdf32c18
BLAKE2b-256 51639f588adacf5d5f194335cea54e56d9309de9e78b7aff72cb2484ecf20dfc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page