aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/nn/rewrite/core/extract.py
blob: 4fcf735cc47e9adc88859b6dac44c31f725d9e7c (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# SPDX-FileCopyrightText: Copyright 2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Extract module."""
# pylint: disable=too-many-arguments, too-many-locals
from __future__ import annotations

import os
from functools import partial
from pathlib import Path

import tensorflow as tf
from tensorflow.lite.python.schema_py_generated import SubGraphT

from mlia.nn.rewrite.core.graph_edit.cut import cut_model
from mlia.nn.rewrite.core.graph_edit.record import dequantized_path
from mlia.nn.rewrite.core.graph_edit.record import record_model

os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)


def _get_path(
    ext: str, name: str, dir_path: str | Path, model_is_quantized: bool = False
) -> Path:
    """Create a file path for extracted files."""
    path = Path(dir_path, f"{name}{ext}")
    return dequantized_path(path) if model_is_quantized else path


class TFLitePaths:  # pylint: disable=too-few-public-methods
    """Provide safe access to TensorFlow Lite file paths."""

    _get_path_tflite = partial(_get_path, ".tflite")

    start = partial(_get_path_tflite, "start")
    replace = partial(_get_path_tflite, "replace")
    end = partial(_get_path_tflite, "end")


class TFRecordPaths:  # pylint: disable=too-few-public-methods
    """Provide safe access to tfrec file paths."""

    _get_path_tfrec = partial(_get_path, ".tfrec")

    input = partial(_get_path_tfrec, "input")
    output = partial(_get_path_tfrec, "output")
    end = partial(_get_path_tfrec, "end")


class ExtractPaths:  # pylint: disable=too-few-public-methods
    """Get paths to extract files.

    This is meant to be the single source of truth regarding all file names
    created by the extract() function in an output directory.
    """

    tflite = TFLitePaths
    tfrec = TFRecordPaths


def extract(
    output_path: str,
    model_file: str,
    input_filename: str,
    input_names: list,
    output_names: list,
    subgraph: SubGraphT = 0,
    skip_outputs: bool = False,
    show_progress: bool = False,
    num_procs: int = 1,
    num_threads: int = 0,
    dequantize_output: bool = False,
) -> None:
    """Extract a model after cut and record."""
    try:
        os.mkdir(output_path)
    except FileExistsError:
        pass

    start_file = ExtractPaths.tflite.start(output_path)
    cut_model(
        model_file,
        input_names=None,
        output_names=input_names,
        subgraph_index=subgraph,
        output_file=start_file,
    )

    input_tfrec = ExtractPaths.tfrec.input(output_path)
    record_model(
        input_filename,
        start_file,
        input_tfrec,
        show_progress=show_progress,
        num_procs=num_procs,
        num_threads=num_threads,
        dequantize_output=dequantize_output,
    )

    replace_file = ExtractPaths.tflite.replace(output_path)
    cut_model(
        model_file,
        input_names=input_names,
        output_names=output_names,
        subgraph_index=subgraph,
        output_file=replace_file,
    )

    end_file = ExtractPaths.tflite.end(output_path)
    cut_model(
        model_file,
        input_names=output_names,
        output_names=None,
        subgraph_index=subgraph,
        output_file=end_file,
    )

    if not skip_outputs:
        output_tfrec = ExtractPaths.tfrec.output(output_path)
        record_model(
            input_tfrec,
            replace_file,
            output_tfrec,
            show_progress=show_progress,
            num_procs=num_procs,
            num_threads=num_threads,
            dequantize_output=dequantize_output,
        )

        end_tfrec = ExtractPaths.tfrec.end(output_path)
        record_model(
            output_tfrec,
            end_file,
            end_tfrec,
            show_progress=show_progress,
            num_procs=num_procs,
            num_threads=num_threads,
            dequantize_output=dequantize_output,
        )