Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add support for returning upper and lower triangles of an array #237

Closed
kgryte opened this issue Jul 26, 2021 · 4 comments · Fixed by #243
Closed

Proposal: add support for returning upper and lower triangles of an array #237

kgryte opened this issue Jul 26, 2021 · 4 comments · Fixed by #243
Labels
API extension Adds new functions or objects to the API.

Comments

@kgryte
Copy link
Contributor

kgryte commented Jul 26, 2021

Proposal

Add APIs for returning the upper (triu) and lower (tril) triangles of an array.

Motivation

Currently, the array API specification does not provide APIs for returning the upper and lower triangles of an array.

These APIs were identified as potential candidates in gh-187 for inclusion in the next version of the standard. However, depending on the outcome of gh-217 may be necessary to include in the current version in order to ensure users can maintain equivalent functionality.

Background

triu and tril APIs are common across array libraries (see here). The notable exception being TensorFlow. MXNet seems to have APIs for returning the triangles of an array, but these are not documented in public documentation, so were not picked up during API comparison analysis.

Both triu and tril should be straightforward to include in the specification, as the signatures are consistent across libraries.

tril(m, k=0) → array
triu(m, k=0) → array

where k specifies the diagonal above/below which to zero elements.

@kgryte kgryte added the API extension Adds new functions or objects to the API. label Jul 26, 2021
@leofang
Copy link
Contributor

leofang commented Jul 26, 2021

@kgryte You're 10 mins ahead of me! I was working on cupy/cupy#5555 and just realized the very same need. I think we need them to be able to handle and return a batch of matrices like all other linalg functions, which is currently not possible with NumPy/CuPy.

@kgryte
Copy link
Contributor Author

kgryte commented Jul 26, 2021

@leofang I think handling batches could make sense; however, I don't think any array libraries currently support batching, including Torch. Should be a straightforward extension, as should not conflict with any existing NumPy et al usage patterns.

@lezcano
Copy link
Contributor

lezcano commented Jul 26, 2021

The docs of tril and triu are outdated, PyTorch does support batched inputs for these functions. I agree with @leofang that it would be good to write the specification for batched inputs.

@kgryte
Copy link
Contributor Author

kgryte commented Aug 23, 2021

I've opened gh-243 which adds both tril and triu to the specification, with support for providing a stack of matrices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API extension Adds new functions or objects to the API.
Projects
None yet
3 participants