• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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_COMMON_TASK_GPU_OBJECT_DESC_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASK_GPU_OBJECT_DESC_H_
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "tensorflow/lite/delegates/gpu/common/access_type.h"
25 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
26 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
27 #include "tensorflow/lite/delegates/gpu/common/status.h"
28 #include "tensorflow/lite/delegates/gpu/common/task/serialization_base_generated.h"
29 
30 namespace tflite {
31 namespace gpu {
32 
33 struct GPUImage2DDescriptor {
34   DataType data_type;
35   bool normalized = false;   // used with INT data types, if normalized, we read
36                              // in kernel float data.
37   DataType normalized_type;  // can be FLOAT32 or FLOAT16, using with normalized
38                              // = true
39   AccessType access_type;
40 };
41 
42 struct GPUImage3DDescriptor {
43   DataType data_type;
44   AccessType access_type;
45 };
46 
47 struct GPUImage2DArrayDescriptor {
48   DataType data_type;
49   AccessType access_type;
50 };
51 
52 struct GPUImageBufferDescriptor {
53   DataType data_type;
54   AccessType access_type;
55 };
56 
57 struct GPUCustomMemoryDescriptor {
58   std::string type_name;
59 };
60 
61 enum class MemoryType { GLOBAL, CONSTANT, LOCAL };
62 
63 struct GPUBufferDescriptor {
64   DataType data_type;
65   AccessType access_type;
66   int element_size;
67   MemoryType memory_type = MemoryType::GLOBAL;
68   std::vector<std::string> attributes;
69 };
70 
71 struct GPUResources {
72   std::vector<std::string> ints;
73   std::vector<std::string> floats;
74   std::vector<std::pair<std::string, GPUBufferDescriptor>> buffers;
75   std::vector<std::pair<std::string, GPUImage2DDescriptor>> images2d;
76   std::vector<std::pair<std::string, GPUImage2DArrayDescriptor>> image2d_arrays;
77   std::vector<std::pair<std::string, GPUImage3DDescriptor>> images3d;
78   std::vector<std::pair<std::string, GPUImageBufferDescriptor>> image_buffers;
79   std::vector<std::pair<std::string, GPUCustomMemoryDescriptor>>
80       custom_memories;
81 
GetNamesGPUResources82   std::vector<std::string> GetNames() const {
83     std::vector<std::string> names = ints;
84     names.insert(names.end(), floats.begin(), floats.end());
85     for (const auto& obj : buffers) {
86       names.push_back(obj.first);
87     }
88     for (const auto& obj : images2d) {
89       names.push_back(obj.first);
90     }
91     for (const auto& obj : image2d_arrays) {
92       names.push_back(obj.first);
93     }
94     for (const auto& obj : images3d) {
95       names.push_back(obj.first);
96     }
97     for (const auto& obj : image_buffers) {
98       names.push_back(obj.first);
99     }
100     for (const auto& obj : custom_memories) {
101       names.push_back(obj.first);
102     }
103     return names;
104   }
105 
GetReadImagesCountGPUResources106   int GetReadImagesCount() const {
107     int counter = 0;
108     for (const auto& t : images2d) {
109       if (t.second.access_type == tflite::gpu::AccessType::READ) {
110         counter++;
111       }
112     }
113     for (const auto& t : image2d_arrays) {
114       if (t.second.access_type == tflite::gpu::AccessType::READ) {
115         counter++;
116       }
117     }
118     for (const auto& t : images3d) {
119       if (t.second.access_type == tflite::gpu::AccessType::READ) {
120         counter++;
121       }
122     }
123     for (const auto& t : image_buffers) {
124       if (t.second.access_type == tflite::gpu::AccessType::READ) {
125         counter++;
126       }
127     }
128     return counter;
129   }
130 
GetWriteImagesCountGPUResources131   int GetWriteImagesCount() const {
132     int counter = 0;
133     for (const auto& t : images2d) {
134       if (t.second.access_type == tflite::gpu::AccessType::WRITE) {
135         counter++;
136       }
137     }
138     for (const auto& t : image2d_arrays) {
139       if (t.second.access_type == tflite::gpu::AccessType::WRITE) {
140         counter++;
141       }
142     }
143     for (const auto& t : images3d) {
144       if (t.second.access_type == tflite::gpu::AccessType::WRITE) {
145         counter++;
146       }
147     }
148     for (const auto& t : image_buffers) {
149       if (t.second.access_type == tflite::gpu::AccessType::WRITE) {
150         counter++;
151       }
152     }
153     return counter;
154   }
155 };
156 
157 class GPUObjectDescriptor {
158  public:
159   GPUObjectDescriptor() = default;
160   GPUObjectDescriptor(const GPUObjectDescriptor&) = default;
161   GPUObjectDescriptor& operator=(const GPUObjectDescriptor&) = default;
162   GPUObjectDescriptor(GPUObjectDescriptor&& obj_desc) = default;
163   GPUObjectDescriptor& operator=(GPUObjectDescriptor&& obj_desc) = default;
164   virtual ~GPUObjectDescriptor() = default;
165 
SetStateVar(const std::string & key,const std::string & value)166   void SetStateVar(const std::string& key, const std::string& value) const {
167     state_vars_[key] = value;
168   }
169 
PerformConstExpr(const std::string & const_expr,std::string * result)170   virtual absl::Status PerformConstExpr(const std::string& const_expr,
171                                         std::string* result) const {
172     return absl::UnimplementedError(
173         "No implementation of perform const expression");
174   }
175 
PerformSelector(const GpuInfo & gpu_info,const std::string & selector,const std::vector<std::string> & args,const std::vector<std::string> & template_args,std::string * result)176   virtual absl::Status PerformSelector(
177       const GpuInfo& gpu_info, const std::string& selector,
178       const std::vector<std::string>& args,
179       const std::vector<std::string>& template_args,
180       std::string* result) const {
181     return absl::UnimplementedError("No implementation of perform selector");
182   }
GetGPUResources(const GpuInfo & gpu_info)183   virtual GPUResources GetGPUResources(const GpuInfo& gpu_info) const {
184     return GPUResources();
185   }
186 
Release()187   virtual void Release() {}
188 
SetAccess(AccessType access_type)189   void SetAccess(AccessType access_type) { access_type_ = access_type; }
GetAccess()190   AccessType GetAccess() const { return access_type_; }
191 
192  protected:
193   friend flatbuffers::Offset<tflite::gpu::data::GPUObjectDescriptor> Encode(
194       const GPUObjectDescriptor& desc, flatbuffers::FlatBufferBuilder* builder);
195   friend void Decode(const tflite::gpu::data::GPUObjectDescriptor* fb_obj,
196                      GPUObjectDescriptor* obj);
197   mutable std::map<std::string, std::string> state_vars_;
198   AccessType access_type_;
199 };
200 
201 using GPUObjectDescriptorPtr = std::unique_ptr<GPUObjectDescriptor>;
202 
203 }  // namespace gpu
204 }  // namespace tflite
205 
206 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TASK_GPU_OBJECT_DESC_H_
207