Skip to content

Rewrite away reshape that drop dims #1123

@ricardoV94

Description

@ricardoV94

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, so that rewrites can focus on only one source of behavior

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions