(args)
| 57 | |
| 58 | |
| 59 | def main(args): |
| 60 | qnn_config = QnnConfig.load_config(args.config_file if args.config_file else args) |
| 61 | |
| 62 | # ensure the working directory exist. |
| 63 | os.makedirs(args.artifact, exist_ok=True) |
| 64 | |
| 65 | data_num = 100 |
| 66 | inputs, targets = get_imagenet_dataset( |
| 67 | dataset_path=f"{args.dataset}", |
| 68 | data_size=data_num, |
| 69 | image_shape=(256, 256), |
| 70 | ) |
| 71 | |
| 72 | pte_filename = "fastvit_qnn" |
| 73 | |
| 74 | def get_custom_quantizer(): |
| 75 | quantizer = make_quantizer( |
| 76 | quant_dtype=QuantDtype.use_8a8w, |
| 77 | backend=qnn_config.backend, |
| 78 | soc_model=qnn_config.soc_model, |
| 79 | ) |
| 80 | |
| 81 | # there are lots of outliers appearing in fastvit parameters |
| 82 | # we need to apply special configuration to saturate their impact |
| 83 | act_qspec = QuantizationSpec( |
| 84 | dtype=torch.uint8, |
| 85 | qscheme=torch.per_tensor_affine, |
| 86 | observer_or_fake_quant_ctr=MovingAverageMinMaxObserver.with_args( |
| 87 | **{"averaging_constant": 0.01} |
| 88 | ), |
| 89 | ) |
| 90 | weight_qspec = QuantizationSpec( |
| 91 | dtype=torch.int8, |
| 92 | quant_min=torch.iinfo(torch.int8).min + 1, |
| 93 | quant_max=torch.iinfo(torch.int8).max, |
| 94 | qscheme=torch.per_channel_symmetric, |
| 95 | ch_axis=0, |
| 96 | observer_or_fake_quant_ctr=PerChannelParamObserverWithLossEvaluation.with_args( |
| 97 | **{"steps": 100, "use_mse": True} |
| 98 | ), |
| 99 | ) |
| 100 | # rewrite default per-channel ptq config |
| 101 | quantizer.default_quant_config.per_channel_quant_config = QuantizationConfig( |
| 102 | input_activation=act_qspec, |
| 103 | output_activation=act_qspec, |
| 104 | weight=weight_qspec, |
| 105 | bias=_derived_bias_quant_spec, |
| 106 | ) |
| 107 | |
| 108 | # rewrite default ptq config |
| 109 | q_config = quantizer.default_quant_config.quant_config |
| 110 | quantizer.default_quant_config.quant_config = QuantizationConfig( |
| 111 | input_activation=act_qspec, |
| 112 | output_activation=act_qspec, |
| 113 | weight=q_config.weight, |
| 114 | bias=q_config.bias, |
| 115 | ) |
| 116 | return quantizer |
no test coverage detected