Beispiel #1
0
    def test_python_template(self):
        py_template = NodeTemplate([('a', int), ('b', int)], [('out', int)])

        with DeltaGraph("test_python_template") as test_graph:
            print_then_exit(py_template.call(a=1, b=2))

        self.check_build(test_graph)
    def test_template_input_raises_exception(self):
        template_1 = NodeTemplate(inputs=[('a', bool)])

        with self.assertRaises(DeltaTypeError):
            with DeltaGraph() as graph:
                template_1.call(a=add_const(1, 2))
            graph.check()
    def test_merge_use_in_graph(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate.
        """
        test_template3 = NodeTemplate(name="test_3",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template3, allow_const=False)
        def _simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template2 = NodeTemplate(name="test_2",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template2, allow_const=False)
        def simple_add_3(a: int, b: int) -> int:
            return a + b

        test_template3.merge(test_template2)

        saver = StateSaver()
        with DeltaGraph() as graph:
            n1 = simple_add_3(4, 73)
            saver.save_and_exit(n1)

        self.assertEqual(len(n1.bodies), 2)

        graph.select_bodies(preferred=['_simple_add_2'])
        self.assertIn("_simple_add_2", n1.body.access_tags)
        DeltaPySimulator(graph).run()
        self.assertEqual([77], saver.saved)
    def setUp(self):

        self.test_template = NodeTemplate(name="TestTemplate",
                                          inputs=[('a', int), ('b', int)])

        @DeltaBlock(template=self.test_template,
                    allow_const=False,
                    tags=["func_1"])
        def func_1(a: int, b: int) -> Void:
            print("func_1")
            raise DeltaRuntimeExit

        @DeltaBlock(template=self.test_template,
                    allow_const=False,
                    tags=["func_2", "preferred"])
        def func_2(a: int, b: int) -> Void:
            print("func_2")
            raise DeltaRuntimeExit

        @DeltaBlock(template=self.test_template,
                    allow_const=False,
                    tags=["func_3", "excluded"])
        def func_3(a: int, b: int) -> Void:
            print("func_3")
            raise DeltaRuntimeExit
Beispiel #5
0
    def test_placeholder_input(self):
        template_3 = NodeTemplate(inputs=[('a', int)])

        with DeltaGraph() as graph:
            a = placeholder_node_factory()
            template_3.call(a=a)

        a.specify_by_func(self.func_for_placeholder)
        self.assertTrue(graph.check())
Beispiel #6
0
    def test_migen_template(self):
        migen_template = NodeTemplate([('a', int)], [('out', int)])

        with DeltaGraph("test_migen_template") as test_graph:
            c1 = DUT1(tb_num_iter=2000, name='counter1').call(i1=return_1000())
            c2 = DUT1(tb_num_iter=2000,
                      name='counter2').call(i1=migen_template.call(a=c1.o1))
            print_then_exit(c2.o1)

        self.check_build(test_graph)
Beispiel #7
0
    def test_template_input_raises_exception(self):
        @DeltaBlock()
        def add(a: int, b: int) -> int:
            return a + b

        template_1 = NodeTemplate(inputs=[('a', bool)])

        with self.assertRaises(DeltaTypeError):
            with DeltaGraph() as graph:
                template_1.call(a=add(a=1, b=2))
            graph.check()
    def test_add_invalid_constructor(self):
        test_template_h = NodeTemplate(name="test_1",
                                       inputs=[('a', int), ('b', int)],
                                       outputs=[('output', int)])

        @DeltaBlock(allow_const=False)
        def simple_add_to_bool(a: int, b: int) -> bool:
            return bool(a + b)

        with self.assertRaises(ValueError):
            test_template_h.add_constructor(simple_add_to_bool)
Beispiel #9
0
    def test_add_method(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=int)
        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(OpCacher2().cached_add)
        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('cached_add', n1.body.access_tags)
    def test_add_migen(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', DOptional(int)),
                                              ('b', DOptional(int))])

        test_template1.add_constructor(AMigenNode2())

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 1)
        self.assertIn(PyMigenBody, n1.body.access_tags)
    def test_add_func(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('n1', int), ('n2', int)],
                                      outputs=[('output', int)])

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(add_non_const)
        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('add_non_const', n1.body.access_tags)
    def test_multi_input_from_same(self):
        template_2 = NodeTemplate(inputs=[('a', int), ('b', bool)])

        @DeltaBlock(outputs=[('a', int), ('b', bool)])
        def add_1_true(n: int):
            return n + 1, True

        with DeltaGraph() as graph:
            forked_input = add_1_true(n=5)
            template_2.call(a=forked_input.a, b=forked_input.b)

        self.assertTrue(graph.check())
Beispiel #13
0
    def test_partial_arg_types(self):
        template_4 = NodeTemplate(inputs=[('a', int), ('b', bool)])

        @DeltaBlock()
        def bool_and(a: bool, b: bool) -> bool:
            return a and b

        with DeltaGraph() as graph:
            a = placeholder_node_factory()
            template_4.call(a=a, b=bool_and(a=True, b=False))

        a.specify_by_func(self.func_for_placeholder)
        self.assertTrue(graph.check())
Beispiel #14
0
    def test_forked_input(self):
        ForkedReturnT, ForkedReturn = make_forked_return({'a': int, 'b': bool})
        template_2 = NodeTemplate(inputs=[('a', int), ('b', bool)])

        @DeltaBlock()
        def add_1_true(n: int) -> ForkedReturnT:
            return ForkedReturn(a=n + 1, b=True)

        with DeltaGraph() as graph:
            forked_input = add_1_true(n=5)
            template_2.call(a=forked_input.a, b=forked_input.b)

        self.assertTrue(graph.check())
    def test_add_multiple(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('n1', int), ('n2', int)],
                                      outputs=[('output', int)])

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(OpCacher2().cached_add)
        n1.add_body(add_non_const)
        self.assertEqual(len(n1.bodies), 2)
        self.assertIn('cached_add', n1.body.access_tags)
    def test_outputs_enforced_merge(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate.
        """
        test_template2 = NodeTemplate(name="test_2",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', bool)])

        with self.assertRaises(ValueError):
            self.test_template1.merge(test_template2)

        with self.assertRaises(ValueError):
            test_template2.merge(self.test_template1)
Beispiel #17
0
    def test_add_invalid_body(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=int)

        @DeltaBlock(allow_const=False)
        def simple_add_to_bool(a: int, b: int) -> bool:
            return bool(a + b)

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        with self.assertRaises(ValueError):
            n1.add_body(simple_add_to_bool)
    def test_add_method(self):
        test_template_b = NodeTemplate(name="test_1",
                                       inputs=[('a', int), ('b', int)],
                                       outputs=[('output', int)])

        @DeltaBlock(template=test_template_b, allow_const=False)
        def _simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template_b.add_constructor(OpCacher2.cached_add_2)

        with DeltaGraph():
            n1 = OpCacher2().cached_add_2(2, 3)

        self.assertEqual(len(n1.bodies), 2)
        self.assertIn('cached_add_2', n1.body.access_tags)
    def test_add_func(self):
        test_template_a = NodeTemplate(name="test_1",
                                       inputs=[('a', int), ('b', int)],
                                       outputs=[('output', int)])

        @DeltaBlock(allow_const=False)
        def simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template_a.add_constructor(simple_add_2)

        with DeltaGraph():
            n1 = test_template_a.call(2, 3)

        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('simple_add_2', n1.body.access_tags)
Beispiel #20
0
    def test_add_func(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=int)

        @DeltaBlock(allow_const=False)
        def simple_add(a: int, b: int) -> int:
            return a + b

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(simple_add)
        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('simple_add', n1.body.access_tags)
Beispiel #21
0
    def test_add_migen(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', DOptional(int)),
                                              ('b', DOptional(int))])

        class AMigenNode(MigenNodeTemplate):
            def migen_body(self, template):
                template.add_pa_in_port('a', DOptional(int))
                template.add_pa_in_port('b', DOptional(int))

        with DeltaGraph():
            n1 = test_template1.call(a=2, b=3)

        self.assertEqual(len(n1.bodies), 0)
        n1.add_body(AMigenNode())
        self.assertEqual(len(n1.bodies), 1)
        self.assertIn(PyMigenBody, n1.body.access_tags)
    def test_add_interactive(self):
        test_template1 = NodeTemplate(name="test_1",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=int)

        @Interactive(inputs=[('a', int), ('b', int)], outputs=int)
        def broken_adder_2(node: RealNode):
            a = node.receive('a')
            b = node.receive('b')
            node.send(a + b + 1)

        test_template1.add_constructor(broken_adder_2)

        with DeltaGraph():
            n1 = test_template1.call(2, 3)

        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('broken_adder_2', n1.body.access_tags)
    def test_add_with_existing_same_template(self):
        """Test for when the constructor we are adding is already on the
        ``NodeTemplate``. The constructor should not be added twice.
        """
        test_template_g = NodeTemplate(name="test_1",
                                       inputs=[('a', int), ('b', int)],
                                       outputs=[('output', int)])

        @DeltaBlock(template=test_template_g, allow_const=False)
        def simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template_g.add_constructor(simple_add_2)

        with DeltaGraph():
            n1 = test_template_g.call(2, 3)

        self.assertEqual(len(n1.bodies), 1)
        self.assertIn('simple_add_2', n1.body.access_tags)
Beispiel #24
0
    def test_template_node_capnp(self):
        """Test serialisation of nodes with no body.
        """
        template = NodeTemplate(inputs=[('a', int), ('b', int)],
                                outputs=[('output', int)],
                                name="temp-test")

        with DeltaGraph() as test_graph:
            template.call(a=1, b=2)

        data, _ = serialise_graph(test_graph)
        g_capnp = deserialise_graph(data)

        for n in g_capnp.nodes:
            if n.name.split("_")[1] == 'temp-test':
                node = n
                break
        self.assertEqual(node.name.split("_")[0], 'template')
        self.assertEqual(len(node.bodies), 0)
    def __init__(self, methodName):
        super().__init__(methodName)
        test_template_x = NodeTemplate(name="test_1",
                                       inputs=[('a', int), ('b', int)],
                                       outputs=[('output', int)])

        @DeltaBlock(template=test_template_x, allow_const=False)
        def simple_add_2(a: int, b: int) -> int:
            return a + b

        self.template_1_body = simple_add_2.template
        self.test_template1 = test_template_x
    def test_outputs_enforced(self):
        """Test to ensure out type must match when associating
        constructors with a NodeTemplate
        """
        test_template2 = NodeTemplate(name="test",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        with self.assertRaises(ValueError):
            @DeltaBlock(template=test_template2, allow_const=False)
            def _test1(a: int, b: int) -> bool:
                return a + b
    def test_multi_output(self):
        """Test that a multi-output pure template is allowed
        """
        template_o = NodeTemplate(outputs=[('a', int), ('b', bool)])
        template_i = NodeTemplate(inputs=[('a', int), ('b', bool)])

        with DeltaGraph() as graph:
            forked_input = template_o.call()
            template_i.call(a=forked_input.a, b=forked_input.b)

        self.assertTrue(graph.check())
    def test_merge_adds_bodies(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate.
        """
        test_template3 = NodeTemplate(name="test_3",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template3, allow_const=False)
        def simple_add_2(a: int, b: int) -> int:
            return a + b

        test_template2 = NodeTemplate(name="test_2",
                                      inputs=[('a', int), ('b', int)],
                                      outputs=[('output', int)])

        @DeltaBlock(template=test_template2, allow_const=False)
        def simple_add_3(a: int, b: int) -> int:
            return a + b

        test_template3.merge(test_template2)
        self.assertEqual(len(test_template3._body_templates), 2)
        self.assertIn(simple_add_2.template, test_template3._body_templates)
        self.assertIn(simple_add_3.template, test_template3._body_templates)
    def test_via_migen_init(self):

        m_template = NodeTemplate(name="MTemplate",
                                  inputs=[('a', DOptional(int)),
                                          ('b', DOptional(int))])

        @DeltaBlock(m_template, allow_const=False)
        def m_simple_add(a: DOptional(int), b: DOptional(int)):
            raise DeltaRuntimeExit

        m_maker = AMigenNode(node_template=m_template)
        with DeltaGraph():
            n1 = m_maker.call(a=1, b=3)
        self.assertEqual(len(n1.bodies), 2)
        self.assertIn(PyMigenBody, n1.body.access_tags)
    def test_inputs_enforced_optional(self):
        """Test to ensure in params must match when associating
        constructors with a NodeTemplate
        """
        test_template2 = NodeTemplate(
            name="test",
            inputs=[('a', Optional(int)), ('b', Optional(int))],
            outputs=[('output', int)]
        )

        with self.assertRaises(ValueError):
            @DeltaBlock(template=test_template2, allow_const=False)
            def _test1(a: Optional(int), b: int) -> int:
                return a + b

        with self.assertRaises(ValueError):
            @ DeltaBlock(template=test_template2, allow_const=False)
            def _test2(wrong_name: int, b: int) -> int:
                return wrong_name + b