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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. var sess = tf.Session();
  24. var default_graph = tf.get_default_graph();
  25. var sess_graph = sess.graph;
  26. Assert.IsNotNull(default_graph);
  27. Assert.IsNotNull(sess_graph);
  28. Assert.AreEqual(default_graph, sess_graph);
  29. }
  30. }
  31. [TestMethod]
  32. public void SessionCreation_x2()
  33. {
  34. ops.uid(); //increment id by one
  35. MultiThreadedUnitTestExecuter.Run(16, Core);
  36. //the core method
  37. void Core(int tid)
  38. {
  39. Assert.IsNull(tf.peak_default_graph());
  40. //tf.Session created an other graph
  41. var sess = tf.Session();
  42. var default_graph = tf.get_default_graph();
  43. var sess_graph = sess.graph;
  44. Assert.IsNotNull(default_graph);
  45. Assert.IsNotNull(sess_graph);
  46. Assert.AreEqual(default_graph, sess_graph);
  47. }
  48. }
  49. [TestMethod]
  50. public void GraphCreation()
  51. {
  52. ops.uid(); //increment id by one
  53. MultiThreadedUnitTestExecuter.Run(8, Core);
  54. //the core method
  55. void Core(int tid)
  56. {
  57. Assert.IsNull(tf.peak_default_graph());
  58. var beforehand = tf.get_default_graph(); //this should create default automatically.
  59. beforehand.as_default();
  60. Assert.IsNotNull(tf.peak_default_graph());
  61. var sess = tf.Session();
  62. var default_graph = tf.peak_default_graph();
  63. var sess_graph = sess.graph;
  64. Assert.IsNotNull(default_graph);
  65. Assert.IsNotNull(sess_graph);
  66. Assert.AreEqual(default_graph, sess_graph);
  67. }
  68. }
  69. [TestMethod]
  70. public void Marshal_AllocHGlobal()
  71. {
  72. MultiThreadedUnitTestExecuter.Run(8, Core);
  73. //the core method
  74. void Core(int tid)
  75. {
  76. for (int i = 0; i < 100; i++)
  77. {
  78. Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
  79. }
  80. }
  81. }
  82. [TestMethod]
  83. public void TensorCreation()
  84. {
  85. MultiThreadedUnitTestExecuter.Run(8, Core);
  86. //the core method
  87. void Core(int tid)
  88. {
  89. var sess = tf.Session();
  90. for (int i = 0; i < 100; i++)
  91. {
  92. var t = new Tensor(1);
  93. }
  94. }
  95. }
  96. [TestMethod]
  97. public void TensorCreation_Array()
  98. {
  99. MultiThreadedUnitTestExecuter.Run(8, Core);
  100. //the core method
  101. void Core(int tid)
  102. {
  103. //tf.Session created an other graph
  104. var sess = tf.Session();
  105. for (int i = 0; i < 100; i++)
  106. {
  107. var t = new Tensor(new int[] { 1, 2, 3 });
  108. }
  109. }
  110. }
  111. [TestMethod]
  112. public void SessionRun()
  113. {
  114. MultiThreadedUnitTestExecuter.Run(2, Core);
  115. //the core method
  116. void Core(int tid)
  117. {
  118. tf.compat.v1.disable_eager_execution();
  119. var graph = tf.Graph().as_default();
  120. //graph is created automatically to perform create these operations
  121. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  122. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  123. var math = a1 + a2;
  124. var sess = tf.Session(graph);
  125. for (int i = 0; i < 100; i++)
  126. {
  127. var result = sess.run(math);
  128. Assert.AreEqual(result[0], 5f);
  129. }
  130. }
  131. }
  132. [TestMethod]
  133. public void SessionRun_InsideSession()
  134. {
  135. MultiThreadedUnitTestExecuter.Run(8, Core);
  136. //the core method
  137. void Core(int tid)
  138. {
  139. tf.compat.v1.disable_eager_execution();
  140. var graph = tf.Graph().as_default();
  141. var sess = tf.Session(graph);
  142. Assert.IsNotNull(tf.get_default_graph());
  143. //graph is created automatically to perform create these operations
  144. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  145. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  146. var math = a1 + a2;
  147. var result = sess.run(math);
  148. Assert.AreEqual(result[0], 5f);
  149. }
  150. }
  151. [TestMethod]
  152. public void SessionRun_Initialization()
  153. {
  154. MultiThreadedUnitTestExecuter.Run(8, Core);
  155. //the core method
  156. void Core(int tid)
  157. {
  158. var sess = tf.Session();
  159. Assert.IsNotNull(tf.get_default_graph());
  160. //graph is created automatically to perform create these operations
  161. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  162. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  163. var math = a1 + a2;
  164. }
  165. }
  166. [TestMethod]
  167. public void SessionRun_Initialization_OutsideSession()
  168. {
  169. MultiThreadedUnitTestExecuter.Run(8, Core);
  170. //the core method
  171. void Core(int tid)
  172. {
  173. Assert.IsNull(tf.peak_default_graph());
  174. //graph is created automatically to perform create these operations
  175. var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
  176. var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
  177. var math = a1 + a2;
  178. }
  179. }
  180. [TestMethod]
  181. public void TF_GraphOperationByName()
  182. {
  183. MultiThreadedUnitTestExecuter.Run(8, Core);
  184. //the core method
  185. void Core(int tid)
  186. {
  187. Assert.IsNull(tf.peak_default_graph());
  188. tf.compat.v1.disable_eager_execution();
  189. var graph = tf.Graph().as_default();
  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 }, name: "ConstantK");
  193. var math = a1 + a2;
  194. for (int i = 0; i < 100; i++)
  195. {
  196. var op = tf.get_default_graph().OperationByName("ConstantK");
  197. }
  198. }
  199. }
  200. private static readonly string modelPath = Path.GetFullPath("./Utilities/models/example1/");
  201. [Ignore]
  202. public void TF_GraphOperationByName_FromModel()
  203. {
  204. MultiThreadedUnitTestExecuter.Run(8, Core);
  205. //the core method
  206. void Core(int tid)
  207. {
  208. Console.WriteLine();
  209. for (int j = 0; j < 100; j++)
  210. {
  211. var sess = Session.LoadFromSavedModel(modelPath).as_default();
  212. var inputs = new[] { "sp", "fuel" };
  213. var inp = inputs.Select(name => sess.graph.OperationByName(name).output).ToArray();
  214. var outp = sess.graph.OperationByName("softmax_tensor").output;
  215. for (var i = 0; i < 8; i++)
  216. {
  217. var data = new float[96];
  218. FeedItem[] feeds = new FeedItem[2];
  219. for (int f = 0; f < 2; f++)
  220. feeds[f] = new FeedItem(inp[f], new NDArray(data));
  221. sess.run(outp, feeds);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }