Closed kgryte closed 5 months ago
I've updated this PR based on feedback from the 30 November 2023 workgroup meeting. Namely,
clamp
to clip
. Given widespread usage of this API, it was considered undesirable to rename and deprecate clip
in NumPy et al. Even though this PR introduces behavior which differs from NumPy (e.g., kwargs, output data type, etc), it was considered better if NumPy simply moved to adopt specified behavior.x
and not the result of type promotion. An argument could be made either way; however, as discussed during the workgroup meeting, user expectation is most likely to be that the output data type matches x
, and the specification does not have precedent (TMK) for array kwargs influencing the output data type. As such, specification was guidance was updated accordingly. Note, however, that this differs from current behavior in, e.g., NumPy.@rgommers I've updated min
and max
to be both positional and kwarg. And I added a note stating that arguments having different data type kinds will result in implementation-dependent behavior.
As this PR has received the OK and has not received any further comments, will go ahead and merge. Thanks all!
This PR
clip
to the Array API specification for clamping each element of an input array to a specified range.x
,min
,max
). This would be in the spirit of earlier efforts to ensure that type promotion rules are applied consistently throughout the specification. However, in contrast to https://github.com/data-apis/array-api/pull/201,min
andmax
are kwargs, and we do not have, TMK, any precedent for array kwargs influencing the data type of the output array. Furthermore, when clamping, users are more likely to want an output array of the same data type as the input array (this was also raised on the NumPy issue tracker: https://github.com/numpy/numpy/issues/24976).x
to be broadcast, thus allowing the output array to have a rank greater than the input array. This differs from TensorFlow, which requires that the output array shape be the same as the input array shape. NumPy, however, supports such broadcasting behavior. Note thatx
can be broadcast is somewhat at odds with not allowing type promotion. For the output data type, I argued thatmin
andmax
should not affect the output data type, but, in allowingx
to be broadcast, this would mean thatmin
andmax
should affect the output array shape. This is likely fine and consistent with the rest of the specification, where we have plenty of kwargs which affect the output array shape, although this would be the first, TMK, involving broadcasting.min > max
, behavior is unspecified. NumPy et al set output values tomax
; however, other implementations should be free to raise an exception or support alternative behavior.min
andmax
to be optional. When bothmin
andmax
areNone
, the function is essentially a no-op. This follows PyTorch, but differs from NumPy which allowsmin
andmax
beNone
, but not at the same time.x
is an integer data type andmin
ormax
is a floating-point data type), which is consistent elsewhere in the specification. TensorFlow raises an exception in such a scenario.min
andmax
positional and keyword arguments.clip
. TensorFlow uses the nameclip_by_value
. PyTorch also includesclip
, but this aliases toclamp
.Note that this PR would introduce changes to existing
clip
functionality in NumPy et al. Namely,min
andmax
are positional and keyword arguments; whereas, in NumPy,a_min
anda_max
are positional.a_min
anda_max
.min
ormax
be allowed to beNone
at the same time.