File: reverse_packed_segs_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 (90 lines) | stat: -rw-r--r-- 2,805 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
#ifndef CAFFE2_OPERATORS_REVERSE_PACKED_SEGS_OP_H_
#define CAFFE2_OPERATORS_REVERSE_PACKED_SEGS_OP_H_

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

namespace caffe2 {

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

  bool RunOnDevice() override {
    return DispatchHelper<TensorTypes<float, double, int, long, bool>>::call(
        this, Input(DATA));
  }

  template <typename T>
  bool DoRunWithType() {
    if (Input(LENGTHS).template IsType<int>()) {
      DoRunWithLengthType<T, int>();
    } else {
      DoRunWithLengthType<T, long>();
    }
    return true;
  }

 private:
  INPUT_TAGS(DATA, LENGTHS);

  template <typename T, typename LengthType>
  void DoRunWithLengthType() {
    const auto& data = Input(DATA);
    const auto& lengths = Input(LENGTHS);

    CAFFE_ENFORCE(
        data.dim() == 3,
        "DATA should be 3-D tensor <lengths, "
        "segments, embeddings>");
    CAFFE_ENFORCE(lengths.dim() == 1, "LENGTH should be 1-D");

    const auto shape = data.sizes();
    auto* output = Output(0, shape, at::dtype<T>());

    const auto max_length = data.sizes()[0];
    const auto batch_size = data.sizes()[1];
    const auto block_size = data.sizes()[2];
    CAFFE_ENFORCE(
        lengths.sizes()[0] == batch_size,
        "lenths size should be"
        " equal to batch size");

    const T* data_ptr = data.template data<T>();
    const LengthType* lengths_ptr = lengths.template data<LengthType>();

    vector<LengthType> lengths_host(batch_size);
    context_.template CopyToCPU<LengthType>(
        batch_size, lengths_ptr, &lengths_host[0]);
    context_.FinishDeviceComputation();

    T* rev_data_ptr = output->template mutable_data<T>();
    for (const auto i : c10::irange(batch_size)) {
      const auto& seg_length = lengths_host[i];
      CAFFE_ENFORCE_LE(seg_length, max_length);
      int64_t j = 0;
      for (; j < seg_length; j++) {
        const T* data_block_ptr = data_ptr + (j * batch_size + i) * block_size;
        T* rev_data_block_ptr =
            rev_data_ptr + ((seg_length - 1 - j) * batch_size + i) * block_size;
        context_.template CopySameDevice<T>(
            block_size, data_block_ptr, rev_data_block_ptr);
      }
      for (; j < max_length; j++) {
        const T* data_block_ptr = data_ptr + (j * batch_size + i) * block_size;
        T* rev_data_block_ptr =
            rev_data_ptr + (j * batch_size + i) * block_size;
        context_.template CopySameDevice<T>(
            block_size, data_block_ptr, rev_data_block_ptr);
      }
    }
  }
};

} // namespace caffe2

#endif // CAFFE2_OPERATORS_REVERSE_PACKED_SEGS_OP_H_