1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/core/graph/graph_node_util.h"
16
17#include <vector>
18
19#include "absl/container/btree_set.h"
20#include "tensorflow/core/framework/node_def.pb.h"
21#include "tensorflow/core/framework/node_def_util.h"
22#include "tensorflow/core/graph/graph.h"
23#include "tensorflow/core/lib/core/errors.h"
24#include "tensorflow/core/lib/core/stringpiece.h"
25
26namespace tensorflow {
27
28string SummarizeNode(const Node& node) { return SummarizeNodeDef(node.def()); }
29
30string FormatNodeForError(const Node& node) {
31 return FormatNodeDefForError(node.def());
32}
33
34Status NameRangesForNode(const Node& node, const OpDef& op_def,
35 NameRangeMap* inputs, NameRangeMap* outputs) {
36 return NameRangesForNode(node.def(), op_def, inputs, outputs);
37}
38
39Status AttachDef(const Status& status, const Node& node,
40 bool allow_multiple_formatted_node) {
41 return AttachDef(status, node.def(), allow_multiple_formatted_node);
42}
43
44absl::btree_set<string> GetMergedNames(const std::vector<string>& from_names,
45 const std::vector<string>& to_names) {
46 absl::btree_set<string> merged_names;
47 merged_names.insert(from_names.begin(), from_names.end());
48 merged_names.insert(to_names.begin(), to_names.end());
49 return merged_names;
50}
51
52void MergeDebugInfo(const NodeDebugInfo& from, Node* to_node) {
53 NodeDebugInfo to = NodeDebugInfo(*to_node);
54 if (!from.original_node_names.empty()) {
55 auto node_names =
56 GetMergedNames(from.original_node_names, to.original_node_names);
57 to_node->set_original_node_names({node_names.begin(), node_names.end()});
58 }
59 if (!from.original_func_names.empty()) {
60 auto func_names =
61 GetMergedNames(from.original_func_names, to.original_func_names);
62 to_node->set_original_func_names({func_names.begin(), func_names.end()});
63 }
64}
65
66void MergeDebugInfo(const NodeDebugInfo& from, NodeDef* to_node_def) {
67 NodeDebugInfo to = NodeDebugInfo(*to_node_def);
68 if (!from.original_node_names.empty()) {
69 auto node_names =
70 GetMergedNames(from.original_node_names, to.original_node_names);
71 to_node_def->mutable_experimental_debug_info()->clear_original_node_names();
72 *to_node_def->mutable_experimental_debug_info()
73 ->mutable_original_node_names() = {node_names.begin(),
74 node_names.end()};
75 }
76 if (!from.original_func_names.empty()) {
77 auto func_names =
78 GetMergedNames(from.original_func_names, to.original_func_names);
79 to_node_def->mutable_experimental_debug_info()->clear_original_func_names();
80 *to_node_def->mutable_experimental_debug_info()
81 ->mutable_original_func_names() = {func_names.begin(),
82 func_names.end()};
83 }
84}
85
86void MergeDebugInfo(const NodeDef& from_node_def, NodeDef* to_node_def) {
87 MergeDebugInfo(NodeDebugInfo(from_node_def), to_node_def);
88}
89} // namespace tensorflow
90