Align quant scale and offset to larger input config. The first input config will be set as master config, all slave config will share the same scale and offset with master. Any change to slave config will be rejected since then.
(self, op: QuantableOperation)
| 427 | return master_config |
| 428 | |
| 429 | def align_to_large(self, op: QuantableOperation) -> TensorQuantizationConfig: |
| 430 | """Align quant scale and offset to larger input config. The first input |
| 431 | config will be set as master config, all slave config will share the |
| 432 | same scale and offset with master. |
| 433 | |
| 434 | Any change to slave config will be rejected since then. |
| 435 | """ |
| 436 | global_min, global_max, master_config = 0, 0, op.config.input_quantization_config[0] |
| 437 | for config in op.config.input_quantization_config: |
| 438 | if config.state in {QuantizationStates.FP32, QuantizationStates.SOI}: continue |
| 439 | if config.policy.has_property(QuantizationProperty.FLOATING): continue |
| 440 | |
| 441 | assert config.policy.has_property(QuantizationProperty.PER_TENSOR), ( |
| 442 | 'Quant Alignment can only happen with per tensor quantization.') |
| 443 | local_min = config.scale * (config.quant_min - config.offset) |
| 444 | local_max = config.scale * (config.quant_max - config.offset) |
| 445 | |
| 446 | assert isinstance(local_min, torch.Tensor) |
| 447 | assert isinstance(local_max, torch.Tensor) |
| 448 | global_max = max(global_max, local_max.item()) |
| 449 | global_min = min(global_min, local_min.item()) |
| 450 | |
| 451 | # recompute scale and offset |
| 452 | scale, offset = minmax_to_scale_offset( |
| 453 | global_min, global_max, op.config.input_quantization_config[0]) |
| 454 | |
| 455 | device = master_config.scale.device |
| 456 | master_config._dominator = master_config |
| 457 | master_config.state = QuantizationStates.PASSIVE |
| 458 | master_config.scale = torch.tensor(scale, dtype=torch.float32, device=device) |
| 459 | master_config.offset = torch.tensor(offset, dtype=torch.float32, device=device) |
| 460 | |
| 461 | for slave_config in op.config.input_quantization_config[1: ]: |
| 462 | if config.state in {QuantizationStates.FP32, QuantizationStates.SOI}: continue |
| 463 | if config.policy.has_property(QuantizationProperty.FLOATING): continue |
| 464 | slave_config.master_by = master_config |
| 465 | return master_config |
| 466 | |
| 467 | def align_to_output(self, op: QuantableOperation) -> TensorQuantizationConfig: |
| 468 | """Align quant scale and offset to output config. All input configs |
no test coverage detected