-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
test_lite_script_module.py
174 lines (129 loc) · 6.78 KB
/
test_lite_script_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import unittest
import torch
import torch.utils.bundled_inputs
import io
from typing import NamedTuple
from collections import namedtuple
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)
def test_save_mobile_module_with_debug_info(self):
class A(torch.nn.Module):
def __init__(self):
super(A, self).__init__()
def forward(self, x):
return x + 1
class B(torch.nn.Module):
def __init__(self):
super(B, self).__init__()
self.A0 = A()
self.A1 = A()
def forward(self, x):
return self.A0(x) + self.A1(x)
input = torch.tensor([5])
trace_module = torch.jit.trace(B(), input)
bytes = trace_module._save_to_buffer_for_lite_interpreter(_save_mobile_debug_info=True)
assert(b"mobile_debug.pkl" in bytes)
assert(b"module_debug_info" in bytes)
assert(b"top(B).forward" in bytes)
assert(b"top(B).A0(A).forward" in bytes)
assert(b"top(B).A1(A).forward" in bytes)
def test_load_mobile_module_with_debug_info(self):
class MyTestModule(torch.nn.Module):
def __init__(self):
super(MyTestModule, self).__init__()
def forward(self, x):
return x + 5
input = torch.tensor([3])
script_module = torch.jit.script(MyTestModule())
script_module_result = script_module(input)
buffer = io.BytesIO(script_module._save_to_buffer_for_lite_interpreter(_save_mobile_debug_info=True))
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)
def test_find_and_run_method(self):
class MyTestModule(torch.nn.Module):
def forward(self, arg):
return arg
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)
has_bundled_inputs = mobile_module.find_method("get_all_bundled_inputs")
self.assertFalse(has_bundled_inputs)
torch.utils.bundled_inputs.augment_model_with_bundled_inputs(
script_module, [input], [])
buffer = io.BytesIO(script_module._save_to_buffer_for_lite_interpreter())
buffer.seek(0)
mobile_module = _load_for_lite_interpreter(buffer)
has_bundled_inputs = mobile_module.find_method("get_all_bundled_inputs")
self.assertTrue(has_bundled_inputs)
bundled_inputs = mobile_module.run_method("get_all_bundled_inputs")
mobile_module_result = mobile_module.forward(*bundled_inputs[0])
torch.testing.assert_allclose(script_module_result, mobile_module_result)
def test_unsupported_createobject(self):
class Foo():
def __init__(self):
return
def func(self, x: int, y: int):
return x + y
class MyTestModule(torch.nn.Module):
def forward(self, arg):
f = Foo()
return f.func(1, 2)
script_module = torch.jit.script(MyTestModule())
with self.assertRaisesRegex(RuntimeError,
r"^CREATE_OBJECT is not supported in mobile module\. "
r"Workaround: instead of using arbitrary class type \(class Foo\(\)\), "
r"define a pytorch class \(class Foo\(torch\.nn\.Module\)\)\.$"):
script_module._save_to_buffer_for_lite_interpreter()
def test_unsupported_return_typing_namedtuple(self):
myNamedTuple = NamedTuple('myNamedTuple', [('a', torch.Tensor)])
class MyTestModule(torch.nn.Module):
def forward(self):
return myNamedTuple(torch.randn(1))
script_module = torch.jit.script(MyTestModule())
with self.assertRaisesRegex(RuntimeError,
r"A named tuple type is not supported in mobile module. "
r"Workaround: instead of using a named tuple type\'s fields, "
r"use a dictionary type\'s key-value pair itmes or "
r"a pytorch class \(class Foo\(torch\.nn\.Module\)\)\'s attributes."):
script_module._save_to_buffer_for_lite_interpreter()
def test_unsupported_return_collections_namedtuple(self):
myNamedTuple = namedtuple('myNamedTuple', [('a')])
class MyTestModule(torch.nn.Module):
def forward(self):
return myNamedTuple(torch.randn(1))
script_module = torch.jit.script(MyTestModule())
with self.assertRaisesRegex(RuntimeError,
r"A named tuple type is not supported in mobile module. "
r"Workaround: instead of using a named tuple type\'s fields, "
r"use a dictionary type\'s key-value pair itmes or "
r"a pytorch class \(class Foo\(torch\.nn\.Module\)\)\'s attributes."):
script_module._save_to_buffer_for_lite_interpreter()
if __name__ == '__main__':
unittest.main()