Caffe2 - C++ API
A deep learning, cross platform ML framework
gather_ranges_to_dense_op.cc
1 #include "caffe2/operators/gather_ranges_to_dense_op.h"
2 
3 namespace caffe2 {
4 namespace {
5 
6 OPERATOR_SCHEMA(GatherRangesToDense)
7  .NumInputs(2, 3)
8  .NumOutputs(1, INT_MAX)
9  .SetDoc(R"DOC(
10 Given DATA tensor of rank 1, and RANGES tensor of rank 3, gather values
11 corresponding to each range into a separate output tensor. If the optional input
12 KEY tensor is also given, the output will be sorted by KEY for each example.
13 
14 RANGES dimensions description:
15 1: represents list of examples within a batch
16 2: represents list features
17 3: two values which are start and length or a range (to be applied on DATA)
18 
19 Each feature has fixed lengths which are passed as lengths argument and a
20 separate tensor will be produced for each feature.
21 i.e. DATA.dim(1) = len(lengths) = NumOuptuts.
22 
23 Missing features (represented by empty ranges) filled with default_value.
24 
25 Example 1:
26  DATA = [1, 2, 3, 4, 5, 6, 7, 8]
27  RANGES = [
28  [
29  [2, 4],
30  [0, 2],
31  ],
32  [
33  [0, 0],
34  [6, 2],
35  ]
36  ]
37  lengths = [4, 2]
38  OUTPUT[0] = [[3, 4, 5, 6], [0, 0, 0, 0]]
39  OUTPUT[1] = [[1, 2], [7, 8]]
40 
41 Example 2 (with KEY):
42 DATA = [1, 2, 3, 4, 5, 6, 7, 8]
43 KEY = [0, 1, 3, 2, 1, 0, 1, 0]
44 RANGES = [
45  [
46  [2, 4],
47  [0, 2],
48  ],
49  [
50  [0, 0],
51  [6, 2],
52  ]
53 ]
54 lengths = [4, 2]
55 OUTPUT[0] = [[6, 5, 4, 3], [0, 0, 0, 0]]
56 OUTPUT[1] = [[1, 2], [8, 7]]
57 
58 Contrast Example 2 with Example 1. For each data point per feature, the values
59 are sorted by the corresponding KEY.
60 )DOC")
61  .Input(0, "DATA", "Tensor of rank 1.")
62  .Input(
63  1,
64  "RANGES",
65  "Tensor of int32/int64 ranges, of dims (N, M, 2). "
66  "Where N is number of examples and M is a size of each example. "
67  "Last dimention represents a range in the format (start, lengths)")
68  .Input(2, "KEY", "Tensor of rank 1 and type int64.")
69  .Output(0, "OUTPUT", "1-D tensor of size sum of range lengths")
70  .Arg("lengths", "Expected lengths for ranges")
71  .TensorInferenceFunction([](const OperatorDef& def,
72  const vector<TensorShape>& in) {
73  ArgumentHelper helper(def);
74  auto lengths = helper.GetRepeatedArgument<int>("lengths");
75  CAFFE_ENFORCE_EQ(in[0].dims_size(), 1, "DATA should be 1-D tensor.");
76  CAFFE_ENFORCE_EQ(in[1].dims_size(), 3, "RANGES should be 3-D tensor.");
77  if (in.size() > 2) {
78  CAFFE_ENFORCE_EQ(in[2].dims_size(), 1, "KEY should be 1-D tensor.");
79  }
80  CAFFE_ENFORCE_GT(lengths.size(), 0, "lengths should be non-empty.");
81  std::vector<TensorShape> out(lengths.size());
82  for (int i = 0; i < lengths.size(); ++i) {
83  out[i].set_data_type(in[0].data_type());
84  out[i].add_dims(in[1].dims(0));
85  out[i].add_dims(lengths[i]);
86  }
87  return out;
88  });
89 
90 REGISTER_CPU_OPERATOR(GatherRangesToDense, GatherRangesToDenseOp<CPUContext>);
91 NO_GRADIENT(GatherRangesToDense);
92 
93 } // namespace
94 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13