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

ENH: create_diagonal: support broadcasting #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NeilGirdhar
Copy link
Contributor

@NeilGirdhar NeilGirdhar commented Feb 7, 2025

Fixes #136

@NeilGirdhar
Copy link
Contributor Author

I'm not entirely sure what I should do about the numpy dependency. Do I re-implement ndindex somewhere?

Also, I'm getting a strange error when trying to run the tests about a bad device.

@lucascolley lucascolley added the enhancement New feature or request label Feb 7, 2025
@lucascolley lucascolley changed the title Make create_diagonal support broadcasting ENH: create_diagonal: support broadcasting Feb 7, 2025
@lucascolley
Copy link
Member

lucascolley commented Feb 7, 2025

yes, we will want to remove the NumPy dependency. I don't know how easy it is to make ndindex agnostic.

Also, I'm getting a strange error when trying to run the tests about a bad device.

np.ndindex will be producing arrays with the default NumPy device, so you need to be careful to coerce to the correct device when that is used (things are probably going wrong in the at call). But this problem should be subsumed in removing the NumPy dependency.

@NeilGirdhar NeilGirdhar force-pushed the create_diagonal branch 3 times, most recently from 5be6c57 to 9d28bf5 Compare February 8, 2025 05:58
@NeilGirdhar
Copy link
Contributor Author

Ready for your review 😄

@NeilGirdhar NeilGirdhar force-pushed the create_diagonal branch 3 times, most recently from 538e551 to 04b970b Compare February 8, 2025 10:50
@lucascolley
Copy link
Member

@mdhaber has ndindex come up in any of your work on vectorising things?

@NeilGirdhar
Copy link
Contributor Author

What do you mean? I just need it for this create_diagonal.

@lucascolley
Copy link
Member

I'm just wondering whether this is an instance of a problem that has appeared elsewhere

@NeilGirdhar
Copy link
Contributor Author

Whenever I need ndindex, I use the one from Numpy. I've never been reluctant to import Numpy, so I would not use a version from xpx.

@lucascolley
Copy link
Member

lucascolley commented Feb 8, 2025

Sure, I was just asking Matt if anything like this had come up in the context of array-agnostic code yet for him

@NeilGirdhar
Copy link
Contributor Author

Oh sorry! I didn't see the tag. I haven't been able to sleep tonight and I'm just tired.

@lucascolley
Copy link
Member

no worries!

@mdhaber
Copy link
Contributor

mdhaber commented Feb 8, 2025

Yes, I used it in the decorator that adds batch support in linalg. For the decorator used in stats, I considered using ndindex before going with apply_along_axis, but if I were to rewrite it I'd just use ndindex. Both of these are just for NumPy right now, though.

I've thought about having a heuristic in marray that decides whether to loop over slices and compress (remove masked elements from) each slice before performing the operation. This can be faster than the usual thing (say, data[mask] = 0 in sum) when there are fewer slices and they are very big. But for that, I assumed I would reshape to have just one batch dimension at the front, loop over that single axis, and then reshape the result. But by "reshape" I really mean moveaxis + reshape, and ndindex would save the trouble of reshape.

@lucascolley
Copy link
Member

Makes sense, thanks! In this case, does the approach of making ndindex agnostic seem good to you?

@NeilGirdhar NeilGirdhar force-pushed the create_diagonal branch 2 times, most recently from 91b2096 to 48bae8b Compare February 9, 2025 17:48
@lucascolley lucascolley added this to the 0.7.0 milestone Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: create_diagonal: support broadcasting
3 participants