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 44 // Move only 45 ComputeTask(ComputeTask&& args) = default; 46 ComputeTask& operator=(ComputeTask&& args) = default; 47 ComputeTask(const ComputeTask&) = delete; 48 ComputeTask& operator=(const ComputeTask&) = delete; 49 50 void Init(std::unique_ptr<GPUOperation>&& operation); 51 52 const OperationDef& GetDefinition() const; 53 bool IsLinkable() const; 54 55 absl::Status AddTask(ComputeTask* task); 56 57 absl::Status Compile(MetalDevice* device); 58 59 // should be called after changes of inputs/outputs. 60 absl::Status UpdateParams(); 61 62 void Encode(id<MTLComputeCommandEncoder> encoder); 63 64 void SetSrcTensor(MetalSpatialTensor* tensor, int index); 65 66 void SetDstTensor(MetalSpatialTensor* tensor, int index); 67 68 absl::Status Tune(TuningType tuning_type, MetalDevice* device); 69 70 private: 71 absl::Status CompileProgram(MetalDevice* device, 72 CalculationsPrecision precision, 73 const std::string& kernel_code); 74 75 std::unique_ptr<GPUOperation> operation_; 76 id<MTLComputePipelineState> program_; 77 MetalArguments metal_args_; 78 }; 79 80 } // namespace metal 81 } // namespace gpu 82 } // namespace tflite 83 84 #endif // TENSORFLOW_LITE_DELEGATES_GPU_METAL_COMPUTE_TASK_H_ 85