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_RESOURCE_HANDLE_H_ 17 #define TENSORFLOW_FRAMEWORK_RESOURCE_HANDLE_H_ 18 19 #include "tensorflow/core/platform/types.h" 20 21 namespace tensorflow { 22 23 class ResourceHandleProto; 24 25 // Class representing a handle to a tensorflow resource. Handles are 26 // not valid across executions, but can be serialized back and forth from within 27 // a single run. 28 // 29 // This is the native C++ class equivalent of ResourceHandleProto. They are 30 // separate so that kernels do not need to depend on protos. 31 class ResourceHandle { 32 public: 33 ResourceHandle(); 34 ResourceHandle(const ResourceHandleProto& proto); 35 ~ResourceHandle(); 36 37 // Unique name for the device containing the resource. device()38 const string& device() const { return device_; } set_device(const string & device)39 void set_device(const string& device) { device_ = device; } 40 41 // Container in which this resource is placed. container()42 const string& container() const { return container_; } set_container(const string & container)43 void set_container(const string& container) { container_ = container; } 44 45 // Unique name of this resource. name()46 const string& name() const { return name_; } set_name(const string & name)47 void set_name(const string& name) { name_ = name; } 48 49 // Hash code for the type of the resource. Is only valid in the same device 50 // and in the same execution. hash_code()51 uint64 hash_code() const { return hash_code_; } set_hash_code(uint64 hash_code)52 void set_hash_code(uint64 hash_code) { hash_code_ = hash_code; } 53 54 // For debug-only, the name of the type pointed to by this handle, if 55 // available. maybe_type_name()56 const string& maybe_type_name() const { return maybe_type_name_; } set_maybe_type_name(const string & value)57 void set_maybe_type_name(const string& value) { maybe_type_name_ = value; } 58 59 // Conversion to and from ResourceHandleProto 60 void AsProto(ResourceHandleProto* proto) const; 61 void FromProto(const ResourceHandleProto& proto); 62 63 // Serialization via ResourceHandleProto 64 string SerializeAsString() const; 65 bool ParseFromString(const string& s); 66 67 string DebugString() const; 68 69 public: 70 string device_; 71 string container_; 72 string name_; 73 uint64 hash_code_ = 0; 74 string maybe_type_name_; 75 }; 76 77 // For backwards compatibility for when this was a proto 78 string ProtoDebugString(const ResourceHandle& handle); 79 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_FRAMEWORK_RESOURCE_HANDLE_H_ 83