You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CApi.Eager.OpGetInputAndOutputLengths.cs 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow;
  3. using Tensorflow.Eager;
  4. namespace TensorFlowNET.UnitTest.NativeAPI
  5. {
  6. public partial class CApiEagerTest
  7. {
  8. /// <summary>
  9. /// TEST(CAPI, TestTFE_OpGetInputAndOutputLengths)
  10. /// </summary>
  11. [TestMethod]
  12. public unsafe void OpGetInputAndOutputLengths()
  13. {
  14. using var status = TF_NewStatus();
  15. static SafeContextHandle NewContext(SafeStatusHandle status)
  16. {
  17. using var opts = c_api.TFE_NewContextOptions();
  18. return c_api.TFE_NewContext(opts, status);
  19. }
  20. using var ctx = NewContext(status);
  21. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  22. using var input1 = TestMatrixTensorHandle();
  23. using var input2 = TestMatrixTensorHandle();
  24. var retvals = new SafeTensorHandleHandle[2];
  25. using (var identityOp = TFE_NewOp(ctx, "IdentityN", status))
  26. {
  27. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  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. var inputs = new SafeTensorHandleHandle[] { input1, input2 };
  34. TFE_OpAddInputList(identityOp, inputs, 2, status);
  35. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  36. // Try to retrieve lengths before executing the op (should work)
  37. EXPECT_EQ(2, TFE_OpGetInputLength(identityOp, "input", status));
  38. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  39. EXPECT_EQ(2, TFE_OpGetOutputLength(identityOp, "output", status));
  40. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  41. int num_retvals;
  42. TFE_Execute(identityOp, retvals, out num_retvals, status);
  43. EXPECT_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  44. EXPECT_EQ(2, num_retvals);
  45. try
  46. {
  47. // Try to retrieve lengths after executing the op (should work)
  48. EXPECT_EQ(2, TFE_OpGetInputLength(identityOp, "input", status));
  49. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  50. EXPECT_EQ(2, TFE_OpGetOutputLength(identityOp, "output", status));
  51. CHECK_EQ(TF_OK, TF_GetCode(status), TF_Message(status));
  52. }
  53. finally
  54. {
  55. retvals[0].Dispose();
  56. retvals[1].Dispose();
  57. }
  58. }
  59. }
  60. }
  61. }