Skip to content

Fix strides for 0-sized arrays#497

Closed
alexfikl wants to merge 2 commits into
inducer:mainfrom
alexfikl:fix-strides-empty-array
Closed

Fix strides for 0-sized arrays#497
alexfikl wants to merge 2 commits into
inducer:mainfrom
alexfikl:fix-strides-empty-array

Conversation

@alexfikl

@alexfikl alexfikl commented Oct 7, 2021

Copy link
Copy Markdown
Contributor

Found one last issue with 0-sized arrays in inducer/arraycontext#91.

This was messing up the code generation on the pytato side and it would fail in the invoker when checking strides.

@alexfikl alexfikl force-pushed the fix-strides-empty-array branch 2 times, most recently from 8a2f5ba to a2fd498 Compare October 7, 2021 14:32
Comment thread loopy/kernel/array.py Outdated
@alexfikl alexfikl force-pushed the fix-strides-empty-array branch 3 times, most recently from 60bbcd7 to 59c65fb Compare October 7, 2021 18:59

@kaushikcfd kaushikcfd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, could you please also add a tiny regression that fails on current main.

Comment thread loopy/kernel/array.py Outdated
@alexfikl alexfikl force-pushed the fix-strides-empty-array branch from 59c65fb to f0abbd9 Compare October 15, 2021 19:19
@alexfikl

Copy link
Copy Markdown
Contributor Author

could you please also add a tiny regression that fails on current main.

Definitely! Added one in bd5ed02.

That fails on main because cl.array.to_device reuses the strides that numpy gives (here (8, 8)) and loopy computed the strides differently (here (0, 8)), resulting in a mismatch.

@alexfikl alexfikl force-pushed the fix-strides-empty-array branch 2 times, most recently from 802f419 to 89f0fa3 Compare October 15, 2021 21:27

@kaushikcfd kaushikcfd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment thread loopy/kernel/array.py Outdated
from pymbolic.primitives import If, Comparison
new_stride_axis = If(
Comparison(dim_tag.stride, "<", 1),
1, dim_tag.stride)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not loving this, on multiple levels.

  • If we merge this, the cost of the conditional is incurred on potentially every single array access.
  • I'm not sure why this logic needs to live here (and why code in the invokers don't suffice). Could you explain?
  • Negative strides are totally fine: np.arange(10)[::-1].strides -> (-8,) (Execution targets don't support negative strides #504)
  • Zero strides are also, in principle, well-defined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this logic needs to live here (and why code in the invokers don't suffice). Could you explain?

Mostly because this was the direction I went down the rabbit hole. I'll work on putting it in the invokers, since there's no reason that wouldn't work, as far as I can tell.

Negative strides are totally fine: np.arange(10)[::-1].strides -> (-8,) (#504)

Do any of the other targets support them? Haven't really tried that, but agree that in principle there's no reason why they can't work.

Zero strides are also, in principle, well-defined.

When would those come up? Haven't managed to convince numpy to make some, although for 0-sized arrays, just setting ary.strides to something shouldn't break the world, right?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do any of the other targets support them?

🤷 No reason they shouldn't, though. :)

When would those come up?

stride_tricks? As intermediates in broadcasting? Not sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inducer Moved the stride stuff to the invokers in 23f8e7b.

It still feels quite hacky and/or incomplete, but it works™. Let me know what you think.

@alexfikl alexfikl force-pushed the fix-strides-empty-array branch 3 times, most recently from 23f8e7b to 1e02b47 Compare October 23, 2021 16:06
@alexfikl alexfikl force-pushed the fix-strides-empty-array branch from 1e02b47 to 24d254c Compare December 2, 2021 15:17
@alexfikl

alexfikl commented May 19, 2022

Copy link
Copy Markdown
Contributor Author

Superseded by #584.

@alexfikl alexfikl closed this May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants