Add endpoint parameter to linspace - #4184
Draft
AKnassa wants to merge 1 commit into
Draft
Conversation
numpy, jax and the array API all let the caller drop the stop value and space the samples over the half-open interval [start, stop) instead. Add the same option, spacing by (stop - start) / num rather than (stop - start) / (num - 1) when it is off. endpoint defaults to true, so existing behaviour is unchanged. Fixes ml-explore#4152
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.
Proposed changes
Fixes #4152.
mx.linspacealways includes the stop value. numpy, jax and the array API alllet the caller drop it and space the samples over the half-open interval
[start, stop)instead. This adds the same option:The implementation is the one-line change it looks like: divide by
numinsteadof
num - 1when the endpoint is excluded.endpointdefaults totrue, so existing behaviour is unchanged.Placement of the argument
I put
endpointafterdtypeand before the stream, both in the C++ signatureand in the Python binding, so the stream stays last as it is everywhere else in
the API. That does shift the position of
StreamOrDevicein the C++ signature,but no in-tree caller passes the stream positionally to
linspace, and thePython
streamargument is keyword-only in practice. Happy to move it if youwould rather it went elsewhere — the array API puts
endpointkeyword-onlyafter
dtype/device, while numpy has it fourth positional, so there is nosingle obvious answer.
Testing
test_linspace_endpointinpython/tests/test_ops.py, checked against numpyfor
numin{0, 1, 2, 5, 50}, for negative ranges, and with an integerdtype. It fails before this change and passes after.
test linspacecase intests/ops_tests.cpp, since theC++ signature changed, including the
num == 1andnum == 0edges..pyipicks the new signature up on build.Built CPU-only (
MLX_BUILD_METAL=OFF); the GPU suite was not run on my machine.Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes