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

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

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