(weight: torch.Tensor, block_size: torch.Tensor, dtype: torch.dtype)
| 260 | return out |
| 261 | |
| 262 | def blockwise_quantize(weight: torch.Tensor, block_size: torch.Tensor, dtype: torch.dtype) -> Tuple[torch.Tensor, torch.Tensor]: |
| 263 | assert weight.ndim == block_size.numel() and weight.ndim == 2 |
| 264 | scale_shape = torch.Size((torch.tensor(list(weight.shape)) / block_size).ceil().long()) |
| 265 | scale = torch.empty(scale_shape, dtype=torch.float32) |
| 266 | out = torch.empty_like(weight, dtype=dtype) |
| 267 | for i in range(scale.shape[0]): |
| 268 | for j in range(scale.shape[1]): |
| 269 | block_size_i = block_size[0] |
| 270 | block_size_j = block_size[1] |
| 271 | w_block = weight[i*block_size_i:(i+1)*block_size_i, j*block_size_j:(j+1)*block_size_j] |
| 272 | qw_block, scale_block = per_tensor_quantize(w_block, dtype) |
| 273 | out[i*block_size_i:(i+1)*block_size_i, j*block_size_j:(j+1)*block_size_j] = qw_block |
| 274 | scale[i, j] = scale_block |
| 275 | return out, scale |
| 276 | |
| 277 | def per_expert_blockwise_quantize(expert_weights: torch.Tensor, block_size: torch.Tensor, dtype: torch.dtype) -> Tuple[torch.Tensor, torch.Tensor]: |
| 278 | assert expert_weights.ndim == 3 |
no test coverage detected