-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
EdgeIndex
tests for torch.compile
and torch.jit.script
; Add…
… `is_torch_instance` (#8461)
- Loading branch information
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import torch | ||
|
||
from torch_geometric import is_torch_instance | ||
from torch_geometric.testing import onlyLinux, withPackage | ||
|
||
|
||
def test_basic(): | ||
assert is_torch_instance(torch.nn.Linear(1, 1), torch.nn.Linear) | ||
|
||
|
||
@onlyLinux | ||
@withPackage('torch>=2.0.0') | ||
def test_compile(): | ||
model = torch.compile(torch.nn.Linear(1, 1)) | ||
assert not isinstance(model, torch.nn.Linear) | ||
assert is_torch_instance(model, torch.nn.Linear) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import Any, Tuple, Type, Union | ||
|
||
import torch | ||
|
||
import torch_geometric.typing | ||
|
||
if torch_geometric.typing.WITH_PT20: | ||
import torch._dynamo | ||
|
||
|
||
def is_torch_instance(obj: Any, cls: Union[Type, Tuple[Type]]) -> bool: | ||
r"""Checks if the :obj:`obj` is an instance of a :obj:`cls`. | ||
This function extends :meth:`isinstance` to be applicable during | ||
:meth:`torch.compile` usage by checking against the original class of | ||
compiled models. | ||
""" | ||
# `torch.compile` removes the model inheritance and converts the model to | ||
# a `torch._dynamo.OptimizedModule` instance, leading to `isinstance` being | ||
# unable to check the model's inheritance. This function unwraps the | ||
# compiled model before evaluating via `isinstance`. | ||
if (torch_geometric.typing.WITH_PT20 | ||
and isinstance(obj, torch._dynamo.OptimizedModule)): | ||
return isinstance(obj._orig_mod, cls) | ||
return isinstance(obj, cls) |