From 4f85982a55ae417edde6864304aeb1d69972973e Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Wed, 6 Feb 2019 18:08:04 +0000 Subject: COMPMID-1923: Input is ignored if peephole optimization is enabled in LSTM Change-Id: I9e6c4fe279aafaf7f77af453785a8f677868a25a Signed-off-by: Georgios Pinitas Reviewed-on: https://review.mlplatform.org/639 Reviewed-by: Michalis Spyrou Tested-by: Arm Jenkins --- src/runtime/CL/functions/CLLSTMLayer.cpp | 30 ++++++++++++++---------- src/runtime/NEON/functions/NELSTMLayer.cpp | 32 +++++++++++++++---------- tests/validation/CL/LSTMLayer.cpp | 4 +++- tests/validation/NEON/LSTMLayer.cpp | 6 +++-- tests/validation/fixtures/LSTMLayerFixture.h | 35 +++++++++++++++++++++------- 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/src/runtime/CL/functions/CLLSTMLayer.cpp b/src/runtime/CL/functions/CLLSTMLayer.cpp index 8a27d68d4a..f01b1b898f 100644 --- a/src/runtime/CL/functions/CLLSTMLayer.cpp +++ b/src/runtime/CL/functions/CLLSTMLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -111,8 +111,8 @@ void CLLSTMLayer::configure(const ICLTensor *input, _forget_gate_out2.allocator()->allocate(); _memory_group.manage(&_forget_gate_out5); _accum_forget_gate1.configure(ArithmeticOperation::ADD, &_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out5, ConvertPolicy::SATURATE); + _forget_gate_out1.allocator()->allocate(); CLTensor *forget_gate_out = &_forget_gate_out5; - if(lstm_params.has_peephole_opt()) { _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); @@ -129,17 +129,18 @@ void CLLSTMLayer::configure(const ICLTensor *input, { _forget_gate_out3.allocator()->allocate(); } - _activation_forget_gate.configure(forget_gate_out, &_forget_gate_out1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); // Configure block that calculates the input gate // 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 // input_gate = 1 - forget_gate, with CIFG _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + CLTensor *input_gate_out = &_input_gate_out1; if(lstm_params.has_cifg_opt()) { _memory_group.manage(&_input_gate_out1); _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); - _subtract_input_gate.configure(ArithmeticOperation::SUB, &_ones, &_forget_gate_out1, &_input_gate_out1, ConvertPolicy::SATURATE); + _subtract_input_gate.configure(ArithmeticOperation::SUB, &_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE); _ones.allocator()->allocate(); _run_cifg_opt = true; } @@ -161,16 +162,22 @@ void CLLSTMLayer::configure(const ICLTensor *input, _input_gate_out2.allocator()->allocate(); _memory_group.manage(&_input_gate_out4); _accum_input_gate1.configure(ArithmeticOperation::ADD, &_input_gate_out1, &_input_gate_out3, &_input_gate_out4, ConvertPolicy::SATURATE); + _input_gate_out3.allocator()->allocate(); + input_gate_out = &_input_gate_out4; if(_run_peephole_opt) { _memory_group.manage(&_input_gate_out5); _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); _accum_input_gate2.configure(&_input_gate_out4, &_input_gate_out5, &_input_gate_out1, ConvertPolicy::SATURATE); + _input_gate_out4.allocator()->allocate(); _input_gate_out5.allocator()->allocate(); + input_gate_out = &_input_gate_out1; } - _input_gate_out3.allocator()->allocate(); - _input_gate_out4.allocator()->allocate(); - _activation_input_gate.configure(&_input_gate_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + else + { + _input_gate_out1.allocator()->allocate(); + } + _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); } // Configure block that calculates the cell state @@ -193,10 +200,9 @@ void CLLSTMLayer::configure(const ICLTensor *input, _accum_cell_state1.configure(ArithmeticOperation::ADD, &_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); _memory_group.manage(&_cell_state_out5); - _pixelwise_mul_cell_state1.configure(&_cell_state_out4, &_input_gate_out1, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); _cell_state_out4.allocator()->allocate(); - _pixelwise_mul_cell_state2.configure(&_forget_gate_out1, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); - _forget_gate_out1.allocator()->allocate(); + _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); _accum_cell_state2.configure(ArithmeticOperation::ADD, &_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); _cell_state_out3.allocator()->allocate(); _cell_state_out5.allocator()->allocate(); @@ -284,13 +290,13 @@ void CLLSTMLayer::configure(const ICLTensor *input, std::vector scratch_inputs; if(!lstm_params.has_cifg_opt()) { - scratch_inputs.emplace_back(&_input_gate_out1); + scratch_inputs.emplace_back(input_gate_out); } scratch_inputs.emplace_back(&_cell_state_out1); scratch_inputs.emplace_back(forget_gate_out); scratch_inputs.emplace_back(output_gate_out); _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer); - _input_gate_out1.allocator()->allocate(); + input_gate_out->allocator()->allocate(); _cell_state_out1.allocator()->allocate(); forget_gate_out->allocator()->allocate(); output_gate_out->allocator()->allocate(); diff --git a/src/runtime/NEON/functions/NELSTMLayer.cpp b/src/runtime/NEON/functions/NELSTMLayer.cpp index 2a2980bebe..73afd054fd 100644 --- a/src/runtime/NEON/functions/NELSTMLayer.cpp +++ b/src/runtime/NEON/functions/NELSTMLayer.cpp @@ -111,8 +111,8 @@ void NELSTMLayer::configure(const ITensor *input, _forget_gate_out2.allocator()->allocate(); _memory_group.manage(&_forget_gate_out5); _accum_forget_gate1.configure(&_forget_gate_out1, &_forget_gate_out3, &_forget_gate_out5, ConvertPolicy::SATURATE); + _forget_gate_out1.allocator()->allocate(); Tensor *forget_gate_out = &_forget_gate_out5; - if(lstm_params.has_peephole_opt()) { _forget_gate_out4.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); @@ -129,18 +129,19 @@ void NELSTMLayer::configure(const ITensor *input, { _forget_gate_out3.allocator()->allocate(); } - _activation_forget_gate.configure(forget_gate_out, &_forget_gate_out1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + _activation_forget_gate.configure(forget_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); forget_gate_out->allocator()->allocate(); // Configure block that calculates the input gate // 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 // input_gate = 1 - forget_gate, with CIFG _input_gate_out1.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); + Tensor *input_gate_out = &_input_gate_out1; if(lstm_params.has_cifg_opt()) { _memory_group.manage(&_input_gate_out1); _ones.allocator()->init(TensorInfo(cell_state_shape, 1, input->info()->data_type())); - _subtract_input_gate.configure(&_ones, &_forget_gate_out1, &_input_gate_out1, ConvertPolicy::SATURATE); + _subtract_input_gate.configure(&_ones, forget_gate_out, &_input_gate_out1, ConvertPolicy::SATURATE); _ones.allocator()->allocate(); _run_cifg_opt = true; } @@ -162,16 +163,22 @@ void NELSTMLayer::configure(const ITensor *input, _input_gate_out2.allocator()->allocate(); _memory_group.manage(&_input_gate_out4); _accum_input_gate1.configure(&_input_gate_out1, &_input_gate_out3, &_input_gate_out4, ConvertPolicy::SATURATE); + _input_gate_out3.allocator()->allocate(); + input_gate_out = &_input_gate_out4; if(_run_peephole_opt) { _memory_group.manage(&_input_gate_out5); _pixelwise_mul_input_gate.configure(cell_state_in, lstm_params.cell_to_input_weights(), &_input_gate_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); _accum_input_gate2.configure(&_input_gate_out4, &_input_gate_out5, &_input_gate_out1, ConvertPolicy::SATURATE); + _input_gate_out4.allocator()->allocate(); _input_gate_out5.allocator()->allocate(); + input_gate_out = &_input_gate_out1; } - _input_gate_out3.allocator()->allocate(); - _input_gate_out4.allocator()->allocate(); - _activation_input_gate.configure(&_input_gate_out1, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); + else + { + _input_gate_out1.allocator()->allocate(); + } + _activation_input_gate.configure(input_gate_out, nullptr, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); } // Configure block that calculates the cell state @@ -194,11 +201,9 @@ void NELSTMLayer::configure(const ITensor *input, _accum_cell_state1.configure(&_cell_state_out1, &_cell_state_out3, &_cell_state_out4, ConvertPolicy::SATURATE); _activation_cell_state.configure(&_cell_state_out4, nullptr, activation_info); _memory_group.manage(&_cell_state_out5); - _pixelwise_mul_cell_state1.configure(&_cell_state_out4, &_input_gate_out1, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); - _input_gate_out1.allocator()->allocate(); + _pixelwise_mul_cell_state1.configure(&_cell_state_out4, input_gate_out, &_cell_state_out5, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); _cell_state_out4.allocator()->allocate(); - _pixelwise_mul_cell_state2.configure(&_forget_gate_out1, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); - _forget_gate_out1.allocator()->allocate(); + _pixelwise_mul_cell_state2.configure(forget_gate_out, cell_state_in, &_cell_state_out3, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); _accum_cell_state2.configure(&_cell_state_out5, &_cell_state_out3, &_cell_state_out1, ConvertPolicy::SATURATE); _cell_state_out3.allocator()->allocate(); _cell_state_out5.allocator()->allocate(); @@ -281,19 +286,22 @@ void NELSTMLayer::configure(const ITensor *input, // Copy cell state and output _copy_cell_state.configure(&_cell_state_out1, cell_state_out); - _cell_state_out1.allocator()->allocate(); _copy_output.configure(output_state_out, output); // Vector for holding the tensors to store in scratch buffer std::vector scratch_inputs; if(!lstm_params.has_cifg_opt()) { - scratch_inputs.emplace_back(&_input_gate_out1); + scratch_inputs.emplace_back(input_gate_out); } scratch_inputs.emplace_back(&_cell_state_out1); scratch_inputs.emplace_back(forget_gate_out); scratch_inputs.emplace_back(output_gate_out); _concat_scratch_buffer.configure(scratch_inputs, scratch_buffer); + input_gate_out->allocator()->allocate(); + _cell_state_out1.allocator()->allocate(); + forget_gate_out->allocator()->allocate(); + output_gate_out->allocator()->allocate(); } Status NELSTMLayer::validate(const ITensorInfo *input, diff --git a/tests/validation/CL/LSTMLayer.cpp b/tests/validation/CL/LSTMLayer.cpp index 4afebdc2ce..ea20bd6c2c 100644 --- a/tests/validation/CL/LSTMLayer.cpp +++ b/tests/validation/CL/LSTMLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -160,6 +160,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMo { // Validate output validate(CLAccessor(_target), _reference, tolerance_f32); + validate(CLAccessor(_target_scratch), _reference_scratch, tolerance_f32); } TEST_SUITE_END() // FP32 @@ -170,6 +171,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, CLLSTMLayerFixture, framework::DatasetMod { // Validate output validate(CLAccessor(_target), _reference, tolerance_f16); + validate(CLAccessor(_target_scratch), _reference_scratch, tolerance_f16); } TEST_SUITE_END() // FP16 TEST_SUITE_END() // LSTMLayer diff --git a/tests/validation/NEON/LSTMLayer.cpp b/tests/validation/NEON/LSTMLayer.cpp index 3693d4f20d..5dfd32b8e5 100644 --- a/tests/validation/NEON/LSTMLayer.cpp +++ b/tests/validation/NEON/LSTMLayer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -39,7 +39,7 @@ namespace validation { namespace { -RelativeTolerance tolerance_f32(0.001f); +RelativeTolerance tolerance_f32(0.00001f); RelativeTolerance tolerance_f16(half(0.1)); } // namespace @@ -160,6 +160,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, framework::DatasetMo { // Validate output validate(Accessor(_target), _reference, tolerance_f32); + validate(Accessor(_target_scratch), _reference_scratch, tolerance_f32); } TEST_SUITE_END() // FP32 @@ -171,6 +172,7 @@ FIXTURE_DATA_TEST_CASE(RunSmall, NELSTMLayerFixture, framework::DatasetMod { // Validate output validate(Accessor(_target), _reference, tolerance_f16); + validate(Accessor(_target_scratch), _reference_scratch, tolerance_f16); } TEST_SUITE_END() // FP16 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ diff --git a/tests/validation/fixtures/LSTMLayerFixture.h b/tests/validation/fixtures/LSTMLayerFixture.h index 7f11265932..b0b2a8fae1 100644 --- a/tests/validation/fixtures/LSTMLayerFixture.h +++ b/tests/validation/fixtures/LSTMLayerFixture.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -33,6 +33,7 @@ #include "tests/validation/reference/GEMM.h" #include "tests/validation/reference/PixelWiseMultiplication.h" #include "tests/validation/reference/Transpose.h" +#include "tests/validation/reference/WidthConcatenateLayer.h" namespace arm_compute { @@ -237,7 +238,8 @@ protected: cell_to_output_w.allocator()->allocate(); ARM_COMPUTE_EXPECT(!cell_to_forget_w.info()->is_resizable(), framework::LogLevel::ERRORS); ARM_COMPUTE_EXPECT(!cell_to_output_w.info()->is_resizable(), framework::LogLevel::ERRORS); - fill(AccessorType(cell_to_output_w), 18); + fill(AccessorType(cell_to_forget_w), 18); + fill(AccessorType(cell_to_output_w), 19); } if(projection_opt) @@ -251,13 +253,14 @@ protected: ARM_COMPUTE_EXPECT(!projection_w.info()->is_resizable(), framework::LogLevel::ERRORS); ARM_COMPUTE_EXPECT(!projection_bias.info()->is_resizable(), framework::LogLevel::ERRORS); - fill(AccessorType(projection_w), 19); - fill(AccessorType(projection_bias), 20); + fill(AccessorType(projection_w), 20); + fill(AccessorType(projection_bias), 21); } // Compute function lstm.run(); + _target_scratch = std::move(scratch); return output; } @@ -322,11 +325,12 @@ protected: fill(cell_to_input_w, 15); fill(recurrent_to_input_w, 16); fill(input_gate_bias, 17); - fill(cell_to_output_w, 18); - fill(projection_w, 19); - fill(projection_bias, 20); + fill(cell_to_forget_w, 18); + fill(cell_to_output_w, 19); + fill(projection_w, 20); + fill(projection_bias, 21); - bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? true : false; + bool cifg_opt = scratch_shape.x() == cell_bias_shape.x() * 4 ? false : true; // Compute forget_gate SimpleTensor fully_connected_forget = reference::fully_connected_layer(input, input_to_forget_w, forget_gate_bias, output_cell_shape); @@ -336,7 +340,7 @@ protected: if(peephole_opt) { - SimpleTensor pixelwise_mul_forget_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_forget_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_NEAREST_EVEN); + SimpleTensor pixelwise_mul_forget_gate = reference::pixel_wise_multiplication(cell_state_in, cell_to_forget_w, 1, ConvertPolicy::SATURATE, RoundingPolicy::TO_ZERO); forget_gate = reference::arithmetic_operation(reference::ArithmeticOperation::ADD, forget_gate, pixelwise_mul_forget_gate, data_type, ConvertPolicy::SATURATE); } @@ -402,11 +406,24 @@ protected: output_state_out = reference::activation_layer(fully_connected_projection, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, -projection_threshold, projection_threshold)); } } + + std::vector> scratch_inputs; + if(!cifg_opt) + { + scratch_inputs.emplace_back(std::move(input_gate)); + } + scratch_inputs.emplace_back(std::move(cell_state_out)); + scratch_inputs.emplace_back(std::move(forget_gate)); + scratch_inputs.emplace_back(std::move(output)); + scratch = reference::widthconcatenate_layer(scratch_inputs); + _reference_scratch = std::move(scratch); return output_state_out; } TensorType _target{}; + TensorType _target_scratch{}; SimpleTensor _reference{}; + SimpleTensor _reference_scratch{}; }; } // namespace validation } // namespace test -- cgit v1.2.1