forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkldnn.py
202 lines (165 loc) · 6.48 KB
/
mkldnn.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import torch
class MkldnnLinear(torch.jit.ScriptModule):
def __init__(self, dense_module):
super(MkldnnLinear, self).__init__()
self.register_buffer('weight', dense_module.weight.to_mkldnn())
if dense_module.bias is not None:
self.register_buffer('bias', dense_module.bias.to_mkldnn())
else:
# TODO: Remove this once ScriptModule supports registering None buffer
self.register_buffer(
'bias',
torch.zeros([dense_module.weight.size(0)], dtype=torch.float).to_mkldnn())
@torch.jit.script_method
def __getstate__(self):
return (self.weight.to_dense(), self.bias.to_dense(), self.training)
@torch.jit.script_method
def __setstate__(self, state):
self.weight = state[0].to_mkldnn()
self.bias = state[1].to_mkldnn()
self.training = state[2]
@torch.jit.script_method
def forward(self, x):
x_mkldnn = x if x.is_mkldnn else x.to_mkldnn()
y_mkldnn = torch._C._nn.mkldnn_linear(x_mkldnn, self.weight, self.bias)
y = y_mkldnn if x.is_mkldnn else y_mkldnn.to_dense()
return y
class _MkldnnConvNd(torch.jit.ScriptModule):
"""Common base of MkldnnConv1d and MkldnnConv2d"""
__constants__ = ['stride', 'padding', 'dilation', 'groups']
def __init__(self, dense_module):
super(_MkldnnConvNd, self).__init__()
self.stride = dense_module.stride
self.padding = dense_module.padding
self.dilation = dense_module.dilation
self.groups = dense_module.groups
if dense_module.bias is not None:
self.register_buffer('bias', dense_module.bias.to_mkldnn())
else:
# TODO: Remove this once ScriptModule supports registering None buffer
self.register_buffer(
'bias',
torch.zeros([dense_module.weight.size(0)], dtype=torch.float).to_mkldnn())
@torch.jit.script_method
def __getstate__(self):
return (self.weight.to_dense(), self.bias.to_dense(), self.training)
@torch.jit.script_method
def forward(self, x):
return torch.mkldnn_convolution(
x,
self.weight,
self.bias,
self.padding,
self.stride,
self.dilation,
self.groups)
class MkldnnConv1d(_MkldnnConvNd):
def __init__(self, dense_module):
super(MkldnnConv1d, self).__init__(dense_module)
self.register_buffer('weight', dense_module.weight.to_mkldnn())
@torch.jit.script_method
def __setstate__(self, state):
self.weight = state[0].to_mkldnn()
self.bias = state[1].to_mkldnn()
self.training = state[2]
class MkldnnConv2d(_MkldnnConvNd):
def __init__(self, dense_module):
super(MkldnnConv2d, self).__init__(dense_module)
self.register_buffer('weight', torch._C._nn.mkldnn_reorder_conv2d_weight(
dense_module.weight.to_mkldnn(),
self.padding,
self.stride,
self.dilation,
self.groups))
@torch.jit.script_method
def __setstate__(self, state):
self.weight = torch._C._nn.mkldnn_reorder_conv2d_weight(
state[0].to_mkldnn(),
self.padding,
self.stride,
self.dilation,
self.groups)
self.bias = state[1].to_mkldnn()
self.training = state[2]
class MkldnnConv3d(_MkldnnConvNd):
def __init__(self, dense_module):
super(MkldnnConv3d, self).__init__(dense_module)
self.register_buffer('weight', torch._C._nn.mkldnn_reorder_conv3d_weight(
dense_module.weight.to_mkldnn(),
self.padding,
self.stride,
self.dilation,
self.groups))
@torch.jit.script_method
def __setstate__(self, state):
self.weight = torch._C._nn.mkldnn_reorder_conv3d_weight(
state[0].to_mkldnn(),
self.padding,
self.stride,
self.dilation,
self.groups)
self.bias = state[1].to_mkldnn()
self.training = state[2]
class MkldnnBatchNorm(torch.jit.ScriptModule):
__constants__ = ['exponential_average_factor', 'eps']
def __init__(self, dense_module):
super(MkldnnBatchNorm, self).__init__()
assert(not dense_module.training)
assert(dense_module.track_running_stats)
assert(dense_module.affine)
if dense_module.momentum is None:
self.exponential_average_factor = 0.0
else:
self.exponential_average_factor = dense_module.momentum
self.eps = dense_module.eps
self.register_buffer('weight', dense_module.weight.to_mkldnn())
self.register_buffer('bias', dense_module.bias.to_mkldnn())
self.register_buffer('running_mean', dense_module.running_mean.to_mkldnn())
self.register_buffer('running_var', dense_module.running_var.to_mkldnn())
@torch.jit.script_method
def __getstate__(self):
weight = self.weight.to_dense()
bias = self.bias.to_dense()
running_mean = self.running_mean.to_dense()
running_var = self.running_var.to_dense()
return (weight, bias, running_mean, running_var, self.training)
@torch.jit.script_method
def __setstate__(self, state):
self.weight = state[0].to_mkldnn()
self.bias = state[1].to_mkldnn()
self.running_mean = state[2].to_mkldnn()
self.running_var = state[3].to_mkldnn()
self.training = state[4]
@torch.jit.script_method
def forward(self, x):
return torch.batch_norm(
x,
self.weight,
self.bias,
self.running_mean,
self.running_var,
False, # training
self.exponential_average_factor,
self.eps,
False, # cuda_enabled
)
def to_mkldnn(module):
def m_fn(m):
if isinstance(m, torch.nn.Linear):
return MkldnnLinear(m)
elif isinstance(m, torch.nn.Conv1d):
return MkldnnConv1d(m)
elif isinstance(m, torch.nn.Conv2d):
return MkldnnConv2d(m)
elif isinstance(m, torch.nn.Conv3d):
return MkldnnConv3d(m)
elif isinstance(m, torch.nn.BatchNorm2d) or isinstance(m, torch.nn.BatchNorm3d):
return MkldnnBatchNorm(m)
else:
return m
def m_fn_rec(m):
new_m = m_fn(m)
for name, sub_m in m.named_children():
setattr(new_m, name, m_fn_rec(sub_m))
return new_m
return m_fn_rec(module)