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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
#include "caffe2/operators/resize_op.h"
#include "caffe2/utils/cpu_neon.h"
#include "caffe2/utils/math.h"
#ifdef USE_MKLDNN
#include "caffe2/ideep/operators/operator_fallback_ideep.h"
#include "caffe2/ideep/utils/ideep_operator.h"
#endif
namespace caffe2 {
void resizeNearestNCHW2x(
int batch_size,
int num_channels,
int input_height,
int input_width,
const float* input,
float* output) {
const int output_height = input_height * 2;
const int output_width = input_width * 2;
for (int n = 0; n < batch_size; ++n) {
for (int c = 0; c < num_channels; ++c) {
for (int y = 0; y < output_height; ++y) {
const int in_y = y / 2;
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
int vecW = (input_width / 4) * 4; // round down
int x = 0;
for (; x < vecW; x += 4) {
// load 0 1 2 3
float32x4_t v = vld1q_f32(input + in_y * input_width + x);
const int oidx = output_width * y + x * 2;
float32x4x2_t v2 = {{v, v}};
// store 00 11 22 33
vst2q_f32(output + oidx + 0, v2);
}
// handle remainder
for (; x < input_width; ++x) {
const float v = input[in_y * input_width + x];
const int oidx = output_width * y + x * 2;
output[oidx + 0] = v;
output[oidx + 1] = v;
}
#else
for (int x = 0; x < input_width; ++x) {
const float v = input[in_y * input_width + x];
const int oidx = output_width * y + x * 2;
output[oidx + 0] = v;
output[oidx + 1] = v;
}
#endif
}
input += input_height * input_width;
output += output_height * output_width;
}
}
}
template <>
bool ResizeNearestOp<float, CPUContext>::RunOnDeviceWithOrderNCHW() {
const auto& X = Input(0);
const int batch_size = X.dim32(0), num_channels = X.dim32(1),
input_height = X.dim32(2), input_width = X.dim32(3);
if (InputSize() == 2) {
const auto& scales = Input(1);
CAFFE_ENFORCE_EQ(scales.dim(), 1);
CAFFE_ENFORCE_EQ(scales.numel(), 2);
const float* scales_data = scales.data<float>();
height_scale_ = scales_data[0];
width_scale_ = scales_data[1];
}
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
int output_width = input_width * width_scale_;
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
int output_height = input_height * height_scale_;
auto* Y = Output(
0,
{batch_size, num_channels, output_height, output_width},
at::dtype<float>());
const float* Xdata = X.data<float>();
float* Ydata = Y->template mutable_data<float>();
// Specialized implementation for fast 2x upsampling
if (width_scale_ == 2.0 && height_scale_ == 2.0) {
resizeNearestNCHW2x(
batch_size, num_channels, input_height, input_width, Xdata, Ydata);
return true;
}
for (int n = 0; n < batch_size; ++n) {
for (int c = 0; c < num_channels; ++c) {
for (int y = 0; y < output_height; ++y) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int in_y = std::min((int)(y / height_scale_), (input_height - 1));
for (int x = 0; x < output_width; ++x) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int in_x = std::min((int)(x / width_scale_), (input_width - 1));
Ydata[output_width * y + x] = Xdata[input_width * in_y + in_x];
}
}
Xdata += input_height * input_width;
Ydata += output_width * output_height;
}
}
return true;
}
template <>
bool ResizeNearestOp<float, CPUContext>::RunOnDeviceWithOrderNHWC() {
const auto& X = Input(0);
const int batch_size = X.dim32(0), input_height = X.dim32(1),
input_width = X.dim32(2), num_channels = X.dim32(3);
if (InputSize() == 2) {
const auto& scales = Input(1);
CAFFE_ENFORCE_EQ(scales.dim(), 1);
CAFFE_ENFORCE_EQ(scales.numel(), 2);
const float* scales_data = scales.data<float>();
height_scale_ = scales_data[0];
width_scale_ = scales_data[1];
}
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
int output_width = input_width * width_scale_;
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
int output_height = input_height * height_scale_;
const int output_width_stride = output_width * num_channels;
const int input_width_stride = input_width * num_channels;
auto* Y = Output(
0,
{batch_size, output_height, output_width, num_channels},
at::dtype<float>());
const float* Xdata = X.data<float>();
float* Ydata = Y->template mutable_data<float>();
for (int n = 0; n < batch_size; ++n) {
for (int y = 0; y < output_height; ++y) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int in_y = std::min((int)(y / height_scale_), (input_height - 1));
for (int x = 0; x < output_width; ++x) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int in_x = std::min((int)(x / width_scale_), (input_width - 1));
std::memcpy(
&Ydata[output_width_stride * y + num_channels * x],
&Xdata[input_width_stride * in_y + num_channels * in_x],
num_channels * sizeof(float));
}
}
Xdata += input_height * input_width_stride;
Ydata += output_height * output_width_stride;
}
return true;
}
template <>
bool ResizeNearestOp<float, CPUContext>::RunOnDevice() {
switch (order_) {
case StorageOrder::NHWC:
return RunOnDeviceWithOrderNHWC();
case StorageOrder::NCHW:
return RunOnDeviceWithOrderNCHW();
default:
CAFFE_THROW("Unknown Storage order: ", order_);
}
}
template <>
bool ResizeNearestGradientOp<float, CPUContext>::RunOnDeviceWithOrderNCHW() {
const auto& dY = Input(0);
const auto& X = Input(1);
const auto inputDims = dY.sizes();
CAFFE_ENFORCE_EQ(4, inputDims.size());
const int batch_size = dY.dim32(0), num_channels = dY.dim32(1),
input_height = dY.dim32(2), input_width = dY.dim32(3);
const int output_height = X.dim32(2);
const int output_width = X.dim32(3);
if (InputSize() == 3) {
const auto& scales = Input(2);
CAFFE_ENFORCE_EQ(scales.dim(), 1);
CAFFE_ENFORCE_EQ(scales.numel(), 2);
const float* scales_data = scales.data<float>();
height_scale_ = scales_data[0];
width_scale_ = scales_data[1];
}
auto* dX = Output(
0,
{batch_size, num_channels, output_height, output_width},
at::dtype<float>());
math::Set<float, CPUContext>(
dX->numel(), 0.0f, dX->template mutable_data<float>(), &context_);
const float* dYdata = dY.data<float>();
float* dXdata = dX->template mutable_data<float>();
for (int n = 0; n < batch_size; ++n) {
for (int c = 0; c < num_channels; ++c) {
for (int y = 0; y < input_height; ++y) {
const int out_y =
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
std::min((int)(y / height_scale_), (output_height - 1));
for (int x = 0; x < input_width; ++x) {
const int out_x =
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
std::min((int)(x / width_scale_), (output_width - 1));
dXdata[output_width * out_y + out_x] += dYdata[input_width * y + x];
}
}
dYdata += input_height * input_width;
dXdata += output_height * output_width;
}
}
return true;
}
template <>
bool ResizeNearestGradientOp<float, CPUContext>::RunOnDeviceWithOrderNHWC() {
const auto& dY = Input(0);
const auto& X = Input(1);
const auto inputDims = dY.sizes();
CAFFE_ENFORCE_EQ(4, inputDims.size());
const int batch_size = dY.dim32(0), input_height = dY.dim32(1),
input_width = dY.dim32(2), num_channels = dY.dim32(3);
const int output_height = X.dim32(1);
const int output_width = X.dim32(2);
if (InputSize() == 3) {
const auto& scales = Input(2);
CAFFE_ENFORCE_EQ(scales.dim(), 1);
CAFFE_ENFORCE_EQ(scales.numel(), 2);
const float* scales_data = scales.data<float>();
height_scale_ = scales_data[0];
width_scale_ = scales_data[1];
}
auto* dX = Output(
0,
{batch_size, output_height, output_width, num_channels},
at::dtype<float>());
math::Set<float, CPUContext>(
dX->numel(), 0.0f, dX->template mutable_data<float>(), &context_);
const int output_width_stride = output_width * num_channels;
const int input_width_stride = input_width * num_channels;
const float* dYdata = dY.data<float>();
float* dXdata = dX->template mutable_data<float>();
for (int n = 0; n < batch_size; ++n) {
for (int y = 0; y < input_height; ++y) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int out_y = std::min((int)(y / height_scale_), (output_height - 1));
for (int x = 0; x < input_width; ++x) {
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
const int out_x = std::min((int)(x / width_scale_), (output_width - 1));
float* dXdata_c0 =
dXdata + output_width_stride * out_y + num_channels * out_x;
const float* dYdata_c0 =
dYdata + input_width_stride * y + num_channels * x;
for (int c = 0; c < num_channels; ++c) {
dXdata_c0[c] += dYdata_c0[c];
}
}
}
dYdata += input_height * input_width_stride;
dXdata += output_height * output_width_stride;
}
return true;
}
template <>
bool ResizeNearestGradientOp<float, CPUContext>::RunOnDevice() {
switch (order_) {
case StorageOrder::NHWC:
return RunOnDeviceWithOrderNHWC();
case StorageOrder::NCHW:
return RunOnDeviceWithOrderNCHW();
default:
CAFFE_THROW("Unknown Storage order: ", order_);
}
}
REGISTER_CPU_OPERATOR(ResizeNearest, ResizeNearestOp<float, CPUContext>);
REGISTER_CPU_GRADIENT_OPERATOR(
ResizeNearestGradient,
ResizeNearestGradientOp<float, CPUContext>);
#ifdef USE_MKLDNN
REGISTER_IDEEP_OPERATOR(
ResizeNearest,
IDEEPFallbackOp<ResizeNearestOp<float, CPUContext>>);
#endif
// Input: X, output: Y
OPERATOR_SCHEMA(ResizeNearest)
.NumInputs(1, 2)
.NumOutputs(1)
.Arg("width_scale", "Scale along width dimension")
.Arg("height_scale", "Scale along height dimension")
.SetDoc(R"DOC(
Resizes the spatial dimensions of the input using nearest neighbor
interpolation. The `width_scale` and `height_scale` arguments
control the size of the output, which is given by:
output_width = floor(input_width * width_scale)
output_height = floor(output_height * height_scale)
)DOC")
.Input(0, "X", "Input tensor")
.Input(
1,
"scales", // the hack to support onnx spec
"1D, 2-element, Scales tensor, [height_scale, width_scale]")
.Output(0, "Y", "Output tensor")
.InheritOnnxSchema("Upsample");
// Input: dY, output: dX
GRADIENT_OPERATOR_SCHEMA(ResizeNearestGradient)
.NumInputs(2, 3)
.NumOutputs(1)
.Arg("width_scale", "Scale along width dimension")
.Arg("height_scale", "Scale along height dimension");
class GetResizeNearestGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
vector<OperatorDef> GetGradientDefs() override {
if (def_.input().size() == 2) {
// this is a hack to support the second input as dynamic
// width_scale and height_scale to align with onnx change
return SingleGradientDef(
"ResizeNearestGradient",
"",
vector<string>{GO(0), I(0), I(1)},
vector<string>{GI(0)});
}
return SingleGradientDef(
"ResizeNearestGradient",
"",
vector<string>{GO(0), I(0)},
vector<string>{GI(0)});
}
};
REGISTER_GRADIENT(ResizeNearest, GetResizeNearestGradient);
} // namespace caffe2
using ResizeNearestOpFloatCPU =
caffe2::ResizeNearestOp<float, caffe2::CPUContext>;
// clang-format off
C10_EXPORT_CAFFE2_OP_TO_C10_CPU(
ResizeNearest,
"_caffe2::ResizeNearest("
"Tensor X, "
"str order, "
"float width_scale, "
"float height_scale"
") -> (Tensor Y)",
ResizeNearestOpFloatCPU);
// clang-format on
|