xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/cl/util.h (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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_CL_UTIL_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_UTIL_H_
18 
19 #include <string>
20 
21 #include "absl/types/span.h"
22 #include "tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h"
23 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
24 #include "tensorflow/lite/delegates/gpu/common/status.h"
25 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
26 #include "tensorflow/lite/delegates/gpu/common/util.h"
27 
28 namespace tflite {
29 namespace gpu {
30 namespace cl {
31 
32 std::string CLErrorCodeToString(cl_int error_code);
33 
34 int ChannelTypeToSizeInBytes(cl_channel_type type);
35 
36 bool OpenCLSupported();
37 
38 template <DataType S, typename T>
CopyLinearFLT4(const tflite::gpu::Tensor<Linear,S> & src,absl::Span<T> dst)39 void CopyLinearFLT4(const tflite::gpu::Tensor<Linear, S>& src,
40                     absl::Span<T> dst) {
41   const int dst_depth = dst.size();
42   for (int d = 0; d < dst_depth; ++d) {
43     T val;
44     for (int i = 0; i < 4; ++i) {
45       const int dst_ch = d * 4 + i;
46       val[i] = dst_ch >= src.shape.v ? 0.0f : src.data[dst_ch];
47     }
48     dst[d] = val;
49   }
50 }
51 
52 absl::Status CreateCLBuffer(cl_context context, int size_in_bytes,
53                             bool read_only, void* data, cl_mem* result);
54 
55 absl::Status CreateCLSubBuffer(cl_context context, cl_mem parent,
56                                size_t origin_in_bytes, size_t size_in_bytes,
57                                bool read_only, cl_mem* result);
58 
59 absl::Status CreateRGBAImage2D(cl_context context, int width, int height,
60                                cl_channel_type channel_type, void* data,
61                                cl_mem* result);
62 }  // namespace cl
63 }  // namespace gpu
64 }  // namespace tflite
65 
66 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_CL_UTIL_H_
67