Skip to content

Commit 838b12e

Browse files
committed
CApiVariableTest
1 parent 9665bf5 commit 838b12e

File tree

6 files changed

+161
-8
lines changed

6 files changed

+161
-8
lines changed

src/TensorFlowNET.Core/Eager/Context.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@
44

55
namespace Tensorflow.Eager
66
{
7-
public class Context
7+
public class Context : IDisposable
88
{
9+
private IntPtr _handle;
10+
911
public static int GRAPH_MODE = 0;
1012
public static int EAGER_MODE = 1;
1113

1214
public int default_execution_mode;
1315

16+
public Context(ContextOptions opts, Status status)
17+
{
18+
_handle = c_api.TFE_NewContext(opts, status);
19+
status.Check(true);
20+
}
21+
22+
public void Dispose()
23+
{
24+
c_api.TFE_DeleteContext(_handle);
25+
}
1426

27+
public static implicit operator IntPtr(Context ctx)
28+
{
29+
return ctx._handle;
30+
}
1531
}
1632
}

src/TensorFlowNET.Core/Eager/ContextOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void Dispose()
1818
c_api.TFE_DeleteContextOptions(_handle);
1919
}
2020

21-
public static implicit operator IntPtr(ContextOptions ctx)
21+
public static implicit operator IntPtr(ContextOptions opts)
2222
{
23-
return ctx._handle;
23+
return opts._handle;
2424
}
2525
}
2626
}

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

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,106 @@ public partial class c_api
1010
/// <summary>
1111
/// Return a new options object.
1212
/// </summary>
13-
/// <returns></returns>
13+
/// <returns>TFE_ContextOptions*</returns>
1414
[DllImport(TensorFlowLibName)]
1515
public static extern IntPtr TFE_NewContextOptions();
1616

1717
/// <summary>
1818
/// Destroy an options object.
1919
/// </summary>
20-
/// <param name="options"></param>
20+
/// <param name="options">TFE_ContextOptions*</param>
2121
[DllImport(TensorFlowLibName)]
2222
public static extern void TFE_DeleteContextOptions(IntPtr options);
2323

24+
/// <summary>
25+
///
26+
/// </summary>
27+
/// <param name="opts">const TFE_ContextOptions*</param>
28+
/// <param name="status">TF_Status*</param>
29+
/// <returns>TFE_Context*</returns>
2430
[DllImport(TensorFlowLibName)]
2531
public static extern IntPtr TFE_NewContext(IntPtr opts, IntPtr status);
2632

33+
/// <summary>
34+
///
35+
/// </summary>
36+
/// <param name="ctx">TFE_Context*</param>
2737
[DllImport(TensorFlowLibName)]
2838
public static extern void TFE_DeleteContext(IntPtr ctx);
2939

40+
/// <summary>
41+
/// Execute the operation defined by 'op' and return handles to computed
42+
/// tensors in `retvals`.
43+
/// </summary>
44+
/// <param name="op">TFE_Op*</param>
45+
/// <param name="retvals">TFE_TensorHandle**</param>
46+
/// <param name="num_retvals">int*</param>
47+
/// <param name="status">TF_Status*</param>
48+
[DllImport(TensorFlowLibName)]
49+
public static extern void TFE_Execute(IntPtr op, IntPtr retvals, int[] num_retvals, IntPtr status);
50+
51+
/// <summary>
52+
///
53+
/// </summary>
54+
/// <param name="ctx">TFE_Context*</param>
55+
/// <param name="op_or_function_name">const char*</param>
56+
/// <param name="status">TF_Status*</param>
57+
/// <returns></returns>
3058
[DllImport(TensorFlowLibName)]
3159
public static extern IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status);
60+
61+
/// <summary>
62+
///
63+
/// </summary>
64+
/// <param name="op">TFE_Op*</param>
65+
[DllImport(TensorFlowLibName)]
66+
public static extern void TFE_DeleteOp(IntPtr op);
67+
68+
/// <summary>
69+
///
70+
/// </summary>
71+
/// <param name="op">TFE_Op*</param>
72+
/// <param name="attr_name">const char*</param>
73+
/// <param name="value">TF_DataType</param>
74+
[DllImport(TensorFlowLibName)]
75+
public static extern void TFE_OpSetAttrType(IntPtr op, string attr_name, TF_DataType value);
76+
77+
/// <summary>
78+
///
79+
/// </summary>
80+
/// <param name="op">TFE_Op*</param>
81+
/// <param name="attr_name">const char*</param>
82+
/// <param name="dims">const int64_t*</param>
83+
/// <param name="num_dims">const int</param>
84+
/// <param name="out_status">TF_Status*</param>
85+
[DllImport(TensorFlowLibName)]
86+
public static extern void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, Status out_status);
87+
88+
/// <summary>
89+
///
90+
/// </summary>
91+
/// <param name="op">TFE_Op*</param>
92+
/// <param name="attr_name">const char*</param>
93+
/// <param name="value">const void*</param>
94+
/// <param name="length">size_t</param>
95+
[DllImport(TensorFlowLibName)]
96+
public static extern void TFE_OpSetAttrString(IntPtr op, string attr_name, string value, uint length);
97+
98+
/// <summary>
99+
///
100+
/// </summary>
101+
/// <param name="op">TFE_Op*</param>
102+
/// <param name="h">TFE_TensorHandle*</param>
103+
/// <param name="status">TF_Status*</param>
104+
[DllImport(TensorFlowLibName)]
105+
public static extern void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status);
106+
107+
/// <summary>
108+
///
109+
/// </summary>
110+
/// <param name="t">const tensorflow::Tensor&</param>
111+
/// <returns>TFE_TensorHandle*</returns>
112+
[DllImport(TensorFlowLibName)]
113+
public static extern IntPtr TFE_NewTensorHandle(IntPtr t);
32114
}
33115
}

src/TensorFlowNET.Core/ops.name_scope.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using Tensorflow.Eager;
45

56
namespace Tensorflow
67
{
@@ -20,7 +21,7 @@ public name_scope(string name, string default_name = "", List<T> values = null)
2021
_name = name;
2122
_default_name = default_name;
2223
_values = values;
23-
_ctx = new Context();
24+
// _ctx = new Context();
2425
}
2526

2627
public string __enter__()

src/TensorFlowNET.Core/tf.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using Tensorflow.Eager;
45

56
namespace Tensorflow
67
{
@@ -12,7 +13,7 @@ public static partial class tf
1213
public static TF_DataType float64 = TF_DataType.TF_DOUBLE;
1314
public static TF_DataType chars = TF_DataType.TF_STRING;
1415

15-
public static Context context = new Context();
16+
public static Context context;
1617

1718
public static Graph g = new Graph(c_api.TF_NewGraph());
1819

@@ -28,6 +29,7 @@ public static unsafe Tensor placeholder(TF_DataType dtype, TensorShape shape = n
2829

2930
public static void enable_eager_execution()
3031
{
32+
// contex = new Context();
3133
context.default_execution_mode = Context.EAGER_MODE;
3234
}
3335

test/TensorFlowNET.UnitTest/Eager/CApiVariableTest.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Text;
55
using Tensorflow;
6+
using Tensorflow.Eager;
67

78
namespace TensorFlowNET.UnitTest.Eager
89
{
@@ -13,16 +14,67 @@ namespace TensorFlowNET.UnitTest.Eager
1314
public class CApiVariableTest : CApiTest, IDisposable
1415
{
1516
Status status = new Status();
17+
ContextOptions opts = new ContextOptions();
18+
Context ctx;
1619

1720
[TestMethod]
1821
public void Variables()
1922
{
23+
ctx = new Context(opts, status);
24+
ASSERT_EQ(TF_Code.TF_OK, status.Code);
25+
opts.Dispose();
2026

27+
var var_handle = CreateVariable(ctx, 12.0F);
28+
ASSERT_EQ(TF_OK, TF_GetCode(status));
29+
}
30+
31+
private IntPtr CreateVariable(Context ctx, float value)
32+
{
33+
// Create the variable handle.
34+
var op = c_api.TFE_NewOp(ctx, "VarHandleOp", status);
35+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
36+
37+
c_api.TFE_OpSetAttrType(op, "dtype", TF_DataType.TF_FLOAT);
38+
c_api.TFE_OpSetAttrShape(op, "shape", new long[0], 0, status);
39+
c_api.TFE_OpSetAttrString(op, "container", "", 0);
40+
c_api.TFE_OpSetAttrString(op, "shared_name", "", 0);
41+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
42+
var var_handle = IntPtr.Zero;
43+
int[] num_retvals = { 1 };
44+
c_api.TFE_Execute(op, var_handle, num_retvals, status);
45+
c_api.TFE_DeleteOp(op);
46+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
47+
ASSERT_EQ(1, num_retvals);
48+
49+
// Assign 'value' to it.
50+
op = c_api.TFE_NewOp(ctx, "AssignVariableOp", status);
51+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
52+
c_api.TFE_OpSetAttrType(op, "dtype", TF_FLOAT);
53+
c_api.TFE_OpAddInput(op, var_handle, status);
54+
55+
// Convert 'value' to a TF_Tensor then a TFE_TensorHandle.
56+
var t = new Tensor(value);
57+
58+
var value_handle = c_api.TFE_NewTensorHandle(t);
59+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
60+
61+
c_api.TFE_OpAddInput(op, value_handle, status);
62+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
63+
64+
num_retvals = new int[] { 0 };
65+
c_api.TFE_Execute(op, IntPtr.Zero, num_retvals, status);
66+
c_api.TFE_DeleteOp(op);
67+
if (TF_GetCode(status) != TF_OK) return IntPtr.Zero;
68+
ASSERT_EQ(0, num_retvals);
69+
70+
return var_handle;
2171
}
2272

2373
public void Dispose()
2474
{
25-
75+
status.Dispose();
76+
opts.Dispose();
77+
ctx.Dispose();
2678
}
2779
}
2880
}

0 commit comments

Comments
 (0)