-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable TritonFusedRMSNorm with local_map annotation (#404)
Summary This PR enables the use of TritonFusedRMSNorm with Tensor Parallel with 7%-8% performance gain compared to RMSNorm with TP. #364
- Loading branch information
Showing
5 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pytest | ||
expecttest==0.1.6 | ||
pytest==7.3.2 | ||
pytest-cov | ||
pre-commit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch | ||
from torch.distributed._tensor import ( | ||
distribute_tensor, | ||
init_device_mesh, | ||
Replicate, | ||
Shard, | ||
) | ||
from torch.distributed._tensor.debug import CommDebugMode | ||
from torch.testing._internal.common_utils import run_tests | ||
from torch.testing._internal.distributed._tensor.common_dtensor import ( | ||
DTensorTestBase, | ||
skip_if_lt_x_gpu, | ||
with_comms, | ||
) | ||
|
||
from torchtitan.models.norms import fused_rms_norm_fn | ||
|
||
|
||
class TestFusedRMSNorm(DTensorTestBase): | ||
@property | ||
def world_size(self): | ||
return 4 | ||
|
||
@skip_if_lt_x_gpu(4) | ||
@with_comms | ||
def test_fused_rms_norm(self): | ||
mesh = init_device_mesh( | ||
device_type=self.device_type, mesh_shape=(self.world_size,) | ||
) | ||
x = torch.randn(4, 4, 4, device=self.device_type) # Shard(1) | ||
w = torch.randn(4, device=self.device_type, requires_grad=True) # Replicate | ||
|
||
dist_x = distribute_tensor(x, mesh, [Shard(1)]) | ||
dist_w = distribute_tensor(w, mesh, [Replicate()]) | ||
|
||
x = x.clone().detach() | ||
w = w.clone().detach().requires_grad_() | ||
|
||
self.assertEqual(dist_x.full_tensor(), x) | ||
self.assertEqual(dist_w.full_tensor(), w) | ||
|
||
# fused rmsnorm on DTensor | ||
comm_mode = CommDebugMode() | ||
# fused rmsnorm | ||
with comm_mode: | ||
dist_out = fused_rms_norm_fn(dist_x, dist_w) | ||
|
||
self.assertEqual(comm_mode.get_total_counts(), 0) | ||
|
||
with comm_mode: | ||
dist_grad_out = torch.ones_like(dist_out) | ||
dist_out.backward(dist_grad_out) | ||
|
||
self.assertEqual(comm_mode.get_total_counts(), 0) | ||
|
||
# fused rmsnorm on Tensor | ||
out = fused_rms_norm_fn(x, w) | ||
grad_out = torch.ones_like(out) | ||
out.backward(grad_out) | ||
|
||
self.assertEqual(dist_out.full_tensor(), out) | ||
self.assertEqual(dist_grad_out.full_tensor(), grad_out) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters