-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmp.py
152 lines (124 loc) · 5.51 KB
/
tmp.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import time
from tqdm import tqdm
def normalize(in_channels):
return torch.nn.GroupNorm(num_groups=32 if 32 < in_channels else 1, num_channels=in_channels, eps=1e-6, affine=True)
@torch.jit.script
def swish(x):
return x*torch.sigmoid(x)
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels=None):
super(ResBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels = in_channels if out_channels is None else out_channels
self.norm1 = normalize(in_channels)
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.norm2 = normalize(out_channels)
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
self.conv_out = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x_in):
x = x_in
x = self.norm1(x)
x = swish(x)
x = self.conv1(x)
x = self.norm2(x)
x = swish(x)
x = self.conv2(x)
if self.in_channels != self.out_channels:
x_in = self.conv_out(x_in)
return x + x_in
class LayerNorm(nn.Module):
def __init__(self, dim, eps = 1e-5):
super().__init__()
self.eps = eps
self.gamma = nn.Parameter(torch.ones(1, dim, 1, 1, 1))
def forward(self, x):
var = torch.var(x, dim = 1, unbiased = False, keepdim = True)
mean = torch.mean(x, dim = 1, keepdim = True)
return (x - mean) / (var + self.eps).sqrt() * self.gamma
class DepthwiseBlock(nn.Module):
def __init__(self, in_channels, out_channels=None):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels = in_channels if out_channels is None else out_channels
self.norm1 = LayerNorm(in_channels)
self.conv1 = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
self.norm2 = LayerNorm(out_channels)
self.conv2 = nn.Conv3d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=out_channels)
self.conv_out = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x_in):
x = x_in
x = self.norm1(x)
x = F.gelu(x)
x = self.conv1(x)
x = self.norm2(x)
x = F.gelu(x)
x = self.conv2(x)
if self.in_channels != self.out_channels:
x_in = self.conv_out(x_in)
return x + x_in
class SeparableConvBlock(nn.Module):
def __init__(self, in_channels, out_channels=None):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels = in_channels if out_channels is None else out_channels
self.conv_in = nn.Conv3d(in_channels, out_channels, kernel_size=1)
self.norm1 = LayerNorm(in_channels)
self.norm2 = LayerNorm(out_channels)
self.branch1 = nn.Sequential(
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(1,3,3), stride=1, padding=(0,1,1)),
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(3, 1, 1), stride=1, padding=(1,0,0))
)
self.branch2 = nn.Sequential(
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(3, 3, 1), stride=1, padding=(1,1,0)),
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(1, 1, 3), stride=1, padding=(0,0,1))
)
self.branch3 = nn.Sequential(
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(3, 1, 3), stride=1, padding=(1,0,1)),
nn.Conv3d(out_channels // 3, out_channels // 3, kernel_size=(1, 3, 1), stride=1, padding=(0,1,0))
)
self.conv_out = nn.Conv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
def forward(self, x_in):
x = x_in
x = self.norm1(x)
x = F.gelu(x)
x = self.conv_in(x)
x1, x2, x3 = torch.chunk(x, 3, dim=1)
x1 = self.branch1(x1)
x2 = self.branch2(x2)
x3 = self.branch3(x3)
x = torch.cat((x1, x2, x3), dim=1)
x = self.norm2(x)
x = F.gelu(x)
if self.in_channels != self.out_channels:
x_in = self.conv_out(x_in)
return x + x_in
def test_block(x, block):
torch.cuda.synchronize()
start_time = time.time()
with torch.no_grad():
out = block(x)
torch.cuda.synchronize()
return time.time() - start_time
def repeat_test(num_iters, test_fn, *args, **kwargs):
total_time = 0
for _ in tqdm(range(num_iters)):
total_time += test_fn(*args, **kwargs)
return total_time / num_iters
device = 'cuda'
num_iters = 100
x = torch.randn(4, 32, 128, 128, 128, device=device)
resblock = ResBlock(32).to(device)
depthwise_block = DepthwiseBlock(32).to(device)
separable_block = SeparableConvBlock(30).to(device)
print(f"Res Block: {repeat_test(num_iters, test_block, x, resblock)}")
print(f"Depthwise Block: {repeat_test(num_iters, test_block, x, depthwise_block)}")
x = torch.randn(4, 30, 128, 128, 128, device=device)
print(f"Separable Block: {repeat_test(num_iters, test_block, x, separable_block)}")
x = torch.randn(4, 16, 128, 128, 128, device=device)
resblock = ResBlock(16).to(device)
depthwise_block = DepthwiseBlock(16).to(device)
print(f"Res Block - 16: {repeat_test(num_iters, test_block, x, resblock)}")
print(f"Depthwise Block - 16: {repeat_test(num_iters, test_block, x, depthwise_block)}")