File: int8_resize_nearest_op.h

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (83 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download
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
#ifndef CAFFE2_OPERATORS_INT8_RESIZE_NEAREST_OP_H_
#define CAFFE2_OPERATORS_INT8_RESIZE_NEAREST_OP_H_

#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/core/tensor_int8.h"
#include "caffe2/operators/quantized/int8_utils.h"
#include <c10/util/irange.h>

namespace caffe2 {

namespace int8 {

class Int8ResizeNearestOp final : public Operator<CPUContext> {
 public:
  template <class... Args>
  explicit Int8ResizeNearestOp(Args&&... args)
      : Operator<CPUContext>(std::forward<Args>(args)...) {
    width_scale_ = this->template GetSingleArgument<float>("width_scale", 1);
    height_scale_ = this->template GetSingleArgument<float>("height_scale", 1);
    output_dims =
        this->template GetRepeatedArgument<int>("output_size", vector<int>{});
    CAFFE_ENFORCE_GT(width_scale_, 0);
    CAFFE_ENFORCE_GT(height_scale_, 0);
  }

  bool RunOnDevice() override {
    // Assume NHWC layout.
    const auto& X = Inputs()[0]->template Get<Int8TensorCPU>();
    auto* Y = Outputs()[0]->template GetMutable<Int8TensorCPU>();

    CAFFE_ENFORCE_EQ(4, X.t.dim());

    const int N = X.t.dim32(0);
    const int IH = X.t.dim32(1);
    const int IW = X.t.dim32(2);
    const int C = X.t.dim32(3);
    if (!output_dims.empty()) {
      CAFFE_ENFORCE_EQ(
          2, output_dims.size(), "Int8ResizeNearest expects 2 dim output size");
      height_scale_ = output_dims[0] / IH;
      width_scale_ = output_dims[1] / IW;
    }
    const int OW = IW * width_scale_;
    const int OH = IH * height_scale_;
    ReinitializeTensor(&Y->t, {N, OH, OW, C}, at::dtype<uint8_t>().device(CPU));
    Y->scale = X.scale;
    Y->zero_point = X.zero_point;

    int32_t Y_offset = this->template GetSingleArgument<int>("Y_zero_point", 0);
    auto Y_scale = this->template GetSingleArgument<float>("Y_scale", 1);
    TORCH_CHECK_EQ(Y_offset, X.zero_point);
    TORCH_CHECK_EQ(Y_scale, X.scale);

    const uint8_t* Xdata = X.t.data<uint8_t>();
    uint8_t* Ydata = Y->t.mutable_data<uint8_t>();

    for (const auto n : c10::irange(N)) {
      for (const auto y : c10::irange(OH)) {
        const int in_y = std::min((int)(y / height_scale_), (IH - 1));
        for (const auto x : c10::irange(OW)) {
          const int in_x = std::min((int)(x / width_scale_), (IW - 1));
          std::memcpy(
              &Ydata[C * x + C * OW * y + C * OW * OH * n],
              &Xdata[C * in_x + C * IW * in_y + C * IW * IH * n],
              C);
        }
      }
    }
    return true;
  }

 private:
  float width_scale_;
  float height_scale_;
  vector<int> output_dims;
};

} // namespace int8

} // namespace caffe2

#endif // CAFFE2_OPERATORS_INT8_RESIZE_NEAREST_OP_H_