• 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_CL_CL_ARGUMENTS_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_ARGUMENTS_H_
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h"
24 #include "tensorflow/lite/delegates/gpu/cl/gpu_object.h"
25 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
26 #include "tensorflow/lite/delegates/gpu/common/status.h"
27 #include "tensorflow/lite/delegates/gpu/common/task/arguments.h"
28 
29 namespace tflite {
30 namespace gpu {
31 namespace cl {
32 
33 class CLArguments : public ArgumentsBinder {
34  public:
35   CLArguments() = default;
36 
37   absl::Status Init(const GpuInfo& gpu_info,
38                     CLContext* context, Arguments* args, std::string* code);
39   absl::Status Init(const GpuInfo& gpu_info, Arguments* args,
40                     CLContext* context);
41 
42   // Move only
43   CLArguments(CLArguments&& args) = default;
44   CLArguments& operator=(CLArguments&& args) = default;
45   CLArguments(const CLArguments&) = delete;
46   CLArguments& operator=(const CLArguments&) = delete;
47 
48   absl::Status SetInt(const std::string& name, int value) override;
49   absl::Status SetFloat(const std::string& name, float value) override;
50   absl::Status SetHalf(const std::string& name, half value) override;
51   absl::Status SetObjectRef(const std::string& name, const GPUObject* object);
52 
53   absl::Status Bind(cl_kernel kernel, int offset = 0);
54 
55  private:
56   absl::Status AllocateObjects(const Arguments& args, CLContext* context);
57   absl::Status AddObjectArgs(const GpuInfo& gpu_info, const Arguments& args);
58 
59   void CopyArguments(const Arguments& args, bool use_f32_for_halfs);
60   void RenameArgumentsInCode(std::string* code);
61   std::string GetListOfArgs();
62 
63   void AddBuffer(const std::string& name, const GPUBufferDescriptor& desc);
64   void AddImage2D(const std::string& name, const GPUImage2DDescriptor& desc);
65   void AddImage2DArray(const std::string& name,
66                        const GPUImage2DArrayDescriptor& desc);
67   void AddImage3D(const std::string& name, const GPUImage3DDescriptor& desc);
68   void AddImageBuffer(const std::string& name,
69                       const GPUImageBufferDescriptor& desc);
70   void AddCustomMemory(const std::string& name,
71                        const GPUCustomMemoryDescriptor& desc);
72   void AddGPUResources(const std::string& name, const GPUResources& resources);
73   absl::Status SetObjectsResources(const Arguments& args);
74   absl::Status SetGPUResources(const std::string& name,
75                                const GPUResourcesWithValue& resources);
76 
77   absl::Status SetImage2D(const std::string& name, cl_mem memory);
78   absl::Status SetBuffer(const std::string& name, cl_mem memory);
79   absl::Status SetImage2DArray(const std::string& name, cl_mem memory);
80   absl::Status SetImage3D(const std::string& name, cl_mem memory);
81   absl::Status SetImageBuffer(const std::string& name, cl_mem memory);
82   absl::Status SetCustomMemory(const std::string& name, cl_mem memory);
83 
84   static constexpr char kArgsPrefix[] = "args.";
85   struct IntValue {
86     int value;
87 
88     // many arguments generated automatically and not used
89     // to reduce amount of data transferred we adding this optimization
90     bool active = false;
91 
92     // offset to shared storage.
93     uint32_t offset = -1;
94   };
95   std::map<std::string, IntValue> int_values_;
96   std::vector<int32_t> shared_int4s_data_;
97 
98   struct FloatValue {
99     float value;
100 
101     // many arguments generated automatically and not used
102     // to reduce amount of data transferred we adding this optimization
103     bool active = false;
104 
105     // offset to shared storage.
106     uint32_t offset = -1;
107   };
108   std::map<std::string, FloatValue> float_values_;
109   std::vector<float> shared_float4s_data_;
110 
111   struct HalfValue {
112     half value;
113 
114     // many arguments generated automatically and not used
115     // to reduce amount of data transferred we adding this optimization
116     bool active = false;
117 
118     // some devices have issues with half parameters.
119     bool store_as_f32 = false;
120 
121     // offset to shared uniform storage.
122     uint32_t offset = -1;
123   };
124   std::map<std::string, HalfValue> half_values_;
125   std::vector<half> shared_half4s_data_;
126 
127   struct CLBufferDescriptor {
128     GPUBufferDescriptor desc;
129     cl_mem memory;
130   };
131   struct CLImage2DDescriptor {
132     GPUImage2DDescriptor desc;
133     cl_mem memory;
134   };
135   struct CLImage2DArrayDescriptor {
136     GPUImage2DArrayDescriptor desc;
137     cl_mem memory;
138   };
139   struct CLImage3DDescriptor {
140     GPUImage3DDescriptor desc;
141     cl_mem memory;
142   };
143   struct CLImageBufferDescriptor {
144     GPUImageBufferDescriptor desc;
145     cl_mem memory;
146   };
147   struct CLCustomMemoryDescriptor {
148     GPUCustomMemoryDescriptor desc;
149     cl_mem memory;
150   };
151 
152   std::map<std::string, CLBufferDescriptor> buffers_;
153   std::map<std::string, CLImage2DDescriptor> images2d_;
154   std::map<std::string, CLImage2DArrayDescriptor> image2d_arrays_;
155   std::map<std::string, CLImage3DDescriptor> images3d_;
156   std::map<std::string, CLImageBufferDescriptor> image_buffers_;
157   std::map<std::string, CLCustomMemoryDescriptor> custom_memories_;
158 
159   std::map<std::string, GPUObjectDescriptorPtr> object_refs_;
160   std::vector<GPUObjectPtr> objects_;
161 };
162 
163 }  // namespace cl
164 }  // namespace gpu
165 }  // namespace tflite
166 
167 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_ARGUMENTS_H_
168