Uh oh!
There was an error while loading. Please reload this page.
Add repeat to the specification - #690
Conversation
kgryte
commented
Oct 19, 2023
I've updated the proposed specification to include a note advising conforming array libraries to include a warning regarding device synchronization if |
kgryte
commented
Jan 25, 2024
@leofang Would you mind giving this PR a review? I believe this PR addresses the concerns you raised in #654 (comment), but I want to confirm before merging. |
kgryte
commented
Feb 8, 2024
@leofang Pinging in case you missed the above. |
leofang
left a comment
There was a problem hiding this comment.
Thanks for pushing this, Athan. Sorry I missed the ping. Took a stab at it, no concerns.
Uh oh!
There was an error while loading. Please reload this page.
leofang
left a comment
There was a problem hiding this comment.
One more question: Should we also add a note on "data-dependent output shapes" like what we do for unique*/nonzero?
kgryte
commented
Feb 13, 2024
@leofang Re: admonition. I am not certain. It's only when And for that case, we include a note regarding device synchronization. If we add a "data-dependent admonition" here, this would make this API optional, which I am not sure is desirable. As a point of reference, in |
leofang
commented
Feb 13, 2024
Doesn't this API always have the output shape determined by the input data ( |
@leofang I added the data-dependent shape admonition. Given that JAX requires a Now that this has been added, I believe that this PR should be ready for another review. cc @rgommers |
rgommers
commented
Feb 20, 2024
Kinda sorta, but I think the "data-dependent shape" admonition is more aimed at the input values of the input array. E.g., the most common usage here will be with a literal int: I played with this a bit with JAX: >>>importjax>>>importjax.numpyasjnp>>>x=jnp.arange(3)
>>>jnp.repeat(x, 2)
Array([0, 0, 1, 1, 2, 2], dtype=int32)
>>>jnp.repeat(x, 2, total_repeat_length=x.size*2) # the documented way to allow JIT-ingArray([0, 0, 1, 1, 2, 2], dtype=int32)
>>>deffunc(x):
... returnjnp.repeat(x, 2)
... >>>func(x)
Array([0, 0, 1, 1, 2, 2], dtype=int32)
>>># It's not actually needed to use `total_repeat_length` if `repeats` is a literal int:>>>jax.jit(func)(x)
Array([0, 0, 1, 1, 2, 2], dtype=int32)
>>># It is needed if we make `repeats` data-dependent:>>>deffunc(x):
... returnjnp.repeat(x, x[2])
... >>>func(x)
Array([0, 0, 1, 1, 2, 2], dtype=int32)
>>>jax.jit(func)(x)
...
ConcretizationTypeError: Abstracttracervalueencounteredwhereconcretevalueisexpected: Traced<ShapedArray(int32[])>with<DynamicJaxprTrace(level=0/1)>A few conclusions:
# from scipy.signal testsrepeats[1::2] =x[1::2]
x=np.repeat(x, repeats)
# from scipy.integrate functionalitydiff.data/=np.repeat(h, np.diff(diff.indptr))My suggested resolution:
|
kgryte
commented
Feb 20, 2024
@rgommers We discussed making |
I'd say I agree with Leo's comments in that thread. There's just not much of a point of it being a sequence. It is not like you can do NumPy also documents it as an ndarray; the only reason a sequence works for NumPy is because it calls |
leofang
commented
Feb 20, 2024
Q: Does it make sense to say "when the input is an array, this API has data-dependent output shape" and followed by the note? |
rgommers
commented
Feb 20, 2024
I think so. I'd generalize it slightly - maybe "unless the |
kgryte
commented
Feb 21, 2024
Okay. I've dropped support for sequences and updated the admonition. The admonition now only allows optional support for providing an array as the second argument; all conforming libraries must support providing an integer. As providing sequences is still controversial and can be added in a subsequent revision of the standard, I think it is fine to omit for the time being. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
rgommers
left a comment
There was a problem hiding this comment.
LGTM now. I think this should be good to go; will aim to merge this at the end of today unless there are new comments.
rgommers
commented
Feb 22, 2024
Thanks @kgryte & all reviewers! |
This PR
repeatto the array API specification.repeatsto be either anintor anarray. NumPy and other inspired libraries and TensorFlow support one-dimensional arrays. NumPy also supports lists and tuples. CuPy docs suggest support for only lists and tuples. PyTorch supports a one-dimensional array; however, there has been discussion (linked to in the linked RFC) preferring sequences over arrays due to synchronization issues. However, it's not clear that providing a sequence of integers is particularly common or useful. In this PR, I've chosen to explicitly typerepeatsto supportintand array. Should sequences be considered acceptable, this can be revisited in a future revision of the Array API standard.repeatsargument.