1 /* Copyright 2015 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_FRAMEWORK_OP_SEGMENT_H_ 17 #define TENSORFLOW_FRAMEWORK_OP_SEGMENT_H_ 18 19 #include <string> 20 #include <unordered_map> 21 22 #include "tensorflow/core/framework/op_kernel.h" 23 #include "tensorflow/core/lib/core/status.h" 24 #include "tensorflow/core/platform/macros.h" 25 #include "tensorflow/core/platform/mutex.h" 26 #include "tensorflow/core/platform/thread_annotations.h" 27 #include "tensorflow/core/platform/types.h" 28 29 namespace tensorflow { 30 31 // OpSegment keeps track of OpKernels registered for sessions running 32 // on a device. 33 // 34 // The implementation maintains a two-level map. The 1st level maps 35 // session handle to the map of registered OpKernels. The 2nd level 36 // map maps node names to instantiated OpKernel objects. 37 // 38 // Each 2-nd level map is reference-counted and the caller can call 39 // AddHold to obtain a reference on all kernels of a session and 40 // ensure these kernels are alive until a corresponding RemoveHold is 41 // called on the same session. 42 class OpSegment { 43 public: 44 OpSegment(); 45 ~OpSegment(); 46 47 // A hold can be placed on a session, preventing all its kernels 48 // from being deleted. 49 void AddHold(const string& session_handle); 50 void RemoveHold(const string& session_handle); 51 52 // If the kernel for "node_name" has been created in the 53 // "session_handle", returns the existing op kernel in "*kernel". 54 // Otherwise, creates the kernel by calling create_fn(), cache it, 55 // and returns it in "*kernel". If create_fn() fails, returns the 56 // error. 57 // 58 // OpSegment keeps the ownership of the returned "*kernel". 59 typedef std::function<Status(OpKernel**)> CreateKernelFn; 60 Status FindOrCreate(const string& session_handle, const string& node_name, 61 OpKernel** kernel, CreateKernelFn create_fn); 62 63 // Returns true if OpSegment should own the kernel. 64 static bool ShouldOwnKernel(FunctionLibraryRuntime* lib, 65 const string& node_op); 66 67 private: 68 // op name -> OpKernel 69 typedef std::unordered_map<string, OpKernel*> KernelMap; 70 struct Item { 71 int num_holds = 1; // Num of holds put on the session. 72 KernelMap name_kernel; // op name -> kernel. 73 ~Item(); 74 }; 75 76 // session handle -> item. 77 // Session handles are produced by strings::FpToString() 78 typedef std::unordered_map<string, Item*> SessionMap; 79 80 mutable mutex mu_; 81 SessionMap sessions_ GUARDED_BY(mu_); 82 83 TF_DISALLOW_COPY_AND_ASSIGN(OpSegment); 84 }; 85 86 } // end namespace tensorflow 87 88 #endif // TENSORFLOW_FRAMEWORK_OP_SEGMENT_H_ 89