Quantized linear (transposed matmul) operation. Args: - src (Tensor): The activations tensor - weight (Tensor): The weight tensor - bias (Tensor): The bias tensor - in_zero_point (int): The quantized mapping of zero for the input - weight_zero_point
(
src: torch.Tensor,
weight: torch.Tensor,
bias: torch.Tensor,
in_zero_point: int,
weight_zero_point: torch.Tensor | int,
out_multiplier: int,
out_shift: int,
out_zero_point: int,
)
| 465 | |
| 466 | |
| 467 | def quantized_linear_common( |
| 468 | src: torch.Tensor, |
| 469 | weight: torch.Tensor, |
| 470 | bias: torch.Tensor, |
| 471 | in_zero_point: int, |
| 472 | weight_zero_point: torch.Tensor | int, |
| 473 | out_multiplier: int, |
| 474 | out_shift: int, |
| 475 | out_zero_point: int, |
| 476 | ) -> torch.Tensor: |
| 477 | """ |
| 478 | Quantized linear (transposed matmul) operation. |
| 479 | |
| 480 | Args: |
| 481 | - src (Tensor): The activations tensor |
| 482 | - weight (Tensor): The weight tensor |
| 483 | - bias (Tensor): The bias tensor |
| 484 | - in_zero_point (int): The quantized mapping of zero for the input |
| 485 | - weight_zero_point (Tensor): The quantized mapping of zero for the weight |
| 486 | - out_multiplier (Tensor): The multiplier used to scale the output |
| 487 | - out_shift (Tensor): The shift used to scale the output |
| 488 | - out_zero_point (int): The quantized mapping of zero for the output |
| 489 | - offset (Tensor): Unused |
| 490 | """ |
| 491 | out_scale = 1.0 / (-out_multiplier * (1 / (1 << 31)) * (2**out_shift)) |
| 492 | |
| 493 | N, K = weight.shape |
| 494 | |
| 495 | leading_dims = src.shape[:-1] |
| 496 | src = src.view(-1, K) |
| 497 | |
| 498 | dtype = src.dtype |
| 499 | supported_dtypes = [torch.int8, torch.uint8, torch.int16, torch.int32] |
| 500 | if dtype not in supported_dtypes: |
| 501 | raise ValueError( |
| 502 | f"Unsupported dtype to quantize to {dtype}. Supported dtypes must be one of {supported_dtypes}" |
| 503 | ) |
| 504 | |
| 505 | out = torch.nn.functional.linear( |
| 506 | (src - in_zero_point).float(), |
| 507 | (weight - weight_zero_point).float(), |
| 508 | bias.float(), |
| 509 | ) |
| 510 | return quantize_per_tensor( |
| 511 | out, |
| 512 | out_scale, |
| 513 | out_zero_point, |
| 514 | torch.iinfo(dtype).min, |
| 515 | torch.iinfo(dtype).max, |
| 516 | dtype, |
| 517 | ).reshape(*leading_dims, N) |
| 518 | |
| 519 | |
| 520 | def quantized_linear_variant( |
no test coverage detected