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.

CApiTest.cs 5.1 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using Tensorflow;
  4. using Buffer = System.Buffer;
  5. namespace TensorFlowNET.UnitTest
  6. {
  7. public class CApiTest
  8. {
  9. protected TF_Code TF_OK = TF_Code.TF_OK;
  10. protected TF_DataType TF_FLOAT = TF_DataType.TF_FLOAT;
  11. protected void EXPECT_TRUE(bool expected, string msg = "")
  12. => Assert.IsTrue(expected, msg);
  13. protected void EXPECT_EQ(object expected, object actual, string msg = "")
  14. => Assert.AreEqual(expected, actual, msg);
  15. protected void CHECK_EQ(object expected, object actual, string msg = "")
  16. => Assert.AreEqual(expected, actual, msg);
  17. protected void EXPECT_NE(object expected, object actual, string msg = "")
  18. => Assert.AreNotEqual(expected, actual, msg);
  19. protected void EXPECT_GE(int expected, int actual, string msg = "")
  20. => Assert.IsTrue(expected >= actual, msg);
  21. protected void ASSERT_EQ(object expected, object actual, string msg = "")
  22. => Assert.AreEqual(expected, actual, msg);
  23. protected void ASSERT_TRUE(bool condition, string msg = "")
  24. => Assert.IsTrue(condition, msg);
  25. protected OperationDescription TF_NewOperation(Graph graph, string opType, string opName)
  26. => c_api.TF_NewOperation(graph, opType, opName);
  27. protected void TF_AddInput(OperationDescription desc, TF_Output input)
  28. => c_api.TF_AddInput(desc, input);
  29. protected Operation TF_FinishOperation(OperationDescription desc, Status s)
  30. => c_api.TF_FinishOperation(desc, s);
  31. protected void TF_SetAttrTensor(OperationDescription desc, string attrName, Tensor value, Status s)
  32. => c_api.TF_SetAttrTensor(desc, attrName, value, s);
  33. protected void TF_SetAttrType(OperationDescription desc, string attrName, TF_DataType dtype)
  34. => c_api.TF_SetAttrType(desc, attrName, dtype);
  35. protected void TF_SetAttrBool(OperationDescription desc, string attrName, bool value)
  36. => c_api.TF_SetAttrBool(desc, attrName, value);
  37. protected TF_DataType TFE_TensorHandleDataType(IntPtr h)
  38. => c_api.TFE_TensorHandleDataType(h);
  39. protected TF_Code TF_GetCode(Status s)
  40. => s.Code;
  41. protected TF_Code TF_GetCode(IntPtr s)
  42. => c_api.TF_GetCode(s);
  43. protected string TF_Message(IntPtr s)
  44. => c_api.StringPiece(c_api.TF_Message(s));
  45. protected IntPtr TF_NewStatus()
  46. => c_api.TF_NewStatus();
  47. protected void TF_DeleteStatus(IntPtr s)
  48. => c_api.TF_DeleteStatus(s);
  49. protected IntPtr TF_TensorData(IntPtr t)
  50. => c_api.TF_TensorData(t);
  51. protected ulong TF_TensorByteSize(IntPtr t)
  52. => c_api.TF_TensorByteSize(t);
  53. protected void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status)
  54. => c_api.TFE_OpAddInput(op, h, status);
  55. protected void TFE_OpSetAttrType(IntPtr op, string attr_name, TF_DataType value)
  56. => c_api.TFE_OpSetAttrType(op, attr_name, value);
  57. protected IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status)
  58. => c_api.TFE_NewOp(ctx, op_or_function_name, status);
  59. protected IntPtr TFE_NewContextOptions()
  60. => c_api.TFE_NewContextOptions();
  61. protected void TFE_DeleteContext(IntPtr t)
  62. => c_api.TFE_DeleteContext(t);
  63. protected IntPtr TFE_NewContext(IntPtr opts, IntPtr status)
  64. => c_api.TFE_NewContext(opts, status);
  65. protected void TFE_DeleteContextOptions(IntPtr opts)
  66. => c_api.TFE_DeleteContextOptions(opts);
  67. protected void TFE_DeleteTensorHandle(IntPtr h)
  68. => c_api.TFE_DeleteTensorHandle(h);
  69. protected void TFE_DeleteOp(IntPtr op)
  70. => c_api.TFE_DeleteOp(op);
  71. protected IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status)
  72. => c_api.TFE_TensorHandleResolve(h, status);
  73. protected unsafe void memcpy(void * src, IntPtr dst, ulong size)
  74. {
  75. Buffer.MemoryCopy(src, dst.ToPointer(), size, size);
  76. }
  77. protected unsafe void memcpy<T>(T[] src, IntPtr dst, ulong size)
  78. where T : unmanaged
  79. {
  80. fixed (void* p = &src[0])
  81. Buffer.MemoryCopy(p, dst.ToPointer(), size, size);
  82. }
  83. protected unsafe void memcpy<T>(T[] src, IntPtr dst, long size)
  84. where T : unmanaged
  85. {
  86. fixed (void* p = &src[0])
  87. Buffer.MemoryCopy(p, dst.ToPointer(), size, size);
  88. }
  89. protected unsafe void memcpy<T>(IntPtr src, T[] dst, ulong size)
  90. where T : unmanaged
  91. {
  92. fixed (void* p = &dst[0])
  93. Buffer.MemoryCopy(src.ToPointer(), p, size, size);
  94. }
  95. protected unsafe void memcpy<T>(IntPtr src, T[] dst, long size)
  96. where T: unmanaged
  97. {
  98. fixed (void* p = &dst[0])
  99. Buffer.MemoryCopy(src.ToPointer(), p, size, size);
  100. }
  101. }
  102. }