Skip to content

Commit b27e76e

Browse files
Sarkars/merge master to 17 (#160)
1 parent 5ca002f commit b27e76e

32 files changed

+470
-582
lines changed

ngraph_bridge/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ set(SRC
3535
ngraph_assign_clusters.cc
3636
ngraph_builder.cc
3737
ngraph_backend_manager.cc
38-
ngraph_backend_config.cc
3938
ngraph_capture_variables.cc
4039
ngraph_cluster_manager.cc
4140
ngraph_deassign_clusters.cc

ngraph_bridge/enable_variable_ops/ngraph_assign_op.cc

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ class NGraphAssignOp : public OpKernel {
5858
// use_exclusive_lock_, validate_shape_, relax_constraints_;
5959

6060
public:
61-
~NGraphAssignOp() { NGRAPH_VLOG(4) << "~NGraphAssignOp::" << name() << endl; }
61+
~NGraphAssignOp() {
62+
NGRAPH_VLOG(4) << "~NGraphAssignOp::" << name() << endl;
63+
// Delete from Input Variable Shared Name Map
64+
string key = NGraphCatalog::CreateNodeKey(ng_graph_id_, name(), 0);
65+
NGraphCatalog::DeleteFromInputVariableSharedNameMap(key);
66+
}
67+
6268
explicit NGraphAssignOp(OpKernelConstruction* context)
6369
: OpKernel(context), is_tf_just_looking_(false), copy_to_tf_(false) {
6470
OP_REQUIRES_OK(
@@ -124,19 +130,19 @@ class NGraphAssignOp : public OpKernel {
124130
// DO NOT CARE ABOUT SYNCING AS WE ARE ALWAYS SETTING THE NGTENSOR
125131

126132
// Get input[1]
127-
string valkey = to_string(ng_graph_id_) + "_" + def().input(1);
128-
bool valref_exists = NGraphCatalog::ExistsInEncapOutputTensorMap(valkey);
129-
if (valref_exists) {
130-
// Value is from encap
131-
NGRAPH_VLOG(4) << "NGraphAssign::Getting from catalog: " << valkey;
132-
auto ng_val = NGraphCatalog::GetTensorFromEncapOutputTensorMap(valkey);
133-
var->update_ng_tensor(ng_val);
134-
} else {
135-
NGRAPH_VLOG(4) << "NGraphAssign::Getting from TF : " << valkey;
136-
if (var->update_ng_tensor(rhs_tensor)) {
137-
number_of_copies++;
138-
copy_log_str << " COPY_INP_VAL[0]";
139-
}
133+
134+
// input[1] cannot be from NGraphEncap Op
135+
// No way to get input node and check its type
136+
string input_1_name = def().input(1);
137+
OP_REQUIRES(
138+
context, input_1_name.find("ngraph_cluster") == -1,
139+
errors::Internal(
140+
"Caught exception: Input to NGAssign from Encapsulate Op.\n"));
141+
142+
NGRAPH_VLOG(4) << "NGraphAssign:: Updating";
143+
if (var->update_ng_tensor(rhs_tensor)) {
144+
number_of_copies++;
145+
copy_log_str << " COPY_INP_VAL[0]";
140146
}
141147

142148
mutex_lock l(*context->input_ref_mutex(0));

ngraph_bridge/enable_variable_ops/ngraph_catalog.cc

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,36 @@ namespace tensorflow {
2929
namespace ngraph_bridge {
3030

3131
unordered_map<string, string> NGraphCatalog::input_variable_sharedname_map_;
32-
unordered_map<string, shared_ptr<ng::runtime::Tensor>>
33-
NGraphCatalog::encap_output_tensor_map_;
3432
unordered_map<string, unordered_set<int>>
3533
NGraphCatalog::encap_output_copy_indexes_map_;
3634
unordered_map<string, tuple<string, bool, bool>>
3735
NGraphCatalog::encap_output_info_map_;
3836

37+
// Function to create the Node Key
38+
string NGraphCatalog::CreateNodeKey(int graph_id, string node_name, int index) {
39+
if (index == 0) {
40+
return to_string(graph_id) + "_" + node_name;
41+
}
42+
return to_string(graph_id) + "_" + node_name + ":" + to_string(index);
43+
}
44+
3945
// Functions for Encapsulate Output Copy Indexes Map
40-
void NGraphCatalog::AddToEncapOutputCopyIndexesMap(string key,
46+
void NGraphCatalog::AddToEncapOutputCopyIndexesMap(int graphid,
47+
string node_name,
4148
unordered_set<int> val) {
49+
string key = graphid + "_" + node_name;
4250
NGraphCatalog::encap_output_copy_indexes_map_[key] = val;
4351
}
4452

4553
unordered_set<int> NGraphCatalog::GetEncapOutputIndexesThatNeedCopy(
46-
string key) {
54+
int graphid, string node_name) {
55+
string key = graphid + "_" + node_name;
4756
return NGraphCatalog::encap_output_copy_indexes_map_[key];
4857
}
4958

50-
bool NGraphCatalog::EncapOutputIndexNeedsCopy(string key, int index) {
59+
bool NGraphCatalog::EncapOutputIndexNeedsCopy(int graphid, string node_name,
60+
int index) {
61+
string key = graphid + "_" + node_name;
5162
auto itr = NGraphCatalog::encap_output_copy_indexes_map_.find(key);
5263
if (itr != NGraphCatalog::encap_output_copy_indexes_map_.end()) {
5364
auto op_copy_indexes = itr->second;
@@ -57,37 +68,10 @@ bool NGraphCatalog::EncapOutputIndexNeedsCopy(string key, int index) {
5768
return true;
5869
}
5970

60-
string NGraphCatalog::CreateNodeKey(int graph_id, string node_name, int index) {
61-
if (index == 0) {
62-
return to_string(graph_id) + "_" + node_name;
63-
}
64-
return to_string(graph_id) + "_" + node_name + ":" + to_string(index);
65-
}
66-
67-
// Functions for OutputTensorMap
68-
void NGraphCatalog::AddToEncapOutputTensorMap(
69-
string key, shared_ptr<ng::runtime::Tensor> ng_val) {
70-
NGraphCatalog::encap_output_tensor_map_[key] = ng_val;
71-
}
72-
73-
bool NGraphCatalog::ExistsInEncapOutputTensorMap(string key) {
74-
auto itr = NGraphCatalog::encap_output_tensor_map_.find(key);
75-
return itr != NGraphCatalog::encap_output_tensor_map_.end();
76-
}
77-
78-
bool NGraphCatalog::ExistsInEncapOutputTensorMap(int graphid, string node_name,
79-
int input_index) {
80-
return NGraphCatalog::ExistsInEncapOutputTensorMap(
81-
NGraphCatalog::CreateNodeKey(graphid, node_name, input_index));
82-
}
83-
84-
shared_ptr<ng::runtime::Tensor>
85-
NGraphCatalog::GetTensorFromEncapOutputTensorMap(string key) {
86-
return NGraphCatalog::encap_output_tensor_map_[key];
87-
}
88-
89-
void NGraphCatalog::DeleteFromEncapOutputTensorMap(string key) {
90-
NGraphCatalog::encap_output_tensor_map_.erase(key);
71+
void NGraphCatalog::DeleteFromEncapOutputCopyIndexesMap(int graphid,
72+
string node_name) {
73+
string key = graphid + "_" + node_name;
74+
NGraphCatalog::encap_output_copy_indexes_map_.erase(key);
9175
}
9276

9377
// Functions relating Input Variable Shared Name Map
@@ -114,6 +98,10 @@ bool NGraphCatalog::ExistsInInputVariableSharedNameMap(int graphid,
11498
NGraphCatalog::CreateNodeKey(graphid, node_name, input_index));
11599
}
116100

101+
void NGraphCatalog::DeleteFromInputVariableSharedNameMap(string key) {
102+
NGraphCatalog::input_variable_sharedname_map_.erase(key);
103+
}
104+
117105
// Functions for EncapOutputInfo Map
118106
void NGraphCatalog::AddToEncapOutputInfoMap(string key,
119107
tuple<string, bool, bool> val) {

ngraph_bridge/enable_variable_ops/ngraph_catalog.h

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,12 @@ class NGraphCatalog {
5050
// LOCK?
5151
static unordered_map<string, string> input_variable_sharedname_map_;
5252

53-
// Map keeps track of nodes whose input is a tensor computed by NGraph
54-
// For e.g. if the value to be assigned was computed by NGraphEncapsulate Op
55-
// Will be used by Assign/Optimizers
56-
// Map of
57-
// Key
58-
// when op index ==0
59-
// string : GraphId + _ + nodename
60-
// otherwise
61-
// string : GraphId + _ + nodename + : + output_index
62-
// Value : shared_ptr<ng::runtime::Tensor>
63-
static unordered_map<string, shared_ptr<ng::runtime::Tensor>>
64-
encap_output_tensor_map_;
65-
6653
// Map keeps track of output indexes of NGraphEncapsulate Op
6754
// that will be used by TF Nodes or other NGraphEncapsulate Op
6855
// Will be used by NGraphEncapsulateOP
6956
// Map of
7057
// Key
71-
// string : nodename (nGraphEncapsulateOp name)
58+
// string : GraphId + _ + nodename
7259
// Value : Set of indices
7360
static unordered_map<string, unordered_set<int>>
7461
encap_output_copy_indexes_map_;
@@ -91,12 +78,19 @@ class NGraphCatalog {
9178
encap_output_info_map_;
9279

9380
public:
81+
// Utility to create key to query the maps
82+
static string CreateNodeKey(int graph_id, string node_name, int index);
83+
9484
// Utility Functions for the data structures
9585
// Functions for EncapsulateOutputCopyIndexes Map
96-
static void AddToEncapOutputCopyIndexesMap(string key,
86+
static void AddToEncapOutputCopyIndexesMap(int graphid, string node_name,
9787
unordered_set<int> val);
98-
static bool EncapOutputIndexNeedsCopy(string key, int index);
99-
static unordered_set<int> GetEncapOutputIndexesThatNeedCopy(string key);
88+
static bool EncapOutputIndexNeedsCopy(int graphid, string node_name,
89+
int index);
90+
static unordered_set<int> GetEncapOutputIndexesThatNeedCopy(int graphid,
91+
string node_name);
92+
static void DeleteFromEncapOutputCopyIndexesMap(int graphid,
93+
string node_name);
10094

10195
// Functions for InputVariableSharedName Map
10296
static string GetInputVariableSharedName(int graphid, string node_name,
@@ -107,17 +101,7 @@ class NGraphCatalog {
107101
static bool ExistsInInputVariableSharedNameMap(string key);
108102
static bool ExistsInInputVariableSharedNameMap(int graphid, string node_name,
109103
int input_index);
110-
111-
// Functions for EncapOutputTensorMap
112-
static void AddToEncapOutputTensorMap(string key,
113-
shared_ptr<ng::runtime::Tensor> ng_val);
114-
static bool ExistsInEncapOutputTensorMap(string key);
115-
static bool ExistsInEncapOutputTensorMap(int graphid, string node_name,
116-
int input_index);
117-
118-
static shared_ptr<ng::runtime::Tensor> GetTensorFromEncapOutputTensorMap(
119-
string key);
120-
static void DeleteFromEncapOutputTensorMap(string key);
104+
static void DeleteFromInputVariableSharedNameMap(string key);
121105

122106
// Functions for EncapOutputInfo Map
123107
static void AddToEncapOutputInfoMap(string key,
@@ -134,9 +118,6 @@ class NGraphCatalog {
134118
static void DeleteFromEncapOutputInfoMap(string key);
135119
static void ClearEncapOutputInfoMap();
136120
static void PrintEncapOutputInfoMap();
137-
138-
// Utility to create key to query the maps
139-
static string CreateNodeKey(int graph_id, string node_name, int index);
140121
};
141122

142123
} // ngraph_bridge

ngraph_bridge/enable_variable_ops/ngraph_enter_in_catalog.cc

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ Status EnterInCatalog(Graph* graph, int graph_id) {
9898
NGRAPH_VLOG(4) << "Value: " << get<0>(value) << " " << get<1>(value)
9999
<< " " << get<2>(value);
100100
NGraphCatalog::AddToEncapOutputInfoMap(key, value);
101-
// TODO: Uncomment the continue when all the tasks are integrated
102-
// continue;
101+
// This NGraphAssign will be removed subsequently
102+
// so we dont need to fill the rest of the catalog
103+
continue;
103104
}
104105
}
106+
105107
// Update the input variable map
106108
if (IsNGVariableType(node->type_string())) {
107109
string node_key = NGraphCatalog::CreateNodeKey(graph_id, node->name(), 0);
@@ -141,33 +143,10 @@ Status EnterInCatalog(Graph* graph, int graph_id) {
141143
op_index_to_copy.insert(edge->src_output());
142144
}
143145
}
144-
NGraphCatalog::AddToEncapOutputCopyIndexesMap(node->name(),
146+
NGraphCatalog::AddToEncapOutputCopyIndexesMap(graph_id, node->name(),
145147
op_index_to_copy);
146148

147149
} // end of node is type NGraphEncapsulate
148-
149-
// Update the output tensor map
150-
if (IsNGVariableType(node->type_string())) {
151-
for (auto edge : node->in_edges()) {
152-
if (!edge->src()->IsOp() || edge->IsControlEdge() ||
153-
IsRefType(edge->dst()->input_type(edge->dst_input())) ||
154-
edge->src()->type_string() != "NGraphEncapsulate") {
155-
continue;
156-
}
157-
158-
NGRAPH_VLOG(4) << "Get " << node->type_string()
159-
<< " and input is from NGraphEncapsulate";
160-
161-
auto src = edge->src();
162-
int src_output = edge->src_output();
163-
string node_key =
164-
NGraphCatalog::CreateNodeKey(graph_id, src->name(), src_output);
165-
// Will be updated with real tensors in Encapsulate
166-
NGraphCatalog::AddToEncapOutputTensorMap(node_key, nullptr);
167-
NGRAPH_VLOG(4) << "Adding in Output Tensor Map";
168-
NGRAPH_VLOG(4) << "Key: " << node_key;
169-
}
170-
} // end of if node of type NGraphAssign
171150
} // enter in catalog
172151

173152
NGRAPH_VLOG(4) << "Entered in Catalog";

ngraph_bridge/enable_variable_ops/ngraph_enter_in_catalog.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ namespace ngraph_bridge {
5050
// We add mapping of {graphId_nodename_InputIndex : Shared_Name} to the
5151
// InputVariableSharedNameMap
5252
//
53-
// 2. If the output of NGraphEncapsulate Op is an input to NGraphVariableType
54-
// Op, we store this NG-Tensor
55-
// so that it can be directly accessed in compute call of NGraphVariableType.
56-
// We add mapping of {graphId_encapnodename_OutputIndex : NG-Tensor} to the
57-
// EncapOutputTensorMap
53+
// 2. If the input to NGraphAssign Op is from NGraphEncapsulate Op
54+
// We add mapping of
55+
// {graphId_encapnodename_OutputIndex : tuple:{Variable_Shared_Name, CopyToTF,
56+
// IsTFJustLooking}}
57+
// to the EncapOutputInfoMap
58+
// We attach "_ngraph_remove" attribute to this NGraphAssign node
5859
//
5960
// 3. If the output of NGraphEncapsulate Op is not required by a TF Op or
6061
// NGraphEncapsulate Op,

ngraph_bridge/enable_variable_ops/ngraph_remove_ngraphassigns.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Status RemoveNGraphAssigns(Graph* graph) {
4747

4848
// Handle input edges
4949
NGRAPH_VLOG(3) << "Handling input edges ";
50+
vector<const Edge*> remove_edges;
5051
for (auto edge : node->in_edges()) {
5152
// attach incoming control edge to input_1, as that's where update
5253
// will happen
@@ -55,8 +56,8 @@ Status RemoveNGraphAssigns(Graph* graph) {
5556
if (edge->src() == input_1) continue;
5657
graph->AddEdge(edge->src(), edge->src_output(), input_1,
5758
edge->dst_input());
58-
graph->RemoveEdge(edge);
5959
}
60+
remove_edges.push_back(edge);
6061
}
6162

6263
// Handle output edges
@@ -80,6 +81,10 @@ Status RemoveNGraphAssigns(Graph* graph) {
8081
graph->AddEdge(input_1, Graph::kControlSlot, edge->dst(),
8182
Graph::kControlSlot);
8283
}
84+
remove_edges.push_back(edge);
85+
}
86+
87+
for (auto edge : remove_edges) {
8388
graph->RemoveEdge(edge);
8489
}
8590

ngraph_bridge/enable_variable_ops/ngraph_rewrite_pass.cc

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "logging/ngraph_log.h"
2323
#include "logging/tf_graph_writer.h"
2424
#include "ngraph_bridge/enable_variable_ops/ngraph_enter_in_catalog.h"
25+
#include "ngraph_bridge/enable_variable_ops/ngraph_remove_ngraphassigns.h"
2526
#include "ngraph_bridge/enable_variable_ops/ngraph_replace_variable_modifiers.h"
2627
#include "ngraph_bridge/ngraph_api.h"
2728
#include "ngraph_bridge/ngraph_assign_clusters.h"
@@ -206,16 +207,22 @@ class NGraphVariableCapturePass : public NGraphRewritePass {
206207
// 2. Cluster Assignment [ngraph_assign_clusters.cc]
207208
// 3. Cluster Deassignment [ngraph_deassign_clusters.cc]
208209
// 4. Cluster Encapsulation [ngraph_encapsulate_clusters.cc]
209-
//
210+
// 5. Rewrite Variable Type Ops for Tracking [ngraph_rewrite_for_tracking.cc]
211+
// 6. Enter In Catalog [ngraph_enter_in_catalog.cc]
212+
// 7. Remove NGraphAssigns [ngraph_remove_ngraphassigns.cc]
210213
// Between phases, graph dumps (in both .dot and .pbtxt format) may be
211214
// requested by setting the following environment variables:
212215
//
213-
// NGRAPH_TF_DUMP_UNMARKED_GRAPHS=1 dumps graphs before phase 1
214-
// NGRAPH_TF_DUMP_MARKED_GRAPHS=1 dumps graphs after phase 1
215-
// NGRAPH_TF_DUMP_CLUSTERED_GRAPHS=1 dumps graphs after phase 2
216-
// NGRAPH_TF_DUMP_DECLUSTERED_GRAPHS=1 dumps graphs after phase 3
217-
// NGRAPH_TF_DUMP_ENCAPSULATED_GRAPHS=1 dumps graphs after phase 4
218-
// NGRAPH_TF_DUMP_GRAPHS=1 all of the above
216+
// NGRAPH_TF_DUMP_UNMARKED_GRAPHS=1 dumps graphs before phase 0
217+
// NGRAPH_TF_DUMP_REPLACEDMODIFIERS_GRAPHS=1 dumps graphs after phase 0
218+
// NGRAPH_TF_DUMP_MARKED_GRAPHS=1 dumps graphs after phase 1
219+
// NGRAPH_TF_DUMP_CLUSTERED_GRAPHS=1 dumps graphs after phase 2
220+
// NGRAPH_TF_DUMP_DECLUSTERED_GRAPHS=1 dumps graphs after phase 3
221+
// NGRAPH_TF_DUMP_ENCAPSULATED_GRAPHS=1 dumps graphs after phase 4
222+
// NGRAPH_TF_DUMP_TRACKED_GRAPHS=1 dumps graphs after phase 5
223+
// NGRAPH_TF_DUMP_CATALOGED_GRAPHS=1 dumps graphs after phase 6
224+
// NGRAPH_TF_DUMP_REMOVENGASSIGNS_GRAPHS=1 dumps graphs after phase 7
225+
// NGRAPH_TF_DUMP_GRAPHS=1 all of the above
219226
//
220227
class NGraphEncapsulationPass : public NGraphRewritePass {
221228
public:
@@ -323,6 +330,13 @@ class NGraphEncapsulationPass : public NGraphRewritePass {
323330
"Graph with Variables Inputs Entered in Catalog");
324331
}
325332

333+
// Remove Certain NGraphAssigns then.
334+
TF_RETURN_IF_ERROR(RemoveNGraphAssigns(options.graph->get()));
335+
if (DumpRemoveNGraphAssignsGraphs()) {
336+
DumpGraphs(options, idx, "ngraphssigns_optimized",
337+
"Graph with NGraphAssigns Optimized/Removed");
338+
}
339+
326340
return Status::OK();
327341
}
328342

@@ -360,6 +374,11 @@ class NGraphEncapsulationPass : public NGraphRewritePass {
360374
return DumpAllGraphs() ||
361375
std::getenv("NGRAPH_TF_DUMP_CATALOGED_GRAPHS") != nullptr;
362376
}
377+
378+
static bool DumpRemoveNGraphAssignsGraphs() {
379+
return DumpAllGraphs() ||
380+
std::getenv("NGRAPH_TF_DUMP_REMOVENGASSIGNS_GRAPHS") != nullptr;
381+
}
363382
};
364383

365384
} // namespace ngraph_bridge

0 commit comments

Comments
 (0)