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_ENVIRONMENT_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_ENVIRONMENT_H_
18 
19 #include "tensorflow/lite/delegates/gpu/cl/cl_command_queue.h"
20 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h"
21 #include "tensorflow/lite/delegates/gpu/cl/cl_device.h"
22 #include "tensorflow/lite/delegates/gpu/cl/program_cache.h"
23 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
24 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h"
25 #include "tensorflow/lite/delegates/gpu/common/precision.h"
26 #include "tensorflow/lite/delegates/gpu/common/status.h"
27 #include "tensorflow/lite/delegates/gpu/common/task/tensor_desc.h"
28 #include "tensorflow/lite/delegates/gpu/common/tensor.h"
29 
30 namespace tflite {
31 namespace gpu {
32 namespace cl {
33 
34 class Environment {
35  public:
36   Environment() = default;
37   explicit Environment(CLDevice&& device, CLContext&& context,
38                        CLCommandQueue&& queue,
39                        ProfilingCommandQueue&& profiling_queue);
40   // Move only
41   Environment(Environment&& environment);
42   Environment& operator=(Environment&& environment);
43   Environment(const Environment&) = delete;
44   Environment& operator=(const Environment&) = delete;
45 
device()46   const CLDevice& device() const { return device_; }
GetDevicePtr()47   CLDevice* GetDevicePtr() { return &device_; }
GetDevicePtr()48   const CLDevice* GetDevicePtr() const { return &device_; }
context()49   CLContext& context() { return context_; }
queue()50   CLCommandQueue* queue() { return &queue_; }
profiling_queue()51   ProfilingCommandQueue* profiling_queue() { return &profiling_queue_; }
program_cache()52   ProgramCache* program_cache() { return &program_cache_; }
program_cache()53   const ProgramCache* program_cache() const { return &program_cache_; }
54 
55   std::vector<CalculationsPrecision> GetSupportedPrecisions() const;
56   bool IsSupported(CalculationsPrecision precision) const;
57   std::vector<TensorStorageType> GetSupportedStorages() const;
58   // returns storage types that support zero clamping when reading OOB in HW
59   // (Height/Width) dimensions.
60   std::vector<TensorStorageType> GetSupportedStoragesWithHWZeroClampSupport()
61       const;
62   bool IsSupported(TensorStorageType storage_type) const;
63 
64   absl::Status Init();
65 
66   void SetHighPerformance() const;
67   void SetDefaultPerformance() const;
68   void SetLowPerformance() const;  // for energy saving
69 
70  private:
71   CLDevice device_;
72   CLContext context_;
73   CLCommandQueue queue_;
74   ProfilingCommandQueue profiling_queue_;
75   ProgramCache program_cache_;
76 };
77 
78 TensorStorageType GetFastestStorageType(const GpuInfo& gpu_info);
79 TensorStorageType GetStorageTypeWithMinimalMemoryConsumption(
80     const GpuInfo& gpu_info);
81 
82 absl::Status CreateEnvironment(Environment* result);
83 
84 }  // namespace cl
85 }  // namespace gpu
86 }  // namespace tflite
87 
88 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_CL_ENVIRONMENT_H_
89