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