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.

MultithreadingTests.cs 11 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using FluentAssertions;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using NumSharp;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using Tensorflow;
  9. using Tensorflow.UnitTest;
  10. using static Tensorflow.Binding;
  11. namespace TensorFlowNET.UnitTest
  12. {
  13. [TestClass]
  14. public class MultithreadingTests : GraphModeTestBase
  15. {
  16. [TestMethod]
  17. public void SessionCreation()
  18. {
  19. ops.uid(); //increment id by one
  20. MultiThreadedUnitTestExecuter.Run(8, Core);
  21. //the core method
  22. void Core(int tid)
  23. {
  24. tf.peak_default_graph().Should().BeNull();
  25. using (var sess = tf.Session())
  26. {
  27. var default_graph = tf.peak_default_graph();
  28. var sess_graph = sess.GetPrivate<Graph>("_graph");
  29. sess_graph.Should().NotBeNull();
  30. default_graph.Should().NotBeNull()
  31. .And.BeEquivalentTo(sess_graph);
  32. }
  33. }
  34. }
  35. [TestMethod]
  36. public void SessionCreation_x2()
  37. {
  38. ops.uid(); //increment id by one
  39. MultiThreadedUnitTestExecuter.Run(16, Core);
  40. //the core method
  41. void Core(int tid)
  42. {
  43. tf.peak_default_graph().Should().BeNull();
  44. //tf.Session created an other graph
  45. using (var sess = tf.Session())
  46. {
  47. var default_graph = tf.peak_default_graph();
  48. var sess_graph = sess.GetPrivate<Graph>("_graph");
  49. sess_graph.Should().NotBeNull();
  50. default_graph.Should().NotBeNull()
  51. .And.BeEquivalentTo(sess_graph);
  52. }
  53. }
  54. }
  55. [TestMethod]
  56. public void GraphCreation()
  57. {
  58. ops.uid(); //increment id by one
  59. MultiThreadedUnitTestExecuter.Run(8, Core);
  60. //the core method
  61. void Core(int tid)
  62. {
  63. tf.peak_default_graph().Should().BeNull();
  64. var beforehand = tf.get_default_graph(); //this should create default automatically.
  65. beforehand.graph_key.Should().NotContain("-0/", "Already created a graph in an other thread.");
  66. beforehand.as_default();
  67. tf.peak_default_graph().Should().NotBeNull();
  68. using (var sess = tf.Session())
  69. {
  70. var default_graph = tf.peak_default_graph();
  71. var sess_graph = sess.GetPrivate<Graph>("_graph");
  72. sess_graph.Should().NotBeNull();
  73. default_graph.Should().NotBeNull()
  74. .And.BeEquivalentTo(sess_graph);
  75. Console.WriteLine($"{tid}-{default_graph.graph_key}");
  76. //var result = sess.run(new object[] {g, a});
  77. //var actualDeriv = result[0].GetData<float>()[0];
  78. //var actual = result[1].GetData<float>()[0];
  79. }
  80. }
  81. }
  82. [TestMethod]
  83. public void Marshal_AllocHGlobal()
  84. {
  85. MultiThreadedUnitTestExecuter.Run(8, Core);
  86. //the core method
  87. void Core(int tid)
  88. {
  89. for (int i = 0; i < 100; i++)
  90. {
  91. Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
  92. }
  93. }
  94. }
  95. [TestMethod]
  96. public void TensorCreation()
  97. {
  98. //lock (Locks.ProcessWide)
  99. // tf.Session(); //create one to increase next id to 1.
  100. MultiThreadedUnitTestExecuter.Run(8, Core);
  101. //the core method
  102. void Core(int tid)
  103. {
  104. using (var sess = tf.Session())
  105. {
  106. Tensor t = null;
  107. for (int i = 0; i < 100; i++)
  108. {
  109. t = new Tensor(1);
  110. }
  111. }
  112. }
  113. }
  114. [TestMethod]
  115. public void TensorCreation_Array()
  116. {
  117. //lock (Locks.ProcessWide)
  118. // tf.Session(); //create one to increase next id to 1.
  119. MultiThreadedUnitTestExecuter.Run(8, Core);
  120. //the core method
  121. void Core(int tid)
  122. {
  123. //tf.Session created an other graph
  124. using (var sess = tf.Session())
  125. {
  126. for (int i = 0; i < 100; i++)
  127. {
  128. var t = new Tensor(new int[] { 1, 2, 3 });
  129. }
  130. }
  131. }
  132. }
  133. [TestMethod]
  134. public void TensorCreation_Undressed()
  135. {
  136. //lock (Locks.ProcessWide)
  137. // tf.Session(); //create one to increase next id to 1.
  138. MultiThreadedUnitTestExecuter.Run(8, Core);
  139. //the core method
  140. unsafe void Core(int tid)
  141. {
  142. using (var sess = tf.Session())
  143. {
  144. for (int i = 0; i < 100; i++)
  145. {
  146. var v = (int*)Marshal.AllocHGlobal(sizeof(int));
  147. c_api.DeallocatorArgs _deallocatorArgs = new c_api.DeallocatorArgs();
  148. var handle = c_api.TF_NewTensor(typeof(int).as_dtype(), dims: new long[0], num_dims: 0,
  149. data: (IntPtr)v, len: (UIntPtr)sizeof(int),
  150. deallocator: (IntPtr data, IntPtr size, ref c_api.DeallocatorArgs args) => Marshal.FreeHGlobal(data),
  151. ref _deallocatorArgs);
  152. c_api.TF_DeleteTensor(handle);
  153. }
  154. }
  155. }
  156. }
  157. [TestMethod]
  158. public void SessionRun()
  159. {
  160. MultiThreadedUnitTestExecuter.Run(8, Core);
  161. //the core method
  162. void Core(int tid)
  163. {
  164. tf.peak_default_graph().Should().BeNull();
  165. //graph is created automatically to perform create these operations
  166. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  167. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  168. var math = a1 + a2;
  169. for (int i = 0; i < 100; i++)
  170. {
  171. using (var sess = tf.Session())
  172. {
  173. sess.run(math).GetAtIndex<float>(0).Should().Be(5);
  174. }
  175. }
  176. }
  177. }
  178. [TestMethod]
  179. public void SessionRun_InsideSession()
  180. {
  181. MultiThreadedUnitTestExecuter.Run(8, Core);
  182. //the core method
  183. void Core(int tid)
  184. {
  185. using (var sess = tf.Session())
  186. {
  187. tf.peak_default_graph().Should().NotBeNull();
  188. //graph is created automatically to perform create these operations
  189. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  190. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  191. var math = a1 + a2;
  192. var result = sess.run(math);
  193. result[0].GetAtIndex<float>(0).Should().Be(5);
  194. }
  195. }
  196. }
  197. [TestMethod]
  198. public void SessionRun_Initialization()
  199. {
  200. MultiThreadedUnitTestExecuter.Run(8, Core);
  201. //the core method
  202. void Core(int tid)
  203. {
  204. using (var sess = tf.Session())
  205. {
  206. tf.peak_default_graph().Should().NotBeNull();
  207. //graph is created automatically to perform create these operations
  208. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  209. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  210. var math = a1 + a2;
  211. }
  212. }
  213. }
  214. [TestMethod]
  215. public void SessionRun_Initialization_OutsideSession()
  216. {
  217. MultiThreadedUnitTestExecuter.Run(8, Core);
  218. //the core method
  219. void Core(int tid)
  220. {
  221. tf.peak_default_graph().Should().BeNull();
  222. //graph is created automatically to perform create these operations
  223. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  224. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  225. var math = a1 + a2;
  226. }
  227. }
  228. [TestMethod]
  229. public void TF_GraphOperationByName()
  230. {
  231. MultiThreadedUnitTestExecuter.Run(8, Core);
  232. //the core method
  233. void Core(int tid)
  234. {
  235. tf.peak_default_graph().Should().BeNull();
  236. //graph is created automatically to perform create these operations
  237. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  238. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 }, name: "ConstantK");
  239. var math = a1 + a2;
  240. for (int i = 0; i < 100; i++)
  241. {
  242. var op = tf.get_default_graph().OperationByName("ConstantK");
  243. }
  244. }
  245. }
  246. private static readonly string modelPath = Path.GetFullPath("./Utilities/models/example1/");
  247. [Ignore]
  248. [TestMethod]
  249. public void TF_GraphOperationByName_FromModel()
  250. {
  251. MultiThreadedUnitTestExecuter.Run(8, Core);
  252. //the core method
  253. void Core(int tid)
  254. {
  255. Console.WriteLine();
  256. for (int j = 0; j < 100; j++)
  257. {
  258. var sess = Session.LoadFromSavedModel(modelPath).as_default();
  259. var inputs = new[] { "sp", "fuel" };
  260. var inp = inputs.Select(name => sess.graph.OperationByName(name).output).ToArray();
  261. var outp = sess.graph.OperationByName("softmax_tensor").output;
  262. for (var i = 0; i < 8; i++)
  263. {
  264. var data = new float[96];
  265. FeedItem[] feeds = new FeedItem[2];
  266. for (int f = 0; f < 2; f++)
  267. feeds[f] = new FeedItem(inp[f], new NDArray(data));
  268. sess.run(outp, feeds);
  269. }
  270. }
  271. }
  272. }
  273. }
  274. }