-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
46 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
"""MS-AMP te.replacer module.""" | ||
|
||
import torch | ||
|
||
from msamp.common.dtype import Dtypes | ||
from msamp.nn import LinearReplacer | ||
|
||
|
||
class FsdpReplacer: | ||
"""A replacer to replace the FP8 weights with FP32 nn.Parameter and attributes.""" | ||
|
||
@classmethod | ||
def replace(cls, model): | ||
"""Replace the weights with ScalingParameter in transformer engine modules.""" | ||
|
||
model = LinearReplacer.replace(model, weight_qtype=Dtypes.kfloat8_e4m3) | ||
for _, submodule in model.named_modules(): | ||
params_to_process = list(submodule.named_parameters(recurse=False)) | ||
for param_name, param in params_to_process: | ||
if not isinstance(param, torch.Tensor): | ||
data = param.value.view(-1) | ||
padded = 0 | ||
if data.numel() % 4 != 0: | ||
padded = 4 - data.numel() % 4 | ||
data = torch.nn.functional.pad(data, (0, padded)) | ||
|
||
data = data.view(dtype=torch.float32) | ||
new_param = torch.nn.Parameter(data) | ||
new_param._fp8 = True | ||
new_param._original_shape = param.shape | ||
new_param._padded = 0 | ||
new_param._meta = param.meta | ||
new_param._scaling_metas = param._scaling_metas | ||
|
||
setattr(submodule, param_name, new_param) | ||
return model |
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