Skip to content

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

nicholasjng
Copy link

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:

from zenml import step, pipeline
import jax
import jax.numpy as jnp


@step
def zeros() -> jax.Array:
    return jnp.zeros(5)

@step
def compute_sum(arr: jax.Array) -> float:
    return jnp.sum(arr).item()


@pipeline(enable_cache=False)
def basic_pipeline():
    arr = zeros()
    compute_sum(arr)


if __name__ == "__main__":
    basic_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):

[compute_sum] Array type <class 'jax.Array'> processed by ArrayMaterializer

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.

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.
Copy link
Contributor

coderabbitai bot commented May 28, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nicholasjng
Copy link
Author

Additional information to consider:

  • This can eventually replace the existing NumPy materializer, minus the existing backwards compatibility code.
  • Compressed NumPy arrays (.npz files, saved with np.savez_compressed) could also be supported, which creates smaller archives for integer-valued arrays.

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 ASSOCIATED_TYPES field, which is an immutable class variable attribute.

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 __array_namespace__() method), or

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.

@htahir1
Copy link
Contributor

htahir1 commented May 28, 2025

Wonderful PR @nicholasjng <3 Let me discuss with the team as this has some bigger questions

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