File: prepend_dim_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 (98 lines) | stat: -rw-r--r-- 2,800 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

#ifndef CAFFE2_OPERATORS_PREPEND_DIM_OP_H_
#define CAFFE2_OPERATORS_PREPEND_DIM_OP_H_

#include "caffe2/core/common_omp.h"
#include "caffe2/core/context.h"
#include "caffe2/core/logging.h"
#include "caffe2/core/operator.h"
#include <c10/util/irange.h>

namespace caffe2 {

template <class Context>
class PrependDimOp : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  template <class... Args>
  explicit PrependDimOp(Args&&... args)
      : Operator<Context>(std::forward<Args>(args)...),
        dim_size_(this->template GetSingleArgument<int64_t>("dim_size", 0)) {
    CAFFE_ENFORCE_GT(
        dim_size_, 0, "Argument dim_size must be greater than zero.");
  }

  bool RunOnDevice() override {
    auto& input = Input(0);
    auto* output = Output(0);

    CAFFE_ENFORCE(input.dim() > 0, "Input must be at least 1D.");
    CAFFE_ENFORCE(
        input.size(0) % dim_size_ == 0,
        "First dimension must be multiple of prepend_dim. Current first dimension: ",
        input.size(0));

    vector<int64_t> actual_new_shape(input.dim() + 1);
    actual_new_shape[0] = dim_size_;
    actual_new_shape[1] = input.size(0) / dim_size_;
    // NOLINTNEXTLINE(clang-diagnostic-sign-compare)
    for (const auto i : c10::irange(1, input.sizes().size())) {
      actual_new_shape[i + 1] = input.size(i);
    }
    output->Resize(actual_new_shape);

    if (output != &input) {
      // If we are not doing in-place computation, a copy is needed.
      context_.CopyItemsSameDevice(
          input.dtype(),
          input.numel(),
          input.raw_data(),
          output->raw_mutable_data(input.dtype()));
    }
    return true;
  }

 private:
  int64_t dim_size_;
};

template <class Context>
class MergeDimOp : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  template <class... Args>
  explicit MergeDimOp(Args&&... args)
      : Operator<Context>(std::forward<Args>(args)...) {}

  bool RunOnDevice() override {
    auto& input = Input(0);
    auto* output = Output(0);

    CAFFE_ENFORCE(input.dim() > 1, "Input must be at least 2D.");

    vector<int64_t> actual_new_shape(input.dim() - 1);
    actual_new_shape[0] = input.size(0) * input.size(1);
    // NOLINTNEXTLINE(clang-diagnostic-sign-compare)
    for (int i = 1; i < input.sizes().size() - 1; ++i) {
      actual_new_shape[i] = input.size(i + 1);
    }
    output->Resize(actual_new_shape);

    if (output != &input) {
      // If we are not doing in-place computation, a copy is needed.
      context_.CopyItemsSameDevice(
          input.dtype(),
          input.numel(),
          input.raw_data(),
          output->raw_mutable_data(input.dtype()));
    }
    return true;
  }

 private:
  int64_t dim_size_;
};

} // namespace caffe2

#endif // CAFFE2_OPERATORS_PREPEND_DIM_OP_H_