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