File size: 1,143 Bytes
453129d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import annotations

import torch
import torch.distributed as dist
import torch.nn.functional as F
from torch import Tensor


@torch.no_grad()
def _block_int8_quant_dequant(x_flat: Tensor, block_size: int) -> Tensor:
    n = x_flat.numel()
    if n == 0:
        return x_flat.clone()
    flat = x_flat.contiguous().reshape(-1)
    pad = (-n) % block_size
    if pad:
        flat = F.pad(flat, (0, pad))
    nb = flat.numel() // block_size
    xv = flat.view(nb, block_size)
    scales = xv.abs().amax(dim=1).float().clamp(min=1e-8) / 127.0
    q = (xv.float() / scales.unsqueeze(1)).round().clamp(-127, 127).to(torch.int8)
    out = (q.float() * scales.unsqueeze(1)).reshape(-1)
    return out[:n]


@torch.no_grad()
def solution(
    flat_grad: Tensor,
    block_size: int,
) -> Tensor:
    assert block_size >= 1

    world_size = dist.get_world_size()
    orig_shape = flat_grad.shape
    x = flat_grad.reshape(-1)

    rec = _block_int8_quant_dequant(x, block_size)
    acc = rec.float()
    dist.all_reduce(acc, op=dist.ReduceOp.SUM)
    acc.div_(world_size)

    return acc.to(dtype=flat_grad.dtype).reshape(orig_shape)