1 #include "caffe2/operators/pack_rnn_sequence_op.h" 6 REGISTER_CPU_OPERATOR(PackRNNSequence, PackRNNSequenceOpBase<CPUContext, true>);
9 PackRNNSequenceOpBase<CPUContext, false>);
11 OPERATOR_SCHEMA(PackRNNSequence)
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: 25 values = [v1, v2, v3, v4, v5, v6, v7, v8] 26 lengths = [2, 3, 1, 2]; 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 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");
45 OPERATOR_SCHEMA(UnpackRNNSequence)
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 65 lengths = [2, 3, 1, 2] 69 output = [v1, v2, v3, v4, v5, v6, v7, v8]; 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. 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");
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(
87 vector<string>{GO(0), I(1)},
88 vector<string>{GI(0)});
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(
99 vector<string>{GO(0), I(1)},
100 vector<string>{GI(0)});
104 REGISTER_GRADIENT(PackRNNSequence, GetPackRNNSequenceGradient);
105 REGISTER_GRADIENT(UnpackRNNSequence, GetUnpackRNNSequenceGradient);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...