blob: 290ed3264807505f59fb5bc4c7d946b8dc01a8f4 [file] [log] [blame]
Gian Marco Iodice781cba72020-06-19 16:56:57 +01001/*
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +00002 * Copyright (c) 2020-2023 Arm Limited.
Gian Marco Iodice781cba72020-06-19 16:56:57 +01003 *
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 Bentham314d3e22023-06-23 10:53:52 +000024#include "src/core/CL/CLUtils.h"
25
SiCongLi1af54162021-10-06 15:25:57 +010026#include "arm_compute/core/CL/CLCompileContext.h"
Jakub Sujak8c49f162023-06-16 09:52:50 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
28#include "arm_compute/core/CL/ICLTensor.h"
Jakub Sujak0d27b2e2023-08-24 14:01:20 +010029#include "arm_compute/core/utils/ActivationFunctionUtils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000030#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "arm_compute/core/Validate.h"
32
SiCongLi1af54162021-10-06 15:25:57 +010033#include "support/StringSupport.h"
Gian Marco Iodice781cba72020-06-19 16:56:57 +010034
SiCongLi1af54162021-10-06 15:25:57 +010035namespace arm_compute
36{
Jakub Sujak8c49f162023-06-16 09:52:50 +010037cl::Image2D create_image2d_from_tensor(const ICLTensor *tensor, CLImage2DType image_type)
Gian Marco Iodice781cba72020-06-19 16:56:57 +010038{
Jakub Sujak8c49f162023-06-16 09:52:50 +010039 ARM_COMPUTE_ERROR_ON_NULLPTR(tensor);
40
41 const cl::Context &ctx = CLKernelLibrary::get().context();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 const cl::Buffer &buffer = tensor->cl_buffer();
Jakub Sujak8c49f162023-06-16 09:52:50 +010043 const ITensorInfo *info = tensor->info();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 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 Sujak8c49f162023-06-16 09:52:50 +010046
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 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 Sujak8c49f162023-06-16 09:52:50 +010051
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010062cl::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 Sujak8c49f162023-06-16 09:52:50 +010068{
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 ARM_COMPUTE_ERROR_ON_MSG(buffer.get() == nullptr, "Cannot create cl_image from empty cl_buffer");
Jakub Sujak8c49f162023-06-16 09:52:50 +010074
Gian Marco Iodice6f931342020-09-15 14:17:41 +010075 cl_channel_type cl_data_type;
76
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 switch (data_type)
Gian Marco Iodice6f931342020-09-15 14:17:41 +010078 {
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 Iodice781cba72020-06-19 16:56:57 +010089 cl_mem cl_image;
90 cl_int err = CL_SUCCESS;
91
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 const cl_image_format format = {CL_RGBA, cl_data_type};
Gian Marco Iodice781cba72020-06-19 16:56:57 +010093
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 switch (image_type)
Gian Marco Iodice3cce35d2022-12-30 16:07:45 +0000103 {
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 Iodice781cba72020-06-19 16:56:57 +0100113
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 Do500e10b2023-09-12 17:49:38 +0100119
120void handle_cl_error(const std::string &function_name, cl_int error_code)
121{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 if (error_code != CL_SUCCESS)
Viet-Hoa Do500e10b2023-09-12 17:49:38 +0100123 {
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 Iodice3cce35d2022-12-30 16:07:45 +0000129} // namespace arm_compute