mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Generator comprehensions with any/all are less verbose and potentially help to save memory/CPU : https://eklitzke.org/generator-comprehensions-and-using-any-and-all-in-python To make JIT work with this change, I added code to convert GeneratorExp to ListComp. So the whole PR is basically NoOp for JIT, but potentially memory and speed improvement for eager mode. Also I removed a test from test/jit/test_parametrization.py. The test was bad and had a TODO to actually implement and just tested that UnsupportedNodeError is thrown, and with GeneratorExp support a different error would be thrown. Pull Request resolved: https://github.com/pytorch/pytorch/pull/78142 Approved by: https://github.com/malfet, https://github.com/albanD
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
# Owner(s): ["oncall: jit"]
|
|
|
|
|
|
import torch
|
|
from torch import nn
|
|
import torch.nn.utils.parametrize as parametrize
|
|
|
|
from torch.testing._internal.jit_utils import JitTestCase
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise RuntimeError("This test file is not meant to be run directly, use:\n\n"
|
|
"\tpython test/test_jit.py TESTNAME\n\n"
|
|
"instead.")
|
|
|
|
class TestParametrization(JitTestCase):
|
|
# Define some parametrization
|
|
class Symmetric(nn.Module):
|
|
def forward(self, X):
|
|
return X.triu() + X.triu(1).mT
|
|
|
|
def test_traceable(self):
|
|
r"""Test the jit scripting and tracing of a parametrized model."""
|
|
model = nn.Linear(5, 5)
|
|
parametrize.register_parametrization(model, "weight", self.Symmetric())
|
|
|
|
x = torch.randn(3, 5)
|
|
y = model(x)
|
|
|
|
# Check the tracing works. Because traced functions cannot be called
|
|
# directly, we run the comparison on the activations.
|
|
traced_model = torch.jit.trace_module(model, {'forward': x})
|
|
y_hat = traced_model(x)
|
|
self.assertEqual(y, y_hat)
|
|
|
|
# Check traced model works with caching
|
|
with parametrize.cached():
|
|
y_hat = traced_model(x)
|
|
self.assertEqual(y, y_hat)
|
|
|
|
# Check the tracing throws an error when caching
|
|
with self.assertRaisesRegex(RuntimeError,
|
|
'Cannot trace a model while caching'):
|
|
with parametrize.cached():
|
|
traced_model = torch.jit.trace_module(model, {'forward': x})
|
|
|
|
def test_scriptable(self):
|
|
# TODO: Need to fix the scripting in parametrizations
|
|
# Currently, all the tests below will throw torch.jit.Error
|
|
model = nn.Linear(5, 5)
|
|
parametrize.register_parametrization(model, "weight", self.Symmetric())
|
|
|
|
x = torch.randn(3, 5)
|
|
y = model(x)
|
|
|
|
with self.assertRaises(torch.jit.Error):
|
|
# Check scripting works
|
|
scripted_model = torch.jit.script(model)
|
|
y_hat = scripted_model(x)
|
|
self.assertEqual(y, y_hat)
|
|
|
|
with parametrize.cached():
|
|
# Check scripted model works when caching
|
|
y_hat = scripted_model(x)
|
|
self.assertEqual(y, y_hat)
|
|
|
|
# Check the scripting process throws an error when caching
|
|
with self.assertRaisesRegex(RuntimeError, 'Caching is not implemented'):
|
|
scripted_model = torch.jit.trace_module(model)
|