1 #ifndef CAFFE2_OPERATORS_GATHER_RANGES_TO_DENSE_OPS_H_ 2 #define CAFFE2_OPERATORS_GATHER_RANGES_TO_DENSE_OPS_H_ 6 #include "caffe2/core/common_omp.h" 7 #include "caffe2/core/context.h" 8 #include "caffe2/core/logging.h" 9 #include "caffe2/core/operator.h" 10 #include "caffe2/core/types.h" 11 #include "caffe2/utils/math.h" 18 template <
class Context>
21 USE_OPERATOR_CONTEXT_FUNCTIONS;
22 template <
class... Args>
25 lengths_(this->
template GetRepeatedArgument<int>(
"lengths")) {
26 CAFFE_ENFORCE_GT(lengths_.size(), 0,
"There has to be at least one length");
27 for (
auto length : lengths_) {
28 CAFFE_ENFORCE_GT(length, 0,
"Each length should be positive");
32 bool RunOnDevice()
override {
34 this, this->
template Input<Tensor>(RANGES, CPU));
37 template <
typename Index>
38 bool DoRunWithType() {
39 auto& data =
Input(DATA);
40 auto& ranges =
Input(RANGES);
41 CAFFE_ENFORCE_EQ(data.dim(), 1,
"Data has to be 1-D");
42 CAFFE_ENFORCE_EQ(ranges.dim(), 3,
"Ranges has to be 3-D");
43 if (InputSize() == 3) {
44 auto& key =
Input(KEY);
45 CAFFE_ENFORCE_EQ(key.dim(), 1,
"Key has to be 1-D");
47 key.dtype().template
Match<int64_t>(),
"Key has to be type int64_t");
52 "Nummber of ranges should match number of lengths");
56 "Nummber of ranges should match number of outputs");
58 ranges.size(2), 2,
"Ranges last dimension should be of size 2");
60 auto* rawData =
static_cast<const char*
>(data.raw_data());
61 auto* rangesData = ranges.template data<Index>();
62 int rangesDataOffset = 0;
63 auto itemsize = data.dtype().itemsize();
65 auto batchSize = ranges.size(0);
66 vector<int64_t> outputDims{batchSize, 0};
67 vector<char*> outputRawData;
68 for (
int i = 0; i < OutputSize(); ++i) {
69 auto* output = Output(i);
70 outputDims[1] = lengths_[i];
71 output->Resize(outputDims);
72 char* ptr =
static_cast<char*
>(output->raw_mutable_data(data.dtype()));
73 memset(ptr, 0, output->nbytes());
74 outputRawData.push_back(ptr);
77 for (
int i = 0; i < batchSize; ++i) {
78 for (
int j = 0; j < OutputSize(); ++j) {
79 auto rangeStart = rangesData[rangesDataOffset++];
80 auto rangeLength = rangesData[rangesDataOffset++];
81 if (rangeLength == 0) {
88 "Range lengths missmatch for output #",
91 if (InputSize() == 2) {
92 context_.CopyItemsSameDevice(
95 rawData + rangeStart * itemsize,
96 outputRawData[j] + i * itemsize * lengths_[j]);
98 auto& key =
Input(KEY);
99 auto* key_data = key.template data<int64_t>();
100 vector<std::pair<int64_t, const char*>> buffer;
101 for (
int b_i = 0; b_i < rangeLength; ++b_i) {
102 int64_t one_key_item = key_data[rangeStart + b_i];
103 auto* one_data_item = rawData + (rangeStart + b_i) * itemsize;
104 buffer.emplace_back(one_key_item, one_data_item);
109 [](
const std::pair<int64_t, const char*>& left,
110 const std::pair<int64_t, const char*>& right) {
111 return left.first < right.first;
113 for (
int b_i = 0; b_i < rangeLength; ++b_i) {
116 outputRawData[j] + (i * lengths_[j] + b_i) * itemsize,
123 CAFFE_ENFORCE_EQ(rangesDataOffset, ranges.numel());
128 INPUT_TAGS(DATA, RANGES, KEY);
131 vector<int> lengths_;
136 #endif // CAFFE2_OPERATORS_GATHER_RANGES_TO_DENSE_OPS_H_
const Tensor & Input(int idx, DeviceType type=Context::GetDeviceType())
Retrieve a non-owning reference to the input at position 'idx' for this operator. ...
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...