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