66using Tensorflow . Framework . Models ;
77using Tensorflow . Graphs ;
88using Tensorflow . Train ;
9+ using Tensorflow . Util ;
910using static Tensorflow . Binding ;
1011
1112namespace Tensorflow . Functions
@@ -21,6 +22,7 @@ public class ConcreteFunction: Trackable
2122 protected Dictionary < string , string > _attrs ;
2223 protected FunctionSpec _function_spec ;
2324 protected FunctionSpec _pre_initialized_function_spec = null ;
25+ protected EagerDefinedFunction _inference_function ;
2426 internal ForwardBackwardCall forward_backward ;
2527 public Tensor [ ] Inputs => func_graph . Inputs ;
2628 public Tensor [ ] CapturedInputs => func_graph . external_captures ;
@@ -39,6 +41,7 @@ public ConcreteFunction(string name)
3941 _captured_inputs = func_graph . external_captures ;
4042 _attrs = new Dictionary < string , string > ( ) ;
4143 _delayed_rewrite_functions = new DelayedRewriteGradientFunctions ( func_graph , _attrs ) ;
44+ _inference_function = _delayed_rewrite_functions . Forward ( ) ;
4245 }
4346
4447 public ConcreteFunction ( FuncGraph graph , Dictionary < string , string > attrs = null )
@@ -49,6 +52,7 @@ public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null
4952 //ToGraph(graph.Inputs, graph.Outputs.Where(x => x != null).ToArray());
5053 _attrs = attrs ;
5154 _delayed_rewrite_functions = new DelayedRewriteGradientFunctions ( func_graph , _attrs ) ;
55+ _inference_function = _delayed_rewrite_functions . Forward ( ) ;
5256 }
5357
5458 public ConcreteFunction ( Func < Tensor , Tensor > func , TF_DataType dtype )
@@ -69,6 +73,7 @@ public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
6973 _captured_inputs = func_graph . external_captures ;
7074 _attrs = new Dictionary < string , string > ( ) ;
7175 _delayed_rewrite_functions = new DelayedRewriteGradientFunctions ( func_graph , _attrs ) ;
76+ _inference_function = _delayed_rewrite_functions . Forward ( ) ;
7277 }
7378
7479 public ConcreteFunction ( Func < Tensor , IDatasetV2 > func , TF_DataType dtype )
@@ -92,6 +97,7 @@ public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
9297 _captured_inputs = func_graph . external_captures ;
9398 _attrs = new Dictionary < string , string > ( ) ;
9499 _delayed_rewrite_functions = new DelayedRewriteGradientFunctions ( func_graph , _attrs ) ;
100+ _inference_function = _delayed_rewrite_functions . Forward ( ) ;
95101 }
96102
97103 /*public ConcreteFunction(Func<Tensors, Tensors> func,
@@ -154,9 +160,10 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
154160 {
155161 tensor_inputs . Add ( arg ) ;
156162 // If we're graph building, shape inference is on.
157- if ( ! executing_eagerly )
158- {
159- }
163+ }
164+ if ( ! executing_eagerly )
165+ {
166+
160167 }
161168 tensor_inputs . AddRange ( captured_inputs ) ;
162169
@@ -166,12 +173,13 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
166173 // No tape is watching; skip to running the function.
167174 if ( possible_gradient_type == 0 && executing_eagerly )
168175 {
169- var attrs = new object [ ]
170- {
171- "executor_type" , "" ,
172- "config_proto" , tf . Context . FunctionCallOptions . config_proto_serialized ( )
173- } ;
174- return tf . Runner . Execute ( tf . Context , func_graph . FuncName , func_graph . Outputs . Length , args , attrs ) ;
176+ return _build_call_outputs ( _inference_function . Call ( args ) ) ;
177+ //var attrs = new object[]
178+ //{
179+ // "executor_type", "",
180+ // "config_proto", tf.Context.FunctionCallOptions.config_proto_serialized()
181+ //};
182+ //return tf.Runner.Execute(tf.Context, func_graph.FuncName, func_graph.Outputs.Length, args, attrs);
175183 }
176184
177185 if ( forward_backward == null )
@@ -184,10 +192,11 @@ public Tensors CallFlat(Tensor[] args, Tensor[] captured_inputs)
184192 }
185193 else
186194 {
195+ // TODO(Rinne): add `default_graph._override_gradient_function`.
187196 flat_outputs = forward_function . Call ( args_with_tangents ) ;
188197 }
189198 forward_backward . Record ( flat_outputs ) ;
190- return flat_outputs ;
199+ return _build_call_outputs ( flat_outputs ) ;
191200 }
192201
193202 public void AddTograph ( Graph ? g = null )
@@ -262,6 +271,13 @@ internal void _initialize_function_spec()
262271 } ;
263272 }
264273
274+ private Tensors _build_call_outputs ( Tensors result )
275+ {
276+ // TODO(Rinne): dwal with `func_graph.structured_outputs`
277+
278+ return result ;
279+ }
280+
265281 public override string ToString ( )
266282 => Name ;
267283 }
0 commit comments