blob: 2f1746e31e5801310c2c984e9ad18fdd0188a589 [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{
32void init_lut_fp16(ActivationLayerInfo::LookupTable65536 *lut)
33{
34 for (uint16_t i = 0; i < lut->size() - 1; ++i)
35 {
36 const float16_t *v = reinterpret_cast<float16_t *>(&i);
37 (*lut)[i] = 1.f / (1.f + std::exp(-*v));
38 }
39 // Final value should be filled outside of loop to avoid overflows.
40 const uint16_t i = lut->size() - 1;
41 const float16_t *v = reinterpret_cast<const float16_t *>(&i);
42 (*lut)[i] = 1.f / (1.f + std::exp(-*v));
43}
44} // namespace
45
46std::shared_ptr<ActivationLayerInfo::LookupTable65536> LUTManager::get_lut_table(LUTInfo info)
47{
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000048 const auto itr = map_fp16.find(info);
49 auto s_ptr = (itr != map_fp16.end()) ? itr->second.lock() : nullptr; // nullptr if invalid or not found.
50 if (s_ptr != nullptr)
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000051 {
52 // Found and valid
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000053 return s_ptr; // Return weak ptr as shared ptr
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000054 }
55 else
56 {
57 // Not found, or pointer not valid
Mohammed Suhail Munshi27dee1e2024-01-10 14:29:46 +000058 // We do not use make_shared to prevent the weak_ptr keeping the control block alive
59 std::shared_ptr<ActivationLayerInfo::LookupTable65536> ptr(new ActivationLayerInfo::LookupTable65536);
Mohammed Suhail Munshi7467ba82023-12-05 14:27:31 +000060 init_lut_fp16(ptr.get());
61 map_fp16[info] = ptr;
62 return ptr;
63 }
64}
65#endif // __aarch64__
66
67// Static function to get LutManager instance
68LUTManager &LUTManager::get_instance()
69{
70 static auto inst_ = std::make_unique<LUTManager>(); // The one, single instance.
71 return *inst_;
72}
73
74} // namespace arm_compute