Skip to content

Commit da41466

Browse files
committed
TestTFE_OpGetInputAndOutputLengths
1 parent 4076cd1 commit da41466

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@ public partial class c_api
2020
[DllImport(TensorFlowLibName)]
2121
public static extern void TFE_DeleteContextOptions(IntPtr options);
2222

23+
/// <summary>
24+
/// Returns the length (number of tensors) of the input argument `input_name`
25+
/// found in the provided `op`.
26+
/// </summary>
27+
/// <param name="op">TFE_Op*</param>
28+
/// <param name="input_name">const char*</param>
29+
/// <param name="status">TF_Status*</param>
30+
[DllImport(TensorFlowLibName)]
31+
public static extern int TFE_OpGetInputLength(IntPtr op, string input_name, IntPtr status);
32+
33+
/// <summary>
34+
/// Returns the length (number of tensors) of the output argument `output_name`
35+
/// found in the provided `op`.
36+
/// </summary>
37+
/// <param name="op"></param>
38+
/// <param name="input_name"></param>
39+
/// <param name="status"></param>
40+
/// <returns></returns>
41+
[DllImport(TensorFlowLibName)]
42+
public static extern int TFE_OpGetOutputLength(IntPtr op, string input_name, IntPtr status);
43+
44+
/// <summary>
45+
///
46+
/// </summary>
47+
/// <param name="op">TFE_Op*</param>
48+
/// <param name="inputs">TFE_TensorHandle**</param>
49+
/// <param name="num_inputs">int</param>
50+
/// <param name="status">TF_Status*</param>
51+
/// <returns></returns>
52+
[DllImport(TensorFlowLibName)]
53+
public static extern int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, IntPtr status);
54+
2355
/// <summary>
2456
///
2557
/// </summary>

‎test/TensorFlowNET.UnitTest/CApiTest.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ protected void CHECK_EQ(object expected, object actual, string msg = "")
2222
protected void EXPECT_NE(object expected, object actual, string msg = "")
2323
=> Assert.AreNotEqual(expected, actual, msg);
2424

25+
protected void CHECK_NE(object expected, object actual, string msg = "")
26+
=> Assert.AreNotEqual(expected, actual, msg);
27+
2528
protected void EXPECT_GE(int expected, int actual, string msg = "")
2629
=> Assert.IsTrue(expected >= actual, msg);
2730

@@ -106,6 +109,15 @@ protected IntPtr TFE_NewContext(IntPtr opts, IntPtr status)
106109
protected void TFE_DeleteContextOptions(IntPtr opts)
107110
=> c_api.TFE_DeleteContextOptions(opts);
108111

112+
protected int TFE_OpGetInputLength(IntPtr op, string input_name, IntPtr status)
113+
=> c_api.TFE_OpGetInputLength(op, input_name, status);
114+
115+
protected int TFE_OpAddInputList(IntPtr op, IntPtr[] inputs, int num_inputs, IntPtr status)
116+
=> c_api.TFE_OpAddInputList(op, inputs, num_inputs, status);
117+
118+
protected int TFE_OpGetOutputLength(IntPtr op, string input_name, IntPtr status)
119+
=> c_api.TFE_OpGetOutputLength(op, input_name, status);
120+
109121
protected void TFE_DeleteTensorHandle(IntPtr h)
110122
=> c_api.TFE_DeleteTensorHandle(h);
111123

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using Tensorflow;
4+
using Tensorflow.Eager;
5+
using Buffer = System.Buffer;
6+
7+
namespace TensorFlowNET.UnitTest.Eager
8+
{
9+
public partial class CApiEagerTest
10+
{
11+
/// <summary>
12+
/// TEST(CAPI, TestTFE_OpGetInputAndOutputLengths)
13+
/// </summary>
14+
[TestMethod]
15+
public unsafe void OpGetInputAndOutputLengths()
16+
{
17+
var status = TF_NewStatus();
18+
var opts = TFE_NewContextOptions();
19+
var ctx = TFE_NewContext(opts, status);
20+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
21+
TFE_DeleteContextOptions(opts);
22+
23+
var input1 = TestMatrixTensorHandle();
24+
var input2 = TestMatrixTensorHandle();
25+
var identityOp = TFE_NewOp(ctx, "IdentityN", status);
26+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
27+
28+
// Try to retrieve lengths before building the attributes (should fail)
29+
EXPECT_EQ(-1, TFE_OpGetInputLength(identityOp, "input", status));
30+
CHECK_NE(TF_OK, TF_GetCode(status), TF_Message(status));
31+
EXPECT_EQ(-1, TFE_OpGetOutputLength(identityOp, "output", status));
32+
CHECK_NE(TF_OK, TF_GetCode(status), TF_Message(status));
33+
34+
var inputs = new IntPtr[] { input1, input2 };
35+
TFE_OpAddInputList(identityOp, inputs, 2, status);
36+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
37+
38+
// Try to retrieve lengths before executing the op (should work)
39+
EXPECT_EQ(2, TFE_OpGetInputLength(identityOp, "input", status));
40+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
41+
EXPECT_EQ(2, TFE_OpGetOutputLength(identityOp, "output", status));
42+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
43+
44+
var retvals = new IntPtr[2];
45+
int num_retvals = 2;
46+
TFE_Execute(identityOp, retvals, ref num_retvals, status);
47+
EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
48+
49+
// Try to retrieve lengths after executing the op (should work)
50+
EXPECT_EQ(2, TFE_OpGetInputLength(identityOp, "input", status));
51+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
52+
EXPECT_EQ(2, TFE_OpGetOutputLength(identityOp, "output", status));
53+
CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
54+
55+
TF_DeleteStatus(status);
56+
TFE_DeleteOp(identityOp);
57+
TFE_DeleteTensorHandle(input1);
58+
TFE_DeleteTensorHandle(input2);
59+
TFE_DeleteTensorHandle(retvals[0]);
60+
TFE_DeleteTensorHandle(retvals[1]);
61+
TFE_DeleteContext(ctx);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)