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

Uneccessary deep copy operations on donated inputs #1082

Open
ricardoV94 opened this issue Nov 11, 2024 · 2 comments
Open

Uneccessary deep copy operations on donated inputs #1082

ricardoV94 opened this issue Nov 11, 2024 · 2 comments

Comments

@ricardoV94
Copy link
Member

ricardoV94 commented Nov 11, 2024

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.

@ricardoV94
Copy link
Member Author

Apparently pytensor.Out also has a borrow kwarg. When that's set the deepcopy is avoided, although now there's a funky View Op, probably for rewrite sake?

import pytensor
import pytensor.tensor as pt

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

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

@ricardoV94
Copy link
Member Author

So I guess In(borrow=True) is useless without a corresponding Out(borrow=True)

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