Getting a model onto a Sophgo BM168x: from ONNX to a running bmodel

This guide follows a model from a trained checkpoint to an inference process on a Sophgo BM1688 or BM1684X. Every command and parameter here comes from the vendor's own documentation and repositories, checked on 1 November 2026. We have not run this on hardware.

The pipeline has three compiler stages and one runtime stage. The stages are separate programs on purpose, because each one lets you check something.

Before you start: what the compiler decides for you

A bmodel file is compiled output, not a portable model. Two decisions made at compile time are locked in afterwards.

The target processor is one of them. The compiler emits code for a specific chip, so a model built for the BM1684X is not the same file as the same model built for the BM1688. Shipping on both means compiling twice.

The numeric format is the other. The BM1688 accepts INT4 and the BM1684X does not, so a model quantised to four bits has one home. INT8, FP16, BF16 and FP32 work on both; INT16 is listed for the BM1684X only.

There is a third thing worth knowing before you start, and it is not a decision but a trap. The compiler's pixel format defaults to BGR while most training pipelines use RGB. A mismatch here does not produce an error. It produces a model that loads, runs and returns confident wrong answers, which is considerably harder to diagnose than a crash. Set the preprocessing arguments and set them from the training configuration, not from memory.

Stage one: model to intermediate representation

The environment is a container so that the compiler and its dependencies match across machines.

docker pull sophgo/tpuc_dev:latest
docker run --privileged --name tpu-mlir -v $PWD:/workspace -it sophgo/tpuc_dev:latest

The compiler checkout has to sit at workspace slash tpu-mlir inside the container, with the models directory as a sibling. The privileged flag is needed for device access later.

The vendor's documentation site currently presents an expired TLS certificate, so work from the copy in the repository under the docs directory of the TPU-MLIR source tree.

Now convert the model:

model_transform.py \
  --model_name yolov5s \
  --model_def ../yolov5s.onnx \
  --input_shapes [[1,3,640,640]] \
  --mean 0.0,0.0,0.0 \
  --scale 0.0039216,0.0039216,0.0039216 \
  --keep_aspect_ratio \
  --pixel_format rgb \
  --output_names 350,498,646 \
  --test_input ../image/dog.jpg \
  --test_result yolov5s_top_outputs.npz \
  --mlir yolov5s.mlir

Three arguments earn their keep.

The mean and scale values must match the preprocessing the model was trained with. If the training pipeline divided by 255, the scale is 0.0039216 and the mean is zero. If the pipeline standardised with per-channel means, those go here instead.

The keep aspect ratio flag pads rather than stretches, and it has to match training. A detector trained on stretched images and deployed on padded ones sees a different geometry.

The test input and test result arguments make the compiler compare its own output against the original framework and write the reference to a numpy archive. Always use them.

What you get

The intermediate representation file, plus a numpy archive holding the preprocessed input. That archive becomes the reference for the accuracy checks in the later stages, so keep it with the model.

Stage two: calibration

If you are shipping INT8, this step is not optional.

run_calibration.py yolov5s.mlir \
  --dataset ../COCO2017 \
  --input_num 100 \
  -o yolov5s_cali_table

The vendor guidance is one hundred to a thousand images depending on the situation. The choice matters more than the count: the calibration set should contain the conditions the product will meet. A model deployed in low light and calibrated on daylight images loses accuracy in exactly the conditions that are hardest to test.

FP16 and BF16 skip this stage entirely. If bandwidth allows it and accuracy is the priority, FP16 is a reasonable first target to get a system running, with INT8 as a later optimisation.

Stage three: deployment

model_deploy.py \
  --mlir yolov5s.mlir \
  --quantize INT8 \
  --calibration_table yolov5s_cali_table \
  --processor bm1688 \
  --test_input yolov5s_in_f32.npz \
  --test_reference yolov5s_top_outputs.npz \
  --tolerance 0.85,0.45 \
  --model yolov5s_bm1688_int8.bmodel

The processor argument is required and must name the actual target chip.

The tolerance argument sets the minimum similarity the quantised model must reach against the float model, as a cosine and an Euclidean pair. The correctness argument does the same for the deployed file against the compiled intermediate representation, and the vendor's defaults are tighter there when a quantisation table is in play.

Read both numbers rather than skipping them. A low first number means the quantisation lost too much, which points at the calibration set. A low second number means the compiler or the runtime produced something different from what it was asked to, which is a different investigation.

Stage four: running it

The runtime is a separate repository from the compiler. It loads a compiled file and runs it.

#include "bmruntime_cpp.h"
using namespace bmruntime;

Context ctx;
if (BM_SUCCESS != ctx.load_bmodel("yolov5s_bm1688_int8.bmodel")) { return 1; }

Network net(ctx, "yolov5s_detect");
auto &inputs = net.Inputs();
inputs[0]->Reshape({1, {1, 3, 640, 640}});
inputs[0]->CopyFrom(input_data);

net.Forward();

auto &outputs = net.Outputs();
outputs[0]->CopyTo(result_data);

The names matter here. The namespace is bmruntime, the classes are Context, Network and Tensor, and the header is bmruntime_cpp.h. Most third-party examples use names that no longer exist.

In Python the wrapper is a ctypes binding with the same shape:

from bmrt import BmrtEngine

with BmrtEngine("yolov5s_bm1688_int8.bmodel") as engine:
    outputs = engine.infer({"input": input_data})

Using both tensor cores on the BM1688

The BM1688 has two tensor cores and the vendor ships a tutorial that runs two networks at once, one per core. This is two network slots, not twice the throughput, so it only pays when the two networks are independent of each other.

The partition happens at runtime rather than at compile time. The extended launch function in the C interface takes a list of core identifiers, and that argument is how a network is pinned to a core.

The vendor's demo repository has a two-core two-task YOLOv5 example under its tutorial directory, with both C++ and Python versions and the compiled models it needs.

Inspecting the result

model_tool --info yolov5s_bm1688_int8.bmodel
model_tool --print yolov5s_bm1688_int8.bmodel

The print form gives network names, input and output shapes and numeric types. When a compiled file loads but rejects your input, this is the fastest way to find out what it actually contains.

Files can also be split and rejoined, which matters when a product ships two models:

model_tool --extract multi.bmodel
model_tool --combine model_a.bmodel model_b.bmodel -o combined.bmodel

Checking on the real target

The compiler's checks run on the development machine. They do not carry over to a board.

Before shipping, run the compiled model on the target against a labelled validation set and record the accuracy there. The gap between the two is usually small, but it is not always zero, and the board is where memory behaviour and thermal limits become real.

A note on older material

Articles from an earlier generation of this toolchain describe tools called bmnet, bm_test, bm_perf, sophon_test and top. Those tools are gone. The vendor removed the repository that hosted them and published a migration appendix mapping each old command to its replacement.

The current set is model_transform.py, run_calibration.py, model_deploy.py and model_runner.py, with model_tool for inspecting and combining compiled files. If a tutorial refers to a command that is not in that list, it is describing the previous generation.

Licensing

The compiler is under a two-clause BSD licence with third-party components excepted. The runtime in libsophon is under Apache 2.0. A product that ships a compiled model and the runtime does not need the compiler at all, and the two are separately licensed, which is convenient for supply chain review.