-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_blocks.py
62 lines (43 loc) · 1.63 KB
/
test_blocks.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
from functools import partial
import pytest
import torch
import torch.nn as nn
from cvm.models.ops import blocks
def test_se_block_forward():
inputs = torch.randn(16, 3, 56, 56)
se = blocks.SEBlock(3, 0.25)
outputs = se(inputs)
assert outputs.shape == inputs.shape
assert isinstance(se.act, nn.ReLU)
assert isinstance(se.gate, nn.Sigmoid)
def test_se_block_decorator():
with blocks.se(inner_nonlinear=nn.SiLU, gating_fn=nn.Hardsigmoid):
se = blocks.SEBlock(3, 0.25)
assert isinstance(se.act, nn.SiLU)
assert isinstance(se.gate, nn.Hardsigmoid)
def test_normalizer_decorator():
with blocks.normalizer(None):
layers = blocks.norm_activation(3)
assert len(layers) == 1
assert isinstance(layers[0], nn.ReLU)
with blocks.normalizer(nn.LayerNorm, position='before'):
layers = blocks.norm_activation(3)
assert len(layers) == 2
assert isinstance(layers[0], nn.LayerNorm)
assert isinstance(layers[1], nn.ReLU)
with blocks.normalizer(partial(nn.BatchNorm2d, eps=0.1), position='after'):
layers = blocks.norm_activation(3)
assert len(layers) == 2
assert isinstance(layers[0], nn.ReLU)
assert isinstance(layers[1], nn.BatchNorm2d)
assert layers[1].eps == 0.1
def test_nonlinear_decorator():
with blocks.nonlinear(None):
layers = blocks.norm_activation(3)
assert len(layers) == 1
assert isinstance(layers[0], nn.BatchNorm2d)
with blocks.nonlinear(nn.SiLU):
layers = blocks.norm_activation(3)
assert len(layers) == 2
assert isinstance(layers[0], nn.BatchNorm2d)
assert isinstance(layers[1], nn.SiLU)