xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/cl/util.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/delegates/gpu/cl/util.h"
17 
18 #include <string>
19 
20 #include "absl/status/status.h"
21 #include "absl/strings/str_cat.h"
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 
24 namespace tflite {
25 namespace gpu {
26 namespace cl {
27 
CLErrorCodeToString(cl_int error_code)28 std::string CLErrorCodeToString(cl_int error_code) {
29   switch (error_code) {
30     case CL_SUCCESS:
31       return "Success";
32     case CL_DEVICE_NOT_FOUND:
33       return "Device not found";
34     case CL_DEVICE_NOT_AVAILABLE:
35       return "Device not available";
36     case CL_COMPILER_NOT_AVAILABLE:
37       return "Compiler not available";
38     case CL_MEM_OBJECT_ALLOCATION_FAILURE:
39       return "Memory object allocation failure";
40     case CL_OUT_OF_RESOURCES:
41       return "Out of resources";
42     case CL_OUT_OF_HOST_MEMORY:
43       return "Out of host memory";
44     case CL_PROFILING_INFO_NOT_AVAILABLE:
45       return "Profiling information not available";
46     case CL_MEM_COPY_OVERLAP:
47       return "Memory copy overlap";
48     case CL_IMAGE_FORMAT_MISMATCH:
49       return "Image format mismatch";
50     case CL_IMAGE_FORMAT_NOT_SUPPORTED:
51       return "Image format not supported";
52     case CL_BUILD_PROGRAM_FAILURE:
53       return "Build program failure";
54     case CL_MAP_FAILURE:
55       return "Mapping failure";
56     case CL_MISALIGNED_SUB_BUFFER_OFFSET:
57       return "Misaligned sub-buffer offset";
58     case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
59       return "Execution status error for events in wait list";
60     case CL_COMPILE_PROGRAM_FAILURE:
61       return "Compile program failure";
62     case CL_LINKER_NOT_AVAILABLE:
63       return "Linker not available";
64     case CL_LINK_PROGRAM_FAILURE:
65       return "Link program failure";
66     case CL_DEVICE_PARTITION_FAILED:
67       return "Device partition failed";
68     case CL_KERNEL_ARG_INFO_NOT_AVAILABLE:
69       return "Kernel argument information not available";
70 
71     case CL_INVALID_VALUE:
72       return "Invalid value";
73     case CL_INVALID_DEVICE_TYPE:
74       return "Invalid device type";
75     case CL_INVALID_PLATFORM:
76       return "Invalid platform";
77     case CL_INVALID_DEVICE:
78       return "Invalid device";
79     case CL_INVALID_CONTEXT:
80       return "Invalid context";
81     case CL_INVALID_QUEUE_PROPERTIES:
82       return "Invalid queue properties";
83     case CL_INVALID_COMMAND_QUEUE:
84       return "Invalid command queue";
85     case CL_INVALID_HOST_PTR:
86       return "Invalid host pointer";
87     case CL_INVALID_MEM_OBJECT:
88       return "Invalid memory object";
89     case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
90       return "Invalid image format descriptor";
91     case CL_INVALID_IMAGE_SIZE:
92       return "Invalid image size";
93     case CL_INVALID_SAMPLER:
94       return "Invalid sampler";
95     case CL_INVALID_BINARY:
96       return "Invalid binary";
97     case CL_INVALID_BUILD_OPTIONS:
98       return "Invalid build options";
99     case CL_INVALID_PROGRAM:
100       return "Invalid program";
101     case CL_INVALID_PROGRAM_EXECUTABLE:
102       return "Invalid program executable";
103     case CL_INVALID_KERNEL_NAME:
104       return "Invalid kernel name";
105     case CL_INVALID_KERNEL_DEFINITION:
106       return "Invalid kernel definition";
107     case CL_INVALID_KERNEL:
108       return "Invalid kernel";
109     case CL_INVALID_ARG_INDEX:
110       return "Invalid argument index";
111     case CL_INVALID_ARG_VALUE:
112       return "Invalid argument value";
113     case CL_INVALID_ARG_SIZE:
114       return "Invalid argument size";
115     case CL_INVALID_KERNEL_ARGS:
116       return "Invalid kernel arguments";
117     case CL_INVALID_WORK_DIMENSION:
118       return "Invalid work dimension";
119     case CL_INVALID_WORK_GROUP_SIZE:
120       return "Invalid work group size";
121     case CL_INVALID_WORK_ITEM_SIZE:
122       return "Invalid work item size";
123     case CL_INVALID_GLOBAL_OFFSET:
124       return "Invalid global offset";
125     case CL_INVALID_EVENT_WAIT_LIST:
126       return "Invalid event wait list";
127     case CL_INVALID_EVENT:
128       return "Invalid event";
129     case CL_INVALID_OPERATION:
130       return "Invalid operation";
131     case CL_INVALID_GL_OBJECT:
132       return "Invalid GL object";
133     case CL_INVALID_BUFFER_SIZE:
134       return "Invalid buffer size";
135     case CL_INVALID_MIP_LEVEL:
136       return "Invalid mip-level";
137     case CL_INVALID_GLOBAL_WORK_SIZE:
138       return "Invalid global work size";
139     case CL_INVALID_PROPERTY:
140       return "Invalid property";
141     case CL_INVALID_IMAGE_DESCRIPTOR:
142       return "Invalid image descriptor";
143     case CL_INVALID_COMPILER_OPTIONS:
144       return "Invalid compiler options";
145     case CL_INVALID_LINKER_OPTIONS:
146       return "Invalid linker options";
147     case CL_INVALID_DEVICE_PARTITION_COUNT:
148       return "Invalid device partition count";
149     case CL_INVALID_PIPE_SIZE:
150       return "Invalid pipe size";
151     case CL_INVALID_DEVICE_QUEUE:
152       return "Invalid device queue";
153     case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR:
154       return "Invalid GL sharegroup reference KHR";
155     case CL_INVALID_COMMAND_BUFFER_KHR:
156       return "Invalid command buffer KHR";
157     case CL_INVALID_SYNC_POINT_WAIT_LIST_KHR:
158       return "Invalid sync point wait list KHR";
159     case CL_INCOMPATIBLE_COMMAND_QUEUE_KHR:
160       return "Incompatible command queue KHR";
161 
162     default:
163       return absl::StrCat("Unknown OpenCL error code - ", error_code);
164   }
165 }
166 
ChannelTypeToSizeInBytes(cl_channel_type type)167 int ChannelTypeToSizeInBytes(cl_channel_type type) {
168   switch (type) {
169     case CL_FLOAT:
170       return 4;
171     case CL_HALF_FLOAT:
172       return 2;
173     default:
174       return 0;
175   }
176 }
177 
OpenCLSupported()178 bool OpenCLSupported() { return LoadOpenCL().ok(); }
179 
CreateCLBuffer(cl_context context,int size_in_bytes,bool read_only,void * data,cl_mem * result)180 absl::Status CreateCLBuffer(cl_context context, int size_in_bytes,
181                             bool read_only, void* data, cl_mem* result) {
182   cl_mem_flags flags = read_only ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE;
183   if (data) {
184     flags |= CL_MEM_COPY_HOST_PTR;
185   }
186   cl_int error_code;
187   *result = clCreateBuffer(context, flags, size_in_bytes, data, &error_code);
188   if (!*result) {
189     return absl::UnknownError(
190         absl::StrCat("Failed to allocate device memory (clCreateBuffer): ",
191                      CLErrorCodeToString(error_code)));
192   }
193   return absl::OkStatus();
194 }
195 
CreateCLSubBuffer(cl_context context,cl_mem parent,size_t origin_in_bytes,size_t size_in_bytes,bool read_only,cl_mem * result)196 absl::Status CreateCLSubBuffer(cl_context context, cl_mem parent,
197                                size_t origin_in_bytes, size_t size_in_bytes,
198                                bool read_only, cl_mem* result) {
199   cl_mem_flags flags = read_only ? CL_MEM_READ_ONLY : CL_MEM_READ_WRITE;
200 
201   cl_buffer_region region{};
202   region.origin = origin_in_bytes;
203   region.size = size_in_bytes;
204 
205   cl_int error_code;
206   if (!clCreateSubBuffer) {
207     return absl::InternalError("clCreateSubBuffer is not supported.");
208   }
209   *result = clCreateSubBuffer(parent, flags, CL_BUFFER_CREATE_TYPE_REGION,
210                               &region, &error_code);
211 
212   if (!*result) {
213     return absl::UnknownError(
214         absl::StrCat("Failed to allocate device memory (clCreateSubBuffer): ",
215                      CLErrorCodeToString(error_code)));
216   }
217   return absl::OkStatus();
218 }
219 
CreateRGBAImage2D(cl_context context,int width,int height,cl_channel_type channel_type,void * data,cl_mem * result)220 absl::Status CreateRGBAImage2D(cl_context context, int width, int height,
221                                cl_channel_type channel_type, void* data,
222                                cl_mem* result) {
223   cl_image_desc desc;
224   desc.image_type = CL_MEM_OBJECT_IMAGE2D;
225   desc.image_width = width;
226   desc.image_height = height;
227   desc.image_depth = 0;
228   desc.image_row_pitch = 0;
229   desc.image_slice_pitch = 0;
230   desc.num_mip_levels = 0;
231   desc.num_samples = 0;
232   desc.buffer = nullptr;
233 
234   cl_image_format format;
235   format.image_channel_order = CL_RGBA;
236   format.image_channel_data_type = channel_type;
237 
238   cl_mem_flags flags = CL_MEM_READ_WRITE;
239   if (data) {
240     flags |= CL_MEM_COPY_HOST_PTR;
241   }
242 
243   cl_int error_code;
244   *result =
245       CreateImage2DLegacy(context, flags, &format, &desc, data, &error_code);
246   if (error_code != CL_SUCCESS) {
247     return absl::UnknownError(
248         absl::StrCat("Failed to create 2D texture (clCreateImage): ",
249                      CLErrorCodeToString(error_code)));
250   }
251   return absl::OkStatus();
252 }
253 
254 }  // namespace cl
255 }  // namespace gpu
256 }  // namespace tflite
257