SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 2 | * Copyright (c) 2022-2023 Arm Limited. |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +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 | */ |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 24 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 25 | #include "arm_compute/dynamic_fusion/sketch/gpu/GpuWorkloadContext.h" |
| 26 | #include "arm_compute/core/CL/CLCompileContext.h" |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 27 | #include "src/dynamic_fusion/sketch/gpu/GpuWorkloadContextImpl.h" |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | namespace experimental |
| 32 | { |
| 33 | namespace dynamic_fusion |
| 34 | { |
| 35 | GpuWorkloadContext::GpuWorkloadContext(CLCompileContext *cl_compile_ctx) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 36 | : _impl{ std::make_unique<Impl>(GpuLanguage::OpenCL, cl_compile_ctx) } |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 40 | GpuWorkloadContext::~GpuWorkloadContext() = default; |
| 41 | |
| 42 | GpuWorkloadContext::GpuWorkloadContext(GpuWorkloadContext &&other) = default; |
| 43 | |
| 44 | GpuWorkloadContext &GpuWorkloadContext::operator=(GpuWorkloadContext &&other) = default; |
| 45 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 46 | GpuTarget GpuWorkloadContext::gpu_target() const |
| 47 | { |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 48 | return _impl->cl_compile_context()->get_gpu_target(); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GpuLanguage GpuWorkloadContext::gpu_language() const |
| 52 | { |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 53 | return _impl->gpu_language(); |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | const CLCompileContext *GpuWorkloadContext::cl_compile_context() const |
| 57 | { |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 58 | return _impl->cl_compile_context(); |
| 59 | } |
| 60 | |
| 61 | void GpuWorkloadContext::register_user_tensor(ITensorInfo &tensor_info) |
| 62 | { |
| 63 | _impl->register_user_tensor(tensor_info); |
| 64 | } |
| 65 | |
| 66 | GpuWorkloadContext::Impl &GpuWorkloadContext::implementation() |
| 67 | { |
| 68 | return *_impl; |
| 69 | } |
| 70 | |
| 71 | const GpuWorkloadContext::Impl &GpuWorkloadContext::implementation() const |
| 72 | { |
| 73 | return *_impl; |
| 74 | } |
| 75 | |
| 76 | GpuWorkloadContext::Impl::Impl(GpuLanguage gpu_language, CLCompileContext *cl_compile_ctx) |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 77 | : _gpu_language(gpu_language), _cl_compile_ctx(cl_compile_ctx), _next_tensor_id(1), _mem_map(), _managed_tensor_info() |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 78 | { |
| 79 | } |
| 80 | |
| 81 | GpuLanguage GpuWorkloadContext::Impl::gpu_language() const |
| 82 | { |
| 83 | return _gpu_language; |
| 84 | } |
| 85 | |
| 86 | const CLCompileContext *GpuWorkloadContext::Impl::cl_compile_context() const |
| 87 | { |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 88 | return _cl_compile_ctx; |
| 89 | } |
| 90 | |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 91 | const MemoryDescriptorMap &GpuWorkloadContext::Impl::mem_map() const |
| 92 | { |
| 93 | return _mem_map; |
| 94 | } |
| 95 | |
| 96 | void GpuWorkloadContext::Impl::register_user_tensor(ITensorInfo &tensor_info) |
| 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON(tensor_info.has_valid_id()); |
| 99 | |
| 100 | const auto tensor_id = next_tensor_id(); |
| 101 | |
| 102 | tensor_info.set_id(tensor_id); |
| 103 | _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::User }; |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 104 | // Save a *copy* of the user tensor info in workload context for future reference |
| 105 | // Note that this means if the user modifies the @p tensor_info, the change will not be reflected in the context |
| 106 | _managed_tensor_info.emplace(tensor_info.id(), std::make_unique<TensorInfo>(tensor_info)); |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 107 | } |
| 108 | |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 109 | ITensorInfo *GpuWorkloadContext::Impl::create_virtual_tensor() |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 110 | { |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 111 | auto tensor_info = std::make_unique<TensorInfo>(); |
| 112 | const auto tensor_id = -next_tensor_id(); |
| 113 | tensor_info->set_id(tensor_id); |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 114 | _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::Virtual }; |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame^] | 115 | auto inserted = _managed_tensor_info.emplace(tensor_info->id(), std::move(tensor_info)); |
| 116 | return inserted.first->second.get(); |
| 117 | } |
| 118 | |
| 119 | ITensorInfo *GpuWorkloadContext::Impl::create_auxiliary_tensor(const ITensorInfo &itensor_info) |
| 120 | { |
| 121 | auto tensor_info = std::make_unique<TensorInfo>(itensor_info); |
| 122 | const auto tensor_id = next_tensor_id(); |
| 123 | tensor_info->set_id(tensor_id); |
| 124 | _mem_map[tensor_id] = MemoryDescriptor{ MemoryType::Auxiliary, AuxMemoryInfo{ tensor_info->total_size() } }; |
| 125 | auto inserted = _managed_tensor_info.emplace(tensor_info->id(), std::move(tensor_info)); |
| 126 | return inserted.first->second.get(); |
| 127 | } |
| 128 | |
| 129 | ITensorInfo *GpuWorkloadContext::Impl::get_tensor_info(ITensorInfo::Id id) |
| 130 | { |
| 131 | return _managed_tensor_info.at(id).get(); |
| 132 | } |
| 133 | |
| 134 | const ITensorInfo *GpuWorkloadContext::Impl::get_tensor_info(ITensorInfo::Id id) const |
| 135 | { |
| 136 | return _managed_tensor_info.at(id).get(); |
Viet-Hoa Do | 3fcf3dc | 2023-05-17 15:17:48 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | ITensorInfo::Id GpuWorkloadContext::Impl::next_tensor_id() |
| 140 | { |
| 141 | return _next_tensor_id++; |
| 142 | } |
| 143 | |
SiCong Li | f44bbc5 | 2022-08-29 18:25:51 +0100 | [diff] [blame] | 144 | } // namespace dynamic_fusion |
| 145 | } // namespace experimental |
| 146 | } // namespace arm_compute |