-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a refresh of of `OptimizedLinear` with the following features to improve performance and usability: * More efficient sharing of base weights using `all_gather_into_tensor` * Flattened sharded weights * Selectively offload frozen weights to cpu * `deepspeed.linear.Init` that allows injecting OptimizedLinear during model construction (similar to zero.Init) * Support for load state dict directly in OptimizedLinear, this allows loading HF model weights correctly into sharded params * Various bug fixes for the LoRA implementation introduced previously * Several new unit tests Builds on-top of @RezaYazdaniAminabadi's previous FP8 updates (#5764) to support dense model fp8 quantization. Example usage of this to fine-tune llama-3.1-405B on a single node: https://github.com/Snowflake-Labs/snowflake-arctic/tree/main/training/llama3.1 --------- Co-authored-by: Reza Yazdani <[email protected]> Co-authored-by: Reza Yazdani <[email protected]>
- Loading branch information
1 parent
1890b81
commit 6e5d58d
Showing
8 changed files
with
379 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# DeepSpeed Team | ||
|
||
from .optimized_linear import LoRAOptimizedLinear, OptimizedLinear | ||
|
||
import torch | ||
|
||
try: | ||
import transformers | ||
except ImportError: | ||
transformers = None | ||
|
||
|
||
def init_lora(model): | ||
model.requires_grad_(False) | ||
for m in model.modules(): | ||
if isinstance(m, LoRAOptimizedLinear): | ||
m.init_lora() | ||
|
||
|
||
class Init(object): | ||
""" | ||
Init context wrapper similar in style to zero.Init. Allows for injecting OptimizedLinear during model | ||
construction which will shard base weights and reduce overall memory usage during model init. Primarily | ||
useful when initializing a model via transformers.AutoModelForCausalLM. | ||
Example usage: | ||
lora_config = deepspeed.linear.LoRAConfig(..) | ||
quant_config = deepspeed.linear.QuantizationConfig(..) | ||
with deepspeed.linear.Init(lora_config=lora_config, quant_config=quant_config): | ||
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3.1-405B") | ||
""" | ||
|
||
def __init__(self, lora_config=None, quant_config=None): | ||
self._orig_nn_linear = torch.nn.Linear | ||
self._orig_causallm_pretrained = None | ||
if transformers != None: | ||
self._orig_causallm_pretrained = transformers.AutoModelForCausalLM.from_pretrained | ||
self._orig_causallm_config = transformers.AutoModelForCausalLM.from_config | ||
self.lora_config = lora_config | ||
self.quant_config = quant_config | ||
self._post_init_complete = False | ||
|
||
def __enter__(self): | ||
|
||
class OptLinearWrapper: | ||
_orig_nn_linear = self._orig_nn_linear | ||
_lora_config = self.lora_config | ||
_quant_config = self.quant_config | ||
|
||
def __new__(self, *args, **kwargs): | ||
self._lora_config.delay_lora_init = True | ||
kwargs['lora_config'] = self._lora_config | ||
kwargs['quantization_config'] = self._quant_config | ||
kwargs['linear_cls'] = self._orig_nn_linear | ||
return OptimizedLinear(*args, **kwargs) | ||
|
||
def _model_init(model): | ||
if self.lora_config != None: | ||
init_lora(model) | ||
self._post_init_complete = True | ||
return model | ||
|
||
# ensures non-lora params are frozen and lora weights are initialized | ||
def from_pretrained(*args, **kwargs): | ||
model = self._orig_causallm_pretrained(*args, **kwargs) | ||
return _model_init(model) | ||
|
||
def from_config(*args, **kwargs): | ||
model = self._orig_causallm_config(*args, **kwargs) | ||
return _model_init(model) | ||
|
||
torch.nn.Linear = OptLinearWrapper | ||
if transformers != None: | ||
transformers.AutoModelForCausalLM.from_pretrained = from_pretrained | ||
transformers.AutoModelForCausalLM.from_config = from_config | ||
|
||
def __exit__(self, *args, **kwargs): | ||
torch.nn.Linear = self._orig_nn_linear | ||
if not self._post_init_complete: | ||
print('WARNING: For some reason LoRA modules are not initialized, this is usually done automatically ' | ||
'if using transformers via (AutoModelForCausalLM from_pretrained/from_config). ' | ||
'You must call `init_lora` on each module in order to use DeepSpeed LoRA, otherwise ' | ||
'you will error out during runtime.') | ||
else: | ||
transformers.AutoModelForCausalLM.from_pretrained = self._orig_causallm_pretrained | ||
transformers.AutoModelForCausalLM.from_config = self._orig_causallm_config |
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
Oops, something went wrong.