diff --git a/tests/analysis/test_topology_checks.py b/tests/analysis/test_topology_checks.py index 34777081739135107f171dd4abc2722e65e03549..7c9a131f69c8ea7b511bdb3a7765b3b7633b4f40 100644 --- a/tests/analysis/test_topology_checks.py +++ b/tests/analysis/test_topology_checks.py @@ -26,11 +26,13 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import os from pkgutil import get_data import onnx.helper as oh from onnx import TensorProto - +import brevitas.onnx as bo +from finn.util.test import get_test_model_trained import finn.analysis.topology as ta from finn.core.modelwrapper import ModelWrapper from finn.transformation.infer_shapes import InferShapes @@ -91,6 +93,9 @@ def test_node_inputs_in_expected_order(): def test_nodes_in_expected_order(): + # test analysis pass (nodes_in_expected_order) with different models + + # test with data/onnx/finn-hls-model/tfc_w1_a1_after_conv_to_hls.onnx raw_m = get_data( "finn", "data/onnx/finn-hls-model/tfc_w1_a1_after_conv_to_hls.onnx" ) @@ -105,3 +110,96 @@ def test_nodes_in_expected_order(): graph.node.append(first_node) ret = model.analysis(ta.nodes_in_expected_order) assert ret["nodes_in_expected_order"] is False + + # test with data/onnx/mnist-conv/model.onnx + raw_m = get_data("finn", "data/onnx/mnist-conv/model.onnx") + model = ModelWrapper(raw_m) + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is True + + # remove first node and add it at the end + graph = model.graph + first_node = graph.node[0] + graph.node.remove(first_node) + graph.node.append(first_node) + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is False + + # test with manually created small network + Neg_node = oh.make_node("Neg", inputs=["in1"], outputs=["neg1"],) + Round_node = oh.make_node("Round", inputs=["neg1"], outputs=["round1"],) + + Ceil_node = oh.make_node("Ceil", inputs=["neg1"], outputs=["ceil1"],) + Add_node = oh.make_node("Add", inputs=["round1", "ceil1"], outputs=["out1"],) + + in1 = oh.make_tensor_value_info("in1", TensorProto.FLOAT, [4, 4]) + out1 = oh.make_tensor_value_info("out1", TensorProto.FLOAT, [4, 4]) + + graph = oh.make_graph( + nodes=[Neg_node, Round_node, Ceil_node, Add_node], + name="simple_graph", + inputs=[in1], + outputs=[out1], + value_info=[ + oh.make_tensor_value_info("neg1", TensorProto.FLOAT, [4, 4]), + oh.make_tensor_value_info("round1", TensorProto.FLOAT, [4, 4]), + oh.make_tensor_value_info("ceil1", TensorProto.FLOAT, [4, 4]), + ], + ) + + onnx_model = oh.make_model(graph, producer_name="simple-model") + model = ModelWrapper(onnx_model) + + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is True + + # create same graph but with "wrong" node order + graph = oh.make_graph( + nodes=[Round_node, Ceil_node, Neg_node, Add_node], + name="simple_graph", + inputs=[in1], + outputs=[out1], + value_info=[ + oh.make_tensor_value_info("neg1", TensorProto.FLOAT, [4, 4]), + oh.make_tensor_value_info("round1", TensorProto.FLOAT, [4, 4]), + oh.make_tensor_value_info("ceil1", TensorProto.FLOAT, [4, 4]), + ], + ) + + onnx_model = oh.make_model(graph, producer_name="simple-model") + model = ModelWrapper(onnx_model) + + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is False + + # test with data/onnx/finn-hls-model/finn-hls-onnx-model.onnx + raw_m = get_data("finn", "data/onnx/finn-hls-model/finn-hls-onnx-model.onnx") + model = ModelWrapper(raw_m) + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is True + + # remove first node and add it at the end + graph = model.graph + first_node = graph.node[0] + graph.node.remove(first_node) + graph.node.append(first_node) + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is False + + # test with cnv_w1a1 + build_dir = "/tmp/" + os.environ["FINN_INST_NAME"] + cnv = get_test_model_trained("CNV", 1, 1) + bo.export_finn_onnx( + cnv, (1, 3, 32, 32), build_dir + "/end2end_cnv_w1a1_export.onnx" + ) + model = ModelWrapper(build_dir + "/end2end_cnv_w1a1_export.onnx") + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is True + + # remove first node and add it at the end + graph = model.graph + first_node = graph.node[0] + graph.node.remove(first_node) + graph.node.append(first_node) + ret = model.analysis(ta.nodes_in_expected_order) + assert ret["nodes_in_expected_order"] is False