Skip to content

Uneccessary deep copy operations on donated inputs #1082

Open
@ricardoV94

Description

@ricardoV94

Description

import pytensor
import pytensor.tensor as pt

x = pt.vector("x")
out = x[0]

fn = pytensor.function([pytensor.In(x, borrow=True)], out)
fn.dprint()
# DeepCopyOp [id A] 1
#  └─ Subtensor{i} [id B] 0
#     ├─ x [id C]
#     └─ 0 [id D]

It's the same with mutable=True which also means borrow=True. The DeepCopyOp shouldn't be introduced.

In a more complex example, things work as expected:

import pytensor
import pytensor.tensor as pt

x = pt.vector("x")
out = x[0] * 2

fn = pytensor.function([pytensor.In(x, mutable=True)], out)
fn.dprint(print_view_map=True, print_destroy_map=True)
# Mul [id A] d={0: [1]} 1
#  ├─ 2.0 [id B]
#  └─ Subtensor{i} [id C] v={0: [0]} 0
#     ├─ x [id D]
#     └─ 0 [id E]

We are mutating the first input because we were allowed.

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