-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fork_spec.py
50 lines (40 loc) · 1.4 KB
/
test_fork_spec.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
import pytest
import torch
import torcharc
B = 4 # batch size
@pytest.mark.parametrize(
"spec_file, input_shape",
[
("chunk.yaml", (B, 32)),
("split.yaml", (B, 32)),
],
)
def test_fork(spec_file, input_shape):
# Build the model using torcharc
model = torcharc.build(torcharc.SPEC_DIR / "fork" / spec_file)
assert isinstance(model, torch.nn.Module)
# Run the model and check the output shape
x = torch.randn(*input_shape)
y = model(x)
assert len(y) == 2 # tail_0, tail_1
# Test compatibility with compile and trace
compiled_model = torch.compile(model)
assert len(compiled_model(x)) == 2
traced_model = torch.jit.trace(model, (x))
assert len(traced_model(x)) == 2
def test_reduce_mean():
spec_file = torcharc.SPEC_DIR / "fn" / "reduce_mean.yaml"
# Build the model using torcharc
model = torcharc.build(spec_file)
assert isinstance(model, torch.nn.Module)
# Run the model and check the output shape
text = torch.randint(0, 1000, (B, 10))
y = model(text)
assert y.shape == (B, 128)
# Test compatibility with compile, script and trace
compiled_model = torch.compile(model)
assert compiled_model(text).shape == y.shape
scripted_model = torch.jit.script(model)
assert scripted_model(text).shape == y.shape
traced_model = torch.jit.trace(model, (text))
assert traced_model(text).shape == y.shape