forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Normalization.cpp
83 lines (70 loc) · 2.84 KB
/
Normalization.cpp
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
#include <ATen/ATen.h>
#include <ATen/Config.h>
#include <ATen/NativeFunctions.h>
#include <tuple>
#if !AT_MKLDNN_ENABLED()
namespace at {
namespace native {
std::tuple<Tensor, Tensor, Tensor> mkldnn_batch_norm(
const Tensor& self,
const Tensor& weight,
const Tensor& bias,
const Tensor& running_mean,
const Tensor& running_var,
bool train,
double momentum,
double eps) {
TORCH_CHECK(false, "mkldnn_batch_norm: ATen not compiled with MKLDNN support");
}
} // namespace native
} // namespace at
#else // AT_MKLDNN_EBABLED
#include <ATen/native/mkldnn/MKLDNNCommon.h>
namespace at {
namespace native {
std::tuple<Tensor, Tensor, Tensor> mkldnn_batch_norm(
const Tensor& input,
const Tensor& weight,
const Tensor& bias,
const Tensor& running_mean,
const Tensor& running_var,
bool train,
double momentum,
double eps) {
ideep::tensor& x = itensor_from_mkldnn(input);
ideep::tensor& w = itensor_from_mkldnn(weight);
ideep::tensor& b = itensor_from_mkldnn(bias);
ideep::tensor& m = itensor_from_mkldnn(running_mean);
ideep::tensor& v = itensor_from_mkldnn(running_var);
ideep::tensor y;
if (train) {
// TODO: support training
TORCH_CHECK(false, "mkldnn_batch_norm: mkldnn training is not supported in yet.");
// ideep::tensor saved_mean;
// ideep::tensor saved_var;
// ideep::batch_normalization_forward_training::compute<AllocForMKLDNN>(
// x, w, b, y, saved_mean, saved_var, m, v, momentum, eps);
// return std::make_tuple(
// new_with_itensor_mkldnn(std::move(y), optTypeMetaToScalarType(input.options().dtype_opt()),
// input.options().device_opt()),
// new_with_itensor_mkldnn(std::move(saved_mean), optTypeMetaToScalarType(input.options().dtype_opt()),
// input.options().device_opt()),
// new_with_itensor_mkldnn(std::move(saved_var), optTypeMetaToScalarType(input.options().dtype_opt()),
// input.options().device_opt()));
} else {
TORCH_CHECK(input.dim() == 4 || input.dim() == 5,
"mkldnn_batch_norm: currently mkldnn only support 2d and 3d batchnorm");
ideep::batch_normalization_forward_inference::compute(
x, m, v, w, b, y, eps);
return std::make_tuple(
new_with_itensor_mkldnn(std::move(y), optTypeMetaToScalarType(input.options().dtype_opt()),
input.options().device_opt()),
new_with_itensor_mkldnn(ideep::tensor{}, optTypeMetaToScalarType(input.options().dtype_opt()),
input.options().device_opt()),
new_with_itensor_mkldnn(ideep::tensor{}, optTypeMetaToScalarType(input.options().dtype_opt()),
input.options().device_opt()));
}
}
} // namespace native
} // namespace at
#endif // AT_MKLDNN_EBABLED