File size: 535 Bytes
a6e840f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import torch
import torch.distributed as dist


@torch.no_grad()
def solution(
    hidden_states: torch.Tensor,
    weight_shard: torch.Tensor,
    bias_shard: torch.Tensor,
) -> torch.Tensor:
    world_size = dist.get_world_size()

    local_logits = torch.matmul(hidden_states, weight_shard.t())
    local_logits = local_logits + bias_shard

    gathered = [torch.empty_like(local_logits) for _ in range(world_size)]
    dist.all_gather(gathered, local_logits.contiguous())
    logits = torch.cat(gathered, dim=1)

    return logits