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