6#include <unordered_map>
21using t_tuple = std::tuple<Tensor, Tensor>;
30 initParams(_w_ih, _w_hh, _b_ih, _b_hh, _context);
33 CellParams(
const CellParams& rhs) {
34 initParams(rhs.w_ih, rhs.w_hh, rhs.b_ih, rhs.b_hh, rhs.context);
37 CellParams& operator=(
const CellParams& rhs) {
38 initParams(rhs.w_ih, rhs.w_hh, rhs.b_ih, rhs.b_hh, rhs.context);
48 w_ih = copy_ctor(_w_ih);
49 w_hh = copy_ctor(_w_hh);
50 b_ih = copy_ctor(_b_ih);
51 b_hh = copy_ctor(_b_hh);
74 const CellParams&
params)
const {
75 const auto& hx = std::get<0>(
hidden);
76 const auto& cx = std::get<1>(
hidden);
78 auto linear_hh =
params.linear_hh(hx);
81 auto ingate =
sigmoid(chunked_gates[0]);
82 auto forgetgate =
sigmoid(chunked_gates[1]);
84 auto outgate =
sigmoid(chunked_gates[3]);
96template <
typename output_type,
typename h
idden_type>
101 LayerOutput(
const output_type& _outputs,
const hidden_type&
_hidden) {
107template <
typename h
idden_type,
typename param_type>
109 using output_type = LayerOutput<Tensor, hidden_type>;
111 virtual output_type operator()(
113 const hidden_type& input_hidden,
114 const param_type&
params)
const = 0;
117struct FullLSTMLayer : Layer<t_tuple, CellParams> {
121 LayerOutput<std::vector<Tensor>, t_tuple> operator()(
122 const std::vector<Tensor>& step_inputs,
123 const std::tuple<Tensor, Tensor>& input_hidden,
124 const CellParams&
params)
const {
125 std::vector<Tensor> step_outputs;
126 auto hidden = copy_ctor(input_hidden);
128 for (
size_t i = 0;
i < step_inputs.size();
i++) {
130 step_outputs.push_back(copy_ctor(std::get<0>(
hidden)));
133 return {step_outputs,
hidden};
136 LayerOutput<Tensor, t_tuple> operator()(
138 const std::tuple<Tensor, Tensor>& input_hidden,
139 const CellParams&
params)
const override {
140 auto unstacked_output =
143 unstacked_output.final_hidden};
149struct FullBidirectionalLSTMLayer
150 : Layer<std::pair<t_tuple, t_tuple>, std::pair<CellParams, CellParams>> {
151 using bidir_hidden_type = std::pair<t_tuple, t_tuple>;
152 using param_type = std::pair<CellParams, CellParams>;
153 using output_type = LayerOutput<Tensor, bidir_hidden_type>;
158 output_type operator()(
160 const bidir_hidden_type& input_hidden,
161 const param_type&
params)
const override {
164 auto fw_result =
layer_(step_inputs, input_hidden.first,
params.first);
166 outputs.push_back(copy_ctor(fw_output));
169 layer_(rev_step_inputs, input_hidden.second,
params.second);
170 std::reverse(rev_result.outputs.begin(), rev_result.outputs.end());
172 outputs.push_back(copy_ctor(rev_output));
179 inline std::vector<Tensor>
reverse(std::vector<Tensor>&&
x)
const {
189template <
typename h
idden_type,
typename weight_type>
190LayerOutput<Tensor, std::vector<hidden_type>> apply_layer_stack(
191 const Layer<hidden_type, weight_type>&
layer,
193 const std::vector<hidden_type>& hiddens,
194 const std::vector<weight_type>&
weights,
198 "Expected more hidden states in stacked_rnn");
202 auto layer_input =
input.UnsafeSharedInstance();
203 auto hidden_it = hiddens.begin();
204 auto weight_it =
weights.begin();
205 std::vector<hidden_type> final_hiddens(
num_layers);
207 auto layer_output =
layer(layer_input, *(hidden_it++), *(weight_it++));
208 final_hiddens.at(l) =
std::move(layer_output.final_hidden);
209 layer_input =
std::move(layer_output.outputs);
211 return {layer_input, final_hiddens};
214std::tuple<Tensor, Tensor, Tensor> _lstm_impl(
216 const std::vector<CellParams>&
params,
222 using stack_output = LayerOutput<Tensor, std::vector<t_tuple>>;
225 int64_t total_layers = layer_hx.size();
226 std::vector<std::tuple<Tensor, Tensor>> hiddens;
227 hiddens.reserve(total_layers);
228 for (
int64_t i = 0;
i < total_layers; ++
i) {
232 std::shared_ptr<stack_output> stack_output_ptr;
234 auto bidir_result = apply_layer_stack(
235 FullBidirectionalLSTMLayer{cell,
context},
240 stack_output_ptr.reset(
new stack_output(
241 bidir_result.outputs,
242 unpair_vec(
std::move(bidir_result.final_hidden))));
244 auto result = apply_layer_stack(
246 stack_output_ptr = std::make_shared<stack_output>(
std::move(result));
249 std::vector<Tensor> hy, cy;
250 hy.reserve(total_layers);
251 cy.reserve(total_layers);
252 for (
auto&
hidden : stack_output_ptr->final_hidden) {
263std::vector<CellParams> gather_params(
264 const std::vector<Tensor>&
params,
268 std::vector<CellParams> result;
271 params.size() % 4, 0,
"got an incorrect number of LSTM parameters");
272 for (
size_t i = 0;
i <
params.size();
i += 4) {
278 params.size() % 2, 0,
"got an incorrect number of LSTM parameters");
279 for (
size_t i = 0;
i <
params.size();
i += 2) {
287class InferenceLSTMOp :
public Operator<CPUContext> {
289 template <
class...
Args>
290 explicit InferenceLSTMOp(
Args&&...
args)
294 this->template GetSingleArgument<
bool>(
"bidirectional",
false)),
297 this->template GetSingleArgument<
bool>(
"batch_first",
false)) {}
299 bool RunOnDevice()
override;
Args({2<< 5}) -> Args({2<< 8}) ->Args({2<< 12}) ->Args({2<< 14})
C10_DECLARE_EXPORT_CAFFE2_OP_TO_C10(LSTMOp)
Tensor linear(const Tensor &input, const Tensor &weight, const Tensor &bias)
std::vector< Tensor > unbind(const Tensor &self, int64_t dim)
std::vector< Tensor > chunk(const Tensor &self, int64_t chunks, int64_t dim)
Tensor add(const Tensor &self, Scalar other, Scalar alpha)
Tensor tanh(const Tensor &self)
void forward(int64_t offset)
constexpr Symbol mul(static_cast< unique_t >(_keys::aten_mul))
constexpr remove_reference_t< T > && move(T &&t) noexcept
Copyright (c) 2016-present, Facebook, Inc.
const vector< TensorShape > & inputs
matrix of logits for each example and class weights
the implementation takes an the hidden state the cell and a weight the final hidden cell num_layers
CAFFE_ENFORCE_EQ(in.size(), 1, "New shape must not be specified by the input blob and the " "argument `shape` at the same time.")
the implementation takes an the hidden state the cell and a weight the final hidden cell bidirectional
INT_MAX Subnet with blob bindings Indices of corresponding outer workspace in List of blobs from the forward Do int out bool
in a sequence length aware fashion given the previous hidden and the sequence computes the GRU avoiding computation if the input is Bool to determine if hidden state is zeroes or passed along for timesteps past the given sequence_length hidden
Unscaled log probabilities Optional blob to be used to weight the samples for the loss With spatial weighting is by x
const ArgumentHelper args(def)
we first initialize the output tensor to all and then do accumulation Any further calls to the input
bounding box regression result deltas as well as predefined bounding box shapes anchors Greedy non maximum suppression is applied to generate the final bounding boxes DOC int RPN_PRE_NMS_TOP_N float RPN_NMS_THRESH for rotated angle is normalized to be within[angle_bound_lo, angle_bound_hi] for rotated angle is normalized to be within[angle_bound_lo, angle_bound_hi] Scores from conv layer
required base learning rate default used only for inv policy type default sampling rate on iterations default True in alter policy int64_t
the other dimension is proportionally scaled Defaults to Whether or not to mirror the image Defaults to Vector of means per color Standard deviation by which to normalize color channels Defaults to Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults if the input is in Caffe format Defaults to Number of CPU decode transform threads Defaults to Name of the Type of The sizes of any outputs besides the data and shortest side desired for image resize Defaults to[-1, -1] or no random resize desired Tensor containing the images additional outputs
CAFFE_ENFORCE(dims.front() >=0, "Dimension ids must be non-negative.")
Module caffe2.python.context.
List[Tensor] reverse(List[Tensor] lst)
def make_tuple(example_inputs)