|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Tensorflow.NumPy;
- using System;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using Tensorflow;
- using static Tensorflow.Binding;
-
- namespace TensorFlowNET.UnitTest
- {
- [TestClass]
- public class MultithreadingTests : GraphModeTestBase
- {
- [TestMethod]
- public void SessionCreation()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- Assert.IsNull(tf.peak_default_graph());
-
- using var sess = tf.Session();
- var default_graph = tf.get_default_graph();
- var sess_graph = sess.graph;
- Assert.IsNotNull(default_graph);
- Assert.IsNotNull(sess_graph);
- Assert.AreEqual(default_graph, sess_graph);
- }
- }
-
- [TestMethod]
- public void SessionCreation_x2()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(16, Core);
-
- //the core method
- void Core(int tid)
- {
- Assert.IsNull(tf.peak_default_graph());
- //tf.Session created an other graph
- using var sess = tf.Session();
- var default_graph = tf.get_default_graph();
- var sess_graph = sess.graph;
- Assert.IsNotNull(default_graph);
- Assert.IsNotNull(sess_graph);
- Assert.AreEqual(default_graph, sess_graph);
- }
- }
-
- [TestMethod]
- public void GraphCreation()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- Assert.IsNull(tf.peak_default_graph());
- var beforehand = tf.get_default_graph(); //this should create default automatically.
- beforehand.as_default();
- Assert.IsNotNull(tf.peak_default_graph());
-
- using var sess = tf.Session();
- var default_graph = tf.peak_default_graph();
- var sess_graph = sess.graph;
- Assert.IsNotNull(default_graph);
- Assert.IsNotNull(sess_graph);
- Assert.AreEqual(default_graph, sess_graph);
- }
- }
-
-
- [TestMethod]
- public void Marshal_AllocHGlobal()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- for (int i = 0; i < 100; i++)
- {
- Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
- }
- }
- }
-
- [TestMethod]
- public void TensorCreation()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- using var sess = tf.Session();
- for (int i = 0; i < 100; i++)
- {
- var t = new Tensor(1);
- }
- }
- }
-
- [TestMethod]
- public void TensorCreation_Array()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- //tf.Session created an other graph
- using var sess = tf.Session();
- for (int i = 0; i < 100; i++)
- {
- var t = new Tensor(new int[] { 1, 2, 3 });
- }
- }
- }
-
- [TestMethod]
- public void SessionRun()
- {
- MultiThreadedUnitTestExecuter.Run(2, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.compat.v1.disable_eager_execution();
- var graph = tf.Graph().as_default();
-
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
- var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
- var math = a1 + a2;
- using var sess = tf.Session(graph);
- for (int i = 0; i < 100; i++)
- {
- var result = sess.run(math);
- Assert.AreEqual(result[0], 5f);
- }
- }
- }
-
- [TestMethod]
- public void SessionRun_InsideSession()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.compat.v1.disable_eager_execution();
- var graph = tf.Graph().as_default();
-
- using var sess = tf.Session(graph);
- Assert.IsNotNull(tf.get_default_graph());
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
- var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
- var math = a1 + a2;
-
- var result = sess.run(math);
- Assert.AreEqual(result[0], 5f);
- }
- }
-
- [TestMethod]
- public void SessionRun_Initialization()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- using var sess = tf.Session();
- Assert.IsNotNull(tf.get_default_graph());
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
- var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
- var math = a1 + a2;
- }
- }
-
- [TestMethod]
- public void SessionRun_Initialization_OutsideSession()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- Assert.IsNull(tf.peak_default_graph());
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
- var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 });
- var math = a1 + a2;
- }
- }
-
- [TestMethod]
- public void TF_GraphOperationByName()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- Assert.IsNull(tf.peak_default_graph());
-
- tf.compat.v1.disable_eager_execution();
- var graph = tf.Graph().as_default();
-
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] { 2f }, shape: new[] { 1 });
- var a2 = tf.constant(new[] { 3f }, shape: new[] { 1 }, name: "ConstantK");
- var math = a1 + a2;
- for (int i = 0; i < 100; i++)
- {
- var op = tf.get_default_graph().OperationByName("ConstantK");
- }
- }
- }
-
- private static readonly string modelPath = Path.GetFullPath("./Utilities/models/example1/");
-
- [Ignore]
- public void TF_GraphOperationByName_FromModel()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- Console.WriteLine();
- for (int j = 0; j < 100; j++)
- {
- var sess = Session.LoadFromSavedModel(modelPath).as_default();
- var inputs = new[] { "sp", "fuel" };
-
- var inp = inputs.Select(name => sess.graph.OperationByName(name).output).ToArray();
- var outp = sess.graph.OperationByName("softmax_tensor").output;
-
- for (var i = 0; i < 8; i++)
- {
- var data = new float[96];
- FeedItem[] feeds = new FeedItem[2];
-
- for (int f = 0; f < 2; f++)
- feeds[f] = new FeedItem(inp[f], new NDArray(data));
-
- sess.run(outp, feeds);
- }
- }
- }
- }
- }
- }
|