-
Notifications
You must be signed in to change notification settings - Fork 44
[QEff Finetune] Adding dataset padding changes #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
# SPDX-License-Identifier: BSD-3-Clause | ||
# | ||
# ----------------------------------------------------------------------------- | ||
|
||
import datasets | ||
import torch | ||
import torch.distributed as dist | ||
from transformers.data import DataCollatorForSeq2Seq | ||
|
@@ -62,19 +62,41 @@ def get_dataloader_kwargs(train_config, dataset, dataset_processer, split): | |
return kwargs | ||
|
||
|
||
def padding_dataset(train_config, dataset): | ||
dataset = dataset.map(lambda x: {"input_length": len(x["input_ids"])}) | ||
if train_config.enable_sorting_for_ddp: | ||
dataset = dataset.sort("input_length") | ||
dataset = dataset.remove_columns("input_length") | ||
dummy_row = next(iter(dataset)) | ||
dummy_row["labels"] = [-100] * len(dummy_row["labels"]) | ||
padding_size = 0 | ||
num_replicas = dist.get_world_size() | ||
if len(dataset) % (num_replicas * train_config.train_batch_size) > 0: | ||
padding_size = num_replicas - (len(dataset) % (num_replicas * train_config.train_batch_size)) | ||
|
||
dummy_data = [dummy_row.copy() for _ in range(padding_size)] | ||
dummy_dataset = datasets.Dataset.from_list(dummy_data) | ||
combined_dataset = datasets.concatenate_datasets([dataset, dummy_dataset]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try to enclose this padding logic in separate function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
return combined_dataset | ||
|
||
|
||
def get_dataloader(tokenizer, dataset_config, train_config, split: str = "train"): | ||
dataset = get_preprocessed_dataset(tokenizer, dataset_config, split, context_length=train_config.context_length) | ||
if train_config.enable_ddp: | ||
dataset = padding_dataset(train_config, dataset) | ||
|
||
dl_kwargs = get_dataloader_kwargs(train_config, dataset, tokenizer, split) | ||
|
||
# FIXME (Meet): Add custom data collator registration from the outside by the user. | ||
custom_data_collator = get_custom_data_collator(tokenizer, dataset_config) | ||
|
||
if custom_data_collator: | ||
print("custom_data_collator is used") | ||
dl_kwargs["collate_fn"] = custom_data_collator | ||
|
||
print(f"length of dataset_{split}", len(dataset)) | ||
|
||
# Create data loader | ||
|
||
dataloader = torch.utils.data.DataLoader( | ||
dataset, | ||
num_workers=train_config.num_workers_dataloader, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,7 +151,7 @@ def train( | |
|
||
# enable profile for qaic | ||
qaic_profile.start_profiling(device, 1) if train_config.use_profiler else None | ||
|
||
num_dummy_samples = 0 | ||
for step, batch in enumerate(train_dataloader): | ||
# resume training from a particular checkpoint, assuming the dataset is not shuffled | ||
if train_config.use_peft and train_config.from_peft_checkpoint: | ||
|
@@ -192,6 +192,10 @@ def train( | |
) as verifier: | ||
model_outputs = model(**batch) | ||
loss = model_outputs.loss # Forward call | ||
if (batch["labels"] != -100).sum() == 0: | ||
loss = loss.nan_to_num(nan=0.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loss is zeroed for dummy samples. But the total loss is averaged across all samples including dummy samples. Correct it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
num_dummy_samples += 1 | ||
|
||
if train_config.task_type == "seq_classification": | ||
logits = model_outputs.logits | ||
labels = batch["labels"][:, 0] | ||
|
@@ -201,15 +205,18 @@ def train( | |
else: | ||
model_outputs = model(**batch) | ||
loss = model_outputs.loss # Forward call | ||
if (batch["labels"] != -100).sum() == 0: | ||
loss = loss.nan_to_num(nan=0.0) | ||
num_dummy_samples += 1 | ||
|
||
if train_config.task_type == "seq_classification": | ||
logits = model_outputs.logits | ||
labels = batch["labels"][:, 0] | ||
preds = torch.nn.functional.softmax(logits, dim=-1) | ||
acc_helper.forward(preds, labels) | ||
|
||
total_loss += loss.detach().float() | ||
# Accumalate gradients | ||
loss = loss / train_config.gradient_accumulation_steps | ||
|
||
if train_config.enable_ddp: | ||
if local_rank == 0: | ||
if loss <= train_config.convergence_loss: | ||
|
@@ -237,6 +244,17 @@ def train( | |
step_metric_val = float(torch.exp(loss.detach().float())) | ||
train_step_metric.append(step_metric_val) | ||
|
||
# Accumalate gradients | ||
complete_accum_steps = ( | ||
len(train_dataloader) - len(train_dataloader) % train_config.gradient_accumulation_steps | ||
) | ||
if step < complete_accum_steps: | ||
num_samples_in_cur_update = train_config.gradient_accumulation_steps | ||
else: | ||
num_samples_in_cur_update = len(train_dataloader) % train_config.gradient_accumulation_steps | ||
|
||
loss = loss / num_samples_in_cur_update | ||
|
||
if train_config.grad_scaler: | ||
scaler.scale(loss).backward() # backward pass | ||
else: | ||
|
@@ -296,14 +314,22 @@ def train( | |
|
||
if loss_0_counter.item() == train_config.convergence_counter: | ||
if train_config.use_peft and train_config.from_peft_checkpoint and epoch == intermediate_epoch: | ||
train_epoch_loss = total_loss / (step - intermediate_step) | ||
train_epoch_loss = ( | ||
0.0 if total_loss == 0.0 else total_loss / (step - intermediate_step - num_dummy_samples) | ||
) | ||
else: | ||
train_epoch_loss = total_loss / step | ||
train_epoch_loss = 0.0 if total_loss == 0.0 else total_loss / (step - num_dummy_samples) | ||
else: | ||
if train_config.use_peft and train_config.from_peft_checkpoint and epoch == intermediate_epoch: | ||
train_epoch_loss = total_loss / (len(train_dataloader) - intermediate_step) | ||
train_epoch_loss = ( | ||
0.0 | ||
if total_loss == 0.0 | ||
else total_loss / (len(train_dataloader) - intermediate_step - num_dummy_samples) | ||
) | ||
else: | ||
train_epoch_loss = total_loss / len(train_dataloader) | ||
train_epoch_loss = ( | ||
0.0 if total_loss == 0.0 else total_loss / (len(train_dataloader) - num_dummy_samples) | ||
) | ||
|
||
if train_config.task_type == "seq_classification": | ||
metric_val = acc_helper.compute() | ||
|
@@ -421,6 +447,7 @@ def evaluation_helper(model, train_config, eval_dataloader, device): | |
eval_loss = 0.0 # Initialize evaluation loss | ||
device_type = torch.device(device).type | ||
|
||
num_dummy_samples = 0 | ||
for step, batch in enumerate(tqdm(eval_dataloader, colour="green", desc="evaluating Epoch", dynamic_ncols=True)): | ||
# stop when the maximum number of eval steps is reached | ||
if train_config.max_eval_step > 0 and step > train_config.max_eval_step: | ||
|
@@ -439,6 +466,10 @@ def evaluation_helper(model, train_config, eval_dataloader, device): | |
outputs = model(**batch) | ||
loss = outputs.loss | ||
|
||
if (batch["labels"] != -100).sum() == 0: | ||
loss = loss.nan_to_num(nan=0.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
num_dummy_samples += 1 | ||
|
||
if train_config.task_type == "seq_classification": | ||
logits = outputs.logits | ||
labels = batch["labels"][:, 0] | ||
|
@@ -455,7 +486,7 @@ def evaluation_helper(model, train_config, eval_dataloader, device): | |
eval_loss += loss.detach().float() | ||
|
||
# Compute average loss and metric | ||
eval_epoch_loss = eval_loss / len(eval_dataloader) | ||
eval_epoch_loss = 0.0 if eval_loss == 0.0 else eval_loss / (len(eval_dataloader) - num_dummy_samples) | ||
if train_config.task_type == "seq_classification": | ||
eval_metric = acc_helper.compute() | ||
else: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L78 to L80 can be refactored.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found this way cleaner. Please suggest if you have anything better idea.