Repository navigation
Expand file tree
/
Copy pathGraph.Control.cs
More file actions
105 lines (91 loc) · 3.44 KB
/
Copy pathGraph.Control.cs
File metadata and controls
105 lines (91 loc) · 3.44 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tensorflow.Eager;
namespace Tensorflow
{
public partial class Graph
{
public Context _control_flow_context;
private Queue<_ControlDependenciesController> _graph_control_dependencies_stack = new Queue<_ControlDependenciesController>();
public Queue<_ControlDependenciesController> _control_dependencies_stack
{
get
{
return _graph_control_dependencies_stack;
}
set
{
_graph_control_dependencies_stack = value;
}
}
/// <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>
private ITensorOrOperation[] _control_dependencies_for_inputs(ITensorOrOperation[] input_ops)
{
var ret = new ITensorOrOperation[0];
foreach(var controller in _control_dependencies_stack)
{
bool dominated = 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(var op in input_ops)
{
if (controller.op_in_group(op))
{
dominated = true;
break;
}
}
if (!dominated)
ret = controller.control_inputs.Where(x => !input_ops.Contains(x)).ToArray();
}
return ret;
}
public _ControlDependenciesController control_dependencies(ITensorOrOperation[] control_inputs)
{
if (control_inputs == null)
return new _ControlDependenciesController(this, null);
var control_ops = new List<ITensorOrOperation>();
foreach (var c in control_inputs)
{
control_ops.Add(c);
}
return new _ControlDependenciesController(this, control_ops);
}
/// <summary>
/// Returns the current control flow context.
/// </summary>
/// <returns>A context object.</returns>
public Context _get_control_flow_context()
{
return _control_flow_context;
}
/// <summary>
/// Sets the current control flow context.
/// </summary>
/// <param name="ctx">a context object.</param>
public void _set_control_flow_context(Context ctx)
{
_control_flow_context = ctx;
}
public void _push_control_dependencies_controller(_ControlDependenciesController controller)
{
_control_dependencies_stack.Enqueue(controller);
}
public void _pop_control_dependencies_controller(_ControlDependenciesController controller)
{
_control_dependencies_stack.Dequeue();
}
public void _record_op_seen_by_control_dependencies(Operation op)
{
foreach (var controller in _control_dependencies_stack)
controller.add_op(op);
}
}
}