diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2018-09-26 11:25:40 +0100 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:55:45 +0000 |
commit | c93691717a6e7ca67e32b4dedd233b8c63b6daf2 (patch) | |
tree | d3929606b525e89f60299b16f95eb4223d11d5a8 /tests/validation | |
parent | e6dbde0128bf33b5d72a00c480bd92c290fd17b7 (diff) | |
download | ComputeLibrary-c93691717a6e7ca67e32b4dedd233b8c63b6daf2.tar.gz |
COMPMID-1523: Fuse BN node with convolution.
Change-Id: I146936c9e98b343496a4b61cdbadf0eaa38e885a
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/154008
Reviewed-by: Michele DiGiorgio <michele.digiorgio@arm.com>
Reviewed-by: Giuseppe Rossini <giuseppe.rossini@arm.com>
Tested-by: bsgcomp <bsgcomp@arm.com>
Diffstat (limited to 'tests/validation')
3 files changed, 224 insertions, 10 deletions
diff --git a/tests/validation/CL/BatchNormalizationLayer.cpp b/tests/validation/CL/BatchNormalizationLayer.cpp index 0d80ff7eb7..cbf3c7092d 100644 --- a/tests/validation/CL/BatchNormalizationLayer.cpp +++ b/tests/validation/CL/BatchNormalizationLayer.cpp @@ -25,16 +25,20 @@ #include "arm_compute/runtime/CL/CLTensor.h" #include "arm_compute/runtime/CL/CLTensorAllocator.h" #include "arm_compute/runtime/CL/functions/CLBatchNormalizationLayer.h" +#include "arm_compute/runtime/CL/functions/CLConvolutionLayer.h" +#include "arm_compute/runtime/CL/functions/CLFuseBatchNormalization.h" #include "tests/CL/CLAccessor.h" #include "tests/PaddingCalculator.h" +#include "tests/datasets/LargeConvolutionLayerDataset.h" #include "tests/datasets/RandomBatchNormalizationLayerDataset.h" -#include "tests/datasets/ShapeDatasets.h" +#include "tests/datasets/SmallConvolutionLayerDataset.h" #include "tests/framework/Asserts.h" #include "tests/framework/Macros.h" #include "tests/framework/datasets/Datasets.h" #include "tests/validation/Helpers.h" #include "tests/validation/Validation.h" #include "tests/validation/fixtures/BatchNormalizationLayerFixture.h" +#include "tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h" namespace arm_compute { @@ -44,14 +48,20 @@ namespace validation { namespace { -constexpr AbsoluteTolerance<float> tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ -constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ +RelativeTolerance<float> rel_tolerance_f32(0.05f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +constexpr AbsoluteTolerance<float> abs_tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +constexpr AbsoluteTolerance<float> tolerance_f16(0.01f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F16 */ const auto act_infos = framework::dataset::make("ActivationInfo", { ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, 6.f), ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 8.f, 2.f), }); + +const auto common_fusion_dataset = combine(combine(combine(framework::dataset::make("UseBias", { false, true }), + framework::dataset::make("UseBeta", { false, true })), + framework::dataset::make("UseGamma", { false, true })), + framework::dataset::make("Epsilon", { 0.001f })); } // namespace TEST_SUITE(CL) @@ -150,9 +160,9 @@ FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<float>, framewor framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) { // Validate output - validate(CLAccessor(_target), _reference, tolerance_f32, 0); + validate(CLAccessor(_target), _reference, abs_tolerance_f32, 0); } -TEST_SUITE_END() +TEST_SUITE_END() //FP32 TEST_SUITE(FP16) FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework::DatasetMode::PRECOMMIT, combine(combine(combine(combine(datasets::RandomBatchNormalizationLayerDataset(), @@ -165,11 +175,30 @@ FIXTURE_DATA_TEST_CASE(Random, CLBatchNormalizationLayerFixture<half>, framework // Validate output validate(CLAccessor(_target), _reference, tolerance_f16, 0); } -TEST_SUITE_END() -TEST_SUITE_END() +TEST_SUITE_END() // FP16 +TEST_SUITE_END() // Float + +TEST_SUITE_END() // BatchNormalizationLayer + +TEST_SUITE(BatchNormalizationLayerFusion) +template <typename T> +using CLBatchNormalizationLayerFusionFixture = BatchNormalizationLayerFusionValidationFixture<CLTensor, CLAccessor, CLConvolutionLayer, CLFuseBatchNormalization, T>; + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLBatchNormalizationLayerFusionFixture<float>, framework::DatasetMode::PRECOMMIT, + combine(combine(combine(datasets::SmallConvolutionLayerDataset(), common_fusion_dataset), + framework::dataset::make("DataType", DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, rel_tolerance_f32, 0.f, abs_tolerance_f32); +} +TEST_SUITE_END() // FP32 +TEST_SUITE_END() // Float -TEST_SUITE_END() -TEST_SUITE_END() +TEST_SUITE_END() // BatchNormalizationLayerFusion +TEST_SUITE_END() // CL } // namespace validation } // namespace test } // namespace arm_compute diff --git a/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h b/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h new file mode 100644 index 0000000000..39c7d46114 --- /dev/null +++ b/tests/validation/fixtures/BatchNormalizationLayerFusionFixture.h @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE +#define ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/BatchNormalizationLayer.h" +#include "tests/validation/reference/ConvolutionLayer.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, typename AccessorType, typename ConvolutionFunctionType, typename FusionFunctionType, typename T> +class BatchNormalizationLayerFusionValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, Size2D dilation, + bool use_conv_b, bool use_beta, bool use_gamma, float epsilon, DataType dt, DataLayout data_layout) + { + ARM_COMPUTE_UNUSED(dilation); + + _data_type = dt; + _data_layout = data_layout; + _use_conv_b = use_conv_b; + _use_beta = use_beta; + _use_gamma = use_gamma; + + _target = compute_target(src_shape, w_shape, b_shape, dst_shape, info, epsilon); + _reference = compute_reference(src_shape, w_shape, b_shape, dst_shape, info, epsilon); + } + +protected: + template <typename U> + void fill(U &&src, U &&w_tensor, U &&b_tensor, U &&mean_tensor, U &&var_tensor, U &&beta_tensor, U &&gamma_tensor) + { + std::uniform_real_distribution<> distribution(-1.f, 1.f); + std::uniform_real_distribution<> distribution_gz(0, 1.f); + + library->fill(src, distribution, 0); + library->fill(w_tensor, distribution, 1); + library->fill(mean_tensor, distribution, 2); + library->fill(var_tensor, distribution_gz, 3); + _use_conv_b ? library->fill(b_tensor, distribution, 4) : library->fill_tensor_value(b_tensor, 0.f); + _use_beta ? library->fill(beta_tensor, distribution, 5) : library->fill_tensor_value(beta_tensor, 0.f); + _use_gamma ? library->fill(gamma_tensor, distribution, 6) : library->fill_tensor_value(gamma_tensor, 1.f); + } + + TensorType compute_target(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon) + { + if(_data_layout == DataLayout::NHWC) + { + permute(src_shape, PermutationVector(2U, 0U, 1U)); + permute(w_shape, PermutationVector(2U, 0U, 1U)); + permute(dst_shape, PermutationVector(2U, 0U, 1U)); + } + + // Create tensors + TensorType src = create_tensor<TensorType>(src_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType conv_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType conv_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType bn_mean = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType bn_var = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType bn_beta = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType bn_gamma = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType fused_w = create_tensor<TensorType>(w_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType fused_b = create_tensor<TensorType>(b_shape, _data_type, 1, QuantizationInfo(), _data_layout); + TensorType dst = create_tensor<TensorType>(dst_shape, _data_type, 1, QuantizationInfo(), _data_layout); + + // Create and configure function + FusionFunctionType fuse_fn; + ConvolutionFunctionType conv_fn; + TensorType *conv_b_ptr = _use_conv_b ? &conv_b : nullptr; + TensorType *beta_ptr = _use_beta ? &bn_beta : nullptr; + TensorType *gamma_ptr = _use_gamma ? &bn_gamma : nullptr; + fuse_fn.configure(&conv_w, &bn_mean, &bn_var, &fused_w, &fused_b, conv_b_ptr, beta_ptr, gamma_ptr, epsilon); + conv_fn.configure(&src, &fused_w, &fused_b, &dst, info); + + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(conv_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(conv_b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bn_mean.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bn_var.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bn_beta.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(bn_gamma.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(fused_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(fused_b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src.allocator()->allocate(); + conv_w.allocator()->allocate(); + conv_b.allocator()->allocate(); + bn_mean.allocator()->allocate(); + bn_var.allocator()->allocate(); + bn_beta.allocator()->allocate(); + bn_gamma.allocator()->allocate(); + fused_w.allocator()->allocate(); + fused_b.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!conv_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!conv_b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!bn_mean.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!bn_var.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!bn_beta.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!bn_gamma.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!fused_w.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!fused_b.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), + AccessorType(conv_w), AccessorType(conv_b), + AccessorType(bn_mean), AccessorType(bn_var), AccessorType(bn_beta), AccessorType(bn_gamma)); + + // Compute function + fuse_fn.run(); + conv_fn.run(); + + return dst; + } + + SimpleTensor<T> compute_reference(TensorShape src_shape, TensorShape w_shape, TensorShape b_shape, TensorShape dst_shape, PadStrideInfo info, float epsilon) + { + // Create reference + SimpleTensor<T> src{ src_shape, _data_type, 1 }; + SimpleTensor<T> conv_w{ w_shape, _data_type, 1 }; + SimpleTensor<T> conv_b{ b_shape, _data_type, 1 }; + SimpleTensor<T> bn_var{ b_shape, _data_type, 1 }; + SimpleTensor<T> bn_mean{ b_shape, _data_type, 1 }; + SimpleTensor<T> bn_beta{ b_shape, _data_type, 1 }; + SimpleTensor<T> bn_gamma{ b_shape, _data_type, 1 }; + + // Fill reference + fill(src, conv_w, conv_b, bn_mean, bn_var, bn_beta, bn_gamma); + + // Calculate Conv + BN + auto conv_res = reference::convolution_layer(src, conv_w, conv_b, dst_shape, info); + return reference::batch_normalization_layer(conv_res, bn_mean, bn_var, bn_beta, bn_gamma, epsilon, ActivationLayerInfo()); + } + + TensorType _target{}; + SimpleTensor<T> _reference{}; + DataType _data_type{}; + DataLayout _data_layout{}; + bool _use_conv_b{}; + bool _use_beta{}; + bool _use_gamma{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_BATCH_NORMALIZATION_LAYER_FUSION_FIXTURE */ diff --git a/tests/validation/reference/BatchNormalizationLayer.cpp b/tests/validation/reference/BatchNormalizationLayer.cpp index 4ea3769c2c..37713c841d 100644 --- a/tests/validation/reference/BatchNormalizationLayer.cpp +++ b/tests/validation/reference/BatchNormalizationLayer.cpp @@ -77,7 +77,6 @@ template SimpleTensor<float> batch_normalization_layer(const SimpleTensor<float> template SimpleTensor<half> batch_normalization_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &mean, const SimpleTensor<half> &var, const SimpleTensor<half> &beta, const SimpleTensor<half> &gamma, float epsilon, ActivationLayerInfo act_info); - } // namespace reference } // namespace validation } // namespace test |