|
@@ -11,7 +11,9 @@ import torch.optim as optim
|
|
from peft import get_peft_model, prepare_model_for_int8_training
|
|
from peft import get_peft_model, prepare_model_for_int8_training
|
|
from torch.distributed.fsdp import (
|
|
from torch.distributed.fsdp import (
|
|
FullyShardedDataParallel as FSDP,
|
|
FullyShardedDataParallel as FSDP,
|
|
|
|
+ ShardingStrategy
|
|
)
|
|
)
|
|
|
|
+
|
|
from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload
|
|
from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload
|
|
from torch.optim.lr_scheduler import StepLR
|
|
from torch.optim.lr_scheduler import StepLR
|
|
from transformers import (
|
|
from transformers import (
|
|
@@ -35,6 +37,7 @@ from llama_recipes.utils.config_utils import (
|
|
)
|
|
)
|
|
from llama_recipes.utils.dataset_utils import get_preprocessed_dataset
|
|
from llama_recipes.utils.dataset_utils import get_preprocessed_dataset
|
|
|
|
|
|
|
|
+from llama_recipes.utils.fsdp_utils import hdsp_device_mesh
|
|
from llama_recipes.utils.train_utils import (
|
|
from llama_recipes.utils.train_utils import (
|
|
train,
|
|
train,
|
|
freeze_transformer_layers,
|
|
freeze_transformer_layers,
|
|
@@ -42,7 +45,7 @@ from llama_recipes.utils.train_utils import (
|
|
setup_environ_flags,
|
|
setup_environ_flags,
|
|
clear_gpu_cache,
|
|
clear_gpu_cache,
|
|
print_model_size,
|
|
print_model_size,
|
|
- get_policies
|
|
|
|
|
|
+ get_policies,
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -132,7 +135,12 @@ def main(**kwargs):
|
|
peft_config = generate_peft_config(train_config, kwargs)
|
|
peft_config = generate_peft_config(train_config, kwargs)
|
|
model = get_peft_model(model, peft_config)
|
|
model = get_peft_model(model, peft_config)
|
|
model.print_trainable_parameters()
|
|
model.print_trainable_parameters()
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ hsdp_device_mesh = None
|
|
|
|
+ if fsdp_config.hsdp and fsdp_config.sharding_strategy == ShardingStrategy.HYBRID_SHARD:
|
|
|
|
+ hsdp_device_mesh = hdsp_device_mesh(replica_group_size=fsdp_config.replica_group_size, sharding_group_size=fsdp_config.sharding_group_size)
|
|
|
|
+ print("HSDP device mesh is ready")
|
|
|
|
+
|
|
#setting up FSDP if enable_fsdp is enabled
|
|
#setting up FSDP if enable_fsdp is enabled
|
|
if train_config.enable_fsdp:
|
|
if train_config.enable_fsdp:
|
|
if not train_config.use_peft and train_config.freeze_layers:
|
|
if not train_config.use_peft and train_config.freeze_layers:
|
|
@@ -148,6 +156,7 @@ def main(**kwargs):
|
|
cpu_offload=CPUOffload(offload_params=True) if fsdp_config.fsdp_cpu_offload else None,
|
|
cpu_offload=CPUOffload(offload_params=True) if fsdp_config.fsdp_cpu_offload else None,
|
|
mixed_precision=mixed_precision_policy if not fsdp_config.pure_bf16 else None,
|
|
mixed_precision=mixed_precision_policy if not fsdp_config.pure_bf16 else None,
|
|
sharding_strategy=fsdp_config.sharding_strategy,
|
|
sharding_strategy=fsdp_config.sharding_strategy,
|
|
|
|
+ device_mesh=hsdp_device_mesh,
|
|
device_id=torch.cuda.current_device(),
|
|
device_id=torch.cuda.current_device(),
|
|
limit_all_gathers=True,
|
|
limit_all_gathers=True,
|
|
sync_module_states=train_config.low_cpu_fsdp,
|
|
sync_module_states=train_config.low_cpu_fsdp,
|