-
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 TorchScript support for
RECT_L
(#6727)
This PR add the TorchScript support for `RECT_L` model. The fail reason and our solution for original code is very similar with PR [#6721](#6712), except that this model using the `torch.jit.export` on `embed` and `get_semantic_labels` methods. And another fail reason is `@torch.no_grad`, which bring some error msg I cann't understand. Adding TorchScript support will bring a lot of extra code and reduce the code readability. I will consider how to do better in another PR. --------- Co-authored-by: rusty1s <matthias.fey@tu-dortmund.de>
- Loading branch information
Showing
3 changed files
with
110 additions
and
12 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,42 @@ | ||
import torch | ||
from torch_sparse import SparseTensor | ||
|
||
from torch_geometric.nn import RECT_L | ||
from torch_geometric.testing import is_full_test | ||
|
||
|
||
def test_rect(): | ||
x = torch.randn(6, 8) | ||
y = torch.tensor([1, 0, 0, 2, 1, 1]) | ||
edge_index = torch.tensor([[0, 1, 1, 2, 4, 5], [1, 0, 2, 1, 5, 4]]) | ||
adj = SparseTensor.from_edge_index(edge_index, sparse_sizes=(6, 6)) | ||
mask = torch.randint(0, 2, (6, ), dtype=torch.bool) | ||
|
||
model = RECT_L(8, 16) | ||
assert str(model) == 'RECT_L(8, 16)' | ||
|
||
out = model(x, edge_index) | ||
assert out.size() == (6, 8) | ||
assert torch.allclose(out, model(x, adj.t())) | ||
|
||
# Test `embed`: | ||
out = model.embed(x, edge_index) | ||
assert out.size() == (6, 16) | ||
embed_out = model.embed(x, edge_index) | ||
assert embed_out.size() == (6, 16) | ||
assert torch.allclose(embed_out, model.embed(x, adj.t())) | ||
|
||
# Test `get_semantic_labels`: | ||
out = model.get_semantic_labels(x, y, mask) | ||
assert out.size() == (int(mask.sum()), 8) | ||
labeds_out = model.get_semantic_labels(x, y, mask) | ||
assert labeds_out.size() == (int(mask.sum()), 8) | ||
|
||
if is_full_test(): | ||
t = '(Tensor, Tensor, OptTensor) -> Tensor' | ||
jit = torch.jit.script(model.jittable(t)) | ||
assert torch.allclose(jit(x, edge_index), out) | ||
assert torch.allclose(embed_out, jit.embed(x, edge_index)) | ||
assert torch.allclose(labeds_out, jit.get_semantic_labels(x, y, mask)) | ||
|
||
t = '(Tensor, SparseTensor, OptTensor) -> Tensor' | ||
jit = torch.jit.script(model.jittable(t)) | ||
assert torch.allclose(jit(x, adj.t()), out) | ||
assert torch.allclose(embed_out, jit.embed(x, adj.t())) | ||
assert torch.allclose(labeds_out, jit.get_semantic_labels(x, y, mask)) |
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