aboutsummaryrefslogtreecommitdiff
path: root/tests/utils/rewrite.py
blob: 29351bf8f79ba6c22427386a1fe607962d94370a (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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 typing import Any

from tensorflow.lite.python.schema_py_generated import ModelT

from mlia.nn.rewrite.core.train import TrainingParameters


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


class MockTrainingParameters(
    TrainingParameters
):  # pylint: disable=too-few-public-methods
    """
    TrainingParameter class for rewrites with different default values.

    To speed things up for the unit tests.
    """

    def __init__(self, *args: Any, steps: int = 32, **kwargs: Any) -> None:
        """Initialize TrainingParameters with different defaults."""
        super().__init__(*args, steps=steps, **kwargs)  # type: ignore