public void RecordOperation(string op_type, Tensor[] input_tensors, TapeTensor[] output_tensors, BackwardFunction backward_function) { if (!ShouldRecord(input_tensors)) { return; } var op_id = next_op_id_++; foreach (var i in input_tensors) { tensor_usage_[i]++; } foreach (var o in output_tensors) { tf.Logger.Debug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}"); tensor_tape_[o.GetTensor()] = op_id; tensor_usage_[o.GetTensor()] = 1; } op_tape_[op_id] = new OpTapeEntry { op_type = op_type, output_tensor_info = output_tensors, input_tensor_id = input_tensors, backward_function = backward_function }; }
public bool RecordGradient(string op_name, Tensor[] inputs, object[] attrs, Tensor[] results, BackwardFunction backwardFunction = null) { bool should_record = ShouldRecord(inputs); if (!should_record) { /*for (TFE_Py_ForwardAccumulator* accumulator : SafeAccumulatorSet()) * { * if (accumulator->accumulator->ShouldRecord(input_ids, input_dtypes)) * { * should_record = true; * break; * } * }*/ } if (!should_record) { return(should_record); } // tf.Logger.Debug($"RecordGradient: op_name={op_name}"); /*Tensor[] op_outputs = null; * var unused_output_indices = gradient_exclustions.OpGradientUnusedOutputIndices(op_name); * if (unused_output_indices != null) * { * if (unused_output_indices.Length == 0) * op_outputs = new Tensor[0]; * else * { * // op_outputs = CopySequenceSettingIndicesToNull(results, *unused_output_indices); * } * } * else * op_outputs = results; * * Tensor[] op_inputs = null; * var unused_input_indices = gradient_exclustions.OpGradientUnusedInputIndices(op_name); * if (unused_input_indices != null) * { * if (unused_input_indices.Length == 0) * op_inputs = new Tensor[0]; * else * { * // op_inputs = CopySequenceSettingIndicesToNull(inputs, *unused_input_indices); * } * } * else * op_inputs = inputs;*/ backwardFunction = backwardFunction ?? GetGradientFunction(op_name, inputs, attrs, results); TapeSetRecordOperation(op_name, inputs, results, backwardFunction); return(true); }
public Tensor[] CallBackwardFunction(BackwardFunction backward_function, List <long> unneeded_gradients, List <Tensor> output_gradients) { var grads = new Tensor[output_gradients.Count]; var result = backward_function(output_gradients.ToArray(), unneeded_gradients.ToArray()); return(result); }
bool TapeSetRecordForwardprop(string op_type, Tensor[] input_tensors, TapeTensor[] output_tensors, BackwardFunction backward_function_getter) { if (!CouldForwardprop()) { return(true); } throw new NotImplementedException(""); }
void TapeSetRecordBackprop(string op_type, Tensor[] input_tensors, TapeTensor[] output_tensors, BackwardFunction backward_function) { if (!CouldBackprop()) { return; } foreach (var tape in tf.GetTapeSet()) { tape.RecordOperation(op_type, input_tensors, output_tensors, backward_function); } }
public bool TapeSetRecordOperation(string op_type, Tensor[] input_tensors, Tensor[] output_tensors, BackwardFunction backward_function) { var output_info = output_tensors.Select(x => new TapeTensor(x)).ToArray(); if (!TapeSetRecordForwardprop(op_type, input_tensors, output_info, backward_function)) { return(false); } TapeSetRecordBackprop(op_type, input_tensors, output_info, backward_function); return(true); }