|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Linq; |
| 5 | +using static Tensorflow.Binding; |
| 6 | + |
| 7 | +namespace Tensorflow.Graphs |
| 8 | +{ |
| 9 | + public class SubGraphUtility |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Copies the tensor and all its inputs recursively to the outer graph. |
| 13 | + /// </summary> |
| 14 | + /// <param name="tensors"></param> |
| 15 | + /// <param name="graph"></param> |
| 16 | + /// <param name="add_sources"></param> |
| 17 | + /// <param name="handle_captures"></param> |
| 18 | + /// <param name="base_graph"></param> |
| 19 | + /// <returns></returns> |
| 20 | + public static Dictionary<ITensorOrOperation, Operation> lift_to_graph(Tensors init_tensors, |
| 21 | + FuncGraph graph, |
| 22 | + List<Tensor> sources, |
| 23 | + bool add_sources = false, |
| 24 | + bool handle_captures = false, |
| 25 | + Graph base_graph = null, |
| 26 | + Dictionary<ITensorOrOperation, Operation> op_map = null) |
| 27 | + { |
| 28 | + base_graph = base_graph ?? init_tensors[0].graph; |
| 29 | + op_map = op_map ?? new Dictionary<ITensorOrOperation, Operation>(); |
| 30 | + var visited_ops = sources.Select(x => x.op).ToList(); |
| 31 | + foreach (var init_tensor in init_tensors) |
| 32 | + { |
| 33 | + var src = map_subgraph(init_tensor, sources, visited_ops, add_sources); |
| 34 | + sources.AddRange(src); |
| 35 | + } |
| 36 | + |
| 37 | + var ops_to_copy = new List<Operation>(); |
| 38 | + var marked_ops = new List<Operation>(); |
| 39 | + var ops_to_visit = new Stack<Operation>(init_tensors.Select(x => x.op)); |
| 40 | + var unvisited_ops = new List<Operation>(ops_to_visit.ToList()); |
| 41 | + while (unvisited_ops.Count > 0) |
| 42 | + { |
| 43 | + while(ops_to_visit.Count > 0) |
| 44 | + { |
| 45 | + var op = ops_to_visit.Pop(); |
| 46 | + if (marked_ops.Contains(op)) |
| 47 | + continue; |
| 48 | + marked_ops.Add(op); |
| 49 | + ops_to_copy.append(op); |
| 50 | + foreach(var inp in op.inputs) |
| 51 | + { |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + // difference_update |
| 56 | + unvisited_ops.difference_update(marked_ops); |
| 57 | + if (unvisited_ops.Count > 0) |
| 58 | + ops_to_visit.Push(unvisited_ops.Last()); |
| 59 | + } |
| 60 | + |
| 61 | + // When lifting from one FuncGraph to another, we will need to capture the |
| 62 | + // relevant tensors as well. |
| 63 | + var inverse_captures = new Dictionary<Tensor, Tensor>(); |
| 64 | + Tensor[] internal_captures = null; |
| 65 | + if (base_graph is FuncGraph base_func_graph) |
| 66 | + { |
| 67 | + var captures = base_func_graph.captures; |
| 68 | + foreach (var (external_capture, internal_capture) in captures) |
| 69 | + inverse_captures[internal_capture] = external_capture; |
| 70 | + internal_captures = base_func_graph.internal_captures; |
| 71 | + } |
| 72 | + |
| 73 | + graph.as_default(); |
| 74 | + var source_ops = new List<Operation>(); |
| 75 | + // Add the sources in the same order as the original graph. |
| 76 | + foreach (var s in internal_captures) |
| 77 | + { |
| 78 | + if (sources.Contains(s)) |
| 79 | + { |
| 80 | + sources.Remove(s); |
| 81 | + source_ops.Add(s.op); |
| 82 | + _copy_source(s: s, |
| 83 | + graph: graph, |
| 84 | + op_map: op_map, |
| 85 | + handle_captures: handle_captures, |
| 86 | + inverse_captures: inverse_captures, |
| 87 | + base_graph: base_graph); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + foreach(var op in reversed(ops_to_copy)) |
| 92 | + { |
| 93 | + if (source_ops.Contains(op) || op_map.ContainsKey(op)) |
| 94 | + continue; |
| 95 | + _copy_non_source(op, graph, op_map, base_graph); |
| 96 | + } |
| 97 | + |
| 98 | + return op_map; |
| 99 | + } |
| 100 | + |
| 101 | + static void _copy_source(Tensor s, |
| 102 | + FuncGraph graph, |
| 103 | + Dictionary<ITensorOrOperation, Operation> op_map, |
| 104 | + bool handle_captures, |
| 105 | + Dictionary<Tensor, Tensor> inverse_captures, |
| 106 | + Graph base_graph) |
| 107 | + { |
| 108 | + Tensor copied_placeholder = null; |
| 109 | + if (handle_captures && inverse_captures.ContainsKey(s)) |
| 110 | + copied_placeholder = graph.capture(inverse_captures[s], name: s.op.name); |
| 111 | + else |
| 112 | + throw new NotImplementedException(""); |
| 113 | + op_map[s] = copied_placeholder; |
| 114 | + // Add an entry for the op of the source tensor so that if there are any nodes |
| 115 | + // depending on that op via control dependencies it can work correctly. |
| 116 | + op_map[s.op] = copied_placeholder.op; |
| 117 | + } |
| 118 | + |
| 119 | + static void _copy_non_source(Operation op, FuncGraph graph, Dictionary<ITensorOrOperation, Operation> op_map, Graph base_graph) |
| 120 | + { |
| 121 | + Operation copied_op = null; |
| 122 | + var copied_inputs = new Tensors(); |
| 123 | + tf_with(ops.control_dependencies(new object[] { op }), delegate |
| 124 | + { |
| 125 | + // Create a new op in the destination graph if it doesn't exist before. |
| 126 | + var attrs = new Dictionary<string, AttrValue>(); |
| 127 | + foreach (var attr_def in op.node_def.Attr) |
| 128 | + attrs[attr_def.Key] = attr_def.Value; |
| 129 | + |
| 130 | + copied_op = graph.create_op(op.type, |
| 131 | + copied_inputs, |
| 132 | + dtypes: op.outputs.Select(x => x.dtype).ToArray(), |
| 133 | + attrs: attrs, |
| 134 | + name: op.name); |
| 135 | + }); |
| 136 | + op_map[op] = copied_op; |
| 137 | + foreach (var (i, o) in enumerate(op.outputs)) |
| 138 | + op_map[o] = copied_op.outputs[i]; |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Walk a Graph and capture the subgraph between init_tensor and sources. |
| 143 | + /// </summary> |
| 144 | + /// <param name="init_tensor"></param> |
| 145 | + /// <param name="add_sources"></param> |
| 146 | + public static List<Tensor> map_subgraph(Tensor init_tensor, |
| 147 | + List<Tensor> sources, |
| 148 | + List<Operation> visited_ops, |
| 149 | + bool add_sources) |
| 150 | + { |
| 151 | + var ops_to_visit = new Stack<Operation>(); |
| 152 | + ops_to_visit.Push(init_tensor.op); |
| 153 | + var extra_sources = new List<Tensor>(); |
| 154 | + while (ops_to_visit.Count > 0) |
| 155 | + { |
| 156 | + var op = ops_to_visit.Pop(); |
| 157 | + if (visited_ops.Contains(op)) |
| 158 | + continue; |
| 159 | + visited_ops.Add(op); |
| 160 | + bool should_raise = false; |
| 161 | + if (should_raise) |
| 162 | + throw new RuntimeError($"Unable to lift tensor {init_tensor.name}."); |
| 163 | + if(op.type == "Placeholder") |
| 164 | + { |
| 165 | + extra_sources.AddRange(op.outputs); |
| 166 | + } |
| 167 | + foreach(var inp in op.inputs) |
| 168 | + { |
| 169 | + |
| 170 | + } |
| 171 | + } |
| 172 | + return extra_sources; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments