Skip to content

Commit 31f79e8

Browse files
committed
Update protobuf and fix autograph.
1 parent 6a9ccea commit 31f79e8

56 files changed

Lines changed: 67955 additions & 1095 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/TensorFlowNET.Core/Checkpoint/SaveUtil.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ private static TrackableObjectGraph fill_object_graph_proto(IList<TrackableData>
106106
{
107107
var td = trackable_data[i];
108108
Debug.Assert(td.node_id == i);
109-
object_graph_proto.Nodes.Add(new TrackableObjectGraph.Types.TrackableObject(td.slot_variable_proto, td.children_proto));
109+
TrackableObjectGraph.Types.TrackableObject trackable_object = new();
110+
trackable_object.SlotVariables.AddRange(td.slot_variable_proto);
111+
trackable_object.Children.AddRange(td.children_proto);
110112
}
111113
return object_graph_proto;
112114
}

‎src/TensorFlowNET.Core/Checkpoint/SaveUtilV1.cs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,10 @@ private static TrackableObjectGraph fill_object_graph_proto(ObjectGraphView grap
110110
{
111111
var trackable = trackable_objects[i];
112112
Debug.Assert(node_ids[trackable] == i);
113-
TrackableObjectGraph.Types.TrackableObject object_proto;
113+
var object_proto = new TrackableObjectGraph.Types.TrackableObject();
114114
if (slot_variables.TryGetValue(trackable, out var slots))
115115
{
116-
object_proto = new TrackableObjectGraph.Types.TrackableObject(slots);
117-
}
118-
else
119-
{
120-
object_proto = new TrackableObjectGraph.Types.TrackableObject();
116+
object_proto.SlotVariables.AddRange(slots);
121117
}
122118
object_graph_proto.Nodes.Add(object_proto);
123119
foreach (var child in graph_view.list_children(trackable))

‎src/TensorFlowNET.Core/Functions/ConcreteFunction.cs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public class ConcreteFunction: Trackable
3434
public TensorSpec[] OutputStructure;
3535
public IEnumerable<string> ArgKeywords { get; set; }
3636
public long NumPositionArgs { get; set; }
37+
public FunctionDef FunctionDef => _delayed_rewrite_functions.Forward().Definition;
3738

3839
public ConcreteFunction(string name)
3940
{
4041
func_graph = new FuncGraph(name);
4142
_captured_inputs = func_graph.external_captures;
4243
_attrs= new Dictionary<string, string>();
43-
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
44-
_inference_function = _delayed_rewrite_functions.Forward();
44+
_set_infer_function();
4545
}
4646

4747
public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null)
@@ -51,8 +51,7 @@ public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null
5151

5252
//ToGraph(graph.Inputs, graph.Outputs.Where(x => x != null).ToArray());
5353
_attrs = attrs;
54-
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
55-
_inference_function = _delayed_rewrite_functions.Forward();
54+
_set_infer_function();
5655
}
5756

5857
public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
@@ -72,8 +71,7 @@ public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
7271
func_graph.Exit();
7372
_captured_inputs = func_graph.external_captures;
7473
_attrs = new Dictionary<string, string>();
75-
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
76-
_inference_function = _delayed_rewrite_functions.Forward();
74+
_set_infer_function();
7775
}
7876

7977
public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
@@ -96,8 +94,7 @@ public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
9694
func_graph.Exit();
9795
_captured_inputs = func_graph.external_captures;
9896
_attrs = new Dictionary<string, string>();
99-
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
100-
_inference_function = _delayed_rewrite_functions.Forward();
97+
_set_infer_function();
10198
}
10299

103100
/*public ConcreteFunction(Func<Tensors, Tensors> func,
@@ -245,6 +242,12 @@ ForwardBackwardCall SelectForwardAndBackwardFunctions(Tensors args, int possible
245242
return new ForwardBackwardCall(_delayed_rewrite_functions, args, tape_watching: false);
246243
}
247244

245+
internal void _set_infer_function()
246+
{
247+
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
248+
_inference_function = _delayed_rewrite_functions.Forward();
249+
}
250+
248251
internal void _set_function_spec(FunctionSpec spec)
249252
{
250253
_function_spec = null;

‎src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class EagerDefinedFunction
2121
OpDef _signature;
2222
string _name;
2323
Tensor[] _func_graph_outputs;
24-
public string Name => _func_graph.FuncName;
24+
public string Name => _name;
2525
public DataType[] OutputTypes { get; protected set; }
2626
public Shape[] OutputShapes { get; protected set; }
2727
public FunctionDef Definition
@@ -51,18 +51,21 @@ public EagerDefinedFunction(string name, FuncGraph graph,
5151
Tensors inputs, Tensors outputs,
5252
Dictionary<string, string> attrs)
5353
{
54-
_num_outputs = outputs.Length;
55-
5654
var input_ops = inputs.Select(x => x.op).ToArray();
5755
var operations = graph.get_operations().Where(x => !input_ops.Contains(x.op))
5856
.Select(x => x as Operation).ToArray();
5957
var output_names = new string[0];
60-
OutputShapes = outputs.Select(x => x.shape).ToArray();
61-
OutputTypes = outputs.Select(x => x.dtype.as_datatype_enum()).ToArray();
62-
6358
_func_graph = new FuncGraph(graph, name, attrs);
6459
_func_graph_outputs = new List<Tensor>(outputs).ToArray();
6560
_func_graph.ToGraph(operations, inputs, outputs, output_names);
61+
62+
var signature = _get_definition().Signature;
63+
_name = signature.Name;
64+
// TODO(Rinne): deal with `fn`
65+
66+
_num_outputs = signature.OutputArg.Count;
67+
OutputTypes = signature.OutputArg.Select(x => x.Type).ToArray();
68+
OutputShapes = outputs.Select(x => x.shape).ToArray();
6669
}
6770

6871
public Tensors Call(Tensors args)

‎src/TensorFlowNET.Core/Graphs/AutoGraphAttribute.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public override void OnEntry(MethodExecutionArgs args)
3838

3939
// make function as an Operation by autograph
4040
// need to restore mode when exits
41+
42+
//var func_graph = new FuncGraph(func_name);
43+
//func_graph.as_default();
44+
//var input_placeholders = args.Arguments.Select(x => tf.placeholder(((Tensor)x).dtype)).ToArray();
45+
//// stop the function from recursive call.
46+
//already_in_boundary = true;
47+
//var outputs = args.Method.Invoke(args.Instance, input_placeholders) as Tensors;
48+
//already_in_boundary = false;
49+
50+
//var opers = func_graph._nodes_by_name.Values.Select(x => x as Operation).ToArray();
51+
//func_graph.ToGraph(opers,
52+
// input_placeholders,
53+
// outputs,
54+
// null);
55+
//func_graph.Exit();
4156
function = new ConcreteFunction(func_name);
4257
function.Enter();
4358

@@ -92,6 +107,7 @@ public override void OnExit(MethodExecutionArgs args)
92107

93108
// cache function.
94109
function.ReturnType = args.ReturnValue.GetType();
110+
function._set_infer_function();
95111
functions[func_name] = function;
96112

97113
// run function

0 commit comments

Comments
 (0)