ENH: add diag_indices, tril_indices, triu_indices#692
Open
bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
Open
ENH: add diag_indices, tril_indices, triu_indices#692bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
Conversation
Resolves data-apis#686. Adds the three index-generating functions that numpy, jax, and cupy all have but that are missing from the array-api standard and (so far) from this library. Signatures follow array-api conventions: parameter `offset` (matching `xp.linalg.diagonal`) instead of numpy's `k`; keyword-only arguments for everything except `n`; `xp` is required (these functions have no input array to infer from, following the `default_dtype` precedent). Delegation: - numpy/cupy/jax: forward directly (signatures match verbatim). - dask: has tril/triu_indices but no diag_indices. - torch: has tril/triu_indices but with (row, col, *, offset) signature returning a 2xN tensor rather than a tuple; delegation translates. No torch.diag_indices exists; falls through to generic. - sparse, array-api-strictest: fall through to generic; marked xfail on those backends (no nonzero / data-dependent shapes). Generic implementation uses `xp.arange` + broadcasting + `xp.nonzero` for the triangle variants. Validation (n >= 0, ndim >= 1, m >= 0) happens in the delegation layer so all backends produce consistent ValueErrors. Also fixes a pre-existing bug in tests/conftest.py's NumPyReadOnly wrapper: `type(o)(*gen)` worked for namedtuples but failed for plain tuples of length >= 2. Exposed here because these are the first functions in the library that return a tuple of arrays.
e905c26 to
2654568
Compare
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.
Resolves #686.
Summary
diag_indices,tril_indices,triu_indices— three index-generating functions that numpy, jax, and cupy all have but that are missing from the array-api standard and from this library.offset(matchingxp.linalg.diagonal) instead of numpy'sk; keyword-only arguments for everything exceptn;xpis required (these functions have no input array to infer from, following thedefault_dtypeprecedent).Numpy migration
Delegation
tril_indices/triu_indicesbut nodiag_indices— the last one falls through to the generic impl.tril_indices/triu_indicesbut with(row, col, *, offset)signature returning a 2×N tensor rather than a tuple; delegation translates. Notorch.diag_indicesexists.nonzero/ data-dependent shapes).Validation (
n >= 0,ndim >= 1,m >= 0) happens in the delegation layer via a shared_check_nonneghelper, so all backends emit consistentValueErrors before any backend-specific code runs.Generic implementation
diag_indices→(xp.arange(n),) * ndim.tril_indices/triu_indices→ shared_tri_indiceshelper:xp.arange+ broadcasting +xp.nonzeroon the mask. Pure array-api, fully lazy on dask.Also in this PR
Fixes a pre-existing bug in
tests/conftest.py'sNumPyReadOnlywrapper:type(o)(*gen)worked for namedtuples but failed for plain tuples of length ≥ 2. Exposed here because these are the first functions in the library that return a tuple of arrays.Test plan
pytest tests/test_funcs.py::TestDiagIndices tests/test_funcs.py::TestTriIndices— 155 passed across numpy, torch, jax, dask, array-api-strict (+ xfail on sparse/strictest/dask-use_to_read where noted).pytest tests/test_funcs.pyfull — all passing.lefthook run pre-commit --all-files— ruff, numpydoc, mypy, pyright, blacken-docs, validate-pyproject, dprint, typos all green.lazy_xp_function(tril_indices)/lazy_xp_function(triu_indices)assert 0.compute()calls, holds for both native and generic paths.