(value: torch.Tensor, dy: torch.Tensor,
scale: torch.Tensor, offset: torch.Tensor)
| 65 | |
| 66 | def __TEST_QUANTIZE_LT_B__(size: List[int], iterations: int, sym: bool): |
| 67 | def ref_grad_func(value: torch.Tensor, dy: torch.Tensor, |
| 68 | scale: torch.Tensor, offset: torch.Tensor): |
| 69 | |
| 70 | qt = ppq_tensor_round(value / scale, policy=ROUNDING_POLICY) + offset |
| 71 | clipped_qt = qt.clip(Q_MIN, Q_MAX) |
| 72 | |
| 73 | dx = torch.where(clipped_qt != qt, torch.zeros_like(dy), dy) |
| 74 | ds = torch.where(clipped_qt == qt, (((qt - offset) * scale) - value) * dy / scale, torch.zeros_like(dy)) |
| 75 | ds += torch.where(qt > Q_MAX, (Q_MAX - offset) * dy, torch.zeros_like(dy)) |
| 76 | ds += torch.where(qt < Q_MIN, (Q_MIN - offset) * dy, torch.zeros_like(dy)) |
| 77 | ds = ds.sum() / sqrt(value.numel() * (Q_MAX - Q_MIN)) |
| 78 | return dx, ds |
| 79 | |
| 80 | for _ in tqdm(range(iterations), desc='QUANTIZE LT_B TESTING...'): |
| 81 | t = torch.rand(size=size).cuda() * 50 |
no test coverage detected