Skip to main content
SDK Version: Next
DX-COM manual (PDF)

Q-Master (Quantization-Aware Training)

Q-Master is the DX-COM feature that runs Quantization-Aware Training (QAT). It fine-tunes the model while simulating INT8 quantization, so it can recover accuracy that post-training quantization (PTQ) alone cannot.

In short: add a qmaster block to your PTQ JSON, run dxcom as usual, and get a fine-tuned .dxnn. You do not need labels, a separate training script, or a different command.

Q-Master reuses the same JSON configuration as a normal compile. Add a qmaster block and DX-COM switches from PTQ to Q-Master automatically — no extra command-line flag is needed.

Version Support

Q-Master is available from DX-COM v2.4.0. From v2.5.0 the Python API takes qmaster=QMasterConfig(...), and Segmentation Q-Master is available. See Version History.


When to Use Q-Master​

Compile with PTQ ─► Accuracy acceptable? ── yes ─► Done
│ no
▼
Tune PTQ first (02_07 Quantization Tuning Workflow)
│ still not enough
▼
Use Q-Master (this chapter)
PTQQ-Master (QAT)
Needs training dataCalibration images only (about 100)Training images (hundreds to thousands)
Needs a GPUNoYes (CUDA GPU, default cuda:0)
Compile timeMinutesMinutes to hours, depending on model and data size
Changes model weightsNoYes (fine-tuned)
Typical useFirst attemptAccuracy still below target after PTQ tuning
Checklist before you start
  • The model already compiles with PTQ using the same JSON config.
  • A CUDA GPU is available to PyTorch (python -c "import torch; print(torch.cuda.is_available())" prints True). To pick a specific GPU, set CUDA_VISIBLE_DEVICES and keep device at "cuda:0".
  • A folder of representative images is set in default_loader.dataset_path (the images are searched recursively by file_extensions). Use images similar to your real input, not the test set you evaluate on.
  • You have a way to measure accuracy before and after (compare the PTQ .dxnn with the Q-Master .dxnn on the same evaluation set).

Quick Start​

1. Add a qmaster block to your config​

Start from the JSON you already use for PTQ and append a qmaster block. Example for an ImageNet classifier:

{
"inputs": { "input": [1, 3, 224, 224] },
"calibration_num": 100,
"calibration_method": "ema",
"default_loader": {
"dataset_path": "/datasets/ILSVRC2012/train",
"file_extensions": ["jpeg", "jpg", "png", "JPEG"],
"preprocessings": [
{ "resize": { "mode": "default", "size": 256 } },
{ "centercrop": { "width": 224, "height": 224 } },
{ "convertColor": { "form": "BGR2RGB" } },
{ "div": { "x": 255 } },
{ "normalize": { "mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225] } },
{ "transpose": { "axis": [2, 0, 1] } },
{ "expandDim": { "axis": 0 } }
]
},
"qmaster": {
"batch_size": 16,
"train_limit": 2000,
"val_limit": 200,
"epochs": 10
}
}
One preprocessing pipeline

inputs, calibration_*, and default_loader work exactly as in JSON File Configuration. default_loader.preprocessings is the single source of truth for both calibration and Q-Master training data — do not redefine it inside qmaster.

Apply convertColor again at inference

convertColor is not integrated into the NPU graph (it is listed under Skipped nodes in compiler.log). Your inference code must apply the same BGR2RGB conversion before feeding the compiled model; otherwise inference sees a different channel order from calibration and training, and accuracy drops without any error.

2. Compile​

dxcom -m model.onnx -c config_with_qmaster.json -o output/

3. Check the result​

  • output/<model>.dxnn — the compiled model.
  • output/qat_checkpoint/qat_checkpoint.qxnn — the best checkpoint (lowest validation loss).
  • compiler.log — starts with a Q-MASTER CONFIG box listing the values actually used. Always check it (see Pitfalls).

4. What to expect​

  • Training time grows with epochs × train_limit and model size. Run the smoke test below first to estimate speed.
  • Q-Master does not guarantee a gain. Compare accuracy against your PTQ result; if it does not improve, see Troubleshooting.
  • The original ONNX file is never modified.

Before a long run, set "fast_run": true inside qmaster. It trains for 1 epoch × 1 batch to prove the pipeline works end to end. The resulting accuracy is not meaningful; remove fast_run for the real run.


How Q-Master Works​

A Q-Master compile runs three steps:

  1. Calibration — estimates initial quantization parameters (shared with PTQ).
  2. Training (Stage 1) — fine-tunes the model with simulated quantization. The best checkpoint (lowest validation loss) is saved to qat_checkpoint/qat_checkpoint.qxnn. Stage 1 trains at the batch_size you set.
  3. Compilation (Stage 2) — converts the trained weights into the NPU binary (*.dxnn), always at batch size 1.

Knowledge Distillation (KD)​

By default Q-Master uses the original floating-point (FP32) model as a teacher, and trains the quantized model (the student) to reproduce the teacher's output. With the defaults (kd_loss = "mse", kd_alpha = 1.0) training uses the teacher output only and no ground-truth labels, so unlabeled images work.


Recipes​

Copy the closest recipe and change only what you need. All keys are described in the Parameter Reference.

A. Default (no labels needed)​

"qmaster": { "batch_size": 16, "train_limit": 2000, "val_limit": 200, "epochs": 10 }

Teacher-only MSE distillation. Good first attempt for any model. Images need no labels and no particular folder layout. If train_limit + val_limit is larger than the number of images found, all available images are used.

B. Classification with labels (KD + cross-entropy)​

"qmaster": {
"batch_size": 16,
"train_limit": 5000,
"val_limit": 500,
"criterion": "cross_entropy",
"kd_loss": "kl_div",
"kd_temperature": 2.0,
"kd_alpha": 0.5
}

Blends the teacher signal (kd_alpha) with the label loss (1 - kd_alpha). Requires an ImageFolder layout (<dataset_path>/<class_name>/<image>).

Labels come from folder names

Class indices are built from the class folders found in the subset actually loaded (train_limit + val_limit). If a class is missing from that subset, label indices shift and the label loss trains toward the wrong classes. Make sure every class is present, or use recipe A.

C. Embedding / encoder models (e.g. CLIP image encoder)​

"qmaster": { "encoder_mode": true, "kd_alpha": 1.0 }

Tracks cosine similarity instead of classification accuracy.

D. Zero-shot classification (e.g. CLIP)​

"qmaster": {
"zeroshot_weights_path": "/path/to/text_embedding.npy",
"use_kd": false
}

Zero-shot cannot be combined with use_kd or encoder_mode.

E. Segmentation (v2.5.0+)​

"qmaster": {
"task_type": "segmentation",
"seg_root": "/path/to/dataset_root",
"seg_pairs_train": "/path/to/dataset_root/lists/train.txt",
"seg_pairs_val": "/path/to/dataset_root/lists/val.txt",
"seg_ignore_index": 255,
"seg_binary": true,
"use_kd": false
}

See Segmentation Parameters.

F. Out of GPU memory​

"qmaster": { "batch_size": 4, "gradient_accumulation_steps": 4 }

Effective batch size stays 4 × 4 = 16.

If CUDA runs out of memory at your batch_size, DX-COM automatically retries Stage 1 at batch size 1. This succeeds but is much slower, so lower batch_size yourself to stay fast.

G. Re-compile without re-training / continue training​

Use stage1_mode — see Re-using a Checkpoint.


Parameter Reference​

Every key is optional. Omitted keys use the defaults below.

Data and Device​

ParameterDefaultDescription
batch_size1Training batch size for Stage 1. Use > 1 (e.g. 16) for faster, more stable training. Models whose inputs have no batch axis of 1 (e.g. fixed-length text encoders) automatically fall back to 1.
num_workers4DataLoader workers.
train_limit500Number of training images used. The default is small (smoke-test scale); raise it for real training.
val_limit50Number of validation images, reserved before training images are taken from the same pool.
train_drop_last / val_drop_lasttrue / falseDrop the last incomplete batch.
device"cuda:0"Training device. Select the physical GPU with CUDA_VISIBLE_DEVICES.
train_cpu_fpfalseAlso train the CPU (non-NPU) parts of the model and write the trained weights back into the compiled model. When false, CPU subgraph weights stay frozen.

Training Loop​

ParameterDefaultDescription
epochs30Maximum epochs.
lr1e-5Learning rate. Lower it (e.g. 5e-6) if accuracy gets worse from the first epoch.
optimizer"adamw""adamw" or "sgd".
weight_decay1e-4Optimizer weight decay.
schedulernullnull, "cosine", or "step".
scheduler_step_sizemax(1, (epochs - warmup_epochs) // 3)Epoch interval for "step" only.
warmup_epochs0Linear LR warmup epochs.
max_grad_norm1.0Gradient clipping.
use_amptrueMixed-precision training.
gradient_accumulation_steps1Effective batch = batch_size × this.
early_stopping_patience5Stop after N epochs without improvement on validation loss. null disables. Requires validation data.
early_stopping_delta1e-3Minimum improvement counted as progress.
save_best_modeltrueSave the checkpoint with the lowest validation loss.
freeze_bn_afternullFrom this epoch on, freeze BatchNorm and stop updating activation quantization ranges. See Pitfalls.

Loss and Knowledge Distillation​

The loss is kd_alpha × KD loss + (1 − kd_alpha) × task loss.

ParameterDefaultDescription
use_kdtrueUse the FP teacher.
kd_loss"mse""mse", "kl_div", "cosine", "bce", "smooth_l1", "ciou", "giou".
kd_alpha1.0KD weight in [0, 1]. At 1.0 the task loss is not computed.
kd_temperature4.0Softening temperature. Used only by "kl_div" and "bce".
kd_box_format"xyxy"Box format for "ciou" / "giou": "xyxy", "cxcywh", "xywh".
criterion"mse"Task loss: "mse" or "cross_entropy" ("seg_ce" for segmentation). Used only when kd_alpha < 1.0 or use_kd = false. "cross_entropy" switches to the labeled (ImageFolder) loader.
label_smoothing0.0Cross-entropy only.

Choosing the loss:

GoalSettings
No labels availabledefaults (kd_alpha = 1.0, kd_loss = "mse")
Classification with labelscriterion = "cross_entropy", kd_loss = "kl_div", kd_alpha = 0.5–0.7
No teacheruse_kd = false, plus a criterion

Mode Switches​

ParameterDefaultDescription
encoder_modefalseTrack cosine similarity instead of accuracy (embedding/encoder models). Does not disable the task loss by itself; keep kd_alpha = 1.0.
zeroshot_weights_path—.npy text-embedding file for zero-shot models.
stage1_mode"fresh""fresh", "resume", "skip". See Re-using a Checkpoint.
fast_runfalseSmoke test: 1 epoch × 1 batch, 100 train / 10 val images. Not accuracy-meaningful.

Segmentation Parameters​

ParameterDefaultDescription
task_type"classification"Set "segmentation" for dense (image, mask) data.
seg_root—Root directory for the pair lists.
seg_pairs_train / seg_pairs_val—Text files, one <image_path> <mask_path> per line, relative to seg_root.
seg_ignore_index255Label value excluded from loss and metric.
seg_binarytrueBinary (foreground/background) segmentation. Set false for multi-class.

With task_type = "segmentation", criterion defaults to "seg_ce". Masks are not passed through preprocessings; only the image is.


Re-using a Checkpoint​

Training can take a long time. stage1_mode controls what happens when output/qat_checkpoint/qat_checkpoint.qxnn already exists:

stage1_modeBehaviorUse when
"fresh" (default)Train from the start and overwrite the checkpoint.First run.
"resume"Load the checkpoint weights and continue training (optimizer and epoch counter restart). If no checkpoint exists, trains from scratch with a warning.Fine-tune further with a lower lr.
"skip"Skip training and run Stage 2 (compilation) only. The checkpoint must exist.Re-generate the .dxnn (e.g. after a compiler update).
"qmaster": { "stage1_mode": "skip" }

To start from a checkpoint made elsewhere, copy it to <output_dir>/qat_checkpoint/qat_checkpoint.qxnn first.


Python API​

import dx_com

# A qmaster block in the config JSON triggers Q-Master.
dx_com.compile(model="model.onnx", config="config_with_qmaster.json", output_dir="output/")
Configure Q-Master in the JSON

Set all Q-Master options in the JSON qmaster block, as in the Quick Start. To re-compile from a checkpoint, use stage1_mode in the JSON (see Re-using a Checkpoint).

Changed in v2.5.0

The flat arguments quantization_mode, qat_config, qat_skip_training, and qat_resume_from_checkpoint were replaced by the single qmaster argument. Do not pass any of the four removed arguments.

fast_run is a config key

Set "fast_run": true inside the qmaster block; it is not a compile() argument.

Not interchangeable with quant_diagnosis

The .qxnn from quant_diagnosis (see Quantization Tuning Workflow) is a different file from the Q-Master qat_checkpoint.qxnn.


Output Files​

FileDescription
<output_dir>/*.dxnnCompiled NPU binary (the deliverable).
<output_dir>/qat_checkpoint/qat_checkpoint.qxnnBest training checkpoint. Input for stage1_mode = "resume"/"skip".
<output_dir>/compiler.logFull log, including the Q-MASTER CONFIG box and per-epoch metrics.

Pitfalls​

Unknown keys are silently ignored

A misspelled key (e.g. learning_rate) does not raise an error; the default is used instead. Open compiler.log and check the Q-MASTER CONFIG box to confirm the values that were actually applied.

Confirm Q-Master actually ran

After compiling, open compiler.log and check that the Q-MASTER CONFIG box and per-epoch training lines appear, and that no falling back message is present. If training did not run, the result is plain PTQ.

Dataset path falls back silently

If default_loader.dataset_path is missing or invalid, DX-COM does not fail. It falls back to a built-in default ImageNet location, so training may silently use unintended data. Verify the dataset path in the log.

Best checkpoint is chosen by validation loss

The saved checkpoint has the lowest validation loss, which includes the KD term. It can differ from the checkpoint with the best Top-1 accuracy.

BatchNorm / observer freezing

freeze_bn_after freezes BatchNorm and stops updating activation quantization ranges from the given epoch. freeze_bn_after = 0 freezes from the very first epoch (after one warm-up pass), which can make the loss diverge to nan. Leave it null unless you need it, and then start from a later epoch.

Default train_limit is small

train_limit = 500 is meant for quick checks. Use thousands of images for a real run.


Troubleshooting​

SymptomLikely causeAction
CUDA out of memorybatch_size too large for the GPULower batch_size, raise gradient_accumulation_steps. The automatic batch-1 fallback works but is slow.
Loss becomes nanfreeze_bn_after = 0, lr too highSet freeze_bn_after to null or a later epoch; lower lr.
Validation loss is worst at later epochs; best epoch is 1lr too highLower lr; try stage1_mode = "resume" with a smaller lr.
Training stops very earlyearly_stopping_patience too small / validation too noisyRaise patience, raise val_limit.
Accuracy not better than PTQToo little data / loss mismatchRaise train_limit; for classification with labels use recipe B.
Label loss trains the wrong classesA class is missing from the subsetCheck class folders; raise train_limit; or use recipe A.
Training used unexpected datadataset_path invalid → silent fallbackCheck the dataset path in compiler.log.
Settings seem to have no effectKey misspelled or in the wrong blockCheck the Q-MASTER CONFIG box in compiler.log.
Training metrics are good but .dxnn accuracy droppedLoss in a compiler stage that training cannot seeCompare the .dxnn with the original model on the same evaluation set and report the gap to DEEPX.

FAQ​

Do I need labeled data? No. The default recipe distills from the FP32 model and uses images only. Labels are needed only for recipe B.

Can I use my own dataset instead of ImageNet? Yes. Point default_loader.dataset_path at your images. Use the same preprocessing as in PTQ.

Can I use the calibration images for training? Yes, but more varied images generally give better results. Do not train on the images you use to report final accuracy.

Is the compiled .dxnn different from a PTQ one? Same format and runtime. Only the weights and quantization parameters are different.

Training finished but I want to try another setting. Do I need to start over? Use stage1_mode = "resume" to continue from the saved checkpoint, or "fresh" with a new output_dir to start clean.

I only want to rebuild the .dxnn (e.g. new DX-COM version). Use stage1_mode = "skip" with the existing checkpoint.


Version History​

VersionChange
v2.4.0Q-Master (QAT) added via the JSON qmaster block.
v2.5.0Python API: qmaster=QMasterConfig(...); qat_* arguments removed. Segmentation Q-Master (task_type = "segmentation").