File: expand_squeeze_dims_op.cc

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 (133 lines) | stat: -rw-r--r-- 3,856 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
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
#include "caffe2/operators/expand_squeeze_dims_op.h"
#include <caffe2/ideep/ideep_utils.h>
#include <caffe2/ideep/operators/operator_fallback_ideep.h>

using namespace caffe2;

namespace {

class IDEEPExpandDimsOp final : public IDEEPOperator {
 public:
  USE_IDEEP_DEF_ALIASES();
  USE_IDEEP_OPERATOR_FUNCTIONS();
  using FALLBACK_OP = IDEEPFallbackOp<ExpandDimsOp<CPUContext>, SkipIndices<0>>;

  IDEEPExpandDimsOp(const OperatorDef& operator_def, Workspace* ws)
      : IDEEPOperator(operator_def, ws),
        fallback_(operator_def, ws) {
    dims_ = OperatorBase::GetRepeatedArgument<int>("dims");
    auto originalSize = dims_.size();
    CAFFE_ENFORCE_GT(originalSize, 0, "Parameter `dims` must be provided.");
    std::sort(dims_.begin(), dims_.end());
    dims_.erase(std::unique(dims_.begin(), dims_.end()), dims_.end());
    if (dims_.size() < originalSize) {
      LOG(WARNING) << "Parameter `dims` has repeated dimensions.";
    }
    CAFFE_ENFORCE_GE(dims_.front(), 0, "Dimension ids must be non-negative.");
  }
  // NOLINTNEXTLINE(modernize-use-equals-default)
  ~IDEEPExpandDimsOp() override {}

  bool RunOnDevice() override {
    if (!OperatorBase::InputBlob(INPUT).template IsType<itensor>()) {
      return fallback_.Run(0);
    }

    const auto& X = Input(INPUT);
    auto* Y = Output(OUTPUT);
    if (&X != Y) {
      // Copy if not inplace
      ideep::direct_copy::compute(X, *Y);
    }
    if (dims_.empty()) {
      return true;
    }

    auto newDims = X.get_dims();
    CAFFE_ENFORCE_GE(
        newDims.size() + dims_.size(),
        dims_.back() + 1,
        "Input needs at least ",
        (1 + dims_.back() - dims_.size()),
        " dimensions given `dims`.");

    for (const auto dim : dims_) {
      newDims.insert(newDims.begin() + dim, 1);
    }

    Y->reshape(newDims);
    return true;
  }

 private:
  std::vector<int> dims_;
  FALLBACK_OP fallback_;

  INPUT_TAGS(INPUT);
  OUTPUT_TAGS(OUTPUT);
};


class IDEEPSqueezeOp final : public IDEEPOperator {
 public:
  USE_IDEEP_DEF_ALIASES();
  USE_IDEEP_OPERATOR_FUNCTIONS();
  using FALLBACK_OP = IDEEPFallbackOp<SqueezeOp<CPUContext>, SkipIndices<0>>;

  IDEEPSqueezeOp(const OperatorDef& operator_def, Workspace* ws)
      : IDEEPOperator(operator_def, ws),
        fallback_(operator_def, ws) {
    dims_ = OperatorBase::GetRepeatedArgument<int>("dims");
    auto originalSize = dims_.size();
    CAFFE_ENFORCE_GT(originalSize, 0, "Parameter `dims` must be provided.");

    std::sort(dims_.begin(), dims_.end());
    dims_.erase(std::unique(dims_.begin(), dims_.end()), dims_.end());
    if (dims_.size() < originalSize) {
      LOG(WARNING) << "Parameter `dims` has repeated dimensions.";
    }
    CAFFE_ENFORCE_GE(dims_.front(), 0, "Dimension ids must be non-negative.");
  }
  // NOLINTNEXTLINE(modernize-use-equals-default)
  ~IDEEPSqueezeOp() override {}

  bool RunOnDevice() override {
    if (!OperatorBase::InputBlob(INPUT).template IsType<itensor>()) {
      return fallback_.Run(0);
    }

    const auto& X = Input(INPUT);
    auto* Y = Output(OUTPUT);

    CAFFE_ENFORCE_GT(
        X.ndims(),
        dims_.back(),
        "Input needs at least ",
        (dims_.back() + 1),
        " dimensions.");
    const auto& ideep_dims = X.get_dims();
    std::vector<int64_t> dims(ideep_dims.begin(), ideep_dims.end());
    const auto new_dims = SqueezeOp<IDEEPContext>::ComputeDims(dims, dims_);
    itensor::dims new_dims_ideep(new_dims.begin(), new_dims.end());
    if (&X != Y) {
      // Copy if not inplace
      ideep::direct_copy::compute(X, *Y);
    }

    Y->reshape(new_dims_ideep);
    return true;
  }

 private:
  std::vector<int> dims_;
  FALLBACK_OP fallback_;

  INPUT_TAGS(INPUT);
  OUTPUT_TAGS(OUTPUT);
};


REGISTER_IDEEP_OPERATOR(ExpandDims, IDEEPExpandDimsOp);
REGISTER_IDEEP_OPERATOR(Squeeze, IDEEPSqueezeOp);

} // namespace