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_CORE_GRAPH_TENSOR_ID_H_ |
17 | #define TENSORFLOW_CORE_GRAPH_TENSOR_ID_H_ |
18 | |
19 | #include <string> |
20 | |
21 | #include "tensorflow/core/graph/graph.h" |
22 | #include "tensorflow/core/lib/core/stringpiece.h" |
23 | #include "tensorflow/core/lib/hash/hash.h" |
24 | #include "tensorflow/core/lib/strings/strcat.h" |
25 | |
26 | namespace tensorflow { |
27 | |
28 | struct SafeTensorId; |
29 | |
30 | // Identifier for a tensor within a step. |
31 | // first == operation_name, second == output_index |
32 | // Note: does not own backing storage for name. |
33 | struct TensorId : public std::pair<StringPiece, int> { |
34 | typedef std::pair<StringPiece, int> Base; |
35 | |
36 | // Inherit the set of constructors. |
37 | using Base::pair; |
38 | |
39 | // NOTE(skyewm): this is required on some platforms. I'm not sure why the |
40 | // using statement above isn't always sufficient. |
41 | TensorId() : Base() {} |
42 | TensorId(const SafeTensorId& id); |
43 | |
44 | const StringPiece node() const { return first; } |
45 | int index() const { return second; } |
46 | |
47 | string ToString() const { |
48 | if (second == Graph::kControlSlot) return strings::StrCat("^" , first); |
49 | return strings::StrCat(first, ":" , second); |
50 | } |
51 | |
52 | struct Hasher { |
53 | public: |
54 | std::size_t operator()(const TensorId& x) const { |
55 | return Hash32(x.first.data(), x.first.size(), x.second); |
56 | } |
57 | }; |
58 | }; |
59 | |
60 | TensorId ParseTensorName(const string& name); |
61 | TensorId ParseTensorName(StringPiece name); |
62 | |
63 | bool IsTensorIdControl(const TensorId& tensor_id); |
64 | |
65 | // Same as TensorId, except owns the backing storage for the op name. This makes |
66 | // the memory management simpler at the expense of a copy. |
67 | struct SafeTensorId : public std::pair<string, int> { |
68 | typedef std::pair<string, int> Base; |
69 | |
70 | // NOTE(skyewm): this is required on some platforms. I'm not sure why the |
71 | // using "using Base::pair;" isn't always sufficient. |
72 | SafeTensorId() : Base() {} |
73 | SafeTensorId(const string& str, int idx) : Base(str, idx) {} |
74 | SafeTensorId(const TensorId& id); |
75 | |
76 | const string& node() const { return first; } |
77 | int index() const { return second; } |
78 | |
79 | string ToString() const { |
80 | if (second == Graph::kControlSlot) return strings::StrCat("^" , first); |
81 | return strings::StrCat(first, ":" , second); |
82 | } |
83 | |
84 | struct Hasher { |
85 | public: |
86 | std::size_t operator()(const TensorId& x) const { |
87 | return Hash32(x.first.data(), x.first.size(), x.second); |
88 | } |
89 | }; |
90 | }; |
91 | |
92 | } // namespace tensorflow |
93 | |
94 | #endif // TENSORFLOW_CORE_GRAPH_TENSOR_ID_H_ |
95 | |