|
@@ -26,7 +26,7 @@ from llama_recipes.utils.memory_utils import MemoryTrace
|
|
|
def set_tokenizer_params(tokenizer: LlamaTokenizer):
|
|
|
tokenizer.pad_token_id = 0
|
|
|
tokenizer.padding_side = "left"
|
|
|
-
|
|
|
+
|
|
|
# Converting Bytes to Megabytes
|
|
|
def byte2mb(x):
|
|
|
return int(x / 2**20)
|
|
@@ -34,7 +34,7 @@ def byte2mb(x):
|
|
|
def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_scheduler, gradient_accumulation_steps, train_config, fsdp_config=None, local_rank=None, rank=None):
|
|
|
"""
|
|
|
Trains the model on the given dataloader
|
|
|
-
|
|
|
+
|
|
|
Args:
|
|
|
model: The model to be trained
|
|
|
train_dataloader: The dataloader containing the training data
|
|
@@ -46,18 +46,18 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
train_config: The training configuration
|
|
|
eval_dataloader: The dataloader containing the eval data
|
|
|
tokenizer: tokenizer used in the eval for decoding the predicitons
|
|
|
-
|
|
|
+
|
|
|
Returns: results dictionary containing average training and validation perplexity and loss
|
|
|
"""
|
|
|
# Create a gradient scaler for fp16
|
|
|
if train_config.use_fp16 and train_config.enable_fsdp:
|
|
|
scaler = ShardedGradScaler()
|
|
|
elif train_config.use_fp16 and not train_config.enable_fsdp:
|
|
|
- scaler = torch.cuda.amp.GradScaler()
|
|
|
+ scaler = torch.cuda.amp.GradScaler()
|
|
|
if train_config.enable_fsdp:
|
|
|
world_size = int(os.environ["WORLD_SIZE"])
|
|
|
autocast = torch.cuda.amp.autocast if train_config.use_fp16 else nullcontext
|
|
|
-
|
|
|
+
|
|
|
train_prep = []
|
|
|
train_loss = []
|
|
|
val_prep = []
|
|
@@ -78,7 +78,7 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
if train_config.enable_fsdp:
|
|
|
batch[key] = batch[key].to(local_rank)
|
|
|
else:
|
|
|
- batch[key] = batch[key].to('cuda:0')
|
|
|
+ batch[key] = batch[key].to('cuda:0')
|
|
|
with autocast():
|
|
|
loss = model(**batch).loss
|
|
|
loss = loss / gradient_accumulation_steps
|
|
@@ -101,9 +101,9 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
|
|
|
pbar.set_description(f"Training Epoch: {epoch+1}/{train_config.num_epochs}, step {step}/{len(train_dataloader)} completed (loss: {loss.detach().float()})")
|
|
|
pbar.close()
|
|
|
-
|
|
|
+
|
|
|
epoch_end_time = time.perf_counter()-epoch_start_time
|
|
|
- epoch_times.append(epoch_end_time)
|
|
|
+ epoch_times.append(epoch_end_time)
|
|
|
# Reducing total_loss across all devices if there's more than one CUDA device
|
|
|
if torch.cuda.device_count() > 1 and train_config.enable_fsdp:
|
|
|
dist.all_reduce(total_loss, op=dist.ReduceOp.SUM)
|
|
@@ -111,10 +111,10 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
if train_config.enable_fsdp:
|
|
|
train_epoch_loss = train_epoch_loss/world_size
|
|
|
train_perplexity = torch.exp(train_epoch_loss)
|
|
|
-
|
|
|
+
|
|
|
train_prep.append(train_perplexity)
|
|
|
train_loss.append(train_epoch_loss)
|
|
|
-
|
|
|
+
|
|
|
if train_config.enable_fsdp:
|
|
|
if rank==0:
|
|
|
print(f"Max CUDA memory allocated was {memtrace.peak} GB")
|
|
@@ -128,10 +128,10 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
print(f"Peak active CUDA memory was {memtrace.peak_active_gb} GB")
|
|
|
print(f"Cuda Malloc retires : {memtrace.cuda_malloc_retires}")
|
|
|
print(f"CPU Total Peak Memory consumed during the train (max): {memtrace.cpu_peaked + memtrace.cpu_begin} GB")
|
|
|
-
|
|
|
+
|
|
|
# Update the learning rate as needed
|
|
|
lr_scheduler.step()
|
|
|
-
|
|
|
+
|
|
|
if train_config.run_validation:
|
|
|
eval_ppl, eval_epoch_loss = evaluation(model, train_config, eval_dataloader, local_rank, tokenizer)
|
|
|
checkpoint_start_time = time.perf_counter()
|
|
@@ -144,23 +144,23 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
print(f"we are about to save the PEFT modules")
|
|
|
else:
|
|
|
print(f"we are about to save the PEFT modules")
|
|
|
- model.save_pretrained(train_config.output_dir)
|
|
|
+ model.save_pretrained(train_config.output_dir)
|
|
|
if train_config.enable_fsdp:
|
|
|
- if rank==0:
|
|
|
+ if rank==0:
|
|
|
print(f"PEFT modules are saved in {train_config.output_dir} directory")
|
|
|
else:
|
|
|
print(f"PEFT modules are saved in {train_config.output_dir} directory")
|
|
|
-
|
|
|
+
|
|
|
else:
|
|
|
if not train_config.use_peft and fsdp_config.checkpoint_type == StateDictType.FULL_STATE_DICT:
|
|
|
-
|
|
|
+
|
|
|
save_model_checkpoint(
|
|
|
model, optimizer, rank, train_config, epoch=epoch
|
|
|
)
|
|
|
elif not train_config.use_peft and fsdp_config.checkpoint_type == StateDictType.SHARDED_STATE_DICT:
|
|
|
print(" Saving the FSDP model checkpoints using SHARDED_STATE_DICT")
|
|
|
print("=====================================================")
|
|
|
-
|
|
|
+
|
|
|
save_model_and_optimizer_sharded(model, rank, train_config)
|
|
|
if train_config.save_optimizer:
|
|
|
save_model_and_optimizer_sharded(model, rank, train_config, optim=optimizer)
|
|
@@ -172,7 +172,7 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
model, optimizer, rank, train_config, epoch=epoch
|
|
|
)
|
|
|
print(" Saving the FSDP model checkpoints and optimizer using FULL_STATE_DICT")
|
|
|
- print("=====================================================")
|
|
|
+ print("=====================================================")
|
|
|
if train_config.enable_fsdp:
|
|
|
dist.barrier()
|
|
|
checkpoint_end_time = time.perf_counter() - checkpoint_start_time
|
|
@@ -196,8 +196,8 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
avg_train_prep = sum(train_prep)/len(train_prep)
|
|
|
avg_train_loss = sum(train_loss)/len(train_loss)
|
|
|
if train_config.run_validation:
|
|
|
- avg_eval_prep = sum(val_prep)/len(val_prep)
|
|
|
- avg_eval_loss = sum(val_loss)/len(val_loss)
|
|
|
+ avg_eval_prep = sum(val_prep)/len(val_prep)
|
|
|
+ avg_eval_loss = sum(val_loss)/len(val_loss)
|
|
|
|
|
|
results['avg_train_prep'] = avg_train_prep
|
|
|
results['avg_train_loss'] = avg_train_loss
|
|
@@ -206,27 +206,27 @@ def train(model, train_dataloader,eval_dataloader, tokenizer, optimizer, lr_sche
|
|
|
results['avg_eval_loss'] = avg_eval_loss
|
|
|
results["avg_epoch_time"] = avg_epoch_time
|
|
|
results["avg_checkpoint_time"] = avg_checkpoint_time
|
|
|
-
|
|
|
+
|
|
|
#saving the training params including fsdp setting for reference.
|
|
|
if train_config.enable_fsdp and not train_config.use_peft:
|
|
|
save_train_params(train_config, fsdp_config, rank)
|
|
|
-
|
|
|
+
|
|
|
return results
|
|
|
|
|
|
def evaluation(model,train_config, eval_dataloader, local_rank, tokenizer):
|
|
|
"""
|
|
|
Evaluates the model on the given dataloader
|
|
|
-
|
|
|
+
|
|
|
Args:
|
|
|
model: The model to evaluate
|
|
|
eval_dataloader: The dataloader containing the evaluation data
|
|
|
local_rank: The rank of the current node in a distributed setting
|
|
|
tokenizer: The tokenizer used to decode predictions
|
|
|
-
|
|
|
+
|
|
|
Returns: eval_ppl, eval_epoch_loss
|
|
|
"""
|
|
|
if train_config.enable_fsdp:
|
|
|
- world_size = int(os.environ["WORLD_SIZE"])
|
|
|
+ world_size = int(os.environ["WORLD_SIZE"])
|
|
|
model.eval()
|
|
|
eval_preds = []
|
|
|
eval_loss = 0.0 # Initialize evaluation loss
|
|
@@ -248,24 +248,24 @@ def evaluation(model,train_config, eval_dataloader, local_rank, tokenizer):
|
|
|
eval_preds.extend(
|
|
|
tokenizer.batch_decode(preds.detach().cpu().numpy(), skip_special_tokens=True)
|
|
|
)
|
|
|
-
|
|
|
+
|
|
|
# If there's more than one CUDA device, reduce evaluation loss across all devices
|
|
|
if torch.cuda.device_count() > 1 and train_config.enable_fsdp:
|
|
|
dist.all_reduce(eval_loss, op=dist.ReduceOp.SUM)
|
|
|
-
|
|
|
+
|
|
|
# Compute average loss and perplexity
|
|
|
eval_epoch_loss = eval_loss / len(eval_dataloader)
|
|
|
if train_config.enable_fsdp:
|
|
|
eval_epoch_loss = eval_epoch_loss/world_size
|
|
|
eval_ppl = torch.exp(eval_epoch_loss)
|
|
|
-
|
|
|
+
|
|
|
# Print evaluation metrics
|
|
|
if train_config.enable_fsdp:
|
|
|
if local_rank==0:
|
|
|
print(f" {eval_ppl=} {eval_epoch_loss=}")
|
|
|
else:
|
|
|
print(f" {eval_ppl=} {eval_epoch_loss=}")
|
|
|
-
|
|
|
+
|
|
|
return eval_ppl, eval_epoch_loss
|
|
|
|
|
|
def freeze_transformer_layers(model, num_layer):
|
|
@@ -279,8 +279,8 @@ def check_frozen_layers_peft_model(model):
|
|
|
for i, layer in enumerate(model.base_model.model.model.layers):
|
|
|
for name, param in layer.named_parameters():
|
|
|
print(f"Layer {i}, parameter {name}: requires_grad = {param.requires_grad}")
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
def setup():
|
|
|
"""Initialize the process group for distributed training"""
|
|
|
dist.init_process_group("nccl")
|
|
@@ -293,7 +293,7 @@ def setup_environ_flags(rank):
|
|
|
# os.environ["TORCH_DISTRIBUTED_DEBUG"] = "DETAIL"
|
|
|
# This flag will help with CUDA memory fragmentations that can lead into OOM in some cases.
|
|
|
# Note this is only availble in PyTorch Nighlies (as of July 30 2023)
|
|
|
- # os.environ['PYTORCH_CUDA_ALLOC_CONF']='expandable_segments:True'
|
|
|
+ # os.environ['PYTORCH_CUDA_ALLOC_CONF']='expandable_segments:True'
|
|
|
if rank == 0:
|
|
|
print(f"--> Running with torch dist debug set to detail")
|
|
|
|
|
@@ -338,7 +338,7 @@ def print_model_size(model, config, rank: int = 0) -> None:
|
|
|
|
|
|
def get_policies(cfg, rank):
|
|
|
"""Get the policies for mixed precision and fsdp wrapping"""
|
|
|
-
|
|
|
+
|
|
|
verify_bfloat_support = (
|
|
|
torch.version.cuda
|
|
|
and torch.cuda.is_bf16_supported()
|
|
@@ -374,7 +374,7 @@ def save_train_params(train_config, fsdp_config, rank):
|
|
|
This will be used by converter script in the inference folder to fetch the HF model name or path.
|
|
|
It also would be hepful as a log for future references.
|
|
|
"""
|
|
|
- # Convert the train_config and fsdp_config objects to dictionaries,
|
|
|
+ # Convert the train_config and fsdp_config objects to dictionaries,
|
|
|
# converting all values to strings to ensure they can be serialized into a YAML file
|
|
|
train_config_dict = {k: str(v) for k, v in vars(train_config).items() if not k.startswith('__')}
|
|
|
fsdp_config_dict = {k: str(v) for k, v in vars(fsdp_config).items() if not k.startswith('__')}
|