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 10 kB

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