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/tensor_type_util.h"
17 
18 namespace tflite {
19 namespace gpu {
20 namespace cl {
21 
ToObjectType(TensorStorageType type)22 ObjectType ToObjectType(TensorStorageType type) {
23   switch (type) {
24     case TensorStorageType::IMAGE_BUFFER:
25     case TensorStorageType::BUFFER:
26       return ObjectType::OPENCL_BUFFER;
27     case TensorStorageType::SINGLE_TEXTURE_2D:
28     case TensorStorageType::TEXTURE_2D:
29     case TensorStorageType::TEXTURE_ARRAY:
30     case TensorStorageType::TEXTURE_3D:
31       return ObjectType::OPENCL_TEXTURE;
32     default:
33       return ObjectType::UNKNOWN;
34   }
35 }
36 
ToDataLayout(TensorStorageType type)37 DataLayout ToDataLayout(TensorStorageType type) {
38   switch (type) {
39     case TensorStorageType::BUFFER:
40       return DataLayout::DHWC4;
41     case TensorStorageType::IMAGE_BUFFER:
42       return DataLayout::DHWC4;
43     case TensorStorageType::SINGLE_TEXTURE_2D:
44       return DataLayout::BHWC;
45     case TensorStorageType::TEXTURE_2D:
46       return DataLayout::HDWC4;
47     case TensorStorageType::TEXTURE_ARRAY:
48       return DataLayout::DHWC4;
49     case TensorStorageType::TEXTURE_3D:
50       return DataLayout::DHWC4;
51     default:
52       return DataLayout::UNKNOWN;
53   }
54 }
55 
ToTensorStorageType(ObjectType object_type,DataLayout data_layout)56 TensorStorageType ToTensorStorageType(ObjectType object_type,
57                                       DataLayout data_layout) {
58   switch (object_type) {
59     case ObjectType::OPENCL_BUFFER:
60       return TensorStorageType::BUFFER;
61     case ObjectType::OPENCL_TEXTURE:
62       switch (data_layout) {
63         case DataLayout::BHWC:
64           return TensorStorageType::SINGLE_TEXTURE_2D;
65         case DataLayout::DHWC4:
66           return TensorStorageType::TEXTURE_ARRAY;
67         case DataLayout::HDWC4:
68           return TensorStorageType::TEXTURE_2D;
69         default:
70           return TensorStorageType::UNKNOWN;
71       }
72     default:
73       return TensorStorageType::UNKNOWN;
74   }
75 }
76 
77 }  // namespace cl
78 }  // namespace gpu
79 }  // namespace tflite
80