Metadata-Version: 2.4
Name: tosa-converter-for-tflite
Version: 2026.2.0
Summary: A tool to legalize TFLite FlatBuffer to TOSA MLIR.
Home-page: https://gitlab.arm.com/tosa/tosa-converter-for-tflite
Author: Arm Ltd.
Author-email: 
License: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Dynamic: license-file

<!--
    SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>

    SPDX-License-Identifier: Apache-2.0
-->

# TOSA converter for TFLite

A tool to legalize TFLite FlatBuffer to TOSA MLIR Bytecode or Text.

TOSA Converter for TFLite is currently in the experimental development phase. As such, it may contain bugs or incomplete features. We appreciate your feedback and patience as we continue to improve the tool.

## Usage

### Python API

```python
from tosa_converter_for_tflite import (
    tflite_flatbuffer_to_tosa_mlir,
    TosaConverterDebugInfo,
    TosaConverterOutputFormat,
)

# default (bytecode) output
tflite_flatbuffer_to_tosa_mlir("model_input.tflite", "model_output.tosa.mlirbc")

# textual MLIR output (locations are off by default)
tflite_flatbuffer_to_tosa_mlir(
    "model_input.tflite",
    "model_output.mlir",
    TosaConverterOutputFormat.Text
)

# textual MLIR output with explicit location annotations (debug info)
tflite_flatbuffer_to_tosa_mlir(
    "model_input.tflite",
    "model_output_with_locs.mlir",
    TosaConverterOutputFormat.Text,
    TosaConverterDebugInfo.Enable
)
```

### Command-line Interface (CLI)

The `tosa-converter-for-tflite` CLI provides a simple way to convert `.tflite` models
into TOSA MLIR(bytecode or text). It supports both file-based and stream-based workflows
for easy integration into toolchains or pipelines.

### Usage Examples

**Convert a `.tflite` file to a TOSA bytecode file(default):**

```bash
tosa-converter-for-tflite model.tflite --bytecode -o out.tosa.mlirbc
```

**Version Info**

`--version` prints the converter version along with the TensorFlow and LLVM commit hashes:

```text
TOSA Converter for TFLite version: 2026.02.0
TensorFlow commit: <tf_commit_hash>
LLVM commit:       <llvm_commit_hash>
```

**Read the model from file and write textual TOSA MLIR to file**

```bash
tosa-converter-for-tflite model.tflite --text -o out.mlir
```

**Enable per-operation location annotations in textual MLIR**

```bash
tosa-converter-for-tflite model.tflite --text --emit-debug-info -o out_with_locs.mlir
```


**Read the model from a file and write textual TOSA MLIR to stdout**

```bash
tosa-converter-for-tflite model.tflite --text
```

**Stream the model from stdin and write bytecode to file**
```bash
cat model.tflite | tosa-converter-for-tflite --bytecode -o out.tosa.mlirbc
```

**Fully stream the model from stdin and write textual MLIR to stdout**
```bash
cat model.tflite | tosa-converter-for-tflite --text
```

### Debug info and locations

Textual MLIR can include per-operation location annotations (e.g., `loc(#loc3)`).
These annotations are disabled by default; enable them with
`TosaConverterDebugInfo.Enable` in the Python API or `--emit-debug-info` in the
CLI. They are useful for debugging, but can significantly increase output
size. If you run into memory or storage issues, keep debug info disabled.

When writing to an in-memory Python `BinaryIO` stream, the converter buffers the
entire output before writing it to the stream. For large models (especially with
debug info enabled), prefer writing to a file to avoid excessive peak memory use.

## Supported Platforms

This tool supports Linux(r) and Windows(r) on x86_64 architectures.
Support for Linux(r) on AArch64 is experimental.

## Build Requirements

Before you build the converter, ensure you have the following tools installed and on your PATH.

### Linux (Ubuntu)

| Tool   | Purpose         | Version  |
|--------|-----------------|------------------|
| Bazel  | build           |  v7.4.1          |
| Clang  | build           |  >= v17          |
| Python | build & runtime |  >= v3.10        |

### Windows (x86_64)

| Tool                                                     | Purpose         | Version                               |
|----------------------------------------------------------|-----------------|---------------------------------------|
| Bazel                                                    | build           | 7.4.1                                 |
| LLVM (Clang-CL)                                          | build           | 20.1.0                                |
| MSYS2                                                    | shell & tooling | 3.6.3-2 with Pacman 20.1.0 and bash.exe |
| Python                                                   | build & runtime | ≥3.10                                 |
| Visual Studio 2022 Professional (C++ Desktop Dev Tools)  | build & debug   | 17.11.5                               |

## Python wheel creation

To build the Python wheel Clang is required, the wheel can then be built using:

```console
pip install build
python -m build --wheel
```

The wheel should now be in the `dist` directory.


## Versioning

This project uses Calendar Versioning ([CalVer](https://calver.org/)) in the format: `YYYY.0M.Micro[.Modifier]`

- **YYYY** – 4-digit release year (e.g., 2025)
- **0M** – 2-digit Zero-padded release month (e.g., 07 for July)
- **Micro** – The third and usually final number in the version. Sometimes referred to as the "patch" segment.
- **Modifier** - Optional text tag used to signify extra information
about the build. Currently accepted options values:
  - **.dev0** - (default) Any build produced with default configuration will have this tag.
  - no tag (empty string) - builds produced for release will have no tag at all.

# License

TOSA Converter for TFLite is distributed under the Apache 2.0 License.

Note: the code under `tosa_converter_for_tflite/tfl_passes` was migrated over from the TensorFlow project and therefore inherits copyright attributed to `The TensorFlow Authors` and `Google Inc`. This code is also distributed under the Apache 2.0 License.

# Trademarks and Copyrights

* Bazel(tm) is a trademark of Google(r) LLC.
* Git(tm) is a trademark of Software Freedom Conservancy.
* LLVM(r) and the LLVM logo are trademarks of the LLVM Foundation.
* Linux(r) is the registered trademark of Linus Torvalds in the U.S. and elsewhere.
* Python(r) is a registered trademark of the Python Software Foundation.
* TensorFlow(tm), the TensorFlow logo and any related marks are trademarks of Google Inc.
* Ubuntu(r) is a registered trademark of Canonical(r) Ltd.
* Visual Studio(r) is a registered trademark of Microsoft(r) Corporation.
* Windows(r) is a registered trademark of Microsoft(r) Corporation.
* CUDA(tm) is a registered trademark of NVIDIA(r) Corporation
