Skip to content

Commit fca0368

Browse files
committed
addInConstant unit test successfully
1 parent 2300e61 commit fca0368

6 files changed

Lines changed: 109 additions & 16 deletions

File tree

‎src/TensorFlowNET.Core/Session/BaseSession.cs‎

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using NumSharp.Core;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56

67
namespace 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
}

‎src/TensorFlowNET.Core/Session/_ElementFetchMapper.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public _ElementFetchMapper(List<Tensor> fetches, Action contraction_fn)
2121
}
2222
}
2323

24+
public object build_results(object[] values)
25+
{
26+
return values[0];
27+
}
28+
2429
public List<Object> unique_fetches()
2530
{
2631
return _unique_fetches;

‎src/TensorFlowNET.Core/Session/_FetchHandler.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class _FetchHandler
1313
private List<object> _fetches = new List<object>();
1414
private List<bool> _ops = new List<bool>();
1515
private List<object> _final_fetches = new List<object>();
16+
private List<object> _targets = new List<object>();
1617

1718
public _FetchHandler(Graph graph, Tensor fetches, object feeds = null, object feed_handles = null)
1819
{
@@ -33,12 +34,27 @@ public _FetchHandler(Graph graph, Tensor fetches, object feeds = null, object fe
3334
_final_fetches = _fetches;
3435
}
3536

37+
public object build_results(Session session, object[] results)
38+
{
39+
return _fetch_mapper.build_results(results);
40+
}
41+
3642
private void _assert_fetchable(Graph graph, Operation op)
3743
{
3844
if (!graph.is_fetchable(op))
3945
{
4046
throw new Exception($"Operation {op.name} has been marked as not fetchable.");
4147
}
4248
}
49+
50+
public List<Object> fetches()
51+
{
52+
return _final_fetches;
53+
}
54+
55+
public List<Object> targets()
56+
{
57+
return _targets;
58+
}
4359
}
4460
}

‎src/TensorFlowNET.Core/Tensor.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public class Tensor
1717

1818
public string name;
1919

20+
private readonly IntPtr _handle;
21+
public IntPtr handle => _handle;
22+
public IntPtr buffer => c_api.TF_TensorData(_handle);
23+
24+
public Tensor(IntPtr handle)
25+
{
26+
_handle = handle;
27+
}
28+
2029
public Tensor(Operation op, int value_index, DataType dtype)
2130
{
2231
_op = op;

‎src/TensorFlowNET.Core/c_api.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public static extern unsafe void TF_SessionRun(TF_Session session, IntPtr run_op
7777
[DllImport(TensorFlowLibName)]
7878
public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value);
7979

80+
[DllImport(TensorFlowLibName)]
81+
public static extern unsafe IntPtr TF_TensorData(TF_Tensor tensor);
82+
8083
[DllImport(TensorFlowLibName)]
8184
public static extern TF_Session TF_NewSession(TF_Graph graph, TF_SessionOptions opts, TF_Status status);
8285

‎test/TensorFlowNET.UnitTest/OperationsTest.cs‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ public class OperationsTest
1212
[TestMethod]
1313
public void constant()
1414
{
15-
var a = tf.constant(4.0f);
16-
var b = tf.constant(5.0f);
17-
var c = tf.add(a, b);
18-
using (var sess = tf.Session())
19-
{
20-
var o = sess.run(c);
21-
}
15+
var x = tf.constant(4.0f);
2216
}
2317

2418
[TestMethod]
@@ -28,7 +22,7 @@ public void placeholder()
2822
}
2923

3024
[TestMethod]
31-
public void add()
25+
public void addInPlaceholder()
3226
{
3327
var a = tf.placeholder(tf.float32);
3428
var b = tf.placeholder(tf.float32);
@@ -43,5 +37,19 @@ public void add()
4337
var o = sess.run(c, feed_dict);
4438
}
4539
}
40+
41+
[TestMethod]
42+
public void addInConstant()
43+
{
44+
var a = tf.constant(4.0f);
45+
var b = tf.constant(5.0f);
46+
var c = tf.add(a, b);
47+
48+
using (var sess = tf.Session())
49+
{
50+
var o = sess.run(c);
51+
Assert.AreEqual(o, 9.0f);
52+
}
53+
}
4654
}
4755
}

0 commit comments

Comments
 (0)