8000 add support for numpy by tugsbayasgalan · Pull Request #149288 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

add support for numpy #149288

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

Open
wants to merge 3 commits into
base: gh/tugsbayasgalan/300/base
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions test/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import torch._dynamo as torchdynamo
import torch.nn.functional as F
from functorch.experimental.control_flow import cond, map
import numpy as np
from torch import Tensor
from torch._decomp import decomposition_table
from torch._dynamo.test_case import TestCase
Expand Down Expand Up @@ -1267,6 +1268,29 @@ def false_fn(x):
)
torch.export.export(M(), args)

def test_numpy(self):
class Foo(torch.nn.Module):
def forward(self, x):
a = x.numpy()
return x + np.sin(x.numpy().sum())

foo = Foo()
foo(torch.randn(10, 10))

non_strict_graph = (
8000 export(foo, (torch.randn(10, 10),), strict=False)
.run_decompositions({})
.graph
)
# strict_graph = (
# export(foo, (torch.randn(10, 10),), strict=True)
# .run_decompositions({})
# .graph
# )
# g = torch.export._trace._export_to_torch_ir(foo, (torch.randn(10, 10),))
# print("GRAPH", g.graph)
# self.assertEqual(str(non_strict_graph), str(strict_graph))

def test_cond_int_closure(self):
class M(torch.nn.Module):
def __init__(self):
Expand Down
9 changes: 9 additions & 0 deletions torch/_export/non_strict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ class _NonStrictTorchFunctionHandler(torch.overrides.TorchFunctionMode):
"""

def _override(self, func, args, kwargs):
if "__array__" in str(func):
breakpoint()
if torch.distributed.is_available():
from torch.distributed._functional_collectives import (
REDUCE_OP_TO_STR,
Expand Down Expand Up @@ -660,6 +662,13 @@ def _override(self, func, args, kwargs):
# Redirect to torch.select for indexing with symint.
if isinstance(args[1], torch.SymInt):
return torch.select, [args[0], 0, args[1]], {}

if func.__name__ == "numpy" and isinstance(args[0], torch.Tensor):
return torch._refs.view_as, [args[0], args[0]], {}

if func.__name__ == "__array__" and isinstance(args[0], torch.Tensor):
return torch._numpy.ndarray, [args[0]], {}

return func, args, kwargs

def __torch_function__(self, func, types, args=(), kwargs=None):
Expand Down
Loading
0