aimet icon indicating copy to clipboard operation
aimet copied to clipboard

There seems to be a bug in the lite_map method

Open MrHwc opened this issue 4 months ago • 7 comments

I implemented mixed precision quantization using the code AIMETRegression/features/onnx/lite_mp-runner.py, and I found that method 1 had much lower accuracy than method 2. method 1: w8a8 -> flip_layers_to_higher_precision int16;

providers = ["CUDAExecutionProvider"]
sim = QuantizationSimModel(model,
                           param_type=aimet_onnx.int8,
                           activation_type=aimet_onnx.int8, 
                           quant_scheme=QuantScheme.post_training_tf_enhanced, 
                           config_file="htp_v73", 
                           providers=providers)
                           
sim.compute_encodings(onnx_data_generator())

percentage = 100 # Percentage of layers to flip
override_precision = aimet_onnx.int16 # Precision to sets layers to
flip_layers_to_higher_precision(
    sim, layer_sensitivity_dict, percentage, override_precision
)

sim.compute_encodings(onnx_data_generator())                          

method 2: w8a16

providers = ["CUDAExecutionProvider"]
sim = QuantizationSimModel(model,
                           param_type=aimet_onnx.int8,
                           activation_type=aimet_onnx.int16, 
                           quant_scheme=QuantScheme.post_training_tf_enhanced, 
                           config_file="htp_v73", 
                           providers=providers)
                           
sim.compute_encodings(onnx_data_generator())

Is there a problem with the usage method or a bug in the framework? aimet-onnx version:2.34.0

MrHwc avatar Jul 10 '26 03:07 MrHwc

interesting find @MrHwc

could you please share which model did you try? if you could share your end-to-end script that will be super useful.

bhushan23 avatar Jul 10 '26 03:07 bhushan23

The attachment contains code and models, I found that the accuracy of using CPUExecutionProvider and CUDAExecutionProvider is also different. The accuracy of CPUExecutionProvider is normal. onedrive: https://1drv.ms/u/c/56eb822d0fad6a5b/IQCB-PSSb1C-QL3SqNpyR6rAAX5fj9AYnjJYU5uzkuXU_v8?e=PnUTX5

MrHwc avatar Jul 12 '26 09:07 MrHwc

interesting find MrHwc

could you please share which model did you try? if you could share your end-to-end script that will be super useful.

I have provided the code, is there a solution?

MrHwc avatar Jul 13 '26 06:07 MrHwc

Hi @MrHwc, can you please try again with aimet 2.35.1? This release contains a workaround (a052923) for a HexNN bug that was recently found, and your encoding is touching the condition that triggers this bug. If this issue persists in aimet 2.35.1, please let me know

More precisely, the HexNN bug will be triggered whenever input_scale * weight_scale / output_scale is less than 2**-24. In your case, this is occurring in at least two layers: /backbone/layer2/layer2.0/conv1/Conv and /backbone/layer4_ga/layer4_ga.0/downsample/downsample.0/Conv. The reason you hit this bug in method 1 but not in method 2 is because int16 weight scale is much smaller than int8 weight scale, and hence much more vulnerable to this bug.

I'm still not sure about the accuracy mismatch between CPU and CUDA. But I wouldn't be concerned too much if the gap isn't too big. A minor gap is somewhat expected due to various reasons. For instance, if your CUDA GPU is Ampere architecture or later, TF32 is one of the common reasons of minor CPU-GPU accuracy mistmatch.

kyunggeunlee avatar Jul 22 '26 17:07 kyunggeunlee

The version is 2.30.0, the quantitytype is w8a16, the providers are set to CPUExecutionProvider or CUDAExecutionProvider, and the accuracy is normal. When the version is greater than 2.30. 0, the accuracy of providers is only normal when set to CPUExecutionProvider, while when set to CUDAExecutionProvider, the accuracy is much lower.

Same setting, log warning will be output when version is greater than 2.30:

[ WARNING ] Tensor onnx::Conv_925 has MIN and MAX difference less than EPSILON (0.000100).This could lead to accuracy issues in backend.
[ WARNING ] Tensor onnx::Conv_925 has MIN and MAX difference less than EPSILON (0.000100).This could lead to accuracy issues in backend.
[ WARNING ] Tensor onnx::Conv_925 has MIN and MAX difference less than EPSILON (0.000100).This could lead to accuracy issues in backend.
[ WARNING ] Tensor onnx::Conv_925 has MIN and MAX difference less than EPSILON (0.000100).This could lead to accuracy issues in backend.

MrHwc avatar Jul 31 '26 06:07 MrHwc

Hi @MrHwc could you try running with quant_scheme=QuantScheme.min_max? This is our general recommendation as QuantScheme.post_training_tf_enhanced does not always perform well especially for int16 quantization (we can investigate whether you there might be a numerical stability issue here as well)

michaelgtuttle avatar Aug 14 '26 21:08 michaelgtuttle

I compared the code of v2.30 and v2.36.0 and found the differences in the computeEncoding function. I added the following code to the computeEncoding function in version v2.36.0 to solve the problem of accuracy differences between different versions: newMax = std::max(newMax, newMin + MIN_RANGE);

MrHwc avatar Aug 16 '26 08:08 MrHwc