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