blob: 06e35eed8c70ec1feccccbf3dd72b4885bb679d5 [file] [log] [blame]
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +00001/*
2 * Copyright (c) 2024 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
25#include "src/core/helpers/LUTManager.h"
26
27namespace arm_compute
28{
29#ifdef __aarch64__
30namespace
31{
Mohammed Suhail Munshi8896cf72024-01-16 15:52:39 +000032
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000033void init_lut_fp16(ActivationLayerInfo::LookupTable65536 *lut)
34{
Mohammed Suhail Munshi8896cf72024-01-16 15:52:39 +000035 union Element
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000036 {
Mohammed Suhail Munshi8896cf72024-01-16 15:52:39 +000037 uint16_t i = 0;
38 float16_t fp;
39 } item;
40 // Fill lut by iterating over all 16 bit values using the union.
41 while (true)
42 {
43 (*lut)[item.i] = 1.f / (1.f + std::exp(-item.fp));
44 if (item.i == 65535)
45 break;
46 item.i++;
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000047 }
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000048}
49} // namespace
50
51std::shared_ptr<ActivationLayerInfo::LookupTable65536> LUTManager::get_lut_table(LUTInfo info)
52{
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000053 const auto itr = map_fp16.find(info);
54 auto s_ptr = (itr != map_fp16.end()) ? itr->second.lock() : nullptr; // nullptr if invalid or not found.
55 if (s_ptr != nullptr)
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000056 {
57 // Found and valid
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000058 return s_ptr; // Return weak ptr as shared ptr
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000059 }
60 else
61 {
62 // Not found, or pointer not valid
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000063 // We do not use make_shared to prevent the weak_ptr keeping the control block alive
64 std::shared_ptr<ActivationLayerInfo::LookupTable65536> ptr(new ActivationLayerInfo::LookupTable65536);
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000065 init_lut_fp16(ptr.get());
66 map_fp16[info] = ptr;
67 return ptr;
68 }
69}
70#endif // __aarch64__
71
72// Static function to get LutManager instance
73LUTManager &LUTManager::get_instance()
74{
75 static auto inst_ = std::make_unique<LUTManager>(); // The one, single instance.
76 return *inst_;
77}
78
79} // namespace arm_compute