aboutsummaryrefslogtreecommitdiff
path: root/tests/utils/rewrite.py
blob: 4264b4b1699a82a33841abd9d4b74ade661cb075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# SPDX-FileCopyrightText: Copyright 2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Common test utils for the rewrite tests."""
from __future__ import annotations

from tensorflow.lite.python.schema_py_generated import ModelT


def models_are_equal(model1: ModelT, model2: ModelT) -> bool:
    """Check that the two models are equal."""
    if len(model1.subgraphs) != len(model2.subgraphs):
        return False

    for graph1, graph2 in zip(model1.subgraphs, model2.subgraphs):
        if graph1.name != graph2.name or len(graph1.tensors) != len(graph2.tensors):
            return False
        for tensor1 in graph1.tensors:
            for tensor2 in graph2.tensors:
                if tensor1.name == tensor2.name:
                    if (
                        tensor1.shape == tensor2.shape
                    ).all() or tensor1.type == tensor2.type:
                        break
            else:
                return False  # Tensor from graph1 not found in other graph.")

    return True