Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions examples/example_local_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,14 @@ def replicate_linear(w, x):


@local_map(
out_placements=(
(Shard(0), Shard(0), Replicate()),
None,
),
in_placements=(
(Shard(0), Shard(0), Replicate()),
None,
),
out_placements=((Shard(0), Shard(0), Replicate()),),
in_placements=((Shard(0), Shard(0), Replicate()),),
redistribute_inputs=True,
in_grad_placements=None,
device_mesh=mesh,
)
def sharded_pointwise(x, scalar):
return x + scalar, scalar
def sharded_pointwise(x):
return x + 10


@local_map(
Expand Down Expand Up @@ -114,7 +108,7 @@ def init_weights(self):
torch.nn.init.normal_(lin.bias)

def _compute_attention(self, x):
boosted_weight, scalar = sharded_pointwise(self.wq.weight, 10)
boosted_weight = sharded_pointwise(self.wq.weight)
q = replicate_linear(boosted_weight, x)
k = self.wk(x)
v = self.wv(x)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_optimize_placement.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def input_fn():


class LocalMapTransformerBlock(nn.Module):
def __init__(self, nheads, dim1, dim2):
def __init__(self, nheads, dim1, dim2, mesh):
super().__init__()
self.nheads = nheads
bias = False
Expand All @@ -382,6 +382,7 @@ def __init__(self, nheads, dim1, dim2):
self.wo = nn.Linear(dim1, dim1, bias=bias)
self.w1 = nn.Linear(dim1, dim2, bias=bias)
self.w2 = nn.Linear(dim2, dim1, bias=bias)
self.mesh = mesh

def forward(self, x):
@local_map(
Expand All @@ -393,7 +394,7 @@ def forward(self, x):
),
redistribute_inputs=True,
in_grad_placements=None,
device_mesh=None,
device_mesh=self.mesh,
)
def _context_parallel_attention(query, key, value):
out = F.scaled_dot_product_attention(
Expand Down Expand Up @@ -435,7 +436,7 @@ def test_local_map_placement_respected(device_mesh_local_map, device="cuda"):
seq_len = 256

def model_fn():
return LocalMapTransformerBlock(nheads, dim1, dim2)
return LocalMapTransformerBlock(nheads, dim1, dim2, device_mesh_local_map)

def input_fn():
return torch.randn(bs, seq_len, dim1, device=device, requires_grad=True)
Expand Down
Loading