Skip to content

Commit 4e6431e

Browse files
committed
Align some implementations of Graph and FuncGraph.
1 parent 355ca3a commit 4e6431e

13 files changed

Lines changed: 164 additions & 21 deletions

File tree

src/TensorFlowNET.Core/Contexts/Context.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ public bool has_function(string name)
162162
return c_api.TFE_ContextHasFunction(_handle, name);
163163
}
164164

165+
public void add_function_def(FunctionDef fdef)
166+
{
167+
ensure_initialized();
168+
var fdef_string = fdef.ToString();
169+
c_api.TFE_ContextAddFunctionDef(_handle, fdef_string, fdef_string.Length);
170+
}
171+
165172
public void restore_mode()
166173
{
167174
context_switches.Pop();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ bool SetOpAttrScalar(Context ctx, SafeEagerOpHandle op,
358358
break;
359359
case TF_AttrType.TF_ATTR_FUNC:
360360
if (value is ConcreteFunction func)
361-
c_api.TFE_OpSetAttrFunctionName(op, key, func.Name, func.Name.Length);
361+
c_api.TFE_OpSetAttrFunctionName(op, key, func.func_graph.FuncName, func.func_graph.FuncName.Length);
362362
else
363363
throw new NotImplementedException("TF_AttrType.TF_ATTR_FUNC");
364364
break;

src/TensorFlowNET.Core/Eager/c_api.eager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public partial class c_api
3030
[DllImport(TensorFlowLibName)]
3131
public static extern void TFE_ContextOptionsSetConfig(SafeContextOptionsHandle opts, byte[] proto, ulong proto_len, SafeStatusHandle status);
3232

33+
[DllImport(TensorFlowLibName)]
34+
public static extern void TFE_ContextAddFunctionDef(SafeContextHandle ctx, string serialized_function_def, int size);
35+
3336
[DllImport(TensorFlowLibName)]
3437
public static extern void TFE_ContextOptionsSetDevicePlacementPolicy(SafeContextOptionsHandle opts, ContextDevicePlacementPolicy device_policy);
3538

src/TensorFlowNET.Core/Framework/c_api_util.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,17 @@ public static void DownloadLibrary()
111111

112112
public static ImportGraphDefOptions ScopedTFImportGraphDefOptions() => new ImportGraphDefOptions();
113113

114-
public static Buffer tf_buffer(byte[] data) => new Buffer(data);
114+
public static Buffer tf_buffer(byte[] data = null)
115+
{
116+
if(data is not null)
117+
{
118+
return new Buffer(data); ;
119+
}
120+
else
121+
{
122+
return new Buffer();
123+
}
124+
}
115125

116126
public static IEnumerable<Operation> new_tf_operations(Graph graph)
117127
{

src/TensorFlowNET.Core/Functions/ConcreteFunction.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public class ConcreteFunction: Trackable
1515
{
1616
protected IEnumerable<Tensor> _captured_inputs;
1717
internal FuncGraph func_graph;
18+
protected DelayedRewriteGradientFunctions _delayed_rewrite_functions;
19+
protected Dictionary<string, string> _attrs;
1820
internal ForwardBackwardCall forward_backward;
1921
public Tensor[] Inputs => func_graph.Inputs;
2022
public Tensor[] CapturedInputs => func_graph.external_captures;
2123

22-
public string Name => func_graph?.FuncName;
24+
public string Name => _delayed_rewrite_functions.forward().Name;
2325

2426
public Tensor[] Outputs;
2527
public Type ReturnType;
@@ -31,14 +33,18 @@ public ConcreteFunction(string name)
3133
{
3234
func_graph = new FuncGraph(name);
3335
_captured_inputs = func_graph.external_captures;
36+
_attrs= new Dictionary<string, string>();
37+
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
3438
}
3539

3640
public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null)
3741
{
3842
func_graph = graph;
3943
_captured_inputs = func_graph.external_captures;
4044

41-
ToGraph(graph.Inputs, graph.Outputs.Where(x => x != null).ToArray());
45+
//ToGraph(graph.Inputs, graph.Outputs.Where(x => x != null).ToArray());
46+
_attrs = attrs;
47+
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
4248
}
4349

4450
public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
@@ -57,6 +63,8 @@ public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
5763
null);
5864
func_graph.Exit();
5965
_captured_inputs = func_graph.external_captures;
66+
_attrs = new Dictionary<string, string>();
67+
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
6068
}
6169

6270
public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
@@ -78,6 +86,8 @@ public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
7886
null);
7987
func_graph.Exit();
8088
_captured_inputs = func_graph.external_captures;
89+
_attrs = new Dictionary<string, string>();
90+
_delayed_rewrite_functions = new DelayedRewriteGradientFunctions(func_graph, _attrs);
8191
}
8292

8393
/*public ConcreteFunction(Func<Tensors, Tensors> func,
@@ -176,7 +186,7 @@ public void AddTograph(Graph? g = null)
176186
{
177187
g = ops.get_default_graph();
178188
}
179-
// TODO(Rinne); complete it with `_delayed_rewrite_functions`.
189+
_delayed_rewrite_functions.forward().AddToGraph(g);
180190
}
181191

182192
public void SetExternalCaptures(IEnumerable<Tensor> captures)

src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Text;
6+
using Tensorflow.Contexts;
67
using Tensorflow.Graphs;
78
using static Tensorflow.Binding;
89

@@ -11,9 +12,20 @@ namespace Tensorflow.Functions
1112
public class EagerDefinedFunction
1213
{
1314
public int _num_outputs;
14-
public string Name => _func_graph.FuncName;
15-
1615
FuncGraph _func_graph;
16+
FunctionDef _definition;
17+
public string Name => _func_graph.FuncName;
18+
public FunctionDef Definition
19+
{
20+
get
21+
{
22+
if(_definition is null)
23+
{
24+
_definition = _get_definition();
25+
}
26+
return _definition;
27+
}
28+
}
1729
public EagerDefinedFunction(string name, FuncGraph graph,
1830
Tensors inputs, Tensors outputs,
1931
Dictionary<string, string> attrs)
@@ -46,5 +58,39 @@ public Tensors Call(Tensors args)
4658

4759
return results;
4860
}
61+
62+
public void AddToGraph(Graph g = null)
63+
{
64+
if(g is null && tf.Context.executing_eagerly())
65+
{
66+
var ctx = tf.Context;
67+
if (!ctx.has_function(this.Name))
68+
{
69+
ctx.add_function_def(Definition);
70+
}
71+
}
72+
else
73+
{
74+
if (!g.IsFunction(Name))
75+
{
76+
g.AddFunction(this);
77+
}
78+
foreach(var f in _func_graph.Functions.Values)
79+
{
80+
if (!g.IsFunction(f.Name))
81+
{
82+
g.AddFunction(f);
83+
}
84+
}
85+
}
86+
}
87+
88+
private FunctionDef _get_definition()
89+
{
90+
var buffer = c_api_util.tf_buffer();
91+
// TODO(Rinne): pywrap_tf_session.TF_FunctionToFunctionDef
92+
var proto_data = c_api.TF_GetBuffer(buffer);
93+
throw new NotImplementedException();
94+
}
4995
}
5096
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Graphs;
5+
6+
namespace Tensorflow.Functions
7+
{
8+
public class DelayedRewriteGradientFunctions
9+
{
10+
static readonly string _INFERENCE_PREFIX = "__inference_";
11+
static readonly string _BACKWARD_PREFIX = "__backward_";
12+
static readonly string _FORWARD_PREFIX = "__forward_";
13+
FuncGraph _func_graph;
14+
EagerDefinedFunction _inference_function;
15+
Dictionary<string, string> _attrs;
16+
int _num_inference_outputs;
17+
public DelayedRewriteGradientFunctions(FuncGraph func_graph, Dictionary<string, string> attrs)
18+
{
19+
_func_graph= func_graph;
20+
_inference_function = new EagerDefinedFunction(_inference_name(_func_graph.Name),
21+
_func_graph, _func_graph.Inputs, _func_graph.Outputs, attrs);
22+
_attrs = attrs;
23+
_num_inference_outputs = _func_graph.Outputs.Length;
24+
}
25+
26+
public EagerDefinedFunction forward(Tensors inference_args = null, Tensors input_tangents = null)
27+
{
28+
if(input_tangents is not null)
29+
{
30+
throw new InvalidArgumentError($"unexpectedly got forwardprop information in " +
31+
$"a class that does not support forwardprop.");
32+
}
33+
return _inference_function;
34+
}
35+
36+
private static string _inference_name(string name)
37+
{
38+
return $"{_INFERENCE_PREFIX}{name}_{ops.uid()}";
39+
}
40+
}
41+
}

src/TensorFlowNET.Core/Graphs/AutoGraphAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public sealed class AutoGraphAttribute : OnMethodBoundaryAspect
2222

2323
public override void OnEntry(MethodExecutionArgs args)
2424
{
25-
File.WriteAllText(@"D:\temp\for_test.txt", "jyfgjyfjhfjhc");
2625
// TODO: func_name can be cache in FullName + Args
2726
func_name = $"{args.Method.DeclaringType.FullName}.{args.Method.Name}";
2827

src/TensorFlowNET.Core/Graphs/FuncGraph.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class FuncGraph : Graph, IDisposable
1515

1616
public Tensors Inputs { get; set; } = new Tensors();
1717
public Tensors Outputs { get; set; } = new Tensors();
18+
public string Name { get; set; }
1819
public Dictionary<string, string> Attrs { get; set; }
1920

2021
Dictionary<long, (Tensor, Tensor)> _captures
@@ -39,7 +40,7 @@ public FuncGraph(string name) : base()
3940
outer_graph = ops.get_default_graph();
4041
while (outer_graph.building_function)
4142
outer_graph = outer_graph.OuterGraph;
42-
_graph_key = name;
43+
_graph_key = Name = name;
4344
building_function = true;
4445
}
4546

@@ -48,7 +49,7 @@ public FuncGraph(SafeGraphHandle handle, string name, Dictionary<string, string>
4849
outer_graph = ops.get_default_graph();
4950
while (outer_graph.building_function)
5051
outer_graph = outer_graph.OuterGraph;
51-
_graph_key = name;
52+
_graph_key = Name = name;
5253
building_function = true;
5354
Attrs = attrs;
5455
// Will to test if FuncGraph has memory leak

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ limitations under the License.
1919
using System.Collections.Generic;
2020
using System.Collections.Specialized;
2121
using System.Linq;
22+
using Tensorflow.Framework;
23+
using Tensorflow.Functions;
2224
using static Tensorflow.Binding;
2325

2426
namespace Tensorflow
@@ -85,6 +87,12 @@ public partial class Graph : IEnumerable<Operation>
8587
private int _next_id_counter;
8688
private List<Operation> _unfetchable_ops = new List<Operation>();
8789
private List<Tensor> _unfeedable_tensors = new List<Tensor>();
90+
private Dictionary<string, EagerDefinedFunction> _functions = new();
91+
private VersionDef _graph_def_versions = new VersionDef()
92+
{
93+
Producer = versions.GRAPH_DEF_VERSION,
94+
MinConsumer = versions.GRAPH_DEF_VERSION_MIN_CONSUMER
95+
};
8896

8997
public string _name_stack = "";
9098
protected string _graph_key;
@@ -120,6 +128,7 @@ public int seed
120128

121129
protected Graph outer_graph;
122130
public Graph OuterGraph => outer_graph;
131+
public Dictionary<string, EagerDefinedFunction> Functions => _functions;
123132

124133
public Graph()
125134
{
@@ -148,8 +157,23 @@ public virtual Graph as_default()
148157

149158
public bool IsFunction(string name)
150159
{
151-
// TODO(Rinne): deal with `_functions`.
152-
throw new NotImplementedException();
160+
return _functions.ContainsKey(tf.compat.as_str(name));
161+
}
162+
163+
public void AddFunction(EagerDefinedFunction function)
164+
{
165+
_check_not_finalized();
166+
167+
var name = function.Name;
168+
169+
// TODO(Rinne): deal with c_graph
170+
171+
_functions[tf.compat.as_str(name)] = function;
172+
173+
if(_graph_def_versions.MinConsumer < 12)
174+
{
175+
_graph_def_versions.MinConsumer = 12;
176+
}
153177
}
154178

155179
private Tensor _as_graph_element(object obj)

0 commit comments

Comments
 (0)