Caffe2 - C++ API
A deep learning, cross platform ML framework
lengths_pad_op.cc
1 #include "caffe2/operators/lengths_pad_op.h"
2 
3 namespace caffe2 {
4 REGISTER_CPU_OPERATOR(LengthsPad, LengthsPadOp<CPUContext>);
5 
6 OPERATOR_SCHEMA(LengthsPad)
7  .NumInputs(2)
8  .NumOutputs(1)
9  .SetDoc(R"DOC(
10 Given DATA tensor of rank r >= 1, and LENGTHS tensor of rank 1, pad each
11 segment in DATA with `value`, so that each segment's length is `target_length`.
12 If will throw, if there is segment of length larger than `target_length`.
13 
14 Example:
15  DATA = [
16  [2.3, 3.4],
17  [4.5, 5.7],
18  [6.8, 7.9],
19  ]
20  LENGTHS = [0, 1, 1, 1]
21  and target_length = 2, padding value = -1.0
22  OUTPUT = [
23  [-1.0, -1.0],
24  [-1.0, -1.0],
25  [2.3, 3.4],
26  [-1.0, -1.0],
27  [4.5, 5.7],
28  [-1.0, -1.0],
29  [6.8, 7.9],
30  [-1.0, -1.0],
31  ]
32 )DOC")
33  .Input(
34  0,
35  "DATA",
36  "Tensor of rank r >= 1. First dimension must be equal to the size of "
37  "lengths")
38  .Input(1, "LENGTHS", "Tensor of int32 lengths of rank 1")
39  .Output(0, "OUTPUT", "Padded DATA tensor")
40  .Arg("padding_value", "The value to pad the data")
41  .Arg("target_length", "The target length of each segment")
42  .TensorInferenceFunction([](const OperatorDef& def,
43  const vector<TensorShape>& in) {
44  vector<TensorShape> out(1);
45  ArgumentHelper helper(def);
46  int target_length = helper.GetSingleArgument<int>("target_length", -1);
47  CAFFE_ENFORCE_GE(target_length, 1);
48  vector<int> output_dims;
49  const auto& data_dims = GetDimsVector(in[0]);
50  const auto& lengths_dims = GetDimsVector(in[1]);
51  output_dims.push_back(lengths_dims[0] * target_length);
52  output_dims.insert(
53  output_dims.end(), data_dims.begin() + 1, data_dims.end());
54 
55  out[0] = CreateTensorShape(output_dims, in[0].data_type());
56  return out;
57  });
58 
59 NO_GRADIENT(LengthsPad);
60 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13