Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 13a2080 | 2019-01-16 18:21:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/NEON/functions/NELSTMLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/PixelValue.h" |
| 27 | #include "arm_compute/core/Utils.h" |
| 28 | #include "arm_compute/core/Validate.h" |
| 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 31 | #include "arm_compute/runtime/common/LSTMParams.h" |
| 32 | |
| 33 | #include <cmath> |
| 34 | #include <memory> |
| 35 | #include <tuple> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | using namespace arm_compute::misc::shape_calculator; |
| 39 | |
| 40 | NELSTMLayer::NELSTMLayer(std::shared_ptr<IMemoryManager> memory_manager) |
| 41 | : _memory_group(std::move(memory_manager)), _fully_connected_input_gate(), _gemm_input_gate(), _transpose_input_gate(), _accum_input_gate1(), _accum_input_gate2(), _subtract_input_gate(), |
| 42 | _pixelwise_mul_input_gate(), _activation_input_gate(), _fully_connected_forget_gate(), _gemm_forget_gate(), _transpose_forget_gate(), _accum_forget_gate1(), _accum_forget_gate2(), |
| 43 | _pixelwise_mul_forget_gate(), _activation_forget_gate(), _fully_connected_cell_state(), _gemm_cell_state1(), _gemm_cell_state2(), _transpose_cell_state(), _accum_cell_state1(), _accum_cell_state2(), |
| 44 | _pixelwise_mul_cell_state1(), _activation_cell_state(), _cell_clip(), _pixelwise_mul_cell_state2(), _fully_connected_output(), _gemm_output(), _pixelwise_mul_output_state1(), _transpose_output(), |
| 45 | _accum_output1(), _accum_output2(), _activation_output(), _activation_output_state(), _pixelwise_mul_output_state2(), _fully_connected_output_state(), _gemm_output_state(), _accum_output_state(), |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 46 | _projection_clip(), _copy_cell_state(), _copy_output(), _concat_scratch_buffer(), _concat_inputs_forget_gate(), _concat_weights_forget_gate(), _concat_weights_input_gate(), _concat_weights_output(), |
| 47 | _input_gate_out1(), _input_gate_out2(), _input_gate_out3(), _input_gate_out4(), _forget_gate_out1(), _forget_gate_out2(), _forget_gate_out3(), _forget_gate_out4(), _forget_gate_out5(), |
| 48 | _forget_gate_out6(), _cell_state_out1(), _cell_state_out2(), _cell_state_out3(), _cell_state_out4(), _cell_state_out5(), _output1(), _output2(), _output3(), _output4(), _cell_state_activation(), |
| 49 | _output_state1(), _ones(), _run_peephole_opt(false), _run_cifg_opt(false), _perform_cell_clipping(false), _has_projection_weights(false), _perform_projection_clipping(false), _is_prepared(false) |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
| 53 | void NELSTMLayer::configure(const ITensor *input, |
| 54 | const ITensor *input_to_forget_weights, const ITensor *input_to_cell_weights, const ITensor *input_to_output_weights, |
| 55 | const ITensor *recurrent_to_forget_weights, const ITensor *recurrent_to_cell_weights, const ITensor *recurrent_to_output_weights, |
| 56 | const ITensor *forget_gate_bias, const ITensor *cell_bias, const ITensor *output_gate_bias, |
| 57 | const ITensor *output_state_in, const ITensor *cell_state_in, |
| 58 | ITensor *scratch_buffer, ITensor *output_state_out, ITensor *cell_state_out, ITensor *output, |
| 59 | const LSTMParams<ITensor> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, |
| 62 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 63 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 64 | forget_gate_bias, cell_bias, output_gate_bias, |
| 65 | output_state_in, cell_state_in, |
| 66 | scratch_buffer, output_state_out, cell_state_out, output); |
| 67 | |
| 68 | // Set lstm parameters |
| 69 | LSTMParams<ITensorInfo> lstm_params_info; |
| 70 | if(lstm_params.has_peephole_opt()) |
| 71 | { |
| 72 | lstm_params_info.set_peephole_params(lstm_params.cell_to_forget_weights()->info(), lstm_params.cell_to_output_weights()->info()); |
| 73 | } |
| 74 | if(lstm_params.has_projection()) |
| 75 | { |
| 76 | lstm_params_info.set_projection_params(lstm_params.projection_weights()->info(), |
| 77 | lstm_params.projection_bias() != nullptr ? lstm_params.projection_bias()->info() : nullptr); |
| 78 | } |
| 79 | if(!lstm_params.has_cifg_opt()) |
| 80 | { |
| 81 | const ITensorInfo *cell_to_input_weights_info = (lstm_params.has_peephole_opt()) ? lstm_params.cell_to_input_weights()->info() : nullptr; |
| 82 | lstm_params_info.set_cifg_params(lstm_params.input_to_input_weights()->info(), lstm_params.recurrent_to_input_weights()->info(), |
| 83 | cell_to_input_weights_info, lstm_params.input_gate_bias()->info()); |
| 84 | } |
| 85 | |
| 86 | // Validate |
| 87 | ARM_COMPUTE_ERROR_THROW_ON(NELSTMLayer::validate(input->info(), input_to_forget_weights->info(), |
| 88 | input_to_cell_weights->info(), input_to_output_weights->info(), |
| 89 | recurrent_to_forget_weights->info(), recurrent_to_cell_weights->info(), recurrent_to_output_weights->info(), |
| 90 | forget_gate_bias->info(), cell_bias->info(), output_gate_bias->info(), |
| 91 | output_state_in->info(), cell_state_in->info(), |
| 92 | scratch_buffer->info(), output_state_out->info(), cell_state_out->info(), output->info(), |
| 93 | lstm_params_info, activation_info, cell_threshold, projection_threshold)); |
| 94 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 95 | const TensorShape cell_state_shape = cell_state_in->info()->tensor_shape(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 96 | |
| 97 | // Configure block that calculates the forget gate |
| 98 | // forget_gate = Activation(input * input_to_forget_weights + output_state_in * recurrent_to_forget_weights + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias) |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 99 | // We optimize this as follows: |
| 100 | // forget_gate = Activation( (input,output_state_in) * (input_to_forget_weights,recurrent_to_forget_weights) + PixelWiseMul(cell_state, cell_to_forget_weights) + forget_gate_bias) |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 101 | _forget_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 102 | _forget_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 103 | _forget_gate_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 104 | |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 105 | std::vector<const ITensor *> inputs_vector; |
| 106 | inputs_vector.emplace_back(input); |
| 107 | inputs_vector.emplace_back(output_state_in); |
| 108 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 109 | _memory_group.manage(&_forget_gate_out2); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 110 | _concat_inputs_forget_gate.configure(inputs_vector, &_forget_gate_out2); |
| 111 | |
| 112 | std::vector<const ITensor *> weights_vector; |
| 113 | |
| 114 | weights_vector.emplace_back(input_to_forget_weights); |
| 115 | weights_vector.emplace_back(recurrent_to_forget_weights); |
| 116 | |
| 117 | _concat_weights_forget_gate.configure(weights_vector, &_forget_gate_out6); |
| 118 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 119 | _memory_group.manage(&_forget_gate_out5); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 120 | _fully_connected_forget_gate.configure(&_forget_gate_out2, &_forget_gate_out6, forget_gate_bias, &_forget_gate_out5); |
| 121 | _memory_group.manage(&_forget_gate_out1); |
| 122 | _memory_group.manage(&_forget_gate_out3); |
| 123 | _forget_gate_out6.allocator()->allocate(); |
| 124 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 125 | Tensor *forget_gate_out = &_forget_gate_out5; |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 126 | if(lstm_params.has_peephole_opt()) |
| 127 | { |
| 128 | _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 129 | |
| 130 | _run_peephole_opt = true; |
| 131 | _memory_group.manage(&_forget_gate_out4); |
| 132 | _pixelwise_mul_forget_gate.configure(cell_state_in, lstm_params.cell_to_forget_weights(), &_forget_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
| 133 | _accum_forget_gate2.configure(&_forget_gate_out5, &_forget_gate_out4, &_forget_gate_out3, ConvertPolicy::SATURATE); |
| 134 | _forget_gate_out4.allocator()->allocate(); |
| 135 | _forget_gate_out5.allocator()->allocate(); |
| 136 | forget_gate_out = &_forget_gate_out3; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | _forget_gate_out3.allocator()->allocate(); |
| 141 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 142 | _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 143 | |
| 144 | // Configure block that calculates the input gate |
| 145 | // input_gate = Activation(input * input_to_input_weights + output_state * recurrent_to_input_weights + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG |
| 146 | // input_gate = 1 - forget_gate, with CIFG |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 147 | // We optimize this as follows: |
| 148 | // input_gate = Activation((input,output_state) * (input_to_input_weights,recurrent_to_input_weights) + PixelWiseMul(cell_state, cell_to_input_weights) + input_gate_bias), without CIFG |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 149 | _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 150 | Tensor *input_gate_out = &_input_gate_out1; |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 151 | if(lstm_params.has_cifg_opt()) |
| 152 | { |
| 153 | _memory_group.manage(&_input_gate_out1); |
| 154 | _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 155 | _subtract_input_gate.configure(&_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 156 | _ones.allocator()->allocate(); |
| 157 | _run_cifg_opt = true; |
| 158 | } |
| 159 | else |
| 160 | { |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 161 | _input_gate_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 162 | _input_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 163 | |
| 164 | std::vector<const ITensor *> lstm_weights; |
| 165 | lstm_weights.emplace_back(lstm_params.input_to_input_weights()); |
| 166 | lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights()); |
| 167 | |
| 168 | _concat_weights_input_gate.configure(lstm_weights, &_input_gate_out2); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 169 | |
| 170 | _memory_group.manage(&_input_gate_out1); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 171 | _memory_group.manage(&_input_gate_out4); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 172 | |
| 173 | _fully_connected_input_gate.configure(&_forget_gate_out2, &_input_gate_out2, lstm_params.input_gate_bias(), &_input_gate_out3); |
| 174 | _input_gate_out2.allocator()->allocate(); |
| 175 | input_gate_out = &_input_gate_out3; |
| 176 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 177 | if(_run_peephole_opt) |
| 178 | { |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 179 | _memory_group.manage(&_input_gate_out4); |
| 180 | _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out4, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
| 181 | _accum_input_gate2.configure(&_input_gate_out3, &_input_gate_out4, &_input_gate_out1, ConvertPolicy::SATURATE); |
| 182 | _input_gate_out3.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 183 | _input_gate_out4.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 184 | input_gate_out = &_input_gate_out1; |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 185 | } |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 186 | else |
| 187 | { |
| 188 | _input_gate_out1.allocator()->allocate(); |
| 189 | } |
| 190 | _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Configure block that calculates the cell state |
| 194 | // cell_state = Clip((PixelwiseMul(input_gate, Activation(input * input_to_cell_weights + output_state_in * recurrent_to_cell_weights + cell_bias)) + PixelwiseMul(forget_gate, cell_state)), cell_threshold) |
| 195 | TensorShape cell_state1_shape = compute_transposed_shape(*recurrent_to_output_weights->info()); |
| 196 | _cell_state_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 197 | _cell_state_out2.allocator()->init(TensorInfo(cell_state1_shape, 1, input->info()->data_type())); |
| 198 | _cell_state_out3.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 199 | _cell_state_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 200 | _cell_state_out5.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 201 | |
| 202 | _memory_group.manage(&_cell_state_out1); |
| 203 | _fully_connected_cell_state.configure(input, input_to_cell_weights, cell_bias, &_cell_state_out1); |
| 204 | _memory_group.manage(&_cell_state_out2); |
| 205 | _transpose_cell_state.configure(recurrent_to_cell_weights, &_cell_state_out2); |
| 206 | _memory_group.manage(&_cell_state_out3); |
| 207 | _gemm_cell_state1.configure(output_state_in, &_cell_state_out2, nullptr, &_cell_state_out3, 1.f, 0.f); |
| 208 | _cell_state_out2.allocator()->allocate(); |
| 209 | _memory_group.manage(&_cell_state_out4); |
| 210 | _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); |
| 211 | _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); |
| 212 | _memory_group.manage(&_cell_state_out5); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 213 | _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 214 | _cell_state_out4.allocator()->allocate(); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 215 | _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 216 | _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); |
| 217 | _cell_state_out3.allocator()->allocate(); |
| 218 | _cell_state_out5.allocator()->allocate(); |
| 219 | // Perform clipping |
| 220 | if(cell_threshold != 0.f) |
| 221 | { |
| 222 | _perform_cell_clipping = true; |
| 223 | _cell_clip.configure(&_cell_state_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, cell_threshold)); |
| 224 | } |
| 225 | |
| 226 | // Configure block that calculates the output |
| 227 | // output_state_out = Activation(input * input_to_output_weights + output_state_in * recurrent_to_output_weights + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias) |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 228 | // We optimize this as follows: |
| 229 | // output_state_out = Activation( (input,output_state_in) * (input_to_output_weights, recurrent_to_output_weights) + PixelWiseMul(cell_state, cell_to_output_weights) + output_gate_bias) |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 230 | _output1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 231 | _output4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 232 | |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 233 | std::vector<const ITensor *> in_out_weights; |
| 234 | in_out_weights.emplace_back(input_to_output_weights); |
| 235 | in_out_weights.emplace_back(recurrent_to_output_weights); |
| 236 | |
| 237 | _concat_weights_output.configure(in_out_weights, &_output2); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 238 | _memory_group.manage(&_output1); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 239 | _memory_group.manage(&_output4); |
| 240 | |
| 241 | _fully_connected_output.configure(&_forget_gate_out2, &_output2, output_gate_bias, &_output4); |
| 242 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 243 | _output2.allocator()->allocate(); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 244 | _forget_gate_out2.allocator()->allocate(); |
| 245 | |
| 246 | Tensor *output_gate_out = &_output4; |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 247 | if(lstm_params.has_peephole_opt()) |
| 248 | { |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 249 | _output3.allocator()->init(TensorInfo(_cell_state_out1.info()->tensor_shape(), 1, input->info()->data_type())); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 250 | |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 251 | _memory_group.manage(&_output3); |
| 252 | _pixelwise_mul_output_state1.configure(&_cell_state_out1, lstm_params.cell_to_output_weights(), &_output3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
| 253 | _accum_output2.configure(&_output4, &_output3, &_output1, ConvertPolicy::SATURATE); |
| 254 | _output4.allocator()->allocate(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 255 | output_gate_out = &_output1; |
| 256 | |
| 257 | // Allocate intermediate buffers |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 258 | _output3.allocator()->allocate(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 259 | } |
| 260 | else |
| 261 | { |
| 262 | _output1.allocator()->allocate(); |
| 263 | } |
| 264 | _activation_output.configure(output_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 265 | |
| 266 | // Configure block that calculates the output state |
| 267 | /** lstm_res = PixelwiseMul(output, Activation(cell_state)) |
| 268 | * |
| 269 | * -- Clip(lstm_res * projection_weights + projection_bias, projection_threshold) , if there is a projection |
| 270 | * / |
| 271 | * output_state = -- |
| 272 | * \ |
| 273 | * -- lstm_res , otherwise |
| 274 | */ |
| 275 | ITensor *output_state_out_tmp = lstm_params.has_projection() ? &_output_state1 : output_state_out; |
| 276 | _cell_state_activation.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 277 | _output_state1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); |
| 278 | |
| 279 | _memory_group.manage(&_cell_state_activation); |
| 280 | _activation_output_state.configure(&_cell_state_out1, &_cell_state_activation, activation_info); |
| 281 | _pixelwise_mul_output_state2.configure(&_cell_state_activation, output_gate_out, output_state_out_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); |
| 282 | _cell_state_activation.allocator()->allocate(); |
Georgios Pinitas | 13a2080 | 2019-01-16 18:21:08 +0000 | [diff] [blame] | 283 | output_gate_out->allocator()->allocate(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 284 | |
| 285 | if(lstm_params.has_projection()) |
| 286 | { |
| 287 | _has_projection_weights = true; |
| 288 | _fully_connected_output_state.configure(output_state_out_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out); |
| 289 | _output_state1.allocator()->allocate(); |
| 290 | // Perform clipping |
| 291 | if(projection_threshold != 0.f) |
| 292 | { |
| 293 | _perform_projection_clipping = true; |
| 294 | _projection_clip.configure(output_state_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // Copy cell state and output |
| 299 | _copy_cell_state.configure(&_cell_state_out1, cell_state_out); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 300 | _copy_output.configure(output_state_out, output); |
| 301 | |
| 302 | // Vector for holding the tensors to store in scratch buffer |
| 303 | std::vector<ITensor *> scratch_inputs; |
Georgios Pinitas | 0cc37c3 | 2018-11-14 15:54:26 +0000 | [diff] [blame] | 304 | if(!lstm_params.has_cifg_opt()) |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 305 | { |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 306 | scratch_inputs.emplace_back(input_gate_out); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 307 | } |
| 308 | scratch_inputs.emplace_back(&_cell_state_out1); |
| 309 | scratch_inputs.emplace_back(forget_gate_out); |
| 310 | scratch_inputs.emplace_back(output_gate_out); |
| 311 | _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer); |
Georgios Pinitas | 4f85982 | 2019-02-06 18:08:04 +0000 | [diff] [blame] | 312 | input_gate_out->allocator()->allocate(); |
| 313 | _cell_state_out1.allocator()->allocate(); |
| 314 | forget_gate_out->allocator()->allocate(); |
| 315 | output_gate_out->allocator()->allocate(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | Status NELSTMLayer::validate(const ITensorInfo *input, |
| 319 | const ITensorInfo *input_to_forget_weights, const ITensorInfo *input_to_cell_weights, const ITensorInfo *input_to_output_weights, |
| 320 | const ITensorInfo *recurrent_to_forget_weights, const ITensorInfo *recurrent_to_cell_weights, const ITensorInfo *recurrent_to_output_weights, |
| 321 | const ITensorInfo *forget_gate_bias, const ITensorInfo *cell_bias, const ITensorInfo *output_gate_bias, |
| 322 | const ITensorInfo *output_state_in, const ITensorInfo *cell_state_in, |
| 323 | const ITensorInfo *scratch_buffer, const ITensorInfo *output_state_out, const ITensorInfo *cell_state_out, const ITensorInfo *output, |
| 324 | const LSTMParams<ITensorInfo> &lstm_params, const ActivationLayerInfo &activation_info, float cell_threshold, float projection_threshold) |
| 325 | { |
| 326 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, |
| 327 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 328 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 329 | forget_gate_bias, cell_bias, output_gate_bias, |
| 330 | output_state_in, cell_state_in, |
| 331 | scratch_buffer, output_state_out, cell_state_out, output); |
| 332 | |
| 333 | // Check data types |
| 334 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 335 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, |
| 336 | input_to_forget_weights, input_to_cell_weights, input_to_output_weights, |
| 337 | recurrent_to_forget_weights, recurrent_to_cell_weights, recurrent_to_output_weights, |
| 338 | forget_gate_bias, cell_bias, output_gate_bias, |
| 339 | output_state_in, cell_state_in, |
| 340 | scratch_buffer, output_state_out, cell_state_out, output); |
| 341 | |
| 342 | // Check dimensions |
| 343 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 2); |
| 344 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_forget_weights->num_dimensions() > 2); |
| 345 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_cell_weights->num_dimensions() > 2); |
| 346 | ARM_COMPUTE_RETURN_ERROR_ON(input_to_output_weights->num_dimensions() > 2); |
| 347 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_forget_weights->num_dimensions() > 2); |
| 348 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_cell_weights->num_dimensions() > 2); |
| 349 | ARM_COMPUTE_RETURN_ERROR_ON(recurrent_to_output_weights->num_dimensions() > 2); |
| 350 | ARM_COMPUTE_RETURN_ERROR_ON(forget_gate_bias->num_dimensions() > 1); |
| 351 | ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->num_dimensions() > 1); |
| 352 | ARM_COMPUTE_RETURN_ERROR_ON(output_gate_bias->num_dimensions() > 1); |
| 353 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_in->num_dimensions() > 2); |
| 354 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_in->num_dimensions() > 2); |
| 355 | ARM_COMPUTE_RETURN_ERROR_ON(scratch_buffer->num_dimensions() > 2); |
| 356 | ARM_COMPUTE_RETURN_ERROR_ON(output_state_out->num_dimensions() > 2); |
| 357 | ARM_COMPUTE_RETURN_ERROR_ON(cell_state_out->num_dimensions() > 2); |
| 358 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 2); |
| 359 | ARM_COMPUTE_RETURN_ERROR_ON(cell_bias->dimension(0) * 4 != scratch_buffer->dimension(0) |
| 360 | && cell_bias->dimension(0) * 3 != scratch_buffer->dimension(0)); |
| 361 | |
| 362 | const unsigned int num_batches = input->dimension(1); |
| 363 | const unsigned int num_cells = input_to_output_weights->dimension(1); |
| 364 | |
| 365 | // Check peephole optimization |
| 366 | if(lstm_params.has_peephole_opt()) |
| 367 | { |
| 368 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_output_weights(), lstm_params.cell_to_forget_weights()); |
| 369 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_forget_weights()->num_dimensions() > 1); |
| 370 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_output_weights()->num_dimensions() > 1); |
| 371 | } |
| 372 | |
| 373 | TensorShape units_out_transposed_shape = compute_transposed_shape(*recurrent_to_output_weights); |
| 374 | TensorShape num_units_transposed_shape = compute_transposed_shape(*forget_gate_bias); |
| 375 | const TensorInfo units_out_transposed_info = TensorInfo(units_out_transposed_shape, 1, input->data_type()); |
| 376 | const TensorInfo num_units_transposed_info = TensorInfo(num_units_transposed_shape, 1, input->data_type()); |
| 377 | |
| 378 | TensorInfo input_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 379 | TensorInfo forget_gate = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 380 | TensorInfo output_gate_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 381 | TensorInfo cell_state_tmp = TensorInfo(TensorShape(num_cells, num_batches), 1, input->data_type()); |
| 382 | |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 383 | std::vector<const ITensorInfo *> inputs_vector; |
| 384 | inputs_vector.emplace_back(input); |
| 385 | inputs_vector.emplace_back(output_state_in); |
| 386 | TensorInfo forget_gate_concat; |
| 387 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(inputs_vector, &forget_gate_concat)); |
| 388 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 389 | // Validate forget gate |
| 390 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_forget_weights, forget_gate_bias, &forget_gate)); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 391 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 392 | if(lstm_params.has_peephole_opt()) |
| 393 | { |
| 394 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_forget_weights(), &forget_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 395 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); |
| 396 | } |
| 397 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&forget_gate, &forget_gate, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
| 398 | |
| 399 | // Validate input gate |
| 400 | if(!lstm_params.has_cifg_opt()) |
| 401 | { |
| 402 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.input_to_input_weights(), |
| 403 | lstm_params.recurrent_to_input_weights(), |
| 404 | lstm_params.input_gate_bias()); |
| 405 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_to_input_weights()->num_dimensions() > 2); |
| 406 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.recurrent_to_input_weights()->num_dimensions() > 2); |
| 407 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.input_gate_bias()->num_dimensions() > 1); |
| 408 | |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 409 | std::vector<const ITensorInfo *> lstm_weights; |
| 410 | lstm_weights.emplace_back(lstm_params.input_to_input_weights()); |
| 411 | lstm_weights.emplace_back(lstm_params.recurrent_to_input_weights()); |
| 412 | TensorInfo lstm_gate_concat; |
| 413 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(lstm_weights, &lstm_gate_concat)); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 414 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, lstm_params.input_to_input_weights(), lstm_params.input_gate_bias(), &input_gate)); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 415 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 416 | if(lstm_params.has_peephole_opt()) |
| 417 | { |
| 418 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(lstm_params.cell_to_input_weights()); |
| 419 | ARM_COMPUTE_RETURN_ERROR_ON(lstm_params.cell_to_input_weights()->num_dimensions() > 1); |
| 420 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(cell_state_in, lstm_params.cell_to_input_weights(), &input_gate, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 421 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&input_gate, &input_gate, &input_gate, ConvertPolicy::SATURATE)); |
| 422 | } |
| 423 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&input_gate, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticSubtractionKernel::validate(&forget_gate, &forget_gate, &forget_gate, ConvertPolicy::SATURATE)); |
| 428 | } |
| 429 | |
| 430 | // Validate cell state |
| 431 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_cell_weights, cell_bias, &cell_state_tmp)); |
| 432 | ARM_COMPUTE_RETURN_ON_ERROR(NEGEMM::validate(output_state_in, &units_out_transposed_info, nullptr, &cell_state_tmp, 1.f, 0.f, GEMMInfo())); |
| 433 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); |
| 434 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, activation_info)); |
| 435 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &input_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 436 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &forget_gate, &cell_state_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 437 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&cell_state_tmp, &cell_state_tmp, &cell_state_tmp, ConvertPolicy::SATURATE)); |
| 438 | if(cell_threshold != 0.f) |
| 439 | { |
| 440 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -cell_threshold, |
| 441 | cell_threshold))); |
| 442 | } |
| 443 | |
| 444 | // Validate output gate tmp |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 445 | std::vector<const ITensorInfo *> in_out_weights; |
| 446 | in_out_weights.emplace_back(input_to_output_weights); |
| 447 | in_out_weights.emplace_back(recurrent_to_output_weights); |
| 448 | TensorInfo in_out_gate_concat; |
| 449 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(in_out_weights, &in_out_gate_concat)); |
| 450 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 451 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(input, input_to_output_weights, output_gate_bias, &output_gate_tmp)); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 452 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 453 | if(lstm_params.has_peephole_opt()) |
| 454 | { |
| 455 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, lstm_params.cell_to_output_weights(), &output_gate_tmp, 1, ConvertPolicy::SATURATE, |
| 456 | RoundingPolicy::TO_ZERO)); |
| 457 | ARM_COMPUTE_RETURN_ON_ERROR(NEArithmeticAddition::validate(&output_gate_tmp, &output_gate_tmp, &output_gate_tmp, ConvertPolicy::SATURATE)); |
| 458 | } |
| 459 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&output_gate_tmp, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))); |
| 460 | |
| 461 | // Validate output state |
| 462 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(&cell_state_tmp, &cell_state_tmp, activation_info)); |
| 463 | ARM_COMPUTE_RETURN_ON_ERROR(NEPixelWiseMultiplicationKernel::validate(&cell_state_tmp, &output_gate_tmp, &output_gate_tmp, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO)); |
| 464 | if(lstm_params.has_projection()) |
| 465 | { |
| 466 | ARM_COMPUTE_RETURN_ON_ERROR(NEFullyConnectedLayer::validate(&output_gate_tmp, lstm_params.projection_weights(), lstm_params.projection_bias(), output_state_out)); |
| 467 | if(projection_threshold != 0.f) |
| 468 | { |
| 469 | ARM_COMPUTE_RETURN_ON_ERROR(NEActivationLayerKernel::validate(output_state_out, output_state_out, |
| 470 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold))); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | // Validate copy kernel |
| 475 | ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(&cell_state_tmp, cell_state_out)); |
| 476 | ARM_COMPUTE_RETURN_ON_ERROR(NECopyKernel::validate(output_state_out, output)); |
| 477 | |
| 478 | // Validate scratch concatenation |
| 479 | std::vector<ITensorInfo *> inputs_vector_info_raw; |
Georgios Pinitas | 0cc37c3 | 2018-11-14 15:54:26 +0000 | [diff] [blame] | 480 | if(!lstm_params.has_cifg_opt()) |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 481 | { |
| 482 | inputs_vector_info_raw.push_back(&input_gate); |
| 483 | } |
| 484 | inputs_vector_info_raw.push_back(&cell_state_tmp); |
| 485 | inputs_vector_info_raw.push_back(&forget_gate); |
| 486 | inputs_vector_info_raw.push_back(&output_gate_tmp); |
| 487 | |
| 488 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(inputs_vector_info_raw, scratch_buffer)); |
| 489 | return Status{}; |
| 490 | } |
| 491 | |
| 492 | void NELSTMLayer::run() |
| 493 | { |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 494 | prepare(); |
| 495 | |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 496 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 497 | |
Michalis Spyrou | 2761c2f | 2019-03-22 13:06:08 +0000 | [diff] [blame] | 498 | _concat_inputs_forget_gate.run(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 499 | _fully_connected_forget_gate.run(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 500 | |
| 501 | if(_run_peephole_opt) |
| 502 | { |
| 503 | NEScheduler::get().schedule(&_pixelwise_mul_forget_gate, Window::DimY); |
| 504 | _accum_forget_gate2.run(); |
| 505 | } |
| 506 | NEScheduler::get().schedule(&_activation_forget_gate, Window::DimY); |
| 507 | |
| 508 | if(_run_cifg_opt) |
| 509 | { |
| 510 | if(_ones.info()->data_type() == DataType::F16) |
| 511 | { |
| 512 | std::fill_n(reinterpret_cast<half *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1); |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | std::fill_n(reinterpret_cast<float *>(_ones.buffer()), _ones.info()->total_size() / _ones.info()->element_size(), 1); |
| 517 | } |
| 518 | NEScheduler::get().schedule(&_subtract_input_gate, Window::DimY); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | _fully_connected_input_gate.run(); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 523 | |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 524 | if(_run_peephole_opt) |
| 525 | { |
| 526 | NEScheduler::get().schedule(&_pixelwise_mul_input_gate, Window::DimY); |
| 527 | _accum_input_gate2.run(); |
| 528 | } |
| 529 | NEScheduler::get().schedule(&_activation_input_gate, Window::DimY); |
| 530 | } |
| 531 | |
| 532 | _fully_connected_cell_state.run(); |
| 533 | NEScheduler::get().schedule(&_transpose_cell_state, Window::DimY); |
| 534 | _gemm_cell_state1.run(); |
| 535 | NEScheduler::get().schedule(&_accum_cell_state1, Window::DimY); |
| 536 | NEScheduler::get().schedule(&_activation_cell_state, Window::DimY); |
| 537 | NEScheduler::get().schedule(&_pixelwise_mul_cell_state1, Window::DimY); |
| 538 | NEScheduler::get().schedule(&_pixelwise_mul_cell_state2, Window::DimY); |
| 539 | NEScheduler::get().schedule(&_accum_cell_state2, Window::DimY); |
| 540 | |
| 541 | if(_perform_cell_clipping) |
| 542 | { |
| 543 | NEScheduler::get().schedule(&_cell_clip, Window::DimY); |
| 544 | } |
| 545 | |
| 546 | _fully_connected_output.run(); |
Michalis Spyrou | 25f45a4 | 2018-08-08 12:53:05 +0100 | [diff] [blame] | 547 | if(_run_peephole_opt) |
| 548 | { |
| 549 | NEScheduler::get().schedule(&_pixelwise_mul_output_state1, Window::DimY); |
| 550 | _accum_output2.run(); |
| 551 | } |
| 552 | NEScheduler::get().schedule(&_activation_output, Window::DimY); |
| 553 | |
| 554 | NEScheduler::get().schedule(&_activation_output_state, Window::DimY); |
| 555 | NEScheduler::get().schedule(&_pixelwise_mul_output_state2, Window::DimY); |
| 556 | |
| 557 | if(_has_projection_weights) |
| 558 | { |
| 559 | _fully_connected_output_state.run(); |
| 560 | if(_perform_projection_clipping) |
| 561 | { |
| 562 | NEScheduler::get().schedule(&_projection_clip, Window::DimY); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | NEScheduler::get().schedule(&_copy_cell_state, Window::DimY); |
| 567 | NEScheduler::get().schedule(&_copy_output, Window::DimY); |
| 568 | |
| 569 | _concat_scratch_buffer.run(); |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void NELSTMLayer::prepare() |
| 573 | { |
| 574 | if(!_is_prepared) |
| 575 | { |
John Kesapides | 917959c | 2019-02-04 12:37:29 +0000 | [diff] [blame] | 576 | _concat_weights_forget_gate.run(); |
| 577 | if(!_run_cifg_opt) |
| 578 | { |
| 579 | _concat_weights_input_gate.run(); |
| 580 | } |
| 581 | _concat_weights_output.run(); |
| 582 | _is_prepared = true; |
| 583 | } |
| 584 | } |