Skip to content

jax-pyrefly-stubs: type binary ufuncs with BinaryUfunc protocol - #5664

Open
QEDady wants to merge 1 commit into
pyrefly-org:mainfrom
QEDady:jax-stubs-binary-ufunc
Open

QEDady wants to merge 1 commit into
pyrefly-org:mainfrom
QEDady:jax-stubs-binary-ufunc

Conversation

@QEDady

@QEDady QEDady commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

In jax.numpy, binary universal functions (add, subtract, multiply, maximum, minimum, logaddexp, logaddexp2, bitwise_and, bitwise_or, bitwise_xor, logical_and, logical_or, and logical_xor) are runtime jnp.ufunc instances rather than plain functions, and stock jax/numpy/__init__.pyi types them with a BinaryUfunc protocol exposing .reduce, .accumulate, .at, .reduceat, and .outer. Previously, these were declared with def, so accessing ufunc methods like jnp.maximum.accumulate or jnp.add.reduce failed with Object of class FunctionType has no attribute ...:

import jax.numpy as jnp

x = jnp.ones((3, 4))
running_max = jnp.maximum.accumulate(x, axis=-1)  # previously failed with missing-attribute
row_sums = jnp.add.reduce(x, axis=1)
pairwise = jnp.add.outer(x, jnp.ones((2, 5)))

This PR adds the BinaryUfunc protocol to with shape-preserving/shape-transforming signatures for __call__, reduce, accumulate, at, reduceat, and outer, updates class ufunc to include at and match reduce/reduceat parameters, and types the binary ufuncs as BinaryUfunc.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

Added unit tests.

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you rebase?

Auto-import of PRs got paused when we renamed the repo to pyrefly-org/pyrefly. It seems to be running again, I think a rebase should trigger and import diff. Other than that this LGTM

@QEDady
QEDady force-pushed the jax-stubs-binary-ufunc branch from 63f3b64 to 0719bea Compare November 16, 2026 21:50
@meta-codesync

meta-codesync Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D132380863. (Because this pull request was imported automatically, there will not be any future comments.)

@QEDady

QEDady commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased. Seems the bot picked it up. Thank you!

@QEDady
QEDady requested a review from stroxler November 16, 2026 21:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants