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_GL_RUNTIME_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_RUNTIME_H_ 18 19 #include <vector> 20 21 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h" 22 #include "tensorflow/lite/delegates/gpu/common/status.h" 23 #include "tensorflow/lite/delegates/gpu/common/types.h" 24 #include "tensorflow/lite/delegates/gpu/gl/command_queue.h" 25 #include "tensorflow/lite/delegates/gpu/gl/gl_buffer.h" 26 #include "tensorflow/lite/delegates/gpu/gl/gl_program.h" 27 #include "tensorflow/lite/delegates/gpu/gl/gl_shader.h" 28 #include "tensorflow/lite/delegates/gpu/gl/object.h" 29 #include "tensorflow/lite/delegates/gpu/gl/object_manager.h" 30 #include "tensorflow/lite/delegates/gpu/gl/runtime/shared_buffer.h" 31 #include "tensorflow/lite/delegates/gpu/gl/runtime_options.h" 32 #include "tensorflow/lite/delegates/gpu/gl/stats.h" 33 #include "tensorflow/lite/delegates/gpu/gl/variable.h" 34 35 namespace tflite { 36 namespace gpu { 37 namespace gl { 38 39 // Runtime compiles code and executes it once all code is compiled. It creates 40 // intermediate objects and destroys them when runtime is destroyed. 41 class Runtime { 42 public: 43 Runtime(const RuntimeOptions& options, const GpuInfo& gpu_info, 44 CommandQueue* command_queue, const ObjectManager* external_objects); 45 46 // Takes parameters and objects and prepares GL program. 47 absl::Status AddProgram(const GlShader& shader, 48 const std::vector<Variable>& parameters, 49 const std::vector<Object>& objects, 50 const uint3& num_workgroups); 51 52 // Needs to be called once all programs and shaders has been added to runtime. 53 absl::Status PrepareForExecution(); 54 55 // Executes all compiled programs. 56 // TODO(akulik): add more controls over execution. Execution policy? 57 absl::Status Execute(); 58 59 // Gets access to objects created while executing generated code. internal_objects()60 const ObjectManager* internal_objects() const { return &internal_objects_; } 61 command_queue()62 CommandQueue* command_queue() { return command_queue_; } 63 stats()64 RuntimeStats stats() const { 65 RuntimeStats stats; 66 stats.const_objects = const_objects_.stats(); 67 stats.internal_objects = internal_objects_.stats(); 68 if (external_objects_) { 69 stats.external_objects = external_objects_->stats(); 70 } 71 return stats; 72 } 73 74 private: 75 absl::Status AllocateInternalObject(const Object& object); 76 77 absl::Status AllocateConstObject(const Object& object, uint32_t* id); 78 79 // Goes over objects in programs and decides how to allocate them to 80 // minimize total allocated memory. Returns a collection of objects to be 81 // allocated and shared by internal objects. 82 absl::Status AssignInternalObjects(std::vector<Object>* objects); 83 84 const RuntimeOptions options_; 85 const GpuInfo gpu_info_; 86 const ObjectManager* external_objects_; 87 CommandQueue* command_queue_; 88 89 ObjectManager internal_objects_; 90 ObjectManager const_objects_; 91 uint32_t next_const_id_ = 0; // id for const objects 92 93 std::unique_ptr<SharedBufferData> shared_readonly_buffer_; 94 95 using BindFunc = std::function<absl::Status()>; 96 97 // Encapsulates a program and all object to bind before dispatch. 98 struct CompiledProgramDescriptor { 99 GlProgram program; 100 uint3 num_workgroups; 101 102 std::vector<BindFunc> bindings; 103 std::vector<Object> refs; 104 }; 105 106 std::vector<CompiledProgramDescriptor> programs_; 107 }; 108 109 } // namespace gl 110 } // namespace gpu 111 } // namespace tflite 112 113 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_RUNTIME_H_ 114