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