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

Rewrite away reshape that drop dims #1123

Open
ricardoV94 opened this issue Dec 13, 2024 · 0 comments
Open

Rewrite away reshape that drop dims #1123

ricardoV94 opened this issue Dec 13, 2024 · 0 comments

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Dec 13, 2024

Description

This shows up in the following graph:

import pytensor
import pytensor.tensor as pt

x = pt.vector("x", shape=(9,))
out = pt.repeat(x[None], 12, axis=0)
pytensor.function([x], out).dprint(print_type=True)   

# Reshape{2} [id A] <Matrix(float64, shape=(12, 9))> 1
#  ├─ Alloc [id B] <Tensor3(float64, shape=(1, 12, 9))> 0
#  │  ├─ x [id C] <Vector(float64, shape=(9,))>
#  │  ├─ 1 [id D] <Scalar(int64, shape=())>
#  │  ├─ 12 [id E] <Scalar(int64, shape=())>
#  │  └─ 9 [id F] <Scalar(int64, shape=())>
#  └─ [12  9] [id G] <Vector(int64, shape=(2,))>

When a reshape is just adding/dropping dims, we could rewrite as reshape(expand_dims/squeeze(x, ...), shape), which would have led pytensor to remove the reshape altogether.

In general we want reshape to only be used when it actually does something that Dimshuffle does not, as for the eyes of PyTensor it involves a copy (it can't know at compile time, because it would need to know about strides)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant