# Copyright 2025-2026 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.

from pathlib import Path
from xmos_ai_tools import xformer as xf

cwd = Path(__file__).parent
outpath = cwd.parent / "app_an02008" / "src"

model_in = cwd /  "mobilenetv2.tflite"
model_out = outpath / "model.tflite"

params_file = outpath / "weights.flash"
flash_file = outpath / "weights.bin"

TOP = 0
BOTTOM = 14
SPLITS = 10

assert model_in.exists(), f"Model not found: {model_in}"

params = [
    ("xcore-thread-count", 5),
    ("xcore-op-split-tensor-arena", 1),
    ("xcore-op-split-top-op", TOP), 
    ("xcore-op-split-bottom-op", BOTTOM),
    ("xcore-op-split-num-splits", SPLITS),
    ("xcore-conv-channelwise-split-size", "80000"),
    ("xcore-naming-prefix", "model_"),
    ("xcore-conv-err-threshold", 2.1),
    ("xcore-weights-file", params_file)
]

xf.convert(model_in, model_out, params)
xf.generate_flash(output_file=flash_file, model_files=[model_out], param_files=[params_file])
xf.print_optimization_report()

print("Model in: ", model_in)
print("Model out: ", model_out)
print("Params file: ", params_file)
print("Flash file: ", flash_file)
