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

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 int TFE_TensorHandleNumDims(IntPtr h, IntPtr status)
  40. => c_api.TFE_TensorHandleNumDims(h, status);
  41. protected TF_Code TF_GetCode(Status s)
  42. => s.Code;
  43. protected TF_Code TF_GetCode(IntPtr s)
  44. => c_api.TF_GetCode(s);
  45. protected string TF_Message(IntPtr s)
  46. => c_api.StringPiece(c_api.TF_Message(s));
  47. protected IntPtr TF_NewStatus()
  48. => c_api.TF_NewStatus();
  49. protected void TF_DeleteStatus(IntPtr s)
  50. => c_api.TF_DeleteStatus(s);
  51. protected IntPtr TF_TensorData(IntPtr t)
  52. => c_api.TF_TensorData(t);
  53. protected ulong TF_TensorByteSize(IntPtr t)
  54. => c_api.TF_TensorByteSize(t);
  55. protected void TFE_OpAddInput(IntPtr op, IntPtr h, IntPtr status)
  56. => c_api.TFE_OpAddInput(op, h, status);
  57. protected void TFE_OpSetAttrType(IntPtr op, string attr_name, TF_DataType value)
  58. => c_api.TFE_OpSetAttrType(op, attr_name, value);
  59. protected void TFE_OpSetAttrShape(IntPtr op, string attr_name, long[] dims, int num_dims, IntPtr out_status)
  60. => c_api.TFE_OpSetAttrShape(op, attr_name, dims, num_dims, out_status);
  61. protected void TFE_OpSetAttrString(IntPtr op, string attr_name, string value, uint length)
  62. => c_api.TFE_OpSetAttrString(op, attr_name, value, length);
  63. protected IntPtr TFE_NewOp(IntPtr ctx, string op_or_function_name, IntPtr status)
  64. => c_api.TFE_NewOp(ctx, op_or_function_name, status);
  65. protected void TFE_Execute(IntPtr op, IntPtr[] retvals, ref int num_retvals, IntPtr status)
  66. => c_api.TFE_Execute(op, retvals, ref num_retvals, status);
  67. protected IntPtr TFE_NewContextOptions()
  68. => c_api.TFE_NewContextOptions();
  69. protected void TFE_DeleteContext(IntPtr t)
  70. => c_api.TFE_DeleteContext(t);
  71. protected IntPtr TFE_NewContext(IntPtr opts, IntPtr status)
  72. => c_api.TFE_NewContext(opts, status);
  73. protected void TFE_DeleteContextOptions(IntPtr opts)
  74. => c_api.TFE_DeleteContextOptions(opts);
  75. protected void TFE_DeleteTensorHandle(IntPtr h)
  76. => c_api.TFE_DeleteTensorHandle(h);
  77. protected void TFE_DeleteOp(IntPtr op)
  78. => c_api.TFE_DeleteOp(op);
  79. protected void TFE_DeleteExecutor(IntPtr executor)
  80. => c_api.TFE_DeleteExecutor(executor);
  81. protected IntPtr TFE_ContextGetExecutorForThread(IntPtr ctx)
  82. => c_api.TFE_ContextGetExecutorForThread(ctx);
  83. protected void TFE_ExecutorWaitForAllPendingNodes(IntPtr executor, IntPtr status)
  84. => c_api.TFE_ExecutorWaitForAllPendingNodes(executor, status);
  85. protected IntPtr TFE_TensorHandleResolve(IntPtr h, IntPtr status)
  86. => c_api.TFE_TensorHandleResolve(h, status);
  87. protected string TFE_TensorHandleDeviceName(IntPtr h, IntPtr status)
  88. => c_api.StringPiece(c_api.TFE_TensorHandleDeviceName(h, status));
  89. protected string TFE_TensorHandleBackingDeviceName(IntPtr h, IntPtr status)
  90. => c_api.StringPiece(c_api.TFE_TensorHandleBackingDeviceName(h, status));
  91. protected IntPtr TFE_ContextListDevices(IntPtr ctx, IntPtr status)
  92. => c_api.TFE_ContextListDevices(ctx, status);
  93. protected int TF_DeviceListCount(IntPtr list)
  94. => c_api.TF_DeviceListCount(list);
  95. protected string TF_DeviceListType(IntPtr list, int index, IntPtr status)
  96. => c_api.StringPiece(c_api.TF_DeviceListType(list, index, status));
  97. protected string TF_DeviceListName(IntPtr list, int index, IntPtr status)
  98. => c_api.StringPiece(c_api.TF_DeviceListName(list, index, status));
  99. protected void TF_DeleteDeviceList(IntPtr list)
  100. => c_api.TF_DeleteDeviceList(list);
  101. protected IntPtr TFE_TensorHandleCopyToDevice(IntPtr h, IntPtr ctx, string device_name, IntPtr status)
  102. => c_api.TFE_TensorHandleCopyToDevice(h, ctx, device_name, status);
  103. protected void TFE_OpSetDevice(IntPtr op, string device_name, IntPtr status)
  104. => c_api.TFE_OpSetDevice(op, device_name, status);
  105. protected unsafe void memcpy<T>(T* dst, void* src, ulong size)
  106. where T : unmanaged
  107. {
  108. Buffer.MemoryCopy(src, dst, size, size);
  109. }
  110. protected unsafe void memcpy<T>(void* dst, T* src, ulong size)
  111. where T : unmanaged
  112. {
  113. Buffer.MemoryCopy(src, dst, size, size);
  114. }
  115. protected unsafe void memcpy(void * dst, IntPtr src, ulong size)
  116. {
  117. Buffer.MemoryCopy(src.ToPointer(), dst, size, size);
  118. }
  119. protected unsafe void memcpy<T>(T[] dst, IntPtr src, ulong size)
  120. where T : unmanaged
  121. {
  122. fixed (void* p = &dst[0])
  123. Buffer.MemoryCopy(src.ToPointer(), p, size, size);
  124. }
  125. protected unsafe void memcpy<T>(T[] dst, IntPtr src, long size)
  126. where T : unmanaged
  127. {
  128. fixed (void* p = &dst[0])
  129. Buffer.MemoryCopy(src.ToPointer(), p, size, size);
  130. }
  131. protected unsafe void memcpy<T>(IntPtr dst, T[] src, ulong size)
  132. where T : unmanaged
  133. {
  134. fixed (void* p = &src[0])
  135. Buffer.MemoryCopy(p, dst.ToPointer(), size, size);
  136. }
  137. protected unsafe void memcpy<T>(IntPtr dst, T[] src, long size)
  138. where T: unmanaged
  139. {
  140. fixed (void* p = &src[0])
  141. Buffer.MemoryCopy(p, dst.ToPointer(), size, size);
  142. }
  143. }
  144. }