From 6efaa9603cbcd29c8902601282f751353818a65c Mon Sep 17 00:00:00 2001 From: cmoineau <cyril.moineau@cea.fr> Date: Tue, 8 Aug 2023 13:08:51 +0000 Subject: [PATCH] [Python] Add unittest for Scheduler and Tensor. --- .../unit_tests/test_scheduler.py | 42 ++++++++++++++++ aidge_backend_cpu/unit_tests/test_tensor.py | 49 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 aidge_backend_cpu/unit_tests/test_scheduler.py create mode 100644 aidge_backend_cpu/unit_tests/test_tensor.py diff --git a/aidge_backend_cpu/unit_tests/test_scheduler.py b/aidge_backend_cpu/unit_tests/test_scheduler.py new file mode 100644 index 00000000..bc766203 --- /dev/null +++ b/aidge_backend_cpu/unit_tests/test_scheduler.py @@ -0,0 +1,42 @@ +import unittest +import aidge_core +import aidge_backend_cpu +import numpy as np + + +class test_scheduler(unittest.TestCase): + """Test tensor binding + """ + def setUp(self): + pass + def tearDown(self): + pass + + def test_relu_forward(self): + values = np.arange(6) - 3 + + input_node = aidge_core.Producer(aidge_core.Tensor(values), "Input") + relu = aidge_core.ReLU() + + gv = aidge_core.GraphView() + gv.add(relu) + gv.add(input_node) + + gv.set_datatype(aidge_core.DataType.Int32) + gv.set_backend("cpu") + + input_node.add_child(relu) + + scheduler = aidge_core.SequentialScheduler(gv) + + scheduler.forward() + + out_tensor = relu.get_operator().output(0) + expected_out = [0,0,0,0,1,2] + for i in range(len(expected_out)): + self.assertEqual(expected_out[i], out_tensor[i]) + + + +if __name__ == '__main__': + unittest.main() diff --git a/aidge_backend_cpu/unit_tests/test_tensor.py b/aidge_backend_cpu/unit_tests/test_tensor.py new file mode 100644 index 00000000..1d12fc0c --- /dev/null +++ b/aidge_backend_cpu/unit_tests/test_tensor.py @@ -0,0 +1,49 @@ +import unittest +import aidge_core +import aidge_backend_cpu +import numpy as np + + +class test_tensor(unittest.TestCase): + """Test tensor binding + """ + def setUp(self): + pass + def tearDown(self): + pass + + def test_getavailable_backends(self): + self.assertTrue("cpu" in aidge_core.Tensor.get_available_backends()) + + def test_numpy_int_to_tensor(self): + np_array = np.arange(9).reshape(1,1,3,3) + # Numpy -> Tensor + t = aidge_core.Tensor(np_array) + self.assertEqual(t.dtype(), aidge_core.DataType.Int32) + for i_t, i_n in zip(t, np_array.flatten()): + self.assertTrue(i_t == i_n) + for i,j in zip(t.dims(), np_array.shape): + self.assertEqual(i,j) + def test_tensor_int_to_numpy(self): + np_array = np.arange(9).reshape(1,1,3,3) + # Numpy -> Tensor + t = aidge_core.Tensor(np_array) + # Tensor -> Numpy + nnarray = np.array(t) + for i_nn, i_n in zip(nnarray.flatten(), np_array.flatten()): + self.assertTrue(i_nn == i_n) + for i,j in zip(t.dims(), nnarray.shape): + self.assertEqual(i,j) + + def test_numpy_float_to_tensor(self): + t = aidge_core.Tensor() + np_array = np.random.rand(1, 1, 3, 3).astype(np.float32) + # Numpy -> Tensor + t = aidge_core.Tensor(np_array) + self.assertEqual(t.dtype(), aidge_core.DataType.Float32) + for i_t, i_n in zip(t, np_array.flatten()): + self.assertTrue(i_t == i_n) # TODO : May need to change this to a difference + for i,j in zip(t.dims(), np_array.shape): + self.assertEqual(i,j) +if __name__ == '__main__': + unittest.main() -- GitLab