xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/metal/compute_task.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_METAL_COMPUTE_TASK_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMPUTE_TASK_H_
18 
19 #import <Metal/Metal.h>
20 
21 #include <map>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 #include "tensorflow/lite/delegates/gpu/common/precision.h"
27 #include "tensorflow/lite/delegates/gpu/common/shape.h"
28 #include "tensorflow/lite/delegates/gpu/common/status.h"
29 #include "tensorflow/lite/delegates/gpu/common/task/gpu_operation.h"
30 #include "tensorflow/lite/delegates/gpu/common/task/tuning_type.h"
31 #include "tensorflow/lite/delegates/gpu/metal/common.h"
32 #include "tensorflow/lite/delegates/gpu/metal/metal_arguments.h"
33 #include "tensorflow/lite/delegates/gpu/metal/metal_device.h"
34 #include "tensorflow/lite/delegates/gpu/metal/metal_spatial_tensor.h"
35 
36 namespace tflite {
37 namespace gpu {
38 namespace metal {
39 
40 class ComputeTask {
41  public:
42   ComputeTask() = default;
43   ~ComputeTask();
44 
45   // Move only
46   ComputeTask(ComputeTask&& task);
47   ComputeTask& operator=(ComputeTask&& task);
48   ComputeTask(const ComputeTask&) = delete;
49   ComputeTask& operator=(const ComputeTask&) = delete;
50 
51   void Init(std::unique_ptr<GPUOperation>&& operation);
52 
53   const OperationDef& GetDefinition() const;
GetGpuOperation()54   const GPUOperation& GetGpuOperation() const { return *operation_; }
55 
56   absl::Status Compile(MetalDevice* device);
57 
58   // should be called after changes of inputs/outputs.
59   absl::Status UpdateParams();
60 
61   void Encode(id<MTLComputeCommandEncoder> encoder);
62 
63   API_AVAILABLE(ios(13.0), macos(11.00), tvos(13.0))
64   void EncodeToICB(id<MTLIndirectComputeCommand> icb_command);
65   API_AVAILABLE(ios(11.0), macos(10.13), tvos(11.0))
66   void AddResourcesToEncoder(id<MTLComputeCommandEncoder> encoder) const;
67 
68   void Update();
69 
70   void SetSrcTensor(MetalSpatialTensor* tensor, int index);
71 
72   void SetDstTensor(MetalSpatialTensor* tensor, int index);
73 
74   absl::Status Tune(TuningType tuning_type, MetalDevice* device);
75 
GetWorkGroupSize()76   int3 GetWorkGroupSize() const { return operation_->work_group_size_; }
77   void SetWorkGroupSize(const int3& work_group_size);
78 
GetCode()79   const std::string& GetCode() const { return operation_->code_; }
GetDefines()80   const std::map<std::string, std::string>& GetDefines() const {
81     return defines_;
82   }
83 
84   absl::Status Init(MetalDevice* device, const std::string& code,
85                     const std::map<std::string, std::string>& defines);
86   absl::Status RestoreDeserialized(MetalDevice* device);
87 
88  private:
89   absl::Status CompileProgram(
90       MetalDevice* device, const std::string& code,
91       const std::map<std::string, std::string>& defines);
92   void Release();
93 
94   std::unique_ptr<GPUOperation> operation_;
95   id<MTLComputePipelineState> program_ = nullptr;
96   MetalArguments metal_args_;
97 
98   bool use_arguments_buffer_ = false;  // optional
99   bool need_icb_support_ = false;      // optional
100   id<MTLArgumentEncoder> arguments_encoder_ = nullptr;
101   id<MTLBuffer> arg_buffer_ = nullptr;
102 
103   // for serialization
104   std::map<std::string, std::string> defines_;
105 };
106 
107 }  // namespace metal
108 }  // namespace gpu
109 }  // namespace tflite
110 
111 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMPUTE_TASK_H_
112