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/framework/tensor_shape.h" 20 #include "tensorflow/core/framework/types.pb.h" 21 #include "tensorflow/core/platform/tensor_coding.h" 22 #include "tensorflow/core/platform/types.h" 23 24 namespace tensorflow { 25 26 class ResourceHandleProto; 27 28 // Class representing a handle to a tensorflow resource. Handles are 29 // not valid across executions, but can be serialized back and forth from within 30 // a single run. 31 // 32 // This is the native C++ class equivalent of ResourceHandleProto. They are 33 // separate so that kernels do not need to depend on protos. 34 class ResourceHandle { 35 public: 36 ResourceHandle(); 37 ResourceHandle(const ResourceHandleProto& proto); 38 ~ResourceHandle(); 39 40 // Unique name for the device containing the resource. device()41 const string& device() const { return device_; } set_device(const string & device)42 void set_device(const string& device) { device_ = device; } 43 44 // Container in which this resource is placed. container()45 const string& container() const { return container_; } set_container(const string & container)46 void set_container(const string& container) { container_ = container; } 47 48 // Unique name of this resource. name()49 const string& name() const { return name_; } set_name(const string & name)50 void set_name(const string& name) { name_ = name; } 51 52 // Hash code for the type of the resource. Is only valid in the same device 53 // and in the same execution. hash_code()54 uint64 hash_code() const { return hash_code_; } set_hash_code(uint64 hash_code)55 void set_hash_code(uint64 hash_code) { hash_code_ = hash_code; } 56 57 // For debug-only, the name of the type pointed to by this handle, if 58 // available. maybe_type_name()59 const string& maybe_type_name() const { return maybe_type_name_; } set_maybe_type_name(const string & value)60 void set_maybe_type_name(const string& value) { maybe_type_name_ = value; } 61 62 // Data types and shapes for the underlying resource. dtypes_and_shapes()63 std::vector<DtypeAndPartialTensorShape> dtypes_and_shapes() const { 64 return dtypes_and_shapes_; 65 } set_dtypes_and_shapes(const std::vector<DtypeAndPartialTensorShape> & dtypes_and_shapes)66 void set_dtypes_and_shapes( 67 const std::vector<DtypeAndPartialTensorShape>& dtypes_and_shapes) { 68 dtypes_and_shapes_ = dtypes_and_shapes; 69 } 70 71 // Conversion to and from ResourceHandleProto 72 void AsProto(ResourceHandleProto* proto) const; 73 void FromProto(const ResourceHandleProto& proto); 74 75 // Serialization via ResourceHandleProto 76 string SerializeAsString() const; 77 bool ParseFromString(const string& s); 78 79 string DebugString() const; 80 81 // GUID for anonymous resources. Resources with this shared_name will have 82 // their shared_name replaced with a GUID at creation time 83 static constexpr const char* ANONYMOUS_NAME = 84 "cd2c89b7-88b7-44c8-ad83-06c2a9158347"; 85 86 public: 87 string device_; 88 string container_; 89 string name_; 90 uint64 hash_code_ = 0; 91 string maybe_type_name_; 92 std::vector<DtypeAndPartialTensorShape> dtypes_and_shapes_; 93 }; 94 95 // For backwards compatibility for when this was a proto 96 string ProtoDebugString(const ResourceHandle& handle); 97 98 // Encodes a list of ResourceHandle protos in the given StringListEncoder. 99 void EncodeResourceHandleList(const ResourceHandle* p, int64 n, 100 std::unique_ptr<port::StringListEncoder> e); 101 102 // Decodes a list of ResourceHandle protos from the given StringListDecoder. 103 bool DecodeResourceHandleList(std::unique_ptr<port::StringListDecoder> d, 104 ResourceHandle* ps, int64 n); 105 106 } // namespace tensorflow 107 108 #endif // TENSORFLOW_FRAMEWORK_RESOURCE_HANDLE_H_ 109