-
Notifications
You must be signed in to change notification settings - Fork 511
feature: Add Python array API standard integration #3712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Previously, only NumPy arrays were understood by ZenML's materializer machinery. This, however, is a problem for people using other array libraries, such as JAX, whose `jax.Array` type resembles NumPy array very closely, and supports its entire API. To support more array API types, implement a materializer built on the Python array API standard. This standardizes the array API in question, and exposes the Python module containing the respective array library's implementation in the `__array_namespace__` method of the array. In particular, this immediately adds native support for JAX and MLX arrays, since those two already implement the array API standard today.
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
Additional information to consider:
Interestingly, this integration is unlike others previously, since it can in principle deal with any array type coming from a library that implements the array API standard mentioned above. This is however hard for ZenML, which stores the types associated with a materializer in its To fully exploit the array API polymorphism would require either a) adding all known array API implementers to the materializer's associated types, which is what is currently shown in this PR, but kind of against the spirit of the solution in the first place (ZenML is supposed to call this materializer on anything and anyone having a b) materializers being dispatched not by a type match, but rather a protocol match (in this case, something like class ArrayLike(Protocol):
def __array_namespace__(self, *, version: str | None = None) -> types.ModuleType: ... I don't know enough about ZenML to judge how easy it is to achieve b), but I guess method a) can work well enough until there are complaints. |
Wonderful PR @nicholasjng <3 Let me discuss with the team as this has some bigger questions |
Previously, only NumPy arrays were understood by ZenML's materializer machinery. This, however, is a problem for people using other array libraries, such as JAX, whose
jax.Array
type resembles NumPy arrays very closely, and supports its entire API.To support more array API types, implement a materializer built on the Python array API standard. This standardizes the array API in question, and exposes the Python module containing the respective array library's implementation in the
__array_namespace__()
method of the array.In particular, this immediately adds native support for JAX and MLX arrays to ZenML, since those two already implement the array API standard today.
Consider the following mini pipeline:
Before this change, we see (along with other output):
[zeros] No materializer is registered for type <class 'jaxlib._jax.ArrayImpl'>, so the default Pickle materializer was used. Pickle is not production ready and should only be used for prototyping as the artifacts cannot be loaded when running with a different Python version. Please consider implementing a custom materializer for type <class 'jaxlib._jax.ArrayImpl'> according to the instructions at https://docs.zenml.io/how-to/handle-data-artifacts/handle-custom-data-types
After this change (I'll remove the debug log in the code later):
Left TODO are a few touch-ups as per the inline todo comments, and the dynamic associated types question.
As of now, the array API integration still has a hard dependency on NumPy, due to its use of
np.load()
.Also, I didn't take the time to implement the whole materializer interface yet, and removed NumPy 1.x support in the first prototype.
Happy for opinions and discussion.