1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2020 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "SoftmaxLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/core/Helpers.h"
27*c217d954SCole Faust #include "arm_compute/core/Types.h"
28*c217d954SCole Faust #include "utils/TypePrinter.h"
29*c217d954SCole Faust
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace test
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace validation
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace reference
37*c217d954SCole Faust {
38*c217d954SCole Faust template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
softmax_layer_generic(const SimpleTensor<T> & src,float beta,int32_t axis,bool is_log)39*c217d954SCole Faust SimpleTensor<T> softmax_layer_generic(const SimpleTensor<T> &src, float beta, int32_t axis, bool is_log)
40*c217d954SCole Faust {
41*c217d954SCole Faust // Create reference
42*c217d954SCole Faust SimpleTensor<T> dst{ src.shape(), src.data_type(), 1 };
43*c217d954SCole Faust
44*c217d954SCole Faust const int32_t n_dims = static_cast<int32_t>(src.shape().num_dimensions());
45*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(axis < -n_dims || axis >= n_dims);
46*c217d954SCole Faust
47*c217d954SCole Faust const unsigned int actual_axis = static_cast<unsigned int>(wrap_around(axis, n_dims));
48*c217d954SCole Faust Window window;
49*c217d954SCole Faust window.use_tensor_dimensions(src.shape());
50*c217d954SCole Faust const unsigned int axis_dimension = src.shape()[actual_axis];
51*c217d954SCole Faust window.set(actual_axis, Window::Dimension(0, 1, 1));
52*c217d954SCole Faust
53*c217d954SCole Faust execute_window_loop(window, [&](const Coordinates & id)
54*c217d954SCole Faust {
55*c217d954SCole Faust // Find max along axis
56*c217d954SCole Faust Coordinates offset(id);
57*c217d954SCole Faust offset.set(actual_axis, 0);
58*c217d954SCole Faust T max = *reinterpret_cast<const T *>(src(offset));
59*c217d954SCole Faust for(unsigned int axis_id = 1; axis_id < axis_dimension; ++axis_id)
60*c217d954SCole Faust {
61*c217d954SCole Faust offset.set(actual_axis, axis_id);
62*c217d954SCole Faust const T val = *reinterpret_cast<const T *>(src(offset));
63*c217d954SCole Faust if(val > max)
64*c217d954SCole Faust {
65*c217d954SCole Faust max = val;
66*c217d954SCole Faust }
67*c217d954SCole Faust }
68*c217d954SCole Faust
69*c217d954SCole Faust // Regularize
70*c217d954SCole Faust T sum(0.f);
71*c217d954SCole Faust for(unsigned int axis_id = 0; axis_id < axis_dimension; ++axis_id)
72*c217d954SCole Faust {
73*c217d954SCole Faust offset.set(actual_axis, axis_id);
74*c217d954SCole Faust const T val = *reinterpret_cast<const T *>(src(offset));
75*c217d954SCole Faust T res{ (val - max) *beta };
76*c217d954SCole Faust if(is_log)
77*c217d954SCole Faust {
78*c217d954SCole Faust sum += std::exp(res);
79*c217d954SCole Faust }
80*c217d954SCole Faust else
81*c217d954SCole Faust {
82*c217d954SCole Faust res = std::exp(res);
83*c217d954SCole Faust sum += res;
84*c217d954SCole Faust }
85*c217d954SCole Faust *reinterpret_cast<T *>(dst(offset)) = res;
86*c217d954SCole Faust }
87*c217d954SCole Faust
88*c217d954SCole Faust // Normalize
89*c217d954SCole Faust for(unsigned int axis_id = 0; axis_id < axis_dimension; ++axis_id)
90*c217d954SCole Faust {
91*c217d954SCole Faust offset.set(actual_axis, axis_id);
92*c217d954SCole Faust const T val = *reinterpret_cast<const T *>(dst(offset));
93*c217d954SCole Faust if(is_log)
94*c217d954SCole Faust {
95*c217d954SCole Faust *reinterpret_cast<T *>(dst(offset)) = val - static_cast<T>(std::log(sum));
96*c217d954SCole Faust }
97*c217d954SCole Faust else
98*c217d954SCole Faust {
99*c217d954SCole Faust *reinterpret_cast<T *>(dst(offset)) = val / sum;
100*c217d954SCole Faust }
101*c217d954SCole Faust }
102*c217d954SCole Faust });
103*c217d954SCole Faust return dst;
104*c217d954SCole Faust }
105*c217d954SCole Faust
106*c217d954SCole Faust template SimpleTensor<float> softmax_layer_generic(const SimpleTensor<float> &src, float beta, int32_t axis, bool is_log);
107*c217d954SCole Faust template SimpleTensor<half> softmax_layer_generic(const SimpleTensor<half> &src, float beta, int32_t axis, bool is_log);
108*c217d954SCole Faust
109*c217d954SCole Faust template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type>
softmax_layer(const SimpleTensor<T> & src,float beta,int32_t axis,bool is_log)110*c217d954SCole Faust SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta, int32_t axis, bool is_log)
111*c217d954SCole Faust {
112*c217d954SCole Faust return softmax_layer_generic<T>(src, beta, axis, is_log);
113*c217d954SCole Faust }
114*c217d954SCole Faust
115*c217d954SCole Faust template < typename T, typename std::enable_if < std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value, int >::type >
softmax_layer(const SimpleTensor<T> & src,float beta,int32_t axis,bool is_log)116*c217d954SCole Faust SimpleTensor<T> softmax_layer(const SimpleTensor<T> &src, float beta, int32_t axis, bool is_log)
117*c217d954SCole Faust {
118*c217d954SCole Faust const QuantizationInfo output_quantization_info = arm_compute::get_softmax_output_quantization_info(src.data_type(), is_log);
119*c217d954SCole Faust
120*c217d954SCole Faust SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
121*c217d954SCole Faust SimpleTensor<float> dst_tmp = softmax_layer<float>(src_tmp, beta, axis, is_log);
122*c217d954SCole Faust SimpleTensor<T> dst = convert_to_asymmetric<T>(dst_tmp, output_quantization_info);
123*c217d954SCole Faust return dst;
124*c217d954SCole Faust }
125*c217d954SCole Faust
126*c217d954SCole Faust template SimpleTensor<float> softmax_layer(const SimpleTensor<float> &src, float beta, int32_t axis, bool is_log);
127*c217d954SCole Faust template SimpleTensor<half> softmax_layer(const SimpleTensor<half> &src, float beta, int32_t axis, bool is_log);
128*c217d954SCole Faust template SimpleTensor<uint8_t> softmax_layer(const SimpleTensor<uint8_t> &src, float beta, int32_t axis, bool is_log);
129*c217d954SCole Faust template SimpleTensor<int8_t> softmax_layer(const SimpleTensor<int8_t> &src, float beta, int32_t axis, bool is_log);
130*c217d954SCole Faust
131*c217d954SCole Faust } // namespace reference
132*c217d954SCole Faust } // namespace validation
133*c217d954SCole Faust } // namespace test
134*c217d954SCole Faust } // namespace arm_compute
135