|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Tensorflow.Graphs; |
| 5 | +using static Tensorflow.Binding; |
| 6 | +using static Tensorflow.tensorflow; |
| 7 | + |
| 8 | +namespace Tensorflow.Functions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Caches forward and backward functions compatible with eager gradients. |
| 12 | + /// </summary> |
| 13 | + public abstract class TapeGradientFunctions |
| 14 | + { |
| 15 | + string FORWARD_FUNCTION_ATTRIBUTE_NAME = "forward_function_name"; |
| 16 | + string BACKWARD_FUNCTION_ATTRIBUTE_NAME = "backward_function_name"; |
| 17 | + string _FORWARD_PREFIX = "__forward_"; |
| 18 | + string _BACKWARD_PREFIX = "__backward_"; |
| 19 | + string _INFERENCE_PREFIX = "__inference_"; |
| 20 | + |
| 21 | + protected FuncGraph _func_graph; |
| 22 | + protected EagerDefinedFunction _forward; |
| 23 | + protected FuncGraph _forward_graph; |
| 24 | + protected List<int> _forwardprop_output_indices; |
| 25 | + protected int _num_forwardprop_outputs; |
| 26 | + protected ConcreteFunction _backward; |
| 27 | + |
| 28 | + public TapeGradientFunctions(FuncGraph func_graph, |
| 29 | + bool need_gradients_for_jvps) |
| 30 | + { |
| 31 | + _func_graph = func_graph; |
| 32 | + } |
| 33 | + |
| 34 | + public EagerDefinedFunction Forward(Tensors inference_args) |
| 35 | + { |
| 36 | + return ForwardAndBackwardFunctions(inference_args); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Record the function call operation. |
| 41 | + /// </summary> |
| 42 | + /// <param name="flat_outputs"></param> |
| 43 | + /// <param name="inference_args"></param> |
| 44 | + public void Record(Tensors flat_outputs, Tensors inference_args) |
| 45 | + { |
| 46 | + var (backward_function, to_record) = _wrap_backward_function(_forward_graph, _backward, flat_outputs); |
| 47 | + tf.Runner.RecordGradient(_forward.Name, flat_outputs, new object[0], inference_args, |
| 48 | + getBackwardFunction: () => backward_function); |
| 49 | + } |
| 50 | + |
| 51 | + (BackwardFunction, Tensors) _wrap_backward_function(FuncGraph forward_graph, ConcreteFunction backward, Tensors flat_outputs) |
| 52 | + { |
| 53 | + BackwardFunction _backward_function_wrapper = (output_grads, unneeded_gradients) => |
| 54 | + { |
| 55 | + return new Tensor[0]; |
| 56 | + |
| 57 | + /*var gradients = ops.gradientFunctions[op_name](new EagerOperation |
| 58 | + { |
| 59 | + Name = op_name, |
| 60 | + NumInputs = op_inputs.Length, |
| 61 | + Inputs = op_inputs, |
| 62 | + NumOutputs = op_outputs.Length, |
| 63 | + Outputs = op_outputs, |
| 64 | + SkipInputIndices = unneeded_gradients, |
| 65 | + Attrs = attrs |
| 66 | + }, output_grads); |
| 67 | +
|
| 68 | + return gradients;*/ |
| 69 | + }; |
| 70 | + |
| 71 | + return (_backward_function_wrapper, flat_outputs); |
| 72 | + } |
| 73 | + |
| 74 | + protected (EagerDefinedFunction, FuncGraph, ConcreteFunction, List<int>, int) |
| 75 | + BuildFunctionsForOutputs(Tensors outputs, Tensors inference_args) |
| 76 | + { |
| 77 | + var trainable_outputs = new List<Tensor>(); |
| 78 | + var trainable_indices = new List<int>(); |
| 79 | + foreach(var (index, output) in enumerate(outputs)) |
| 80 | + { |
| 81 | + if (gradients_util.IsTrainable(output)) |
| 82 | + { |
| 83 | + trainable_outputs.Add(output); |
| 84 | + trainable_indices.Add(index); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + var gradients_wrt_outputs = new List<Tensor>(); |
| 89 | + var backwards_graph = new FuncGraph($"{_BACKWARD_PREFIX}{_func_graph.FuncName}_{ops.uid()}"); |
| 90 | + foreach (var output in trainable_outputs) |
| 91 | + gradients_wrt_outputs.Add(tf.placeholder(output.dtype, output.shape)); |
| 92 | + var gradients_wrt_inputs = gradients_util._GradientsHelper(trainable_outputs.ToArray(), |
| 93 | + _func_graph.Inputs, |
| 94 | + grad_ys: gradients_wrt_outputs.ToArray(), |
| 95 | + src_graph: _func_graph); |
| 96 | + |
| 97 | + tf.Context.restore_mode(); |
| 98 | + |
| 99 | + var forward_function_name = $"{_FORWARD_PREFIX}{_func_graph.FuncName}_{ops.uid()}"; |
| 100 | + var backward_function_attr = new Dictionary<string, string>(); |
| 101 | + backward_function_attr[FORWARD_FUNCTION_ATTRIBUTE_NAME] = forward_function_name; |
| 102 | + backwards_graph.Inputs = gradients_wrt_outputs; |
| 103 | + backwards_graph.Outputs = gradients_wrt_inputs; |
| 104 | + |
| 105 | + var backward_function = new ConcreteFunction(backwards_graph, backward_function_attr); |
| 106 | + |
| 107 | + var forward_function_attr = new Dictionary<string, string>(); |
| 108 | + forward_function_attr[BACKWARD_FUNCTION_ATTRIBUTE_NAME] = backward_function.Name; |
| 109 | + var forward_function = new EagerDefinedFunction(forward_function_name, _func_graph, |
| 110 | + _func_graph.Inputs, _func_graph.Outputs, forward_function_attr); |
| 111 | + |
| 112 | + return (forward_function, _func_graph, backward_function, null, 0); |
| 113 | + } |
| 114 | + |
| 115 | + public virtual EagerDefinedFunction ForwardAndBackwardFunctions(Tensors inference_args) |
| 116 | + { |
| 117 | + throw new NotImplementedException(""); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments