-
Notifications
You must be signed in to change notification settings - Fork 89
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
refactor: implement nplike
registry (1 of 2)
#2389
Conversation
nplike
registrynplike
registry (1 of 2)
Codecov Report
Additional details and impacted files
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! Just one question, though:
nplike_of(obj)
fails for objects that are notnp.ndarray
, orcp.ndarray
, ..., orTypeTracerArray
Don't we need this to deal with array-like function arguments? The tests would fail if what I'm thinking is true, and that hasn't happened, so perhaps the array-like → array conversion (to_layout
) has already happened by the time nplike_of
gets called?
Yes! The main point of this PR is to make This PR is an alternative solution to an idea I had of giving |
TL;DR
This PR replaces a hard-coded loop-based search with a faster per-type lookup for each nplike.
TL
Context
We have an
nplike_of
L3 function in Awkward so that we can determine the most suitable array library among a set of complex input objects. This mechanism needs to extend beyond identifying the array library for the array type to layouts, builders, and potentially other objects.Our existing mechanism for finding the appropriate nplike for a given object is flawed, due to the following points:
backend
ornplike
_ext.ArrayBuilder
should not have a backend (it has no concept of such a thing)nplike_for
performs a non-cached lookup of several attributes. For array types, it loops over each nplikeExtending
nplike_for
to includeArrayBuilder
would require more customisation of this logic. At the same time, PRs like #2305 would benefit from the search for an nplike being as fast as possible.Details
In general, we can do better by generalising the search mechanism. This PR makes such a change, by introducing a type-cache that allows us to short-circuit the existing for loop (unless there is a cache miss):
register_nplike
function that lets the caller register an nplike. This replaces the need to explicitly enumerate the nplikes with the need to explicitly import all nplikes. Therefore, this is mainly an aesthetic change.nplike_of
is now only defined for individual array objects operated upon by nplikes, i.e. notak.index.Index
.nplike_of
is memoized by the array type.Changes
ak.index.Index(ak.index.Index(...))
is no longer supportednplike_of(obj)
fails for objects that are notnp.ndarray
, orcp.ndarray
, ..., orTypeTracerArray
.