Improve clipping time with np_compat.clip#447
Conversation
There was a problem hiding this comment.
Pull request overview
This PR replaces the NumPy backend’s clip alias with a NumPy-specific implementation that calls np.clip directly (after casting bounds to x.dtype) to restore near-native NumPy performance, and adds targeted regression tests for dtype/broadcasting/out behavior.
Changes:
- Implement
array_api_compat.numpy.clipas a dedicated wrapper aroundnp.clipwith dtype-preserving bound casting and reduced intermediate allocations. - Add NumPy-specific tests covering broadcasting,
out=behavior, and dtype preservation (including edge cases with bounds outside representable ranges). - Minor formatting cleanups in
numpy/_aliases.py(line wrapping / commas /__all__updates).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
tests/test_numpy.py |
Adds tests for NumPy clip behavior (broadcasting, out, dtype preservation, and edge cases). |
src/array_api_compat/numpy/_aliases.py |
Replaces the generic clip alias with a NumPy-specialized implementation to improve performance and preserve dtype. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def clip( | ||
| x: Array, | ||
| /, | ||
| min: float | Array | None = None, | ||
| max: float | Array | None = None, | ||
| out: Array | None = None, | ||
| **kwargs, | ||
| ) -> Array: |
There was a problem hiding this comment.
Removed out, if in kwargs pop it from there and use internally in function only
ev-br
left a comment
There was a problem hiding this comment.
Thanks.
Given the (somewhat cryptic) comments in the "generic" clip,
https://github.com/data-apis/array-api-compat/blob/main/src/array_api_compat/common/_aliases.py#L395, could you please add some tests to probe the uint64->float64 promotion that this comments hint at?
expand test suite for further type promotion
|
I have reworked the test.
|
|
Unsure why tests fail, seems to be a server issue: Would suggest to wait till tomorrow to see if it fixes itself and then review this |
This PR introduces a numpy specific alias to xp.clip to improve performance, adressing Issue #444.
With respect to the basic implementation I removed array copies and new creations where possible and revert to the intrinsic np.clip.
The benchmark also implies that dask clip is quite below its possible performance, as it remains well below the other programs.
As far as I can tell this rewrite is compliant with the API standard, but please advise if not.
I am happy to receive feedback.
Current on main (run on a laptop, no GPU):
Batch size: 1024
Image shape: (128, 128, 3)
Measurement runs: 8
Skipped backends: cupy (unavailable)
Now on this branch
Batch size: 1024
Image shape: (128, 128, 3)
Measurement runs: 15
As evident the new clip version for numpy restores its native performance, while ensuring the same broadcasting and dtype preservation behaviour
Benchmark:
benchmark.py