(args)
| 157 | |
| 158 | |
| 159 | def quantize(args): |
| 160 | logger = get_logger() |
| 161 | # get corresponding QnnQuantizer |
| 162 | try: |
| 163 | quant_dtype = getattr(QuantDtype, args.config) |
| 164 | act_observer = getattr(pt2e, args.activation_observer) |
| 165 | quantizer = make_quantizer( |
| 166 | quant_dtype=quant_dtype, |
| 167 | per_channel_conv=args.per_channel, |
| 168 | per_channel_linear=args.per_row, |
| 169 | act_observer=act_observer, |
| 170 | backend=get_backend_type(args.backend), |
| 171 | soc_model=args.soc_model, |
| 172 | eps=args.eps, |
| 173 | ) |
| 174 | except Exception: |
| 175 | logger.error( |
| 176 | f"Failed to retrieve expected config {args.config} / {args.activation_observer}." |
| 177 | ) |
| 178 | exit(1) |
| 179 | |
| 180 | # step 0: load saved model |
| 181 | ep = torch.export.load(args.artifact) |
| 182 | # step 1: use prepare_pt2e to annotate QDQ pairs |
| 183 | ep_prepared = prepare_pt2e(ep.module(), quantizer) |
| 184 | logger.info(f"perform calibration on {args.artifact}") |
| 185 | # step 2: perform calibration |
| 186 | input_list_parser = InputListParser(args.input_list) |
| 187 | graph_input_names = [ |
| 188 | spec.arg.name |
| 189 | for spec in ep.graph_signature.input_specs |
| 190 | if spec.kind.name == "USER_INPUT" |
| 191 | ] |
| 192 | for inputs in input_list_parser: |
| 193 | if isinstance(inputs, dict): |
| 194 | inputs = [inputs[name] for name in graph_input_names] |
| 195 | ep_prepared(*inputs) |
| 196 | # step 3: use convert_pt2e to fix encodings of QDQ pairs |
| 197 | logger.info(f"saving calibrated model for {args.artifact}") |
| 198 | ep_converted = convert_pt2e(ep_prepared) |
| 199 | ep_quantized = torch.export.export(ep_converted, tuple(inputs)) |
| 200 | os.makedirs(args.output_folder, exist_ok=True) |
| 201 | torch.export.save( |
| 202 | ep_quantized, f"{args.output_folder}/{Path(args.artifact).stem}_quantized.pt2" |
| 203 | ) |
| 204 | |
| 205 | |
| 206 | def compile(args): |
no test coverage detected