File: pack_rnn_sequence_op.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (107 lines) | stat: -rw-r--r-- 3,476 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "caffe2/operators/pack_rnn_sequence_op.h"

namespace caffe2 {
namespace {

REGISTER_CPU_OPERATOR(PackRNNSequence, PackRNNSequenceOpBase<CPUContext, true>);
REGISTER_CPU_OPERATOR(
    UnpackRNNSequence,
    PackRNNSequenceOpBase<CPUContext, false>);

OPERATOR_SCHEMA(PackRNNSequence)
    .NumInputs(2)
    .NumOutputs(1)
    .SetDoc(R"DOC(
Pack values based on the length blob. Each number from length blob represents
the corresponding values that need to be packed. The dimension for each pack
is the same as the maximum number from the length blob (padding with zero is
implemented for smaller length value). The overall output dimension is:
T * N * D, where T is the max number of lengths, N is the size of lengths,
and D is the dimension of each feature value. The following example shows
the input and output of this operator:


Given:
  values = [v1, v2, v3, v4, v5, v6, v7, v8]
  lengths = [2, 3, 1, 2];


Output:
  output = [
    [v1, v3, v6, v7],
    [v2, v4, 0,  v8],
    [0,  v5, 0,  0 ],
  ]


One application for this operator is the transfer data into the format that is
used for RNN models. Note that the gradient operator of PackRNNSequence is
UnpackRNNSequence.
)DOC")
    .Input(0, "values", "Data tensor, contains a sequence of features")
    .Input(1, "lengths", "lengths with each number representing the pack size.")
    .Output(0, "output", "Output tensor after packing");

OPERATOR_SCHEMA(UnpackRNNSequence)
    .NumInputs(2)
    .NumOutputs(1)
    .SetDoc(R"DOC(
This is the reverse operator for PackRNNSequence. It maps the packed values
back to sequence values based on the length blob. Each number from length blob
represents the corresponding values that has been grouped. The dimension
for each pack is the same as the maximum number from the length blob (padding
with zero was implemented for smaller length value). The overall output
dimension is: M * D, where M is the sum of lengths, and D is the dimension of
each feature value. The following example shows the input and output of
this operator:


Given:
  values = [
    [v1, v3, v6, v7],
    [v2, v4, 0,  v8],
    [0,  v5, 0,  0 ],
  ]
  lengths = [2, 3, 1, 2]


Output:
  output = [v1, v2, v3, v4, v5, v6, v7, v8];


One application for this operator is the transfer data from the format of RNN
back to sequence values. Note that the gradient operator of
UnpackRNNSequence is PackRNNSequence.
)DOC")
    .Input(0, "values", "Data tensor, contains the packed features")
    .Input(1, "lengths", "lengths with each number representing the pack size.")
    .Output(0, "output", "Output tensor before packing");

class GetPackRNNSequenceGradient : public GradientMakerBase {
  using GradientMakerBase::GradientMakerBase;
  vector<OperatorDef> GetGradientDefs() override {
    CAFFE_ENFORCE_EQ(def_.input_size(), 2);
    return SingleGradientDef(
        "UnpackRNNSequence",
        "",
        vector<string>{GO(0), I(1)},
        vector<string>{GI(0)});
  }
};

class GetUnpackRNNSequenceGradient : public GradientMakerBase {
  using GradientMakerBase::GradientMakerBase;
  vector<OperatorDef> GetGradientDefs() override {
    CAFFE_ENFORCE_EQ(def_.input_size(), 2);
    return SingleGradientDef(
        "PackRNNSequence",
        "",
        vector<string>{GO(0), I(1)},
        vector<string>{GI(0)});
  }
};

REGISTER_GRADIENT(PackRNNSequence, GetPackRNNSequenceGradient);
REGISTER_GRADIENT(UnpackRNNSequence, GetUnpackRNNSequenceGradient);
} // namespace
} // namespace caffe2