blob: ef2468df59ed5d1e89a1361b5f91113e9f210808 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +01002 * Copyright (c) 2017-2019 ARM Limited.
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01003 *
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 "SoftmaxLayer.h"
25
Georgios Pinitas583137c2017-08-31 18:12:42 +010026#include "arm_compute/core/Types.h"
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010027
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
34namespace reference
35{
36template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010037SimpleTensor<T> softmax_layer_generic(const SimpleTensor<T> &src, float beta, size_t axis, bool is_log)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010038{
39 // Create reference
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010040 SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 };
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010041
giuros01efbf6c82018-09-03 09:53:53 +010042 // Compute reference. Lower dims are the collapsing of the first axis
43 // dimensions (i.e., the flattened dimension of each batch). The upper dims are
44 // instead the batches we want to normalize
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010045
giuros01efbf6c82018-09-03 09:53:53 +010046 int lower_dims = 1;
47 for(size_t i = 0; i < axis; i++)
48 {
49 lower_dims *= src.shape()[i];
50 }
51
52 int upper_dims = 1;
53 for(size_t i = axis; i < TensorShape::num_max_dimensions; i++)
54 {
55 upper_dims *= src.shape()[i];
56 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010057
58 for(int r = 0; r < upper_dims; ++r)
59 {
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010060 const T *src_row_ptr = src.data() + r * lower_dims;
61 T *dst_row_ptr = dst.data() + r * lower_dims;
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010062
63 // Find max
Giuseppe Rossini87e896a2018-08-24 10:24:12 +010064 const T max = *std::max_element(src_row_ptr, src_row_ptr + lower_dims);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010065
66 // Regularize
67 T sum(0.f);
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010068 std::transform(src_row_ptr, src_row_ptr + lower_dims, dst_row_ptr, [&sum, max, beta, is_log](T val)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010069 {
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010070 T res{ (val - max) *beta };
71
72 if(is_log)
73 {
74 sum += std::exp(res);
75 }
76 else
77 {
78 res = std::exp(res);
79 sum += res;
80 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010081 return res;
82 });
83
84 // Normalize
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010085 std::transform(dst_row_ptr, dst_row_ptr + lower_dims, dst_row_ptr, [sum, is_log](T val)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010086 {
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +010087 if(is_log)
88 {
89 return val - sum;
90 }
91 else
92 {
93 return val / sum;
94 }
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010095 });
96 }
97
98 return dst;
99}
100
Sang-Hoon Parkd24affe2019-10-08 18:07:23 +0100101template SimpleTensor<float> softmax_layer_generic(const SimpleTensor<float> &src, float beta, size_t axis, bool is_log);
102template SimpleTensor<half> softmax_layer_generic(const SimpleTensor<half> &src, float beta, size_t axis, bool is_log);
103
104template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
105SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta, size_t axis)
106{
107 return softmax_layer_generic<T>(src, beta, axis, false);
108}
109
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100110template <typename T, typename std::enable_if<std::is_same<T, uint8_t>::value, int>::type>
giuros01efbf6c82018-09-03 09:53:53 +0100111SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta, size_t axis)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100112{
Chunosovf450caa2017-11-08 16:09:35 +0700113 // Note: Output quantization info should always have scale = 1/256 and offset = 0
114 const QuantizationInfo output_quantization_info = QuantizationInfo(1.f / 256, 0);
115
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100116 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
giuros01efbf6c82018-09-03 09:53:53 +0100117 SimpleTensor<float> dst_tmp = softmax_layer<float>(src_tmp, beta, axis);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100118 SimpleTensor<T> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_quantization_info);
Chunosovf450caa2017-11-08 16:09:35 +0700119 return dst;
120}
121
giuros01efbf6c82018-09-03 09:53:53 +0100122template SimpleTensor<float> softmax_layer(const SimpleTensor<float> &src, float beta, size_t axis);
123template SimpleTensor<half> softmax_layer(const SimpleTensor<half> &src, float beta, size_t axis);
124template SimpleTensor<uint8_t> softmax_layer(const SimpleTensor<uint8_t> &src, float beta, size_t axis);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +0100125} // namespace reference
126} // namespace validation
127} // namespace test
128} // namespace arm_compute