Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Arm Limited. |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 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 | #include "arm_compute/graph/GraphContext.h" |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/graph.h" |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 27 | #include "arm_compute/graph/backends/BackendRegistry.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "arm_compute/graph/Utils.h" |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 29 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 30 | namespace arm_compute |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 31 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 32 | namespace graph |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 33 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | GraphContext::GraphContext() : _config(), _memory_managers(), _weights_managers() |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 38 | GraphContext::~GraphContext() |
| 39 | { |
| 40 | _memory_managers.clear(); |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 41 | _weights_managers.clear(); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 42 | release_default_graph_context(*this); |
| 43 | } |
| 44 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 45 | const GraphConfig &GraphContext::config() const |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 46 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 47 | return _config; |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 50 | void GraphContext::set_config(const GraphConfig &config) |
Georgios Pinitas | ff421f2 | 2017-10-04 16:53:58 +0100 | [diff] [blame] | 51 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 52 | _config = config; |
| 53 | } |
| 54 | |
| 55 | bool GraphContext::insert_memory_management_ctx(MemoryManagerContext &&memory_ctx) |
| 56 | { |
| 57 | Target target = memory_ctx.target; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | if (target == Target::UNSPECIFIED || _memory_managers.find(target) != std::end(_memory_managers)) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 59 | { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | _memory_managers[target] = std::move(memory_ctx); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | MemoryManagerContext *GraphContext::memory_management_ctx(Target target) |
| 68 | { |
| 69 | return (_memory_managers.find(target) != std::end(_memory_managers)) ? &_memory_managers[target] : nullptr; |
| 70 | } |
| 71 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 72 | std::map<Target, MemoryManagerContext> &GraphContext::memory_managers() |
| 73 | { |
| 74 | return _memory_managers; |
| 75 | } |
| 76 | |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 77 | bool GraphContext::insert_weights_management_ctx(WeightsManagerContext &&weights_managers) |
| 78 | { |
| 79 | Target target = weights_managers.target; |
| 80 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 81 | if (_weights_managers.find(target) != std::end(_weights_managers)) |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 82 | { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | _weights_managers[target] = std::move(weights_managers); |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | WeightsManagerContext *GraphContext::weights_management_ctx(Target target) |
| 92 | { |
| 93 | return (_weights_managers.find(target) != std::end(_weights_managers)) ? &_weights_managers[target] : nullptr; |
| 94 | } |
| 95 | |
| 96 | std::map<Target, WeightsManagerContext> &GraphContext::weights_managers() |
| 97 | { |
| 98 | return _weights_managers; |
| 99 | } |
| 100 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 101 | void GraphContext::finalize() |
| 102 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 103 | const size_t num_pools = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | for (auto &mm_obj : _memory_managers) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 105 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_ERROR_ON(!mm_obj.second.allocator); |
| 107 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 108 | // Finalize intra layer memory manager |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | if (mm_obj.second.intra_mm != nullptr) |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 110 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 111 | mm_obj.second.intra_mm->populate(*mm_obj.second.allocator, num_pools); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 112 | } |
| 113 | // Finalize cross layer memory manager |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 114 | if (mm_obj.second.cross_mm != nullptr) |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 115 | { |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 116 | mm_obj.second.cross_mm->populate(*mm_obj.second.allocator, num_pools); |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | } // namespace graph |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 121 | } // namespace arm_compute |