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

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