11using NumSharp . Core ;
22using System ;
33using System . Collections . Generic ;
4+ using System . Linq ;
45using System . Text ;
56
67namespace Tensorflow
@@ -38,12 +39,14 @@ public void Dispose()
3839
3940 }
4041
41- public virtual byte [ ] run ( Tensor fetches , Dictionary < Tensor , object > feed_dict = null )
42+ public virtual object run ( Tensor fetches , Dictionary < Tensor , object > feed_dict = null )
4243 {
43- return _run ( fetches , feed_dict ) ;
44+ var result = _run ( fetches , feed_dict ) ;
45+
46+ return result ;
4447 }
4548
46- private unsafe byte [ ] _run ( Tensor fetches , Dictionary < Tensor , object > feed_dict = null )
49+ private unsafe object _run ( Tensor fetches , Dictionary < Tensor , object > feed_dict = null )
4750 {
4851 var feed_dict_tensor = new Dictionary < Tensor , NDArray > ( ) ;
4952
@@ -66,22 +69,71 @@ private unsafe byte[] _run(Tensor fetches, Dictionary<Tensor, object> feed_dict
6669 // Create a fetch handler to take care of the structure of fetches.
6770 var fetch_handler = new _FetchHandler ( _graph , fetches ) ;
6871
72+ // Run request and get response.
73+ // We need to keep the returned movers alive for the following _do_run().
74+ // These movers are no longer needed when _do_run() completes, and
75+ // are deleted when `movers` goes out of scope when this _run() ends.
76+ var _ = _update_with_movers ( ) ;
77+ var final_fetches = fetch_handler . fetches ( ) ;
78+ var final_targets = fetch_handler . targets ( ) ;
79+
80+ // We only want to really perform the run if fetches or targets are provided,
81+ // or if the call is a partial run that specifies feeds.
82+ var results = _do_run ( final_fetches ) ;
83+
84+ return fetch_handler . build_results ( null , results ) ;
85+ }
86+
87+ private object [ ] _do_run ( List < object > fetch_list )
88+ {
89+ var fetches = fetch_list . Select ( x => ( x as Tensor ) . _as_tf_output ( ) ) . ToArray ( ) ;
90+
91+ return _call_tf_sessionrun ( fetches ) ;
92+ }
93+
94+ private unsafe object [ ] _call_tf_sessionrun ( TF_Output [ ] fetch_list )
95+ {
96+ // Ensure any changes to the graph are reflected in the runtime.
97+ _extend_graph ( ) ;
98+
6999 var status = new Status ( ) ;
70100
101+ var output_values = fetch_list . Select ( x => IntPtr . Zero ) . ToArray ( ) ;
102+
71103 c_api . TF_SessionRun ( _session ,
72104 run_options : IntPtr . Zero ,
73105 inputs : new TF_Output [ ] { } ,
74106 input_values : new IntPtr [ ] { } ,
75107 ninputs : 0 ,
76- outputs : new TF_Output [ ] { new TF_Output ( ) } ,
77- output_values : new IntPtr [ ] { } ,
78- noutputs : 1 ,
108+ outputs : fetch_list ,
109+ output_values : output_values ,
110+ noutputs : fetch_list . Length ,
79111 target_opers : new IntPtr [ ] { } ,
80- ntargets : 1 ,
112+ ntargets : 0 ,
81113 run_metadata : IntPtr . Zero ,
82114 status : status . Handle ) ;
83115
84- return null ;
116+ var result = output_values . Select ( x => new Tensor ( x ) . buffer ) . Select ( x =>
117+ {
118+ return ( object ) * ( float * ) x ;
119+ } ) . ToArray ( ) ;
120+
121+ return result ;
122+ }
123+
124+ /// <summary>
125+ /// If a tensor handle that is fed to a device incompatible placeholder,
126+ /// we move the tensor to the right device, generate a new tensor handle,
127+ /// and update feed_dict to use the new handle.
128+ /// </summary>
129+ private List < object > _update_with_movers ( )
130+ {
131+ return new List < object > { } ;
132+ }
133+
134+ private void _extend_graph ( )
135+ {
136+
85137 }
86138 }
87139}
0 commit comments