Caffe2 - C++ API
A deep learning, cross platform ML framework
lengths_tile_op.cc
1 
17 #include "caffe2/operators/lengths_tile_op.h"
18 
19 namespace caffe2 {
20 REGISTER_CPU_OPERATOR(LengthsTile, LengthsTileOp<CPUContext>);
21 
22 OPERATOR_SCHEMA(LengthsTile)
23  .NumInputs(2)
24  .NumOutputs(1)
25  .SetDoc(R"DOC(
26 Given DATA tensor of rank r >= 1, and LENGTHS tensor of rank 1, duplicate each
27 entry of the outer-most dimension of DATA according to LENGTHS, and concatenate
28 them in an output tensor of rank r.
29 
30 Example:
31  DATA = [
32  [1.0, 1.2],
33  [2.3, 3.4],
34  [4.5, 5.7],
35  [6.8, 7.9],
36  ]
37  LENGTHS = [0, 1, 3, 2]
38  OUTPUT = [
39  [2.3, 3.4],
40  [4.5, 5.7],
41  [4.5, 5.7],
42  [4.5, 5.7],
43  [6.8, 7.9],
44  [6.8, 7.9],
45  ]
46 )DOC")
47  .Input(
48  0,
49  "DATA",
50  "Tensor of rank r >= 1. First dimension must be equal to the size of "
51  "lengths")
52  .Input(1, "LENGTHS", "Tensor of int32 lengths of rank 1")
53  .Output(0, "OUTPUT", "Tensor of rank r");
54 
55 class GetLengthsTileGradient : public GradientMakerBase {
56  using GradientMakerBase::GradientMakerBase;
57  vector<OperatorDef> GetGradientDefs() override {
58  CAFFE_ENFORCE_EQ(def_.input_size(), 2);
59  return SingleGradientDef(
60  "LengthsSum",
61  "",
62  // input 1 is the lengths used to repeat
63  // DATA in the forward pass
64  vector<string>{GO(0), I(1)},
65  // only concerned with the gradient on "DATA"
66  vector<string>{GI(0)});
67  }
68 };
69 REGISTER_GRADIENT(LengthsTile, GetLengthsTileGradient);
70 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.