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.

TensorTest.cs 7.6 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using NumSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Tensorflow;
  11. using static Tensorflow.Python;
  12. namespace TensorFlowNET.UnitTest
  13. {
  14. [TestClass]
  15. public class TensorTest : CApiTest
  16. {
  17. [TestMethod]
  18. public void TensorDeallocationThreadSafety()
  19. {
  20. var tensors = new Tensor[1000];
  21. foreach (var i in range(1000))
  22. {
  23. tensors[i] = new Tensor(new int[1000]);
  24. }
  25. SemaphoreSlim s = new SemaphoreSlim(0, 2);
  26. SemaphoreSlim s_done = new SemaphoreSlim(0, 2);
  27. var t1 = new Thread(() =>
  28. {
  29. s.Wait();
  30. foreach (var t in tensors)
  31. t.Dispose();
  32. s_done.Release();
  33. });
  34. var t2 = new Thread(() =>
  35. {
  36. s.Wait();
  37. foreach (var t in tensors)
  38. t.Dispose();
  39. s_done.Release();
  40. });
  41. t1.Start();
  42. t2.Start();
  43. s.Release(2);
  44. s_done.Wait();
  45. s_done.Wait();
  46. foreach (var t in tensors)
  47. Assert.IsTrue(t.IsDisposed);
  48. }
  49. [TestMethod]
  50. public void TensorFromSpan()
  51. {
  52. var array = new int[1000];
  53. var span = new Span<int>(array, 100, 500);
  54. using (var t = new Tensor(span, new long[] { span.Length }))
  55. {
  56. Assert.IsFalse(t.IsDisposed);
  57. Assert.AreEqual(2000, (int)t.bytesize);
  58. }
  59. }
  60. [TestMethod]
  61. public void AllocateTensor()
  62. {
  63. /*ulong num_bytes = 6 * sizeof(float);
  64. long[] dims = { 2, 3 };
  65. Tensor t = c_api.TF_AllocateTensor(TF_DataType.TF_FLOAT, dims, 2, num_bytes);
  66. EXPECT_EQ(TF_DataType.TF_FLOAT, t.dtype);
  67. EXPECT_EQ(2, t.NDims);
  68. Assert.IsTrue(Enumerable.SequenceEqual(dims, t.shape));
  69. EXPECT_EQ(num_bytes, t.bytesize);
  70. t.Dispose();*/
  71. }
  72. /// <summary>
  73. /// Port from c_api_test.cc
  74. /// `TEST(CAPI, MaybeMove)`
  75. /// </summary>
  76. [TestMethod]
  77. public void MaybeMove()
  78. {
  79. NDArray nd = np.array(2, 3);
  80. Tensor t = new Tensor(nd);
  81. Tensor o = t.MaybeMove();
  82. ASSERT_TRUE(o == IntPtr.Zero); // It is unsafe to move memory TF might not own.
  83. t.Dispose();
  84. }
  85. /// <summary>
  86. /// Port from c_api_test.cc
  87. /// `TEST(CAPI, Tensor)`
  88. /// </summary>
  89. [TestMethod]
  90. public void Tensor()
  91. {
  92. var nd = np.array(1f, 2f, 3f, 4f, 5f, 6f).reshape(2, 3);
  93. var tensor = new Tensor(nd);
  94. var array = tensor.Data<float>();
  95. EXPECT_EQ(tensor.dtype, TF_DataType.TF_FLOAT);
  96. EXPECT_EQ(tensor.rank, nd.ndim);
  97. EXPECT_EQ((int)tensor.shape[0], nd.shape[0]);
  98. EXPECT_EQ((int)tensor.shape[1], nd.shape[1]);
  99. EXPECT_EQ(tensor.bytesize, (ulong)nd.size * sizeof(float));
  100. Assert.IsTrue(Enumerable.SequenceEqual(nd.Data<float>(), new float[] { 1, 2, 3, 4, 5, 6 }));
  101. }
  102. /// <summary>
  103. /// Port from tensorflow\c\c_api_test.cc
  104. /// `TEST(CAPI, SetShape)`
  105. /// </summary>
  106. [TestMethod]
  107. public void SetShape()
  108. {
  109. var s = new Status();
  110. var graph = new Graph();
  111. var feed = c_test_util.Placeholder(graph, s);
  112. var feed_out_0 = new TF_Output(feed, 0);
  113. // Fetch the shape, it should be completely unknown.
  114. int num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  115. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  116. EXPECT_EQ(-1, num_dims);
  117. // Set the shape to be unknown, expect no change.
  118. c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s);
  119. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  120. num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  121. EXPECT_EQ(-1, num_dims);
  122. // Set the shape to be 2 x Unknown
  123. long[] dims = { 2, -1 };
  124. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s);
  125. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  126. num_dims = c_api.TF_GraphGetTensorNumDims(graph, feed_out_0, s);
  127. EXPECT_EQ(2, num_dims);
  128. // Get the dimension vector appropriately.
  129. var returned_dims = new long[dims.Length];
  130. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  131. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  132. Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims));
  133. // Set to a new valid shape: [2, 3]
  134. dims[1] = 3;
  135. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, dims.Length, s);
  136. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  137. // Fetch and see that the new value is returned.
  138. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  139. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  140. Assert.IsTrue(Enumerable.SequenceEqual(dims, returned_dims));
  141. // Try to set 'unknown' with unknown rank on the shape and see that
  142. // it doesn't change.
  143. c_api.TF_GraphSetTensorShape(graph, feed_out_0, null, -1, s);
  144. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  145. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  146. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  147. EXPECT_EQ(2, num_dims);
  148. EXPECT_EQ(2, (int)returned_dims[0]);
  149. EXPECT_EQ(3, (int)returned_dims[1]);
  150. // Try to set 'unknown' with same rank on the shape and see that
  151. // it doesn't change.
  152. dims[0] = -1;
  153. dims[1] = -1;
  154. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s);
  155. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  156. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, num_dims, s);
  157. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  158. EXPECT_EQ(2, num_dims);
  159. EXPECT_EQ(2, (int)returned_dims[0]);
  160. EXPECT_EQ(3, (int)returned_dims[1]);
  161. // Try to fetch a shape with the wrong num_dims
  162. c_api.TF_GraphGetTensorShape(graph, feed_out_0, returned_dims, 5, s);
  163. Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT);
  164. // Try to set an invalid shape (cannot change 2x3 to a 2x5).
  165. dims[1] = 5;
  166. c_api.TF_GraphSetTensorShape(graph, feed_out_0, dims, 2, s);
  167. Assert.IsTrue(s.Code == TF_Code.TF_INVALID_ARGUMENT);
  168. // Test for a scalar.
  169. var three = c_test_util.ScalarConst(3, graph, s);
  170. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  171. var three_out_0 = new TF_Output(three, 0);
  172. num_dims = c_api.TF_GraphGetTensorNumDims(graph, three_out_0, s);
  173. Assert.IsTrue(s.Code == TF_Code.TF_OK);
  174. EXPECT_EQ(0, num_dims);
  175. c_api.TF_GraphGetTensorShape(graph, feed_out_0, null, num_dims, s);
  176. //Assert.IsTrue(s.Code == TF_Code.TF_OK);
  177. // graph.Dispose();
  178. s.Dispose();
  179. }
  180. }
  181. }