|
| 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