Skip to content

Optimize reshape idx lmbda lowering#656

Open
kaushikcfd wants to merge 2 commits intomainfrom
optimize_reshape_idx_lmbda_lowering
Open

Optimize reshape idx lmbda lowering#656
kaushikcfd wants to merge 2 commits intomainfrom
optimize_reshape_idx_lmbda_lowering

Conversation

@kaushikcfd
Copy link
Copy Markdown
Collaborator

On main, reshape lowering generates sub-optimal indexing:

(py314_env) kaushikggg@line:~/temp$ cat hello.py 
import pytato as pt


x = pt.make_placeholder("x", (10, 4))
print(pt.generate_loopy(x.reshape(-1)).program)
(py314_env) kaushikggg@line:~/temp$ python hello.py 
---------------------------------------------------------------------------
KERNEL: _pt_kernel
---------------------------------------------------------------------------
ARGUMENTS:
x: type: np:dtype('float64'), shape: (10, 4), dim_tags: (N1:stride:4, N0:stride:1), offset: <class 'loopy.typing.auto'> in aspace: global
_pt_out: type: np:dtype('float64'), shape: (40), dim_tags: (N0:stride:1) out aspace: global
---------------------------------------------------------------------------
DOMAINS:
{  :  }
{ [_pt_out_dim0] : 0 <= _pt_out_dim0 <= 39 }
---------------------------------------------------------------------------
INAME TAGS:
_pt_out_dim0: None
---------------------------------------------------------------------------
INSTRUCTIONS:
for _pt_out_dim0
  _pt_out[_pt_out_dim0] = x[((0 + _pt_out_dim0*1) % 40) // 4, ((0 + _pt_out_dim0*1) % 4) // 1]  {id=_pt_out_store}
end _pt_out_dim0
---------------------------------------------------------------------------

After this patch, the assignment becomes:

for _pt_out_dim0
  _pt_out[_pt_out_dim0] = x[_pt_out_dim0 // 4, _pt_out_dim0 % 4]  {id=_pt_out_store}
end _pt_out_dim0

Notice the reduction in the redundant product with one and modulo with the size operations.

@kaushikcfd kaushikcfd force-pushed the optimize_reshape_idx_lmbda_lowering branch from d318c3e to c8e06ab Compare March 28, 2026 20:32
@kaushikcfd kaushikcfd requested a review from inducer March 28, 2026 20:33
@kaushikcfd kaushikcfd force-pushed the optimize_reshape_idx_lmbda_lowering branch from c8e06ab to 9ab995b Compare March 28, 2026 23:02
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.

1 participant