Skip to content

[core] Bug : Advanced node testing with Graph Regex and python test function

What commit version of aidge do you use

Problem description

The set_node_key function should be able to take in argument a testing function defined in python. However even if the test function is set to return True all the time, no match is found.

Number of match :  0

Reproducible example code

import aidge_core

# Define Model
model = aidge_core.sequential([aidge_core.Conv2D(1, 1, [7, 7], name="Conv0"),
                                aidge_core.Conv2D(1, 1, [5, 5], name="Conv1"),
                                aidge_core.Conv2D(1, 1, [3, 3], name="Conv2"),
                                aidge_core.Conv2D(1, 1, [5, 5], name="Conv1"),
                                aidge_core.Conv2D(1, 1, [7, 7], name="Conv2")])

# Instanciate Graph Regex
graph_regex = aidge_core.GraphRegex()

# Create testing function
def test(node):
    b1 = node.type() == "Conv"
    # b2 = node.get_operator().get_attr("KernelDims") == [3,3]
    return True

graph_regex.set_node_key("A", test)
# graph_regex.set_node_key("A", "getType($) =='Conv'")
graph_regex.add_query("A#")

all_match = graph_regex.match(model)
print('Number of match : ', len(all_match))

for i, match in enumerate(all_match):
    print('Match ', i ,' associated to query : ', match.get_query())
    print('The start node :', match.get_start_node()[0].name())
    print('All the matched nodes :')
    for n in match.get_all():
        print('\t', n.name())

Remarks

Maybe the python garbage collector was collecting the python test function. To prevent this from happening we added in pybind_GraphRegex.cpp the keep_alive fonctionnality. https://pybind11.readthedocs.io/en/stable/advanced/functions.html#keep-alive

However it did not solve the issue.

.def("set_node_key",
            (void (GraphRegex::*)(const std::string, std::function<bool(NodePtr)>)) &
                    GraphRegex::setNodeKey,
            py::arg("key"), py::arg("f"), py::keep_alive<1, 3>(),
    R"mydelimiter(
    Add a node test
    :param key: the key of the lambda test to use in the conditional expressions.
    :param f: bool lambda (nodePtr).

    )mydelimiter")
Edited by Thibault Allenet