blob: aa640ad5e6e228c6ab28b23ef8020638c312e9f4 [file] [log] [blame]
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +01001/*
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +01002 * Copyright (c) 2017-2018 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>
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010037SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta)
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
42 // Compute reference
43 const int cols = src.shape()[0];
44 const int upper_dims = src.num_elements() / cols;
45
46 for(int r = 0; r < upper_dims; ++r)
47 {
48 const T *src_row_ptr = src.data() + r * cols;
49 T *dst_row_ptr = dst.data() + r * cols;
50
51 // Find max
52 const T max = *std::max_element(src_row_ptr, src_row_ptr + cols);
53
54 // Regularize
55 T sum(0.f);
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010056 std::transform(src_row_ptr, src_row_ptr + cols, dst_row_ptr, [&sum, max, beta](T val)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010057 {
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010058 const T res(std::exp((val - max) * beta));
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010059 sum += res;
60 return res;
61 });
62
63 // Normalize
64 std::transform(dst_row_ptr, dst_row_ptr + cols, dst_row_ptr, [sum](T val)
65 {
66 return val / sum;
67 });
68 }
69
70 return dst;
71}
72
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010073template <typename T, typename std::enable_if<std::is_same<T, uint8_t>::value, int>::type>
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010074SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta)
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010075{
Chunosovf450caa2017-11-08 16:09:35 +070076 // Note: Output quantization info should always have scale = 1/256 and offset = 0
77 const QuantizationInfo output_quantization_info = QuantizationInfo(1.f / 256, 0);
78
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010079 SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
80 SimpleTensor<float> dst_tmp = softmax_layer<float>(src_tmp, beta);
81 SimpleTensor<T> dst = convert_to_asymmetric(dst_tmp, output_quantization_info);
Chunosovf450caa2017-11-08 16:09:35 +070082 return dst;
83}
84
Pablo Palmiera2b89ca2017-10-05 15:01:34 +010085template SimpleTensor<float> softmax_layer(const SimpleTensor<float> &src, float beta);
86template SimpleTensor<half> softmax_layer(const SimpleTensor<half> &src, float beta);
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010087template SimpleTensor<uint8_t> softmax_layer(const SimpleTensor<uint8_t> &src, float beta);
Moritz Pflanzerf6ad98a2017-07-21 17:19:58 +010088} // namespace reference
89} // namespace validation
90} // namespace test
91} // namespace arm_compute