Skip to content

Commit cad4646

Browse files
committed
One shot all reduce Example
stack-info: PR: #245, branch: joydddd/stack/12
1 parent ade850a commit cad4646

File tree

2 files changed

+187
-5
lines changed

2 files changed

+187
-5
lines changed

examples/all_reduce.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
from __future__ import annotations
2+
3+
import os
4+
5+
import torch
6+
import torch.distributed as dist
7+
import torch.distributed._symmetric_memory as symm_mem
8+
import triton
9+
import triton.language as tl
10+
11+
import helion
12+
import helion.language as hl
13+
14+
15+
# Symmemtric Memory Helpers
16+
@triton.jit
17+
def triton_copy(
18+
inp: tl.int64, # pyright: ignore[reportInvalidTypeForm]
19+
out: tl.tensor,
20+
SIZE: tl.constexpr,
21+
) -> None:
22+
tl.static_assert(out.dtype.is_ptr())
23+
inp = inp.to(tl.pointer_type(out.dtype.element_ty)) # pyright: ignore[reportAttributeAccessIssue]
24+
addrs = tl.load(inp + tl.arange(0, SIZE))
25+
tl.store(out + tl.arange(0, SIZE), addrs)
26+
27+
28+
def dev_array_to_tensor_short(
29+
dev_array_ptr: int, shape: tuple[int], dtype: torch.dtype, device: torch.device
30+
) -> torch.Tensor:
31+
tensor = torch.empty(shape, dtype=dtype, device=device)
32+
triton_copy[1,](dev_array_ptr, tensor, tensor.numel()) # pyright: ignore[reportArgumentType]
33+
return tensor
34+
35+
36+
@helion.jit(
37+
config=helion.Config(
38+
block_sizes=[8192],
39+
num_warps=32,
40+
),
41+
)
42+
def one_shot_all_reduce_kernel(
43+
signal_pad_addrs: torch.Tensor,
44+
local_signal_pad: torch.Tensor,
45+
a_shared_tuple: tuple[torch.Tensor, ...],
46+
my_rank: hl.constexpr,
47+
) -> torch.Tensor:
48+
_, world_size = local_signal_pad.size()
49+
world_size = hl.specialize(world_size)
50+
out = torch.empty_like(a_shared_tuple[0])
51+
N = out.size(0)
52+
53+
for tile_n in hl.tile(N):
54+
ptr_tile = signal_pad_addrs[:]
55+
multicast_signalpad = hl.multicast_like(local_signal_pad, ptr_tile)
56+
hl.signal(
57+
multicast_signalpad,
58+
[tile_n.id, my_rank],
59+
signal=1,
60+
wait_for=0,
61+
scope="sys",
62+
hasPreviousMemAccess=False,
63+
)
64+
65+
for world in hl.tile(world_size, block_size=world_size):
66+
hl.wait(
67+
local_signal_pad,
68+
[tile_n.id, world],
69+
signal=1,
70+
update=0,
71+
scope="sys",
72+
)
73+
74+
acc = hl.zeros([tile_n], dtype=torch.float32, device=local_signal_pad.device)
75+
76+
for a in a_shared_tuple:
77+
acc += a[tile_n]
78+
79+
out[tile_n] = acc
80+
81+
hl.signal(
82+
multicast_signalpad, [tile_n.id, my_rank], signal=1, wait_for=0, scope="sys"
83+
)
84+
85+
for world in hl.tile(world_size, block_size=world_size):
86+
hl.wait(
87+
local_signal_pad,
88+
[tile_n.id, world],
89+
signal=1,
90+
update=0,
91+
scope="sys",
92+
hasSubsequentMemAccess=False,
93+
)
94+
return out
95+
96+
97+
def helion_one_shot_all_reduce(a_shared: torch.Tensor) -> torch.Tensor:
98+
assert dist.group.WORLD is not None
99+
100+
symm_mem_hdl = symm_mem.rendezvous(a_shared, group=dist.group.WORLD)
101+
102+
a_shared_tuple = tuple(
103+
[
104+
symm_mem_hdl.get_buffer(i, tuple(a_shared.shape), a_shared.dtype)
105+
for i in range(symm_mem_hdl.world_size)
106+
]
107+
)
108+
109+
local_signal_pad = symm_mem_hdl.get_signal_pad(
110+
symm_mem_hdl.rank, dtype=torch.int32
111+
).view(-1, symm_mem_hdl.world_size)
112+
113+
signal_pad_addrs = dev_array_to_tensor_short(
114+
symm_mem_hdl.signal_pad_ptrs_dev,
115+
(symm_mem_hdl.world_size,),
116+
dtype=torch.uint64,
117+
device=a_shared.device,
118+
)
119+
120+
return one_shot_all_reduce_kernel(
121+
signal_pad_addrs,
122+
local_signal_pad,
123+
a_shared_tuple,
124+
my_rank=symm_mem_hdl.rank,
125+
)
126+
127+
128+
def test(N: int, device: torch.device, dtype: torch.dtype) -> None:
129+
dist_group = dist.group.WORLD
130+
assert dist_group is not None
131+
132+
world_size = dist.get_world_size()
133+
a_shared = symm_mem.empty(N // world_size, dtype=dtype, device=device).normal_()
134+
135+
a_shared_clone = symm_mem.empty(
136+
a_shared.shape,
137+
dtype=a_shared.dtype,
138+
device=a_shared.device,
139+
)
140+
symm_mem.rendezvous(a_shared_clone, dist_group.group_name)
141+
a_shared_clone.copy_(a_shared)
142+
143+
a_out = helion_one_shot_all_reduce(a_shared)
144+
145+
gloden_o = torch.ops.symm_mem.one_shot_all_reduce(
146+
a_shared_clone, "sum", dist_group.group_name
147+
)
148+
149+
torch.testing.assert_close(a_out, gloden_o, rtol=1e-1, atol=1e-1)
150+
151+
152+
def main() -> None:
153+
rank = int(os.environ["LOCAL_RANK"])
154+
torch.manual_seed(42 + rank)
155+
device = torch.device(f"cuda:{rank}")
156+
torch.cuda.set_device(device)
157+
dist.init_process_group("nccl")
158+
test(16384, device, torch.bfloat16)
159+
160+
dist.destroy_process_group()
161+
162+
163+
if __name__ == "__main__":
164+
"""
165+
Run with:
166+
torchrun \
167+
--nnodes 1 --nproc-per-node 8 \
168+
--rdzv-backend c10d --rdzv-endpoint localhost:0 \
169+
--no_python python3 examples/all_reduce.py
170+
"""
171+
main()

helion/language/creation_ops.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
__all__ = ["arange", "full", "zeros"]
1919

2020

21-
def zeros(shape: list[object], dtype: torch.dtype = torch.float32) -> torch.Tensor:
21+
def zeros(
22+
shape: list[object],
23+
dtype: torch.dtype = torch.float32,
24+
device: torch.device | None = None,
25+
) -> torch.Tensor:
2226
"""
2327
Return a device-tensor filled with zeros.
2428
@@ -54,12 +58,17 @@ def process_kernel(input: torch.Tensor) -> torch.Tensor:
5458
- :func:`~helion.language.full`: For filling with arbitrary values
5559
- :func:`~helion.language.arange`: For creating sequences
5660
"""
57-
return full(shape, 0.0 if dtype.is_floating_point else 0, dtype=dtype)
61+
return full(
62+
shape, 0.0 if dtype.is_floating_point else 0, dtype=dtype, device=device
63+
)
5864

5965

6066
@_decorators.api(tiles_as_sizes=True)
6167
def full(
62-
shape: list[object], value: float, dtype: torch.dtype = torch.float32
68+
shape: list[object],
69+
value: float,
70+
dtype: torch.dtype = torch.float32,
71+
device: torch.device | None = None,
6372
) -> torch.Tensor:
6473
"""
6574
Create a device-tensor filled with a specified value.
@@ -103,6 +112,7 @@ def _full_fake(
103112
shape: list[int | torch.SymInt],
104113
value: float,
105114
dtype: torch.dtype = torch.float32,
115+
device: torch.device | None = None,
106116
) -> torch.Tensor:
107117
if not isinstance(shape, (list, tuple)):
108118
raise TypeError(f"Expected list[SymInt], got {type(shape).__name__}")
@@ -111,7 +121,7 @@ def _full_fake(
111121
return torch.empty(
112122
[*shape],
113123
dtype=dtype,
114-
device=env.device,
124+
device=env.device if device is None else device,
115125
)
116126

117127

@@ -147,6 +157,7 @@ def _(
147157
def arange(
148158
*args: int,
149159
dtype: torch.dtype | None = None,
160+
device: torch.device | None = None,
150161
**kwargs: object,
151162
) -> torch.Tensor:
152163
"""
@@ -175,5 +186,5 @@ def arange(
175186
*args,
176187
**kwargs,
177188
dtype=dtype,
178-
device=env.device,
189+
device=env.device if device is None else device,
179190
)

0 commit comments

Comments
 (0)