Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1 | /* |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2023 Arm Limited. |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +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 | */ |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 24 | #include "src/core/CL/CLUtils.h" |
| 25 | |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLCompileContext.h" |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Jakub Sujak | 0d27b2e | 2023-08-24 14:01:20 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/ActivationFunctionUtils.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 33 | #include "support/StringSupport.h" |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 34 | |
SiCongLi | 1af5416 | 2021-10-06 15:25:57 +0100 | [diff] [blame] | 35 | namespace arm_compute |
| 36 | { |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 37 | cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type) |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 38 | { |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 39 | ARM_COMPUTE_ERROR_ON_NULLPTR(tensor); |
| 40 | |
| 41 | const cl::Context &ctx = CLKernelLibrary::get().context(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 42 | const cl::Buffer &buffer = tensor->cl_buffer(); |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 43 | const ITensorInfo *info = tensor->info(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_ERROR_ON_MSG(info->lock_paddings(), "Tensor paddings must not be locked to allow extending paddings to " |
| 45 | "satisfy cl_image pitch alignment requirement"); |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 46 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | const size_t image_w{info->dimension(0) / 4}; |
| 48 | const size_t image_h{info->tensor_shape().total_size() / info->dimension(0)}; |
| 49 | const size_t max_image_w{CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_WIDTH>()}; |
| 50 | const size_t max_image_h{CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_IMAGE2D_MAX_HEIGHT>()}; |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 51 | |
| 52 | ARM_COMPUTE_UNUSED(max_image_w, max_image_h); |
| 53 | ARM_COMPUTE_ERROR_ON_MSG(image_w > max_image_w, "Image width exceeds maximum width for exporting to cl_image"); |
| 54 | ARM_COMPUTE_ERROR_ON_MSG(image_h > max_image_h, "Image height exceeds maximum height for exporting to cl_image"); |
| 55 | |
| 56 | const TensorShape shape2d(image_w, image_h); |
| 57 | const size_t image_row_pitch = info->strides_in_bytes()[1]; |
| 58 | |
| 59 | return create_image2d_from_buffer(ctx, buffer, shape2d, info->data_type(), image_row_pitch, image_type); |
| 60 | } |
| 61 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | cl::Image2D create_image2d_from_buffer(const cl::Context &ctx, |
| 63 | const cl::Buffer &buffer, |
| 64 | const TensorShape &shape2d, |
| 65 | DataType data_type, |
| 66 | size_t image_row_pitch, |
| 67 | CLImage2DType image_type) |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 68 | { |
| 69 | ARM_COMPUTE_ERROR_ON_MSG(!image2d_from_buffer_supported(CLKernelLibrary::get().get_device()), |
| 70 | "The extension cl_khr_image2d_from_buffer is not supported on the target platform"); |
| 71 | ARM_COMPUTE_ERROR_ON_MSG(get_cl_image_pitch_alignment(CLKernelLibrary::get().get_device()) == 0, |
| 72 | "Impossible to retrieve the cl_image pitch alignment"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_MSG(buffer.get() == nullptr, "Cannot create cl_image from empty cl_buffer"); |
Jakub Sujak | 8c49f16 | 2023-06-16 09:52:50 +0100 | [diff] [blame] | 74 | |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 75 | cl_channel_type cl_data_type; |
| 76 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 77 | switch (data_type) |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 78 | { |
| 79 | case DataType::F32: |
| 80 | cl_data_type = CL_FLOAT; |
| 81 | break; |
| 82 | case DataType::F16: |
| 83 | cl_data_type = CL_HALF_FLOAT; |
| 84 | break; |
| 85 | default: |
| 86 | ARM_COMPUTE_ERROR("Data type not support with OpenCL image2d"); |
| 87 | } |
| 88 | |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 89 | cl_mem cl_image; |
| 90 | cl_int err = CL_SUCCESS; |
| 91 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 92 | const cl_image_format format = {CL_RGBA, cl_data_type}; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 93 | |
| 94 | cl_image_desc desc; |
| 95 | memset(&desc, 0, sizeof(desc)); |
| 96 | desc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 97 | desc.mem_object = buffer(); |
| 98 | desc.image_row_pitch = image_row_pitch; |
| 99 | desc.image_width = shape2d[0]; |
| 100 | desc.image_height = shape2d[1]; |
| 101 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 102 | switch (image_type) |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 103 | { |
| 104 | case CLImage2DType::ReadOnly: |
| 105 | cl_image = clCreateImage(ctx(), CL_MEM_READ_ONLY, &format, &desc, nullptr, &err); |
| 106 | break; |
| 107 | case CLImage2DType::WriteOnly: |
| 108 | cl_image = clCreateImage(ctx(), CL_MEM_WRITE_ONLY, &format, &desc, nullptr, &err); |
| 109 | break; |
| 110 | default: |
| 111 | ARM_COMPUTE_ERROR("Unsupported CLImage2DType"); |
| 112 | } |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 113 | |
| 114 | ARM_COMPUTE_UNUSED(err); |
| 115 | ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Error during the creation of CL image from buffer"); |
| 116 | |
| 117 | return cl::Image2D(cl_image); |
| 118 | } |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 119 | |
| 120 | void handle_cl_error(const std::string &function_name, cl_int error_code) |
| 121 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | if (error_code != CL_SUCCESS) |
Viet-Hoa Do | 500e10b | 2023-09-12 17:49:38 +0100 | [diff] [blame] | 123 | { |
| 124 | std::string error_message = function_name + " - Error code: " + std::to_string(error_code); |
| 125 | ARM_COMPUTE_ERROR(error_message.c_str()); |
| 126 | } |
| 127 | } |
| 128 | |
Gian Marco Iodice | 3cce35d | 2022-12-30 16:07:45 +0000 | [diff] [blame] | 129 | } // namespace arm_compute |