diff options
author | Annie Tallund <annie.tallund@arm.com> | 2023-03-15 11:27:08 +0100 |
---|---|---|
committer | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-10-11 15:43:14 +0100 |
commit | 867f37d643e66c0223457c28f5345f2f21db97f2 (patch) | |
tree | 4e3c55896760e24a8b5eadc5176ce7f5586552e1 /src/mlia/nn/rewrite/core/utils/utils.py | |
parent | 62768232c5fe4ed6b87136c336b65e13d030e9d4 (diff) | |
download | mlia-867f37d643e66c0223457c28f5345f2f21db97f2.tar.gz |
Adapt rewrite module to MLIA coding standards
- Fix imports
- Update variable names
- Refactor helper functions
- Add licence headers
- Add docstrings
- Use f-strings rather than % notation
- Create type annotations in rewrite module
- Migrate from tqdm to rich progress bar
- Use logging module in rewrite module: All print statements are
replaced with logging module
Resolves: MLIA-831, MLIA-842, MLIA-844, MLIA-846
Signed-off-by: Benjamin Klimczak <benjamin.klimczak@arm.com>
Change-Id: Idee37538d72b9f01128a894281a8d10155f7c17c
Diffstat (limited to 'src/mlia/nn/rewrite/core/utils/utils.py')
-rw-r--r-- | src/mlia/nn/rewrite/core/utils/utils.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/mlia/nn/rewrite/core/utils/utils.py b/src/mlia/nn/rewrite/core/utils/utils.py index d1ed322..ddf0cc2 100644 --- a/src/mlia/nn/rewrite/core/utils/utils.py +++ b/src/mlia/nn/rewrite/core/utils/utils.py @@ -1,22 +1,28 @@ # SPDX-FileCopyrightText: Copyright 2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 -import os +"""Model and file system utilites.""" +from __future__ import annotations + +from pathlib import Path import flatbuffers -from tensorflow.lite.python import schema_py_generated as schema_fb +from tensorflow.lite.python.schema_py_generated import Model +from tensorflow.lite.python.schema_py_generated import ModelT -def load(input_tflite_file): - if not os.path.exists(input_tflite_file): - raise FileNotFoundError("TFLite file not found at %r\n" % input_tflite_file) +def load(input_tflite_file: str | Path) -> ModelT: + """Load a flatbuffer model from file.""" + if not Path(input_tflite_file).exists(): + raise FileNotFoundError(f"TFLite file not found at {input_tflite_file}\n") with open(input_tflite_file, "rb") as file_handle: file_data = bytearray(file_handle.read()) - model_obj = schema_fb.Model.GetRootAsModel(file_data, 0) - model = schema_fb.ModelT.InitFromObj(model_obj) + model_obj = Model.GetRootAsModel(file_data, 0) + model = ModelT.InitFromObj(model_obj) return model -def save(model, output_tflite_file): +def save(model: ModelT, output_tflite_file: str | Path) -> None: + """Save a flatbuffer model to a given file.""" builder = flatbuffers.Builder(1024) # Initial size of the buffer, which # will grow automatically if needed model_offset = model.Pack(builder) |