MCPcopy Create free account
hub / github.com/FlashSampling/FlashSampling / main

Function main

examples/tensor_parallel.py:21–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19
20
21def main():
22 # Setup distributed environment
23 dist.init_process_group(backend="nccl") # use backend=gloo for single GPU usage
24 rank = int(os.environ["LOCAL_RANK"])
25 world_size = dist.get_world_size()
26 torch.cuda.set_device(rank % torch.cuda.device_count())
27 device = torch.device("cuda")
28 tp = TPInfo(rank=rank, size=world_size)
29
30 # Create inputs
31 vocab_size = 128_256 # divisible by 2 for even sharding
32 hidden_size = 4096
33 n_hidden_states = 4 # (batch size)
34 hidden_states = torch.randn(n_hidden_states, hidden_size, dtype=torch.bfloat16, device=device)
35
36 # Shard weights across ranks
37 torch.manual_seed(42)
38 full_weights = torch.randn(vocab_size, hidden_size, dtype=torch.bfloat16, device=device)
39 weights = shard_weights(full_weights, tp)
40 if rank == 0:
41 print(f" weight shard per rank: {list(weights.shape)}")
42
43 samples = fused_mm_sample_triton(
44 weights=weights,
45 hidden_states=hidden_states,
46 num_samples=1,
47 temperature=torch.tensor(0.8, device=device),
48 seed=rank * 1_000_000,
49 tp=tp,
50 )
51
52 if rank == 0:
53 print("Sample shape: ", samples.shape)
54
55 dist.destroy_process_group()
56
57
58if __name__ == "__main__":

Callers 1

tensor_parallel.pyFile · 0.70

Calls 3

TPInfoClass · 0.90
shard_weightsFunction · 0.90
fused_mm_sample_tritonFunction · 0.90

Tested by

no test coverage detected