You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Graph.Control.cs 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System.Collections.Generic;
  14. using System.Diagnostics.CodeAnalysis;
  15. using System.Linq;
  16. using Tensorflow.Operations;
  17. using static Tensorflow.Binding;
  18. namespace Tensorflow
  19. {
  20. public partial class Graph
  21. {
  22. // Current control flow context. It could be either CondContext or WhileContext
  23. public ControlFlowContext _control_flow_context;
  24. // represents the nested with(...) statements
  25. public List<_ControlDependenciesController> _control_dependencies_stack { get; set; } = new List<_ControlDependenciesController>();
  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. public ITensorOrOperation[] _control_dependencies_for_inputs(ITensorOrOperation[] input_ops)
  32. {
  33. var ret = new List<ITensorOrOperation>();
  34. foreach (var controller in _control_dependencies_stack)
  35. {
  36. bool dominated = false;
  37. // If any of the input_ops already depends on the inputs from controller,
  38. // we say that the new op is dominated (by that input), and we therefore
  39. // do not need to add control dependencies for this controller's inputs.
  40. foreach (var op in input_ops)
  41. {
  42. if (controller.op_in_group(op))
  43. {
  44. dominated = true;
  45. break;
  46. }
  47. }
  48. if (!dominated)
  49. ret.AddRange(controller.control_inputs.Where(x => !input_ops.Contains(x)));
  50. }
  51. return ret.ToArray();
  52. }
  53. /// <summary>
  54. /// Returns a context manager that specifies control dependencies.
  55. ///
  56. /// Use with the `with` keyword to specify that all operations constructed
  57. /// within the context should have control dependencies on
  58. /// `control_inputs`.
  59. /// </summary>
  60. [SuppressMessage("ReSharper", "CoVariantArrayConversion")]
  61. public _ControlDependenciesController control_dependencies(ITensorOrOperation[] control_inputs)
  62. => control_dependencies((object[])control_inputs);
  63. /// <summary>
  64. /// Returns a context manager that specifies control dependencies.
  65. ///
  66. /// Use with the `with` keyword to specify that all operations constructed
  67. /// within the context should have control dependencies on
  68. /// `control_inputs`.
  69. /// </summary>
  70. public _ControlDependenciesController control_dependencies(object[] control_inputs)
  71. {
  72. if (control_inputs == null)
  73. return new _ControlDependenciesController(this, null);
  74. var control_ops = new List<ITensorOrOperation>();
  75. foreach (var c in control_inputs)
  76. {
  77. switch (c)
  78. {
  79. // TODO: implement IndexedSlices
  80. //case IndexedSlices islice:
  81. // control_ops.Add(islice.op);
  82. // break;
  83. case Tensor t:
  84. control_ops.Add(t.op);
  85. break;
  86. case Operation op:
  87. control_ops.Add(op);
  88. break;
  89. default:
  90. var t1 = _as_graph_element(c);
  91. if (t1 == null)
  92. throw new TypeError($"Control input must be Operation or Tensor:{c}");
  93. control_ops.Add(t1.op);
  94. break;
  95. }
  96. }
  97. return new _ControlDependenciesController(this, control_ops);
  98. }
  99. /// <summary>
  100. /// Returns the current control flow context.
  101. /// </summary>
  102. /// <returns>A context object.</returns>
  103. public ControlFlowContext _get_control_flow_context()
  104. {
  105. return _control_flow_context;
  106. }
  107. /// <summary>
  108. /// Sets the current control flow context.
  109. /// </summary>
  110. /// <param name="ctx">a context object.</param>
  111. public void _set_control_flow_context(ControlFlowContext ctx)
  112. {
  113. _control_flow_context = ctx;
  114. }
  115. public void _push_control_dependencies_controller(_ControlDependenciesController controller)
  116. {
  117. _control_dependencies_stack.Add(controller);
  118. }
  119. public void _pop_control_dependencies_controller(_ControlDependenciesController controller)
  120. {
  121. _control_dependencies_stack.RemoveAt(_control_dependencies_stack.Count-1);
  122. }
  123. /// <summary>
  124. /// Record that the given op depends on all registered control dependencies.
  125. /// </summary>
  126. public void _record_op_seen_by_control_dependencies(Operation op)
  127. {
  128. foreach (var controller in _control_dependencies_stack)
  129. controller.add_op(op);
  130. }
  131. }
  132. }