Skip to content

Add MLX dispatch and a Metal kernel for betainc - #2368

Merged
jessegrabowski merged 6 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-betainc
Aug 23, 2026
Merged

Add MLX dispatch and a Metal kernel for betainc#2368
jessegrabowski merged 6 commits into
pymc-devs:mainfrom
jessegrabowski:mlx-betainc

Conversation

@jessegrabowski

Copy link
Copy Markdown
Member

betainc was the last op keeping logcdf off the MLX backend -- Beta, StudentT, SkewStudentT, Binomial and NegativeBinomial all go through it.

The Metal kernel isn't a speedup layered on the vectorized dispatch; that dispatch is memory-bound. MLX materializes a buffer per iteration when a loop's coefficients are arrays, so 120 unrolled continued-fraction steps peak near 2.9 KB per element, which is 20 GiB at n=1e7, while the kernel keeps the recurrence in registers. It stays in as the float64 route and as the reference the kernel is tested against, since Metal is float32-only. Accuracy is 1e-15 to 1e-11 at float64 across the parameter plane, except where min(a, b) is large and a/(a+b) is small, which degrades to about 1e-5.

Comment thread tests/link/mlx/scalar/test_beta.py Outdated
np.testing.assert_allclose(left + right, 1.0, rtol=0.0, atol=1e-9)


def test_beta_logcdf():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we testing logcdfs and the like in pytensor?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's the only real use-case for an integration test of this function. Happy to remove it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use case is we have a betainc that behaves like a reference backend. This seems more like a minimal pymc test, not a pytensor one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed.

They assert PyMC's logcdf expressions rather than that betainc matches the reference backend, which is what this suite is for.
@jessegrabowski
jessegrabowski merged commit f258d7b into pymc-devs:main Aug 23, 2026
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants