| import unittest | |
| import torch | |
| import io | |
| from torch.jit.mobile import _load_for_lite_interpreter | |
| class TestLiteScriptModule(unittest.TestCase): | |
| def test_load_mobile_module(self): | |
| class MyTestModule(torch.nn.Module): | |
| def __init__(self): | |
| super(MyTestModule, self).__init__() | |
| def forward(self, x): | |
| return x + 10 | |
| input = torch.tensor([1]) | |
| script_module = torch.jit.script(MyTestModule()) | |
| script_module_result = script_module(input) | |
| buffer = io.BytesIO(script_module._save_to_buffer_for_lite_interpreter()) | |
| buffer.seek(0) | |
| mobile_module = _load_for_lite_interpreter(buffer) | |
| mobile_module_result = mobile_module(input) | |
| torch.testing.assert_allclose(script_module_result, mobile_module_result) | |
| mobile_module_forward_result = mobile_module.forward(input) | |
| torch.testing.assert_allclose(script_module_result, mobile_module_forward_result) | |
| mobile_module_run_method_result = mobile_module.run_method("forward", input) | |
| torch.testing.assert_allclose(script_module_result, mobile_module_run_method_result) | |
| if __name__ == '__main__': | |
| unittest.main() |