blob: a6a990fd3ff341b3d209151a4feb87f20ca552f7 [file] [log] [blame]
Kaizenbf8b01d2017-10-12 14:26:51 +01001/*
2 * Copyright (c) 2017 ARM Limited.
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/graph/nodes/BatchNormalizationLayer.h"
25
26#include "arm_compute/core/Logger.h"
27#include "arm_compute/runtime/CL/CLTensor.h"
28#include "arm_compute/runtime/CL/functions/CLBatchNormalizationLayer.h"
29#include "arm_compute/runtime/NEON/functions/NEBatchNormalizationLayer.h"
30#include "arm_compute/runtime/Tensor.h"
31#include "support/ToolchainSupport.h"
32#include "utils/TypePrinter.h"
33
34using namespace arm_compute::graph;
35
36namespace
37{
38template <typename BatchBatchNormalizationLayer, typename TensorType, TargetHint target_hint>
39std::unique_ptr<arm_compute::IFunction> instantiate_function(ITensor *input, ITensor *output, Tensor &mean, Tensor &var, Tensor &beta, Tensor &gamma, float epsilon)
40{
41 auto norm = arm_compute::support::cpp14::make_unique<BatchBatchNormalizationLayer>();
42 norm->configure(
43 dynamic_cast<TensorType *>(input),
44 dynamic_cast<TensorType *>(output),
45 dynamic_cast<TensorType *>(mean.set_target(target_hint)),
46 dynamic_cast<TensorType *>(var.set_target(target_hint)),
47 dynamic_cast<TensorType *>(beta.set_target(target_hint)),
48 dynamic_cast<TensorType *>(gamma.set_target(target_hint)),
49 epsilon);
50
51 return std::move(norm);
52}
53
54template <TargetHint target_hint>
55std::unique_ptr<arm_compute::IFunction> instantiate(ITensor *input, ITensor *output, Tensor &mean, Tensor &var, Tensor &beta, Tensor &gamma, float epsilon);
56
57template <>
58std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::OPENCL>(ITensor *input, ITensor *output, Tensor &mean, Tensor &var, Tensor &beta, Tensor &gamma, float epsilon)
59{
60 return instantiate_function<arm_compute::CLBatchNormalizationLayer, arm_compute::ICLTensor, TargetHint::OPENCL>(input, output, mean, var, beta, gamma, epsilon);
61}
62
63template <>
64std::unique_ptr<arm_compute::IFunction> instantiate<TargetHint::NEON>(ITensor *input, ITensor *output, Tensor &mean, Tensor &var, Tensor &beta, Tensor &gamma, float epsilon)
65{
66 return instantiate_function<arm_compute::NEBatchNormalizationLayer, arm_compute::ITensor, TargetHint::NEON>(input, output, mean, var, beta, gamma, epsilon);
67}
68} // namespace
69
70std::unique_ptr<arm_compute::IFunction> BatchNormalizationLayer::instantiate_node(GraphContext &ctx, ITensor *input, ITensor *output)
71{
72 std::unique_ptr<arm_compute::IFunction> func;
73 _target_hint = ctx.hints().target_hint();
74
75 unsigned int batch_norm_size = input->info()->dimension(2);
76 if(_mean.tensor() == nullptr)
77 {
78 _mean.set_info(TensorInfo(TensorShape(batch_norm_size), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
79 }
80 if(_var.tensor() == nullptr)
81 {
82 _var.set_info(TensorInfo(TensorShape(batch_norm_size), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
83 }
84 if(_beta.tensor() == nullptr)
85 {
86 _beta.set_info(TensorInfo(TensorShape(batch_norm_size), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
87 }
88 if(_gamma.tensor() == nullptr)
89 {
90 _gamma.set_info(TensorInfo(TensorShape(batch_norm_size), input->info()->num_channels(), input->info()->data_type(), input->info()->fixed_point_position()));
91 }
92
93 if(_target_hint == TargetHint::OPENCL)
94 {
95 func = instantiate<TargetHint::OPENCL>(input, output, _mean, _var, _beta, _gamma, _epsilon);
96 ARM_COMPUTE_LOG("Instantiating CLBatchNormalizationLayer");
97 }
98 else
99 {
100 func = instantiate<TargetHint::NEON>(input, output, _mean, _var, _beta, _gamma, _epsilon);
101 ARM_COMPUTE_LOG("Instantiating NEBatchNormalizationLayer");
102 }
103
104 ARM_COMPUTE_LOG(" Data Type: " << input->info()->data_type()
105 << " Input shape: " << input->info()->tensor_shape()
106 << " Output shape: " << output->info()->tensor_shape()
107 << std::endl);
108
109 return func;
110}