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

Small perf improvements #1128

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pytensor/link/pytorch/dispatch/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@
def pytorch_funcify_Join(op, **kwargs):
def join(axis, *tensors):
# tensors could also be tuples, and in this case they don't have a ndim
tensors = [torch.tensor(tensor) for tensor in tensors]
tensors = [

Check warning on line 126 in pytensor/link/pytorch/dispatch/basic.py

View check run for this annotation

Codecov / codecov/patch

pytensor/link/pytorch/dispatch/basic.py#L126

Added line #L126 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comprehension is needed at all when I tested in my PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.tensor(tensor) if not torch.is_tensor(tensor) else tensor
for tensor in tensors
]

return torch.cat(tensors, dim=axis)

Expand Down
7 changes: 5 additions & 2 deletions pytensor/link/pytorch/linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class wrapper:
"""

def __init__(self, fn, gen_functors):
self.fn = torch.compile(fn)
with torch.no_grad():
self.fn = torch.compile(fn)
self.gen_functors = gen_functors.copy()

def __call__(self, *inputs, **kwargs):
Expand All @@ -62,7 +63,9 @@ def __call__(self, *inputs, **kwargs):
setattr(pytensor.link.utils, n[1:], fn)

# Torch does not accept numpy inputs and may return GPU objects
outs = self.fn(*(pytorch_typify(inp) for inp in inputs), **kwargs)
with torch.no_grad():
ins = (pytorch_typify(inp) for inp in inputs)
outs = self.fn(*ins, **kwargs)

# unset attrs
for n, _ in self.gen_functors:
Expand Down
Loading