Caffe2 - C++ API
A deep learning, cross platform ML framework
pack_rnn_sequence_op.cc
1 #include "caffe2/operators/pack_rnn_sequence_op.h"
2 
3 namespace caffe2 {
4 namespace {
5 
6 REGISTER_CPU_OPERATOR(PackRNNSequence, PackRNNSequenceOpBase<CPUContext, true>);
7 REGISTER_CPU_OPERATOR(
8  UnpackRNNSequence,
9  PackRNNSequenceOpBase<CPUContext, false>);
10 
11 OPERATOR_SCHEMA(PackRNNSequence)
12  .NumInputs(2)
13  .NumOutputs(1)
14  .SetDoc(R"DOC(
15 Pack values based on the length blob. Each number from length blob represents
16 the corresponding values that need to be packed. The dimension for each pack
17 is the same as the maximum number from the length blob (padding with zero is
18 implemented for smaller length value). The overall output dimension is:
19 T * N * D, where T is the max number of lengths, N is the size of lengths,
20 and D is the dimension of each feature value. The following example shows
21 the input and output of this operator:
22 
23 
24 Given:
25  values = [v1, v2, v3, v4, v5, v6, v7, v8]
26  lengths = [2, 3, 1, 2];
27 
28 
29 Output:
30  output = [
31  [v1, v3, v6, v7],
32  [v2, v4, 0, v8],
33  [0, v5, 0, 0 ],
34  ]
35 
36 
37 One application for this operator is the transfer data into the format that is
38 used for RNN models. Note that the gradient operator of PackRNNSequence is
39 UnpackRNNSequence.
40 )DOC")
41  .Input(0, "values", "Data tensor, contains a sequence of features")
42  .Input(1, "lengths", "lengths with each number representing the pack size.")
43  .Output(0, "output", "Output tensor after packing");
44 
45 OPERATOR_SCHEMA(UnpackRNNSequence)
46  .NumInputs(2)
47  .NumOutputs(1)
48  .SetDoc(R"DOC(
49 This is the reverse operator for PackRNNSequence. It maps the packed values
50 back to sequence values based on the length blob. Each number from length blob
51 represents the corresponding values that has been grouped. The dimension
52 for each pack is the same as the maximum number from the length blob (padding
53 with zero was implemented for smaller length value). The overall output
54 dimension is: M * D, where M is the sum of lengths, and D is the dimension of
55 each feature value. The following example shows the input and output of
56 this operator:
57 
58 
59 Given:
60  values = [
61  [v1, v3, v6, v7],
62  [v2, v4, 0, v8],
63  [0, v5, 0, 0 ],
64  ]
65  lengths = [2, 3, 1, 2]
66 
67 
68 Output:
69  output = [v1, v2, v3, v4, v5, v6, v7, v8];
70 
71 
72 One application for this operator is the transfer data from the format of RNN
73 back to sequence values. Note that the gradient operator of
74 UnpackRNNSequence is PackRNNSequence.
75 )DOC")
76  .Input(0, "values", "Data tensor, contains the packed features")
77  .Input(1, "lengths", "lengths with each number representing the pack size.")
78  .Output(0, "output", "Output tensor before packing");
79 
80 class GetPackRNNSequenceGradient : public GradientMakerBase {
81  using GradientMakerBase::GradientMakerBase;
82  vector<OperatorDef> GetGradientDefs() override {
83  CAFFE_ENFORCE_EQ(def_.input_size(), 2);
84  return SingleGradientDef(
85  "UnpackRNNSequence",
86  "",
87  vector<string>{GO(0), I(1)},
88  vector<string>{GI(0)});
89  }
90 };
91 
92 class GetUnpackRNNSequenceGradient : public GradientMakerBase {
93  using GradientMakerBase::GradientMakerBase;
94  vector<OperatorDef> GetGradientDefs() override {
95  CAFFE_ENFORCE_EQ(def_.input_size(), 2);
96  return SingleGradientDef(
97  "PackRNNSequence",
98  "",
99  vector<string>{GO(0), I(1)},
100  vector<string>{GI(0)});
101  }
102 };
103 
104 REGISTER_GRADIENT(PackRNNSequence, GetPackRNNSequenceGradient);
105 REGISTER_GRADIENT(UnpackRNNSequence, GetUnpackRNNSequenceGradient);
106 } // namespace
107 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13