-
Notifications
You must be signed in to change notification settings - Fork 2k
Proposal: Introduce Array API Standard compat rule #24330
Description
Summary
Hi Ruff Team,
Coming from #24005, I would to propose an ARRAY001 rule for expanding the tooling for Array API Standard compatibility. The purpose of the rule would be to provide guidance and rewrite patterns for popular and library-specific API calls in NumPy, PyTorch, CuPy, and other with wide support for the Array API Standard to an array-agnostic ones.
Details
- Array API is a standard for array producing libraries in the Python ecosystem to unify user-facing API to a common set of operations and objects.
- NumPy is already fully compatible with the Array API, where a lot of this work happened in 2.x releases. Other libraries, such as JAX, PyTorch, or CuPy also have a high coverage.
- Users that want to update their codebase to the Array API agnostic version, to be able to use e.g. JAX instead of NumPy for arrays, need to go to every incompatible API call (and this kind of effort is taking place in e.g. scikit-learn to enable some estimators for GPU with PyTorch).
I think it would be useful to offer some additional automation of this process, by tackling the easiest rewrites with automatic fixes, and flag these API calls that are incompatible and provide a description of the migration pattern. We can't embed in the rule all possible variations how NumPy can be used, but at least the most popular cases which can be relatively easy to port here. For example use permute_dims() instead of transpose(). The rule would be always optional - to be enabled only for these users who care about Array API compatibility.
Would you accept such contribution for introducing a new group and the first rule? If a new group would be too large step to make (considering it's one aggregated rule or one rule per supported library) then it could live in NPY group.
Here are rewrite patterns for NumPy and PyTorch, for a start, in a spreadsheet. They involve rewriting:
module.func1(args, kwargs1)->module.func2(args, kwargs2)arr_obj.attr1->arr_obj.attr2arr_obj.method1(args, kwargs1)->arr_obj.method2(args, kwargs2)
https://docs.google.com/spreadsheets/d/1uA12mssGHV2MSxc0U0YQUiKNbbj8_NwYVuWAp0px73E/edit?usp=sharing