Metadata-Version: 2.4
Name: TACHY-Compiler
Version: 0.2.1
Summary: TACHY model compiler and platform conversion tools.
Author: Deeper-I
License: BSD-3-Clause
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: networkx
Requires-Dist: onnx
Requires-Dist: onnxruntime
Requires-Dist: commentjson
Requires-Dist: ordered-set
Requires-Dist: easydict
Requires-Dist: DDesignerAPI
Provides-Extra: yolov9
Requires-Dist: torch; extra == "yolov9"
Provides-Extra: fp16
Requires-Dist: onnxconverter_common; extra == "fp16"
Provides-Extra: mcp
Requires-Dist: mcp; extra == "mcp"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Dynamic: license-file

# TACHY-Compiler

TACHY-Compiler is a comprehensive deep learning compiler and platform conversion framework developed by Deeper-I. It is designed to bridge the gap between popular deep learning frameworks (like PyTorch) and the TACHY hardware accelerator runtime environment.

This compiler handles model parsing, optimization, tensor alignment, and binary generation. It robustly supports large-scale models, complex operators, and dual-checkpoint pipelines out of the box.

## Key Features

- **PyTorch to ONNX to TACHY**: Seamless end-to-end compilation pipeline.
- **Dual vs single compile**: **Dual** (`compile-dual`) takes distinct prepared **eval** / **comp** (from **pre** + **opt** via deploy) and produces full NPU-runnable instructions **and** parameters. **Single** (`compile-single`) takes one PT/CKPT (usually scratch **pre**), feeds it as **identical** eval and comp through the **same** backend, and is a temporary **NPU usability smoke** — not opt-accuracy verification. See `tachy guide compile-modes`. **eval** allows inference verification; **comp** does not (compile-stage temp). **pre** = scratch (no transform/pruning); **opt** = transform and/or pruning. When pruning follows transform, dual **pre** must still be pre-transform scratch.
- **Large Model Support**: Built-in mechanisms to handle models larger than the Protobuf 2GB limit using `external_data` mapping.
- **Robust ONNX Versioning**: Automatically utilizes ONNX Opset 18 for stability, equipped with automatic version conversion and fallback mechanisms to mitigate `onnxscript` conversion issues.
- **MCP Server Integrated**: Exposes tools to LLMs and agentic frameworks via the Model Context Protocol (MCP) using `FastMCP`.
- **Dynamic Binary Resolution**: Intelligently locates dependency binaries utilizing environment variables (`TACHY_UTIL_PATH`) and relative package paths.

## Installation

You can install TACHY-Compiler directly via PyPI:

```bash
pip install TACHY-Compiler
# MCP server (requires Python >= 3.10):
pip install "TACHY-Compiler[mcp]"
# FP16 convert-pt2onnx:
pip install "TACHY-Compiler[fp16]"
```

### Requirements
- Python >= 3.8 (MCP extra requires Python >= 3.10)
- `numpy`, `networkx`, `onnx`, `onnxruntime`, `commentjson`, `ordered-set`, `easydict`, `DDesignerAPI`
- (Optional) `torch` via `TACHY-Compiler[yolov9]` for PyTorch → ONNX
- (Optional) `mcp` via `TACHY-Compiler[mcp]` for `tachy mcp`
- (Optional) `onnxconverter_common` via `TACHY-Compiler[fp16]` for `--FP16`

## CLI Usage

TACHY-Compiler provides a simple, unified command-line interface under the `tachy` command.

### 1. Compile modes (dual vs single)

```bash
tachy guide compile-modes            # goals + procedures
tachy guide compile-single           # same guide text (topic alias; does not run compile)
```

| Mode | Goal | How |
|------|------|-----|
| **Single** | NPU smoke / usable check | One model as both eval and comp (`tachy compile-single`) |
| **Dual** | Full instr + params | Distinct eval ≠ comp after pre/opt deploy (`tachy compile-dual`) |

Agents: MCP `get_compile_modes_guide`, `compile_single_model`, `compile_dual_model`.

### 2. Deploy dual checkpoints (pre / opt → eval / comp)

`compile-dual` expects **prepared** `eval` / `comp` models, not raw training `pre` / `opt` checkpoints. Adapt checkpoints in your environment first:

| Name | Meaning |
|------|---------|
| **pre** | Scratch — no transform / no pruning |
| **opt** | Transform and/or pruning applied |
| **eval** | Allows inference verification |
| **comp** | Compile-stage input (not for inference verification) |

```bash
tachy guide deploy-dual              # print the full contract + checklist
tachy init-deploy --out ./deploy_dual_pt.py   # blank template (hooks to fill in)
# edit hooks, then:
python deploy_dual_pt.py pre.pt opt.pt eval.pt comp.pt
```

Agents can use the same guide via MCP (`get_deploy_dual_guide`, `init_deploy_template`).

### 3. Dual Checkpoint Compilation
Takes prepared eval and comp models and produces full NPU-runnable instructions and parameters. `eval` artifacts allow inference verification; `comp` artifacts do not (temporary files for the next compile stage). Intermediate files are automatically managed and cleaned up if requested.

```bash
tachy compile-dual \
    --eval_model ./eval_model.pth \
    --comp_model ./comp_model.pth \
    --input_shape "[1,3,224,224]" \
    --output_dir ./dual_output \
    --cleanup
```
Outputs will be cleanly structured into `./dual_output/final_binaries/eval/` and `./dual_output/final_binaries/comp/`.

### 4. Single compile (NPU smoke)

Same backend as dual; pass one model for both slots:

```bash
tachy compile-single \
    --model ./model.pt \
    --input_shape "[1,3,224,224]" \
    --output_dir ./single_output
```

Equivalent: `compile-dual` with `--eval_model` and `--comp_model` set to the same path. Confirm NPU execution; then use dual for production-style deploy.

### 5. Single Model Pipeline Steps
If you need granular control, you can execute the pipeline steps individually.

**Convert PyTorch to ONNX**
```bash
tachy convert-pt2onnx ./model.pth ./model.onnx 1 3 224 224
tachy convert-pt2onnx ./model.pth ./model.onnx 1 3 224 224 --FP16   # needs [fp16] extra
```

**Lint ONNX Model** (`--format table|markdown|json`, `--strict` treats WARN as failure, `--buffer_size N` for width check)
```bash
tachy lint ./model.onnx
tachy lint ./model.onnx --suggest                   # search remedies for failed checks
tachy lint ./model.onnx --fix ./model_fixed.onnx    # repeat lint -> remedy until clean (--iterate N, default 5)
```
Each check lives in `src/tachy_compiler/compiler/lint/rules/<id>_*.py` together with its remedies
(`exact`: same I/O, `layout`: host must permute the input, `advisory`: retraining needed).
Remedies are verified by a full re-lint and an onnxruntime equivalence check.
`compile-dual --auto_fix` applies the best remedy before ONNX → TACHY conversion.

**Convert ONNX to TACHY Format**
```bash
tachy convert-onnx ./model.onnx ./model.tachy
```

**Compile TACHY Model to Runtime Binaries**
```bash
tachy compile ./model.tachy ./output_bin_dir
```

### 6. Model Context Protocol (MCP) Server
Requires `pip install "TACHY-Compiler[mcp]"` (Python >= 3.10). Spin up the server so LLMs and agents can compile models:
```bash
tachy mcp
```

## Environment Variables
- `TACHY_UTIL_PATH` (Optional): Absolute path to the directory containing external utility binaries (`fc.out`, `block_4bit.out`). If unset, the compiler looks under the installed package's `optional/` directory, then the development repo-root `optional/`. Download these binaries from https://gofile.me/5NFjK/PXXLhuIqj, then set `TACHY_UTIL_PATH` or place them under `optional/`.

## License

This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.
