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
|
#include "caffe2/quantization/server/spatial_batch_norm_dnnlowp_op.h"
#include "caffe2/quantization/server/caffe2_dnnlowp_utils.h"
namespace caffe2 {
template <typename T, bool ReluFused>
SpatialBNDNNLowPOp<T, ReluFused>::SpatialBNDNNLowPOp(
const OperatorDef& operator_def,
Workspace* ws)
: DNNLowPOp<T, SpatialBNOp<CPUContext>>(operator_def, ws),
OP_SINGLE_ARG(double, "epsilon", epsilon_, 1e-5),
order_(StringToStorageOrder(
this->template GetSingleArgument<std::string>("order", "NCHW"))) {
bool is_test = this->template GetSingleArgument<bool>("is_test", false);
OPERATOR_NEEDS_FEATURE(
is_test, "SpatialBN DNNLOWP op only works for inference.");
CAFFE_ENFORCE_NE(
order_,
StorageOrder::UNKNOWN,
"order should be either \"NCHW\" or \"NHWC\".");
CAFFE_ENFORCE(OutputSize() == 1);
CAFFE_ENFORCE_GT(epsilon_, 0);
}
template <typename T, bool ReluFused>
void SpatialBNDNNLowPOp<T, ReluFused>::ComputeFusedParam_(
const int C,
const float* scale,
const float* bias,
const float* mean,
const float* var,
float* alpha,
float* beta) {
EigenVectorArrayMap<float> alpha_arr(alpha, C);
EigenVectorArrayMap<float> beta_arr(beta, C);
alpha_arr = ConstEigenVectorArrayMap<float>(scale, C) *
(ConstEigenVectorArrayMap<float>(var, C) + epsilon_).rsqrt();
beta_arr = ConstEigenVectorArrayMap<float>(bias, C) -
alpha_arr * ConstEigenVectorArrayMap<float>(mean, C);
// Adjust alpha and beta considering quantization scales
alpha_arr = alpha_arr * (in_qparams_[0].scale / out_qparams_.scale);
beta_arr = beta_arr / out_qparams_.scale;
}
template <typename T, bool ReluFused>
bool SpatialBNDNNLowPOp<T, ReluFused>::RunOnDevice() {
if (!this->arguments_parsed_) {
dnnlowp::ParseDNNLowPOperatorArguments(
this, &dequantize_output_, &measure_quantization_error_, &followed_by_);
if (ReluFused) {
// It's actually fused with Relu not followed by but setting this to make
// sure quantization error is correctly measured in
// this->MeasureQuantizationError_
followed_by_ = "Relu";
dnnlowp::AdjustOutputTensorQuantizationParamsWithFollowedBy(
this, followed_by_);
}
this->arguments_parsed_ = true;
}
const auto& X = InputTensorCPU_(INPUT);
const auto& scale = Input(SCALE);
const auto& bias = Input(BIAS);
const int ndim = X.dim();
CAFFE_ENFORCE_GE(ndim, 3);
const int N = X.dim32(0);
const int C = (order_ == StorageOrder::NCHW ? X.dim32(1) : X.dim32(ndim - 1));
const std::vector<int> X_dims(X.sizes().cbegin(), X.sizes().cend());
const int HxW = X.size_from_dim(1) / C;
CAFFE_ENFORCE_EQ(scale.numel(), C);
CAFFE_ENFORCE_EQ(bias.numel(), C);
GetOutputQuantizationParams_();
in_qparams_[0] = GetInputTensorQuantizationParamsOf(this, 0, qfactory_.get());
const float* scale_data = scale.template data<float>();
const float* bias_data = bias.template data<float>();
ReinitializeTensor(
&alpha_, {C}, at::dtype<float>().device(CPUContext::GetDeviceType()));
ReinitializeTensor(
&beta_, {C}, at::dtype<float>().device(CPUContext::GetDeviceType()));
float* alpha_data = alpha_.template mutable_data<float>();
float* beta_data = beta_.template mutable_data<float>();
const auto& mean = Input(EST_MEAN);
const auto& var = Input(EST_VAR);
CAFFE_ENFORCE_EQ(mean.numel(), C);
CAFFE_ENFORCE_EQ(var.numel(), C);
auto* Y = OutputTensorCPU_(OUTPUT);
Y->Resize(X.sizes());
T* Y_data = GetQuantizedOutputData_();
if (N == 0) {
return true;
}
ComputeFusedParam_(
C,
scale_data,
bias_data,
mean.template data<float>(),
var.template data<float>(),
alpha_data,
beta_data);
vector<T> X_temp;
const T* X_data =
dnnlowp::QuantizeInputIfNeeded(this, 0, in_qparams_[0], X_temp);
if (order_ == StorageOrder::NCHW) {
for (int c = 0; c < C; ++c) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < HxW; ++j) {
long quantized_down = out_qparams_.zero_point +
std::lrintf(alpha_data[c] *
(X_data[(i * C + c) * HxW + j] -
in_qparams_[0].zero_point) +
beta_data[c]);
if (ReluFused) {
quantized_down =
std::max<long>(quantized_down, out_qparams_.zero_point);
}
Y_data[(i * C + c) * HxW + j] =
fbgemm::clamp<long, T>(quantized_down, 8);
}
}
}
} else {
if (GetCpuId().avx2()) {
internal::SpatialBNNHWCAVX2<T>(
N,
C,
HxW,
in_qparams_[0].zero_point,
out_qparams_.zero_point,
X_data,
alpha_data,
beta_data,
Y_data,
ReluFused);
} else {
for (int i = 0; i < N * HxW; ++i) {
for (int c = 0; c < C; ++c) {
long quantized_down = out_qparams_.zero_point +
std::lrintf(alpha_data[c] *
(X_data[i * C + c] - in_qparams_[0].zero_point) +
beta_data[c]);
if (ReluFused) {
quantized_down =
std::max<long>(quantized_down, out_qparams_.zero_point);
}
Y_data[i * C + c] = fbgemm::clamp<long, T>(quantized_down, 8);
}
}
}
}
RunOnDeviceEpilogue_();
return true;
}
REGISTER_CPU_OPERATOR_WITH_ENGINE(
SpatialBN,
DNNLOWP,
SpatialBNDNNLowPOp<uint8_t>);
REGISTER_CPU_OPERATOR_WITH_ENGINE(
Int8SpatialBN,
DNNLOWP,
SpatialBNDNNLowPOp<uint8_t>);
REGISTER_CPU_OPERATOR_WITH_ENGINE(
Int8SpatialBNRelu,
DNNLOWP,
SpatialBNDNNLowPOp<uint8_t, true>);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-avoid-non-const-global-variables)
OPERATOR_SCHEMA(Int8SpatialBN).NumInputs(5).NumOutputs(1);
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,cppcoreguidelines-avoid-non-const-global-variables)
OPERATOR_SCHEMA(Int8SpatialBNRelu).NumInputs(5).NumOutputs(1);
} // namespace caffe2
|