|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Tensorflow.Eager; |
| 6 | + |
| 7 | +namespace Tensorflow |
| 8 | +{ |
| 9 | + public partial class Graph |
| 10 | + { |
| 11 | + public Context _control_flow_context; |
| 12 | + |
| 13 | + private Queue<_ControlDependenciesController> _graph_control_dependencies_stack = new Queue<_ControlDependenciesController>(); |
| 14 | + public Queue<_ControlDependenciesController> _control_dependencies_stack |
| 15 | + { |
| 16 | + get |
| 17 | + { |
| 18 | + return _graph_control_dependencies_stack; |
| 19 | + } |
| 20 | + set |
| 21 | + { |
| 22 | + _graph_control_dependencies_stack = value; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// For an op that takes `input_ops` as inputs, compute control inputs. |
| 28 | + /// </summary> |
| 29 | + /// <param name="input_ops">The data input ops for an op to be created.</param> |
| 30 | + /// <returns>A list of control inputs for the op to be created.</returns> |
| 31 | + private Operation[] _control_dependencies_for_inputs(Operation[] input_ops) |
| 32 | + { |
| 33 | + Operation[] ret = new Operation[0]; |
| 34 | + |
| 35 | + foreach(var controller in _control_dependencies_stack) |
| 36 | + { |
| 37 | + bool dominated = false; |
| 38 | + // If any of the input_ops already depends on the inputs from controller, |
| 39 | + // we say that the new op is dominated (by that input), and we therefore |
| 40 | + // do not need to add control dependencies for this controller's inputs. |
| 41 | + foreach(var op in input_ops) |
| 42 | + { |
| 43 | + if (controller.op_in_group(op)) |
| 44 | + { |
| 45 | + dominated = true; |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (!dominated) |
| 51 | + ret = controller.control_inputs.Where(x => !input_ops.Contains(x)).ToArray(); |
| 52 | + } |
| 53 | + |
| 54 | + return ret; |
| 55 | + } |
| 56 | + |
| 57 | + public _ControlDependenciesController control_dependencies(Operation[] control_inputs) |
| 58 | + { |
| 59 | + if (control_inputs == null) |
| 60 | + return new _ControlDependenciesController(this, null); |
| 61 | + |
| 62 | + var control_ops = new List<Operation>(); |
| 63 | + foreach (var c in control_inputs) |
| 64 | + { |
| 65 | + control_ops.Add(c); |
| 66 | + } |
| 67 | + |
| 68 | + return new _ControlDependenciesController(this, control_ops); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Returns the current control flow context. |
| 73 | + /// </summary> |
| 74 | + /// <returns>A context object.</returns> |
| 75 | + public Context _get_control_flow_context() |
| 76 | + { |
| 77 | + return _control_flow_context; |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Sets the current control flow context. |
| 82 | + /// </summary> |
| 83 | + /// <param name="ctx">a context object.</param> |
| 84 | + public void _set_control_flow_context(Context ctx) |
| 85 | + { |
| 86 | + _control_flow_context = ctx; |
| 87 | + } |
| 88 | + |
| 89 | + public void _push_control_dependencies_controller(_ControlDependenciesController controller) |
| 90 | + { |
| 91 | + _control_dependencies_stack.Enqueue(controller); |
| 92 | + } |
| 93 | + |
| 94 | + public void _pop_control_dependencies_controller(_ControlDependenciesController controller) |
| 95 | + { |
| 96 | + _control_dependencies_stack.Dequeue(); |
| 97 | + } |
| 98 | + |
| 99 | + public void _record_op_seen_by_control_dependencies(Operation op) |
| 100 | + { |
| 101 | + foreach (var controller in _control_dependencies_stack) |
| 102 | + controller.add_op(op); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments