Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathGraph.Control.cs
More file actions
Latest commit
150 lines (132 loc) · 5.89 KB
/
Copy pathGraph.Control.cs
File metadata and controls
150 lines (132 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
usingSystem.Collections.Generic;
usingSystem.Diagnostics.CodeAnalysis;
usingSystem.Linq;
usingTensorflow.Operations;
usingstaticTensorflow.Binding;
namespaceTensorflow
{
publicpartialclassGraph
{
// Current control flow context. It could be either CondContext or WhileContext
publicControlFlowContext_control_flow_context;
// represents the nested with(...) statements
publicList<_ControlDependenciesController>_control_dependencies_stack{get;set;}=newList<_ControlDependenciesController>();
/// <summary>
/// For an op that takes `input_ops` as inputs, compute control inputs.
/// </summary>
/// <param name="input_ops">The data input ops for an op to be created.</param>
/// <returns>A list of control inputs for the op to be created.</returns>
publicITensorOrOperation[]_control_dependencies_for_inputs(ITensorOrOperation[]input_ops)
{
varret=newList<ITensorOrOperation>();
foreach(varcontrollerin_control_dependencies_stack)
{
booldominated=false;
// If any of the input_ops already depends on the inputs from controller,
// we say that the new op is dominated (by that input), and we therefore
// do not need to add control dependencies for this controller's inputs.
foreach(varopininput_ops)
{
if(controller.op_in_group(op))
{
dominated=true;
break;
}
}
if(!dominated)
ret.AddRange(controller.control_inputs.Where(x =>!input_ops.Contains(x)));
}
returnret.ToArray();
}
/// <summary>
/// Returns a context manager that specifies control dependencies.
///
/// Use with the `with` keyword to specify that all operations constructed
/// within the context should have control dependencies on
/// `control_inputs`.
/// </summary>
[SuppressMessage("ReSharper","CoVariantArrayConversion")]
public_ControlDependenciesControllercontrol_dependencies(ITensorOrOperation[]control_inputs)
=>control_dependencies((object[])control_inputs);
/// <summary>
/// Returns a context manager that specifies control dependencies.
///
/// Use with the `with` keyword to specify that all operations constructed
/// within the context should have control dependencies on
/// `control_inputs`.
/// </summary>
public_ControlDependenciesControllercontrol_dependencies(object[]control_inputs)
{
if(control_inputs==null||tf.Context.executing_eagerly())
returnnew_ControlDependenciesController(this,null);
varcontrol_ops=newList<ITensorOrOperation>();
foreach(varcincontrol_inputs)
{
switch(c)
{
// TODO: implement IndexedSlices
//case IndexedSlices islice:
// control_ops.Add(islice.op);
// break;
caseTensort:
control_ops.Add(t.op);
break;
caseOperationop:
control_ops.Add(op);
break;
default:
vart1=_as_graph_element(c);
if(t1==null)
thrownewTypeError($"Control input must be Operation or Tensor:{c}");
control_ops.Add(t1.op);
break;
}
}
returnnew_ControlDependenciesController(this,control_ops);
}
/// <summary>
/// Returns the current control flow context.
/// </summary>
/// <returns>A context object.</returns>
publicControlFlowContext_get_control_flow_context()
{
return_control_flow_context;
}
/// <summary>
/// Sets the current control flow context.
/// </summary>
/// <param name="ctx">a context object.</param>
publicvoid_set_control_flow_context(ControlFlowContextctx)
{
_control_flow_context=ctx;
}
publicvoid_push_control_dependencies_controller(_ControlDependenciesControllercontroller)
{
_control_dependencies_stack.Add(controller);
}
publicvoid_pop_control_dependencies_controller(_ControlDependenciesControllercontroller)
{
_control_dependencies_stack.RemoveAt(_control_dependencies_stack.Count-1);
}
/// <summary>
/// Record that the given op depends on all registered control dependencies.
/// </summary>
publicvoid_record_op_seen_by_control_dependencies(Operationop)
{
foreach(varcontrollerin_control_dependencies_stack)
controller.add_op(op);
}
}
}