Improve clipping time with np_compat.clip#447
Open
Lasloruhberg wants to merge 6 commits into
Open
Conversation
Contributor
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.
Comment on lines
+108
to
+115
| def clip( | ||
| x: Array, | ||
| /, | ||
| min: float | Array | None = None, | ||
| max: float | Array | None = None, | ||
| out: Array | None = None, | ||
| **kwargs, | ||
| ) -> Array: |
Author
There was a problem hiding this comment.
Removed out, if in kwargs pop it from there and use internally in function only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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