-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathrandomize_constants_test.py
53 lines (37 loc) · 1.41 KB
/
randomize_constants_test.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
# isort:skip_file
# pyre-ignore-all-errors
from __future__ import absolute_import, division, print_function, unicode_literals
import torch
import torch_glow
from glow.glow.torch_glow.tests.tests import utils
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = torch.nn.Linear(5, 10)
def forward(self, x):
return self.linear(x)
def run_model(m, input, randomize):
torch_glow.disableFusionPass()
traced_m = torch.jit.trace(m, input)
if randomize:
torch_glow.enable_randomize_constants()
else:
torch_glow.disable_randomize_constants()
spec = torch_glow.CompilationSpec()
spec.get_settings().set_glow_backend("Interpreter")
compilation_group = torch_glow.CompilationGroup()
spec.compilation_groups_append(compilation_group)
input_spec = torch_glow.InputSpec()
input_spec.set_same_as(input)
compilation_group.input_sets_append([input_spec])
glow_m = torch_glow.to_glow(traced_m, {"forward": spec})
return glow_m(input)
class TestRandomizeWeights(utils.TorchGlowTestCase):
def test_randomize_weights(self):
m = Model()
input = torch.randn(5)
normal1 = run_model(m, input, False)
normal2 = run_model(m, input, False)
rand = run_model(m, input, True)
assert torch.allclose(normal1, normal2)
assert not torch.allclose(normal1, rand)