SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwDriver.h" |
| 25 | |
| 26 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/IGpuCkwComponentDriver.h" |
| 27 | #include "src/dynamic_fusion/sketch/gpu/components/IGpuKernelComponent.h" |
| 28 | |
| 29 | #include "arm_compute/core/Error.h" |
| 30 | #include "arm_compute/core/Window.h" |
| 31 | #include "src/common/utils/Log.h" |
| 32 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwVariableTable.h" |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 33 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/components/utils/type_converter/Common.h" |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 34 | |
Viet-Hoa Do | ce3c48c | 2023-07-03 13:44:43 +0100 | [diff] [blame] | 35 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwKernelWriter.h" |
| 36 | #include "src/dynamic_fusion/sketch/gpu/ckw_driver/GpuCkwScopedKernelWriter.h" |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 37 | |
| 38 | using namespace ckw; |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace experimental |
| 42 | { |
| 43 | namespace dynamic_fusion |
| 44 | { |
| 45 | GpuCkwDriver::GpuCkwDriver(const GpuKernelComponentGroup &components) |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 46 | : _components{ components }, _kernel{ GpuTargetLanguage::OpenCL }, _code{} |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 47 | { |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 48 | // Generate kernel name |
| 49 | std::string name = ""; |
| 50 | for(auto &comp : _components) |
| 51 | { |
| 52 | auto ckw_driver = comp->ckw_component_driver(); |
| 53 | ARM_COMPUTE_ERROR_ON(ckw_driver == nullptr); |
| 54 | name += ckw_driver->get_name(_components) + "__"; |
| 55 | } |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 56 | |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 57 | // Generate kernel code |
| 58 | _kernel.name(name); |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 59 | GpuCkwKernelWriter root_writer(_kernel); |
Viet-Hoa Do | ce3c48c | 2023-07-03 13:44:43 +0100 | [diff] [blame] | 60 | GpuCkwScopedKernelWriter writer(&root_writer); |
| 61 | GpuCkwVariableTable vtable{}; |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 62 | |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 63 | for(auto &comp : _components) |
| 64 | { |
| 65 | auto ckw_driver = comp->ckw_component_driver(); |
| 66 | ARM_COMPUTE_ERROR_ON(ckw_driver == nullptr); |
| 67 | ckw_driver->write_component_code(_components, vtable, writer); |
| 68 | } |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 69 | _code = root_writer.generate_code(); |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 70 | } |
| 71 | |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 72 | std::string GpuCkwDriver::get_name() |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 73 | { |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 74 | return _kernel.name(); |
| 75 | } |
| 76 | |
| 77 | std::string GpuCkwDriver::get_code() |
| 78 | { |
| 79 | return _code; |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | std::string GpuCkwDriver::get_config_id() |
| 83 | { |
SiCong Li | 16b3752 | 2023-07-18 17:56:49 +0100 | [diff] [blame] | 84 | std::string id = ""; |
| 85 | for(auto &comp : _components) |
| 86 | { |
| 87 | auto ckw_driver = comp->ckw_component_driver(); |
| 88 | ARM_COMPUTE_ERROR_ON(ckw_driver == nullptr); |
| 89 | id = ckw_driver->get_tuner_id(_components) + "__"; |
| 90 | } |
| 91 | return id; |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | Window GpuCkwDriver::get_window() const |
| 95 | { |
| 96 | const auto root_comp = _components.get_root_component(); |
| 97 | ARM_COMPUTE_ERROR_ON_MSG(root_comp == nullptr, "No root component found"); |
| 98 | return root_comp->ckw_component_driver()->get_window(); |
| 99 | } |
| 100 | |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 101 | GpuKernelArgumentList GpuCkwDriver::get_kernel_arguments() |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 102 | { |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 103 | GpuKernelArgumentList args{}; |
| 104 | for(const auto &arg : _kernel.arguments()) |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 105 | { |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 106 | switch(arg.type()) |
| 107 | { |
| 108 | case KernelArgument::Type::TensorStorage: |
| 109 | { |
| 110 | args.emplace_back(static_cast<ITensorInfo::Id>(arg.id()), from_ckw(arg.tensor_storage_type())); |
| 111 | break; |
| 112 | } |
| 113 | case KernelArgument::Type::TensorComponent: |
| 114 | { |
| 115 | args.emplace_back(static_cast<ITensorInfo::Id>(arg.id()), from_ckw(arg.tensor_component_type())); |
| 116 | break; |
| 117 | } |
| 118 | default: |
| 119 | { |
| 120 | ARM_COMPUTE_ERROR("Unsupported KernelArgument Type"); |
| 121 | break; |
| 122 | } |
| 123 | } |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 124 | } |
SiCong Li | 23882a9 | 2023-06-28 09:49:45 +0100 | [diff] [blame] | 125 | return args; |
SiCong Li | 19844f6 | 2023-05-16 16:46:34 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | } // namespace dynamic_fusion |
| 129 | } // namespace experimental |
| 130 | } // namespace arm_compute |