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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. 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. Tensor t = null;
  127. for (int i = 0; i < 100; i++)
  128. {
  129. t = new Tensor(new int[] {1, 2, 3});
  130. }
  131. }
  132. }
  133. }
  134. [TestMethod]
  135. public void TensorCreation_Undressed()
  136. {
  137. //lock (Locks.ProcessWide)
  138. // tf.Session(); //create one to increase next id to 1.
  139. MultiThreadedUnitTestExecuter.Run(8, Core);
  140. //the core method
  141. unsafe void Core(int tid)
  142. {
  143. using (var sess = tf.Session())
  144. {
  145. Tensor t = null;
  146. for (int i = 0; i < 100; i++)
  147. {
  148. var v = (int*) Marshal.AllocHGlobal(sizeof(int));
  149. c_api.DeallocatorArgs _deallocatorArgs = new c_api.DeallocatorArgs();
  150. var handle = c_api.TF_NewTensor(typeof(int).as_dtype(), dims: new long[0], num_dims: 0,
  151. data: (IntPtr) v, len: (UIntPtr) sizeof(int),
  152. deallocator: (IntPtr data, IntPtr size, ref c_api.DeallocatorArgs args) => Marshal.FreeHGlobal(data),
  153. ref _deallocatorArgs);
  154. c_api.TF_DeleteTensor(handle);
  155. }
  156. }
  157. }
  158. }
  159. [TestMethod]
  160. public void SessionRun()
  161. {
  162. MultiThreadedUnitTestExecuter.Run(8, Core);
  163. //the core method
  164. void Core(int tid)
  165. {
  166. tf.peak_default_graph().Should().BeNull();
  167. //graph is created automatically to perform create these operations
  168. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  169. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  170. var math = a1 + a2;
  171. for (int i = 0; i < 100; i++)
  172. {
  173. using (var sess = tf.Session())
  174. {
  175. sess.run(math).GetAtIndex<float>(0).Should().Be(5);
  176. }
  177. }
  178. }
  179. }
  180. [TestMethod]
  181. public void SessionRun_InsideSession()
  182. {
  183. MultiThreadedUnitTestExecuter.Run(8, Core);
  184. //the core method
  185. void Core(int tid)
  186. {
  187. using (var sess = tf.Session())
  188. {
  189. tf.peak_default_graph().Should().NotBeNull();
  190. //graph is created automatically to perform create these operations
  191. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  192. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  193. var math = a1 + a2;
  194. var result = sess.run(math);
  195. result[0].GetAtIndex<float>(0).Should().Be(5);
  196. }
  197. }
  198. }
  199. [TestMethod]
  200. public void SessionRun_Initialization()
  201. {
  202. MultiThreadedUnitTestExecuter.Run(8, Core);
  203. //the core method
  204. void Core(int tid)
  205. {
  206. using (var sess = tf.Session())
  207. {
  208. tf.peak_default_graph().Should().NotBeNull();
  209. //graph is created automatically to perform create these operations
  210. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  211. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  212. var math = a1 + a2;
  213. }
  214. }
  215. }
  216. [TestMethod]
  217. public void SessionRun_Initialization_OutsideSession()
  218. {
  219. MultiThreadedUnitTestExecuter.Run(8, Core);
  220. //the core method
  221. void Core(int tid)
  222. {
  223. tf.peak_default_graph().Should().BeNull();
  224. //graph is created automatically to perform create these operations
  225. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  226. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  227. var math = a1 + a2;
  228. }
  229. }
  230. [TestMethod]
  231. public void TF_GraphOperationByName()
  232. {
  233. MultiThreadedUnitTestExecuter.Run(8, Core);
  234. //the core method
  235. void Core(int tid)
  236. {
  237. tf.peak_default_graph().Should().BeNull();
  238. //graph is created automatically to perform create these operations
  239. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  240. var a2 = tf.constant(new[] {3f}, shape: new[] {1}, name: "ConstantK");
  241. var math = a1 + a2;
  242. for (int i = 0; i < 100; i++)
  243. {
  244. var op = tf.get_default_graph().OperationByName("ConstantK");
  245. }
  246. }
  247. }
  248. private static string modelPath = "./model/";
  249. [TestMethod]
  250. public void TF_GraphOperationByName_FromModel()
  251. {
  252. if (!Directory.Exists(modelPath))
  253. return;
  254. MultiThreadedUnitTestExecuter.Run(8, Core);
  255. //the core method
  256. void Core(int tid)
  257. {
  258. Console.WriteLine();
  259. for (int j = 0; j < 100; j++)
  260. {
  261. var sess = Session.LoadFromSavedModel(modelPath).as_default();
  262. var inputs = new[] {"sp", "fuel"};
  263. var inp = inputs.Select(name => sess.graph.OperationByName(name).output).ToArray();
  264. var outp = sess.graph.OperationByName("softmax_tensor").output;
  265. for (var i = 0; i < 100; i++)
  266. {
  267. {
  268. var data = new float[96];
  269. FeedItem[] feeds = new FeedItem[2];
  270. for (int f = 0; f < 2; f++)
  271. feeds[f] = new FeedItem(inp[f], new NDArray(data));
  272. try
  273. {
  274. sess.run(outp, feeds);
  275. } catch (Exception ex)
  276. {
  277. Console.WriteLine(ex);
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }