File: remove_data_blocks_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 (86 lines) | stat: -rw-r--r-- 2,691 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
#ifndef CAFFE2_OPERATORS_REMOVE_DATA_BLOCKS_OP_H_
#define CAFFE2_OPERATORS_REMOVE_DATA_BLOCKS_OP_H_

#include <algorithm>
#include <vector>

#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "c10/util/irange.h"

namespace caffe2 {

template <class Context>
class RemoveDataBlocksOp final : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
  USE_SIMPLE_CTOR_DTOR(RemoveDataBlocksOp);
  USE_DISPATCH_HELPER;

  bool RunOnDevice() override {
    if (Input(INDICES).sizes()[0] == 0) {
      Output(0)->CopyFrom(Input(0));
      return true;
    } else {
      return DispatchHelper<TensorTypes<int, long>>::call(this, Input(INDICES));
    }
  }

  template <typename T>
  bool DoRunWithType() {
    const auto& data = Input(DATA);
    const auto& indices = Input(INDICES);
    CAFFE_ENFORCE(data.dim() > 0, "DATA should be at leat 1-D.");
    CAFFE_ENFORCE(indices.dim() == 1, "INDICES should be 1-D.");

    const auto outer_size = data.sizes()[0];
    const auto block_size = data.size_from_dim(1);
    const auto block_size_bytes = block_size * data.dtype().itemsize();
    auto indices_size = indices.sizes()[0];
    const char* data_ptr = (char*)data.raw_data();
    const auto* ind_ptr = indices.template data<T>();

    std::vector<T> ind_vec;
    for (const auto i : c10::irange(indices_size)) {
      ind_vec.push_back(ind_ptr[i]);
    }
    std::sort(ind_vec.begin(), ind_vec.end());
    CAFFE_ENFORCE(ind_vec[0] >= 0, "The min index should be larger than zero.");
    CAFFE_ENFORCE(
        ind_vec[indices_size - 1] < outer_size,
        "The max index should be smaller than the data outer size.");
    // removes duplicate indices
    ind_vec.erase(std::unique(ind_vec.begin(), ind_vec.end()), ind_vec.end());
    indices_size = ind_vec.size();

    auto* output = Output(0);
    auto shape = data.sizes().vec();
    shape[0] -= indices_size;
    output->Resize(shape);
    char* out_ptr = (char*)output->raw_mutable_data(data.dtype());

    ind_vec.insert(ind_vec.begin(), -1);
    int64_t ind_vec_size = ind_vec.size();
    for (const auto i : c10::irange(ind_vec_size)) {
      int64_t interval_start = ind_vec[i] + 1;
      int64_t interval_end =
          (i == ind_vec_size - 1) ? outer_size : ind_vec[i + 1];
      auto num_items = interval_end - interval_start;
      context_.CopyItemsSameDevice(
          data.dtype(),
          num_items * block_size,
          data_ptr + block_size_bytes * interval_start,
          out_ptr);
      out_ptr += block_size_bytes * num_items;
    }

    return true;
  }

 private:
  INPUT_TAGS(DATA, INDICES);
};

} // namespace caffe2

#endif // CAFFE2_OPERATORS_REMOVE_DATA_BLOCKS_OP_H_