Skip to content

Commit aee6cd6

Browse files
committed
remove generic in OpTape.
1 parent 2ef9a51 commit aee6cd6

19 files changed

Lines changed: 58 additions & 90 deletions

src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public bool RecordGradient(string op_name,
1111
Tensor[] inputs,
1212
object[] attrs,
1313
Tensor[] results,
14-
Func<BackwardFunction> getBackwardFunction = null)
14+
BackwardFunction backwardFunction = null)
1515
{
1616
bool should_record = ShouldRecord(inputs);
1717

@@ -28,9 +28,9 @@ public bool RecordGradient(string op_name,
2828
}
2929

3030
if (!should_record) return should_record;
31-
tf.Logger.Debug($"RecordGradient: op_name={op_name}");
31+
// tf.Logger.Debug($"RecordGradient: op_name={op_name}");
3232

33-
Tensor[] op_outputs;
33+
/*Tensor[] op_outputs = null;
3434
var unused_output_indices = gradient_exclustions.OpGradientUnusedOutputIndices(op_name);
3535
if (unused_output_indices != null)
3636
{
@@ -44,7 +44,7 @@ public bool RecordGradient(string op_name,
4444
else
4545
op_outputs = results;
4646
47-
Tensor[] op_inputs;
47+
Tensor[] op_inputs = null;
4848
var unused_input_indices = gradient_exclustions.OpGradientUnusedInputIndices(op_name);
4949
if (unused_input_indices != null)
5050
{
@@ -56,22 +56,14 @@ public bool RecordGradient(string op_name,
5656
}
5757
}
5858
else
59-
op_inputs = inputs;
59+
op_inputs = inputs;*/
6060

61-
TapeSetRecordOperation(op_name, inputs, results,
62-
getBackwardFunction ?? GetBackwradFunction(op_name, inputs, attrs, results));
61+
backwardFunction = backwardFunction ?? GetGradientFunction(op_name, inputs, attrs, results);
62+
TapeSetRecordOperation(op_name, inputs, results, backwardFunction);
6363

6464
return true;
6565
}
6666

67-
Func<BackwardFunction> GetBackwradFunction(string op_name,
68-
Tensor[] op_inputs,
69-
object[] attrs,
70-
Tensor[] op_outputs)
71-
{
72-
return () => GetGradientFunction(op_name, op_inputs, attrs, op_outputs);
73-
}
74-
7567
BackwardFunction GetGradientFunction(string op_name,
7668
Tensor[] op_inputs,
7769
object[] attrs,

src/TensorFlowNET.Core/Eager/EagerRunner.TapeSetRecordBackprop.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Tensorflow.Gradients;
33
using static Tensorflow.Binding;
4-
using static Tensorflow.tensorflow;
54

65
namespace Tensorflow.Eager
76
{
@@ -10,7 +9,7 @@ public partial class EagerRunner
109
void TapeSetRecordBackprop(string op_type,
1110
Tensor[] input_tensors,
1211
TapeTensor[] output_tensors,
13-
Func<BackwardFunction> backward_function_getter)
12+
BackwardFunction backward_function)
1413
{
1514
if (!CouldBackprop())
1615
{
@@ -19,8 +18,7 @@ void TapeSetRecordBackprop(string op_type,
1918

2019
foreach (var tape in tf.GetTapeSet())
2120
{
22-
tape.RecordOperation(op_type, input_tensors, output_tensors,
23-
backward_function_getter);
21+
tape.RecordOperation(op_type, input_tensors, output_tensors, backward_function);
2422
}
2523
}
2624
}

src/TensorFlowNET.Core/Eager/EagerRunner.TapeSetRecordForwardprop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public partial class EagerRunner
99
bool TapeSetRecordForwardprop(string op_type,
1010
Tensor[] input_tensors,
1111
TapeTensor[] output_tensors,
12-
Func<BackwardFunction> backward_function_getter)
12+
BackwardFunction backward_function_getter)
1313
{
1414
if (!CouldForwardprop())
1515
{

src/TensorFlowNET.Core/Eager/EagerRunner.TapeSetRecordOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ public partial class EagerRunner
1010
public bool TapeSetRecordOperation(string op_type,
1111
Tensor[] input_tensors,
1212
Tensor[] output_tensors,
13-
Func<BackwardFunction> backward_function_getter)
13+
BackwardFunction backward_function)
1414
{
1515
var output_info = output_tensors.Select(x => new TapeTensor(x)).ToArray();
1616

1717
if (!TapeSetRecordForwardprop(op_type, input_tensors, output_info,
18-
backward_function_getter))
18+
backward_function))
1919
return false;
2020

2121
TapeSetRecordBackprop(op_type, input_tensors, output_info,
22-
backward_function_getter);
22+
backward_function);
2323

2424
return true;
2525
}

src/TensorFlowNET.Core/Eager/IEagerRunner.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public interface IEagerRunner
99
{
1010
Tensor[] Execute(Context ctx, string op_name,
1111
int num_outputs,
12-
Tensor[] inputs, object[] attrs,
12+
Tensor[] inputs,
13+
object[] attrs,
1314
string name = null);
1415

1516
(TF_DataType, Tensor[]) ArgsToMatchingEager(Context ctx,
@@ -34,7 +35,7 @@ bool RecordGradient(string op_name,
3435
Tensor[] inputs,
3536
object[] attrs,
3637
Tensor[] results,
37-
Func<BackwardFunction> getBackwardFunction = null);
38+
BackwardFunction getBackwardFunction = null);
3839

3940
bool MustRecordGradient();
4041

src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void Record(Tensors flat_outputs, Tensors inference_args)
4747
{
4848
var (backward_function, to_record) = _wrap_backward_function(_forward_graph, _backward, flat_outputs);
4949
tf.Runner.RecordGradient(_forward.Name, inference_args, new object[0], to_record,
50-
getBackwardFunction: () => backward_function);
50+
getBackwardFunction: backward_function);
5151
}
5252

5353
/// <summary>

src/TensorFlowNET.Core/Gradients/BackpropInitialState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Tensorflow.Gradients
55
{
66
public class BackpropInitialState
77
{
8-
public OpTape<BackwardFunction, TapeTensor> op_tape { get; set; }
8+
public OpTape op_tape { get; set; }
99
/// <summary>
1010
/// Map from tensor to how many references still exist for this tensor in
1111
/// the tape.
@@ -19,7 +19,7 @@ public class BackpropInitialState
1919

2020
public BackpropInitialState()
2121
{
22-
op_tape = new OpTape<BackwardFunction, TapeTensor>();
22+
op_tape = new OpTape();
2323
tensor_usage_counts = new UnorderedMap<Tensor, long>();
2424
op_missing_tensor = new UnorderedMap<Tensor, long>();
2525
}

src/TensorFlowNET.Core/Gradients/ITape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface ITape
1313
void RecordOperation(string op_type,
1414
Tensor[] input_tensors,
1515
TapeTensor[] output_tensors,
16-
Func<BackwardFunction> backward_function_getter);
16+
BackwardFunction backward_function);
1717

1818
void VariableAccessed(ResourceVariable variable);
1919

src/TensorFlowNET.Core/Gradients/OpTape.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ namespace Tensorflow.Gradients
55
/// <summary>
66
/// Map from operation-id to tape entry.
77
/// </summary>
8-
/// <typeparam name="BackwardFunction"></typeparam>
9-
/// <typeparam name="TapeTensor"></typeparam>
10-
public class OpTape<BackwardFunction, TapeTensor> :
11-
UnorderedMap<Tensor, OpTapeEntry<BackwardFunction, TapeTensor>>
8+
public class OpTape : UnorderedMap<Tensor, OpTapeEntry>
129
{
1310

1411
}

src/TensorFlowNET.Core/Gradients/OpTapeEntry.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ namespace Tensorflow.Gradients
55
/// <summary>
66
/// Represents an entry in the tape.
77
/// </summary>
8-
/// <typeparam name="BackwardFunction"></typeparam>
9-
/// <typeparam name="TapeTensor"></typeparam>
10-
public class OpTapeEntry<BackwardFunction, TapeTensor>
8+
public class OpTapeEntry
119
{
1210
public string op_type { get; set; }
1311
public TapeTensor[] output_tensor_info { get; set; }

0 commit comments

Comments
 (0)