Skip to content

Commit e784618

Browse files
committed
TF_Status holds error information.
1 parent 91befe9 commit e784618

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

src/TensorFlowNET.Core/Status.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace Tensorflow
66
{
7-
public class Status
7+
public class Status : IDisposable
88
{
9-
private IntPtr _handle;
9+
private readonly IntPtr _handle;
1010
public IntPtr Handle => _handle;
1111

1212
/// <summary>
@@ -23,5 +23,15 @@ public Status()
2323
{
2424
_handle = c_api.TF_NewStatus();
2525
}
26+
27+
public void SetStatus(TF_Code code, string msg)
28+
{
29+
c_api.TF_SetStatus(_handle, code, msg);
30+
}
31+
32+
public void Dispose()
33+
{
34+
c_api.TF_DeleteStatus(_handle);
35+
}
2636
}
2737
}

src/TensorFlowNET.Core/c_api.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public static class c_api
2828
[DllImport(TensorFlowLibName)]
2929
public static unsafe extern void TF_DeleteSessionOptions(IntPtr opts);
3030

31+
/// <summary>
32+
/// Delete a previously created status object.
33+
/// </summary>
34+
/// <param name="s"></param>
35+
[DllImport(TensorFlowLibName)]
36+
public static unsafe extern void TF_DeleteStatus(IntPtr s);
37+
3138
/// <summary>
3239
/// Destroy a tensor.
3340
/// </summary>
@@ -59,6 +66,10 @@ public static class c_api
5966
[DllImport(TensorFlowLibName)]
6067
public static unsafe extern TF_OperationDescription TF_NewOperation(IntPtr graph, string opType, string oper_name);
6168

69+
/// <summary>
70+
/// Return a new status object.
71+
/// </summary>
72+
/// <returns></returns>
6273
[DllImport(TensorFlowLibName)]
6374
public static unsafe extern IntPtr TF_NewStatus();
6475

@@ -121,6 +132,9 @@ public static extern unsafe void TF_SessionRun(IntPtr session, IntPtr run_option
121132
[DllImport(TensorFlowLibName)]
122133
public static extern unsafe void TF_SetAttrType(TF_OperationDescription desc, string attr_name, TF_DataType value);
123134

135+
[DllImport(TensorFlowLibName)]
136+
public static extern void TF_SetStatus(IntPtr s, TF_Code code, string msg);
137+
124138
/// <summary>
125139
/// Return the size of the underlying data in bytes.
126140
/// </summary>

test/TensorFlowNET.UnitTest/OperationsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void addInPlaceholder()
3434
feed_dict.Add(a, 3.0f);
3535
feed_dict.Add(b, 2.0f);
3636

37-
//var o = sess.run(c, feed_dict);
37+
var o = sess.run(c, feed_dict);
3838
}
3939
}
4040

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Tensorflow;
6+
7+
namespace TensorFlowNET.UnitTest
8+
{
9+
[TestClass]
10+
public class StatusTest
11+
{
12+
[TestMethod]
13+
public void NewStatus()
14+
{
15+
var s = new Status();
16+
Assert.AreEqual(s.Code, TF_Code.TF_OK);
17+
Assert.AreEqual(s.Message, String.Empty);
18+
}
19+
20+
[TestMethod]
21+
public void SetStatus()
22+
{
23+
var s = new Status();
24+
s.SetStatus(TF_Code.TF_CANCELLED, "cancel");
25+
Assert.AreEqual(s.Code, TF_Code.TF_CANCELLED);
26+
// Assert.AreEqual(s.Message, "cancel");
27+
}
28+
29+
[TestMethod]
30+
public void DeleteStatus()
31+
{
32+
var s = new Status();
33+
s.Dispose();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)