qbcompiler.frontend¶
Model_Dict¶
Wrapper around the Mobilint compiler to support compilation and inference workflows.
Capabilities include:
- Compilation of models into MXQ artifacts runnable on Mobilint NPUs.
- Inference using the full-precision high-level compiled model on CPU or GPU.
- Inference with the quantized model on CPU or GPU.
Methods¶
| Name | Description |
|---|---|
__init__ |
Initialize the Mobilint compiler wrapper. |
compile |
Compile the wrapped model into an MXQ artifact. |
__init__¶
def __init__(
self,
model,
backend="onnx",
device="cpu",
feed_dict=None,
dynamic_axes=None,
in_dformats=None,
yolo_decode_include=False,
use_custom_mask=False,
exclude_first_subgraph=False,
**kwargs,
)
Initialize the Mobilint compiler wrapper.
Parameters
model— string or model instance. Model path or in-memory model to compile.backend— string. Framework identifier for the model (for example "onnx"). Defaults to "onnx".device— string. Target device for inference ("cpu" or "gpu"). Defaults to "cpu".feed_dict— dict. Example inputs for shape resolution.dynamic_axes— dict. Marks model axes as dynamic.in_dformats— dict. Describes input data formats.yolo_decode_include— bool. Runs YOLO decode on NPU whenTrue.use_custom_mask— bool. For non-LLM models that include a mask, use a custom mask provided as a separate input instead of the causal mask.exclude_first_subgraph— bool. Applies only when CPU offloading is enabled: exclude the first subgraph from the final graph if it is unsupported.kwargs— dict. Additional compiler arguments.
compile¶
def compile(
self,
save_subgraph_type: int = 0,
output_subgraph_path="",
compile_config: CompileConfig = None,
**kwargs,
)
Compile the wrapped model into an MXQ artifact.
Parameters
save_subgraph_type— int. Controls optional MBLT exports: 0 disables; 1 saves graph structure; 2 saves structure and weights; 3 splits structure into multiple subgraphs; 4 splits structure and weights.output_subgraph_path— string. Destination for exported .mblt whensave_subgraph_typeis 1–4.compile_config— CompileConfig. Compile configuration object.kwargs— dict. Additional arguments forwarded to the compiler.
Functions¶
| Name | Description |
|---|---|
mxq_compile |
Compile a model into a Mobilint eXeCUtable (MXQ) package for execution on Mobilint NPUs. |
mblt_compile |
Export a model to the Mobilint .mblt format without producing an MXQ package. |
run_mblt_model |
mxq_compile¶
def mxq_compile(
model,
calib_data_path: Union[str, List[str]] = _UNSET,
save_subgraph_type: int = 0,
output_subgraph_path="",
save_path: Union[str, List[str]] = _UNSET,
backend="onnx",
# -- ModelDict params --
feed_dict=None,
dynamic_axes=None,
in_dformats=None,
yolo_decode_include=False,
use_custom_mask=False,
exclude_first_subgraph=False,
# -- CompileConfig-overridable (_UNSET = defer to config) --
device=_UNSET,
inference_scheme=_UNSET,
use_random_calib=_UNSET,
cpu_offload=_UNSET,
optimize_option=_UNSET,
buffer_mode=_UNSET,
input_shape_dict=_UNSET,
force_npu_input_reposition=_UNSET,
force_npu_output_reposition=_UNSET,
image_channels=_UNSET,
split_blocks=_UNSET,
split_parts=_UNSET,
# -- Config objects --
config_preset: Optional[str] = None,
compile_config: Optional[CompileConfig] = None,
resource_management_config: Optional[ResourceManagementConfig] = None,
calibration_config: Optional[CalibrationConfig] = None,
bit_config: Optional[BitConfig] = None,
llm_config: Optional[LlmConfig] = None,
optq_config: Optional[OptqConfig] = None,
mod_config: Optional[ModConfig] = None,
equivalent_transformation_config: Optional[EquivalentTransformationConfig] = None,
search_weight_scale_config: Optional[SearchWeightScaleConfig] = None,
save_sample_config: Optional[SaveSampleConfig] = None,
uint8_input_config: Optional[Uint8InputConfig] = None,
preprocessing_config: Optional[PreprocessingConfig] = None,
**kwargs,
)
Compile a model into a Mobilint eXeCUtable (MXQ) package for execution on Mobilint NPUs.
When no explicit value is provided for a parameter marked with _UNSET, the default from CompileConfig is used. This allows a compile_config file or object to supply the value without being overridden by function-level defaults.
Configuration is resolved in priority order (highest to lowest):
- Explicitly passed function arguments
- Individual sub-config objects (calibration_config, llm_config, etc.)
- kwargs partial overrides (quantization_method, weight_dtype, etc.)
- compile_config (CompileConfig object or JSON/YAML file) or config_preset
- CompileConfig field defaults
Parameters
model— string or model instance. Model path. When usingbackend="onnx", this should be the path to an ONNX model file. Whenbackendis "torchscript", provide a TorchScript module; when "torch", provide a standard PyTorch model. Forbackend="tf", pass the directory that contains the TensorFlow SavedModel graph and assets. Forbackend="tflite", pass the TF Lite model path.calib_data_path— string or list of strings. Path(s) to the calibration dataset. Accepts either a text/json file that lists NumPy files or a directory that contains the pre-processed NumPy files.save_subgraph_type— int. Controls optional MBLT subgraph exports: 0 disables exports; 1 saves only the graph structure; 2 saves graph structure plus weights; 3 saves the graph structure split into multiple subgraphs; 4 saves both structure and weights split into multiple subgraphs. Defaults to 0.output_subgraph_path— string. Destination path for the exported .mblt file whensave_subgraph_typeis 1–4. The resulting file can be used for visualization. Defaults to "".save_path— string or list of strings. Output MXQ filename(s). When omitted, defaults to "{model_name}.mxq" derived from the model path basename.backend— string. Framework used to generate the Mobilint IR. Must be one of "onnx", "tf", "tflite", "torchscript", or "torch". Defaults to "onnx".device— string. Compilation and inference device: "cpu" or "gpu". When omitted, uses CompileConfig default.feed_dict— dict. Example input tensors for shape inference and inference validation.dynamic_axes— dict. Marks model axes as dynamic. Keys are input names and values map dimension indices to aliases (for example {"input": {2: "seq_len"}}).in_dformats— dict. Describes input tensor data formats.inference_scheme— string. NPU inference scheme. One of "single", "multi", "global", "global4", or "global8". When omitted, uses CompileConfig default.yolo_decode_include— bool. Determines whether YOLO decode runs on NPU. Defaults toFalse.use_random_calib— bool. Generates random calibration data to validate model compilability. When omitted, uses CompileConfig default.cpu_offload— bool. Enables CPU offloading during NPU inference. When omitted, uses CompileConfig default.optimize_option— int. Compiler optimization strategy selector. When omitted, uses CompileConfig default.buffer_mode— int. Buffer serialization mode: 0 uses a naive buffer, 1 uses an mmap-backed buffer. When omitted, uses CompileConfig default.input_shape_dict— dict. Multi-shape compilation specification for STT/TTS models only (e.g., Conformer-CTC, MeloTTS). The dynamic axis is defined with respect to each input tensor's shape. During compilation, the same axis is varied across all model inputs using the provided values. Currently, only a single entry key ("multi_shape0") is supported. Example: {"multi_shape0": {"axis": 2, "values": [100, 200, 300]}}force_npu_input_reposition— bool. Force input reposition operations to run on NPU instead of CPU. When omitted, uses CompileConfig default.force_npu_output_reposition— bool. Force output reposition operations to run on NPU instead of CPU. When omitted, uses CompileConfig default.image_channels— int. Number of image channels (0 for auto-detect). When omitted, uses CompileConfig default.split_blocks— list of int. Multi-MXQ split points by transformer block index. Only supported for LLM models. When omitted, uses CompileConfig default.split_parts— int. Evenly split transformer blocks into N MXQ parts. Only supported for LLM models. When omitted, uses CompileConfig default.use_custom_mask— bool. For non-LLM models that include a mask, use a custom mask provided as a separate input instead of the causal mask. For LLMs that incorporate RoPE, setting LLMConfig.dynamic_rope toTruenot only enables the use of dynamic RoPE provided via an additional input, but also activates the custom mask. Defaults toFalse.exclude_first_subgraph— bool. Applies only when CPU offloading is enabled: exclude the first subgraph from the final graph if it is unsupported. Defaults toFalse.config_preset— string. Name of a built-in configuration preset. When provided, loads the preset via CompileConfig.from_preset(). Defaults to None (no preset). Available presets: - "classification": Image classification models (ResNet, EfficientNet, ViT, etc.) - "detection": Object detection models (YOLO, SSD, DETR, etc.) - "classification_torchvision": Torchvision classification models with standard preprocessing - "yolo_640": YOLO detection models with 640x640 letterbox preprocessing - "yolo_1280": YOLO detection models with 1280x1280 letterbox preprocessing - "llm": Large Language Models (LLaMA, Qwen, Gemma, etc.) - "llm_fast": LLM with faster compilation (less accuracy optimization) - "vision_transformer": Vision Transformer models (ViT, DeiT, Swin, etc.) - "multimodal": Multimodal models (CLIP, BLIP, LLaVA, etc.)compile_config— CompileConfig or string. CompileConfig object or path to JSON/YAML configuration file containing all compilation settings (resourceManagement, calibration, bit, optq, mod, llm, etc.).resource_management_config— ResourceManagementConfig. Resource management configuration object.calibration_config— CalibrationConfig. Calibration configuration object.bit_config— BitConfig. Bit configuration object for quantization precision settings.llm_config— LlmConfig. LLM configuration object.optq_config— OptqConfig. OPTQ (Optimal Brain Quantization) configuration object.mod_config— ModConfig. MOD (Metric-based Optimization and Distillation) configuration object.equivalent_transformation_config— EquivalentTransformationConfig. Configuration for equivalent transformations like SmoothQuant.search_weight_scale_config— SearchWeightScaleConfig. Configuration for weight scale search.save_sample_config— SaveSampleConfig. Configuration for sample data generation and saving.uint8_input_config— Uint8InputConfig. Configuration for uint8 input handling.preprocessing_config— PreprocessingConfig. Preprocessing pipeline configuration.kwargs— dict. Additional compiler arguments. Supports partial config overrides such asquantization_method,quantization_mode,percentile,weight_dtype,ram_usage,max_sequence_length, etc. Also accepts deprecated parameters (quantization_config,advanced_quantization_config,input_process_config,save_sample,sample_dtype) which will emit DeprecationWarning.
Returns
None.
Using configuration
There are three ways to configure quantization settings:
-
Load all settings from a JSON/YAML config file:
-
Pass individual sub-config objects:
from qbcompiler import mxq_compile from qbcompiler.configs import ( ResourceManagementConfig, CalibrationConfig, BitConfig, OptqConfig, ModConfig, LlmConfig, ) resource_mgmt = ResourceManagementConfig(weight_dtype="float32") calib_cfg = CalibrationConfig(method=1, mode=1) bit_cfg = BitConfig(...) optq_cfg = OptqConfig(apply=True) mod_cfg = ModConfig(apply=False) llm_cfg = LlmConfig(apply=True) mxq_compile( model="path/to/model.onnx", calib_data_path="path/to/calib", resource_management_config=resource_mgmt, calibration_config=calib_cfg, bit_config=bit_cfg, optq_config=optq_cfg, mod_config=mod_cfg, llm_config=llm_cfg, device="gpu", ) -
Automatically applied partial configuration overrides:
from qbcompiler import mxq_compile mxq_compile( model="path/to/model.onnx", calib_data_path="path/to/calib", quantization_method=1, # per channel quantization quantization_mode=1, # max percentile quantization percentile=0.999, # percentile value for max percentile quantization quantization_output=0, # per layer quantization for the output layer device="gpu", )
Please refer to the mxq_compile function and the quantization configuration section for the meaning of the quantization-related numeric values.
Compiling models with custom inputs (ONNX/Torch/TensorFlow)
Provide NumPy inputs whenever the model omits shape information so that qbcompiler can infer unknown dimensions and data formats.
from qbcompiler import mxq_compile
import numpy as np
example_input = {
"input_node_name_1": np.random.randn(1, 3, 224, 224).astype(np.float32),
"input_node_name_2": np.random.randn(1, 8, 224, 224).astype(np.float32),
"input_node_name_3": np.random.randn(1, 4, 56, 56).astype(np.float32),
}
in_dformats = {
"input_node_name_1": "NCHW",
"input_node_name_2": "NCHW",
"input_node_name_3": "NCHW",
}
onnx_model_path = "path/to/your/model.onnx"
mxq_compile(
model=onnx_model_path,
feed_dict=example_input,
in_dformats=in_dformats,
backend="onnx",
compile_config="path/to/config.json",
)
mblt_compile¶
def mblt_compile(
model: str | Any,
mblt_save_path: str,
backend="onnx",
device="cpu",
feed_dict=None,
dynamic_axes=None,
in_dformats=None,
yolo_decode_include=False,
cpu_offload=False,
use_custom_mask=False,
exclude_first_subgraph=False,
**kwargs,
)
Export a model to the Mobilint .mblt format without producing an MXQ package.
Parameters
model— string or model instance. Source model or path to compile.mblt_save_path— string. Output path for the .mblt artifact.backend— string. Framework identifier ("onnx", "tf", "tflite", "torchscript", or "torch"). Defaults to "onnx".device— string. Compilation device ("cpu" or "gpu"). Defaults to "cpu".feed_dict— dict. Example inputs used for shape inference.dynamic_axes— dict. Declares dynamic axes per input name.in_dformats— dict. Input dataformat metadata.yolo_decode_include— bool. Runs YOLO decode on NPU when True.cpu_offload— bool. Enables CPU offloading for unsupported groups.use_custom_mask— bool. For non-LLM models that include a mask, use a custom mask provided as a separate input instead of the causal mask. For LLMs that incorporate RoPE, setting LLMConfig.dynamic_rope to True not only enables the use of dynamic RoPE provided via an additional input, but also activates the custom mask.exclude_first_subgraph— bool. Applies only when CPU offloading is enabled: exclude the first subgraph from the final graph if it is unsupported.kwargs— dict. Additional arguments forwarded to the compiler.
Returns
None.