Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 27 | namespace arm_compute |
| 28 | { |
| 29 | #ifdef __aarch64__ |
| 30 | namespace |
| 31 | { |
Mohammed Suhail Munshi | 8896cf7 | 2024-01-16 15:52:39 +0000 | [diff] [blame^] | 32 | |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 33 | void init_lut_fp16(ActivationLayerInfo::LookupTable65536 *lut) |
| 34 | { |
Mohammed Suhail Munshi | 8896cf7 | 2024-01-16 15:52:39 +0000 | [diff] [blame^] | 35 | union Element |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 36 | { |
Mohammed Suhail Munshi | 8896cf7 | 2024-01-16 15:52:39 +0000 | [diff] [blame^] | 37 | 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 Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 47 | } |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 48 | } |
| 49 | } // namespace |
| 50 | |
| 51 | std::shared_ptr<ActivationLayerInfo::LookupTable65536> LUTManager::get_lut_table(LUTInfo info) |
| 52 | { |
Mohammed Suhail Munshi | 27dee1e | 2024-01-10 14:29:46 +0000 | [diff] [blame] | 53 | 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 Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 56 | { |
| 57 | // Found and valid |
Mohammed Suhail Munshi | 27dee1e | 2024-01-10 14:29:46 +0000 | [diff] [blame] | 58 | return s_ptr; // Return weak ptr as shared ptr |
Mohammed Suhail Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 59 | } |
| 60 | else |
| 61 | { |
| 62 | // Not found, or pointer not valid |
Mohammed Suhail Munshi | 27dee1e | 2024-01-10 14:29:46 +0000 | [diff] [blame] | 63 | // 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 Munshi | 7467ba8 | 2023-12-05 14:27:31 +0000 | [diff] [blame] | 65 | 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 |
| 73 | LUTManager &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 |