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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. tf.peak_default_graph().Should().NotBeNull();
  67. using (var sess = tf.Session())
  68. {
  69. var default_graph = tf.peak_default_graph();
  70. var sess_graph = sess.GetPrivate<Graph>("_graph");
  71. sess_graph.Should().NotBeNull();
  72. default_graph.Should().NotBeNull()
  73. .And.BeEquivalentTo(sess_graph);
  74. Console.WriteLine($"{tid}-{default_graph.graph_key}");
  75. //var result = sess.run(new object[] {g, a});
  76. //var actualDeriv = result[0].GetData<float>()[0];
  77. //var actual = result[1].GetData<float>()[0];
  78. }
  79. }
  80. }
  81. [TestMethod]
  82. public void Marshal_AllocHGlobal()
  83. {
  84. MultiThreadedUnitTestExecuter.Run(8, Core);
  85. //the core method
  86. void Core(int tid)
  87. {
  88. for (int i = 0; i < 100; i++)
  89. {
  90. Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
  91. }
  92. }
  93. }
  94. [TestMethod]
  95. public void TensorCreation()
  96. {
  97. //lock (Locks.ProcessWide)
  98. // tf.Session(); //create one to increase next id to 1.
  99. MultiThreadedUnitTestExecuter.Run(8, Core);
  100. //the core method
  101. void Core(int tid)
  102. {
  103. using (var sess = tf.Session())
  104. {
  105. Tensor t = null;
  106. for (int i = 0; i < 100; i++)
  107. {
  108. t = new Tensor(1);
  109. }
  110. }
  111. }
  112. }
  113. [TestMethod]
  114. public void TensorCreation_Array()
  115. {
  116. //lock (Locks.ProcessWide)
  117. // tf.Session(); //create one to increase next id to 1.
  118. MultiThreadedUnitTestExecuter.Run(8, Core);
  119. //the core method
  120. void Core(int tid)
  121. {
  122. //tf.Session created an other graph
  123. using (var sess = tf.Session())
  124. {
  125. for (int i = 0; i < 100; i++)
  126. {
  127. var t = new Tensor(new int[] { 1, 2, 3 });
  128. }
  129. }
  130. }
  131. }
  132. [TestMethod]
  133. public void TensorCreation_Undressed()
  134. {
  135. //lock (Locks.ProcessWide)
  136. // tf.Session(); //create one to increase next id to 1.
  137. MultiThreadedUnitTestExecuter.Run(8, Core);
  138. //the core method
  139. unsafe void Core(int tid)
  140. {
  141. using (var sess = tf.Session())
  142. {
  143. for (int i = 0; i < 100; i++)
  144. {
  145. var v = (int*)Marshal.AllocHGlobal(sizeof(int));
  146. c_api.DeallocatorArgs _deallocatorArgs = new c_api.DeallocatorArgs();
  147. var handle = c_api.TF_NewTensor(typeof(int).as_dtype(), dims: new long[0], num_dims: 0,
  148. data: (IntPtr)v, len: (UIntPtr)sizeof(int),
  149. deallocator: (IntPtr data, IntPtr size, ref c_api.DeallocatorArgs args) => Marshal.FreeHGlobal(data),
  150. ref _deallocatorArgs);
  151. c_api.TF_DeleteTensor(handle);
  152. }
  153. }
  154. }
  155. }
  156. [TestMethod]
  157. public void SessionRun()
  158. {
  159. MultiThreadedUnitTestExecuter.Run(8, Core);
  160. //the core method
  161. void Core(int tid)
  162. {
  163. tf.peak_default_graph().Should().BeNull();
  164. //graph is created automatically to perform create these operations
  165. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  166. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  167. var math = a1 + a2;
  168. for (int i = 0; i < 100; i++)
  169. {
  170. using (var sess = tf.Session())
  171. {
  172. sess.run(math).GetAtIndex<float>(0).Should().Be(5);
  173. }
  174. }
  175. }
  176. }
  177. [TestMethod]
  178. public void SessionRun_InsideSession()
  179. {
  180. MultiThreadedUnitTestExecuter.Run(8, Core);
  181. //the core method
  182. void Core(int tid)
  183. {
  184. using (var sess = tf.Session())
  185. {
  186. tf.peak_default_graph().Should().NotBeNull();
  187. //graph is created automatically to perform create these operations
  188. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  189. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  190. var math = a1 + a2;
  191. var result = sess.run(math);
  192. result[0].GetAtIndex<float>(0).Should().Be(5);
  193. }
  194. }
  195. }
  196. [TestMethod]
  197. public void SessionRun_Initialization()
  198. {
  199. MultiThreadedUnitTestExecuter.Run(8, Core);
  200. //the core method
  201. void Core(int tid)
  202. {
  203. using (var sess = tf.Session())
  204. {
  205. tf.peak_default_graph().Should().NotBeNull();
  206. //graph is created automatically to perform create these operations
  207. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  208. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  209. var math = a1 + a2;
  210. }
  211. }
  212. }
  213. [TestMethod]
  214. public void SessionRun_Initialization_OutsideSession()
  215. {
  216. MultiThreadedUnitTestExecuter.Run(8, Core);
  217. //the core method
  218. void Core(int tid)
  219. {
  220. tf.peak_default_graph().Should().BeNull();
  221. //graph is created automatically to perform create these operations
  222. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  223. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  224. var math = a1 + a2;
  225. }
  226. }
  227. [TestMethod]
  228. public void TF_GraphOperationByName()
  229. {
  230. MultiThreadedUnitTestExecuter.Run(8, Core);
  231. //the core method
  232. void Core(int tid)
  233. {
  234. tf.peak_default_graph().Should().BeNull();
  235. //graph is created automatically to perform create these operations
  236. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  237. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 }, name: "ConstantK");
  238. var math = a1 + a2;
  239. for (int i = 0; i < 100; i++)
  240. {
  241. var op = tf.get_default_graph().OperationByName("ConstantK");
  242. }
  243. }
  244. }
  245. private static readonly string modelPath = Path.GetFullPath("./Utilities/models/example1/");
  246. [Ignore]
  247. [TestMethod]
  248. public void TF_GraphOperationByName_FromModel()
  249. {
  250. MultiThreadedUnitTestExecuter.Run(8, Core);
  251. //the core method
  252. void Core(int tid)
  253. {
  254. Console.WriteLine();
  255. for (int j = 0; j < 100; j++)
  256. {
  257. var sess = Session.LoadFromSavedModel(modelPath).as_default();
  258. var inputs = new[] { "sp", "fuel" };
  259. var inp = inputs.Select(name => sess.graph.OperationByName(name).output).ToArray();
  260. var outp = sess.graph.OperationByName("softmax_tensor").output;
  261. for (var i = 0; i < 8; i++)
  262. {
  263. var data = new float[96];
  264. FeedItem[] feeds = new FeedItem[2];
  265. for (int f = 0; f < 2; f++)
  266. feeds[f] = new FeedItem(inp[f], new NDArray(data));
  267. sess.run(outp, feeds);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }