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_OPERATION_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_OPERATION_H_ 18 19 #include <string> 20 #include <vector> 21 22 #include "tensorflow/lite/delegates/gpu/cl/cl_arguments.h" 23 #include "tensorflow/lite/delegates/gpu/cl/cl_command_queue.h" 24 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h" 25 #include "tensorflow/lite/delegates/gpu/cl/cl_device.h" 26 #include "tensorflow/lite/delegates/gpu/cl/cl_kernel.h" 27 #include "tensorflow/lite/delegates/gpu/cl/program_cache.h" 28 #include "tensorflow/lite/delegates/gpu/cl/tensor.h" 29 #include "tensorflow/lite/delegates/gpu/common/task/gpu_operation.h" 30 31 namespace tflite { 32 namespace gpu { 33 namespace cl { 34 35 struct CreationContext { 36 const CLDevice* device; 37 CLContext* context; 38 CLCommandQueue* queue; 39 ProgramCache* cache; 40 GetGpuInfoCreationContext41 const GpuInfo& GetGpuInfo() const { return device->info_; } 42 }; 43 44 class ClOperation { 45 public: 46 ClOperation() = default; 47 virtual ~ClOperation() = default; 48 // Move only 49 ClOperation(ClOperation&& operation); 50 ClOperation& operator=(ClOperation&& operation); 51 ClOperation(const ClOperation&) = delete; 52 ClOperation& operator=(const ClOperation&) = delete; 53 Init(std::unique_ptr<GPUOperation> && gpu_operation)54 void Init(std::unique_ptr<GPUOperation>&& gpu_operation) { 55 operation_ = std::move(gpu_operation); 56 } 57 GetGpuOperation()58 GPUOperation& GetGpuOperation() { return *operation_; } GetGpuOperation()59 const GPUOperation& GetGpuOperation() const { return *operation_; } 60 GetDefinition()61 const OperationDef& GetDefinition() const { return operation_->definition_; } 62 63 absl::Status AddOperation(ClOperation* operation); 64 65 // should be called after changes of inputs/outputs. 66 absl::Status UpdateParams(); 67 AddToQueue(CLCommandQueue * queue)68 absl::Status AddToQueue(CLCommandQueue* queue) { 69 RETURN_IF_ERROR(cl_args_.Bind(kernel_.kernel())); 70 return queue->Dispatch(kernel_, operation_->work_groups_count_, 71 operation_->work_group_size_); 72 } 73 74 absl::Status Tune(TuningType tuning_type, const GpuInfo& gpu_info, 75 ProfilingCommandQueue* profiling_queue); 76 77 absl::Status Compile(const CreationContext& creation_context); 78 79 absl::Status CompileDeserialized(const CreationContext& creation_context); 80 MoveObjectRefsFromCLToGeneric()81 void MoveObjectRefsFromCLToGeneric() { 82 cl_args_.MoveObjectRefsOut(&operation_->args_); 83 } MoveObjectRefsFromGenericToCL()84 void MoveObjectRefsFromGenericToCL() { 85 cl_args_.MoveObjectRefsIn(&operation_->args_); 86 } SyncScalarValues()87 void SyncScalarValues() { cl_args_.CopyScalarValues(&operation_->args_); } 88 89 private: 90 std::unique_ptr<GPUOperation> operation_; 91 CLKernel kernel_; 92 CLArguments cl_args_; 93 }; 94 95 } // namespace cl 96 } // namespace gpu 97 } // namespace tflite 98 99 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_CL_OPERATION_H_ 100