Skip to content

[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

Merged
merged 11 commits into from
Jul 4, 2025
16 changes: 10 additions & 6 deletions QEfficient/finetune/data/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
# SPDX-License-Identifier: BSD-3-Clause
#
# -----------------------------------------------------------------------------

import random
from itertools import islice

import numpy as np
import torch


Expand All @@ -22,14 +20,14 @@ def __init__(self, data_source, batch_size: int, drop_last: bool, shuffle: bool
self.batch_size = batch_size
self.drop_last = drop_last
self.shuffle = shuffle
self.data_source = data_source

def __iter__(self):
ids = np.argsort(self.lengths, kind="mergesort")
ids = list(range(len(self.data_source)))
if self.drop_last:
ids = ids[: len(ids) // self.batch_size * self.batch_size]

batches = [ids[i : i + self.batch_size] for i in range(0, len(ids), self.batch_size)]

if self.shuffle:
random.shuffle(batches)

Expand All @@ -45,11 +43,17 @@ def __len__(self):

class DistributedLengthBasedBatchSampler(torch.utils.data.BatchSampler):
def __init__(
self, data_source, batch_size: int, num_replicas: int, rank: int, shuffle: bool = True, seed: int = 0
self,
data_source,
batch_size: int,
num_replicas: int,
rank: int,
shuffle: bool = True,
seed: int = 0,
) -> None:
random.seed(seed)
self.batch_sampler = LengthBasedBatchSampler(
data_source, batch_size=batch_size, drop_last=True, shuffle=shuffle
data_source, batch_size=batch_size, drop_last=False, shuffle=shuffle
)
self.num_replicas = num_replicas
self.rank = rank
Expand Down
40 changes: 36 additions & 4 deletions QEfficient/finetune/utils/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
# SPDX-License-Identifier: BSD-3-Clause
#
# -----------------------------------------------------------------------------

import datasets
import torch
import torch.distributed as dist
from transformers.data import DataCollatorForSeq2Seq

from QEfficient.finetune.data.sampler import DistributedLengthBasedBatchSampler
from QEfficient.finetune.dataset.dataset_config import DATALOADER_COLLATE_FUNC, DATASET_PREPROC
from QEfficient.finetune.utils.helper import get_num_ddp_devices


def get_preprocessed_dataset(
Expand Down Expand Up @@ -54,27 +55,58 @@ def get_dataloader_kwargs(train_config, dataset, dataset_processer, split):
dataset, num_replicas=dist.get_world_size(), rank=dist.get_rank(), shuffle=False
)
kwargs["batch_size"] = batch_size
kwargs["drop_last"] = True
kwargs["drop_last"] = False
else:
kwargs["batch_size"] = batch_size
kwargs["drop_last"] = True
kwargs["drop_last"] = False
kwargs["collate_fn"] = DataCollatorForSeq2Seq(dataset_processer)
return kwargs


def padding_dataset(train_config, dataset, batch_size):
if train_config.enable_ddp and train_config.enable_sorting_for_ddp:
if isinstance(dataset, datasets.Dataset):
# Hugging Face Dataset transformation
dataset = dataset.map(lambda x: {"input_length": len(x["input_ids"])})
dataset = dataset.sort("input_length")

else:
dataset = sorted(dataset, key=lambda x: len(x["input_ids"]))

dummy_row = next(iter(dataset))
dummy_row["labels"] = torch.tensor([-100] * len(dummy_row["labels"]))
padding_size = 0
num_replicas = get_num_ddp_devices()
remainder = len(dataset) % (num_replicas * batch_size)
padding_size = (num_replicas * batch_size) - remainder

dummy_data = [dummy_row.copy() for _ in range(padding_size)]
dummy_dataset = datasets.Dataset.from_list(dummy_data)
if isinstance(dataset, datasets.Dataset):
combined_dataset = datasets.concatenate_datasets([dataset, dummy_dataset])
else:
combined_dataset = dataset + list(dummy_dataset)
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)

batch_size = train_config.train_batch_size if split == "train" else train_config.val_batch_size
dataset = padding_dataset(train_config, dataset, batch_size)

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,
Expand Down
5 changes: 5 additions & 0 deletions QEfficient/finetune/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
# SPDX-License-Identifier: BSD-3-Clause
#
# -----------------------------------------------------------------------------
import os

TASK_TYPE = ["generation", "seq_classification"]
PEFT_METHOD = ["lora"]
DEVICE = ["qaic", "cpu", "cuda"]
BATCHING_STRATEGY = ["padding", "packing"]


def get_num_ddp_devices():
return int(os.getenv("WORLD_SIZE", 1))
81 changes: 70 additions & 11 deletions QEfficient/finetune/utils/train_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -192,6 +192,17 @@ 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)
num_dummy_samples += train_config.train_batch_size
else:
num_dummy_samples_per_batch = (
(torch.sum(batch["labels"] == -100, dim=1) == batch["labels"].shape[1]).sum().item()
)
if num_dummy_samples_per_batch > 0:
num_dummy_samples += num_dummy_samples_per_batch
loss = loss * train_config.train_batch_size / num_dummy_samples_per_batch

if train_config.task_type == "seq_classification":
logits = model_outputs.logits
labels = batch["labels"][:, 0]
Expand All @@ -201,15 +212,25 @@ 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 += train_config.train_batch_size
else:
num_dummy_samples_per_batch = (
(torch.sum(batch["labels"] == -100, dim=1) == batch["labels"].shape[1]).sum().item()
)
if num_dummy_samples_per_batch > 0:
num_dummy_samples += num_dummy_samples_per_batch
loss = loss * train_config.train_batch_size / num_dummy_samples_per_batch

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:
Expand Down Expand Up @@ -237,6 +258,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:
Expand Down Expand Up @@ -296,15 +328,30 @@ 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 / train_config.train_batch_size)
)
else:
train_epoch_loss = total_loss / step
train_epoch_loss = (
0.0
if total_loss == 0.0
else total_loss / (step + 1 - num_dummy_samples / train_config.train_batch_size)
)
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 / (step - intermediate_step - (num_dummy_samples / train_config.train_batch_size))
)
else:
train_epoch_loss = total_loss / len(train_dataloader)

train_epoch_loss = (
0.0
if total_loss == 0.0
else total_loss / (step + 1 - (num_dummy_samples / train_config.train_batch_size))
)
if train_config.task_type == "seq_classification":
metric_val = acc_helper.compute()
acc_helper.reset()
Expand Down Expand Up @@ -389,7 +436,6 @@ def train(
results["avg_checkpoint_time"] = avg_checkpoint_time
if train_config.save_metrics:
results["metrics_filename"] = metrics_filename

return results


Expand Down Expand Up @@ -421,6 +467,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:
Expand All @@ -439,6 +486,17 @@ 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)
num_dummy_samples += 1
else:
num_dummy_samples_per_batch = (
(torch.sum(batch["labels"] == -100, dim=1) == batch["labels"].shape[1]).sum().item()
)
if num_dummy_samples_per_batch > 0:
num_dummy_samples += num_dummy_samples_per_batch
loss = loss * train_config.val_batch_size / num_dummy_samples_per_batch

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not generalize the logic in L492 to L502?

I mean else case considers bs>1 use case with 'labels' shape of [batch, seq]. The same logic should be true for bs=1 use case. Why we need separate logic.

This same can be done in train case. Try to avoid unnecessary if conditions.

if train_config.task_type == "seq_classification":
logits = outputs.logits
labels = batch["labels"][:, 0]
Expand All @@ -453,9 +511,10 @@ def evaluation_helper(model, train_config, eval_dataloader, device):
val_step_metric.append(metric_val)

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 / (step + 1 - num_dummy_samples / train_config.val_batch_size)
)
if train_config.task_type == "seq_classification":
eval_metric = acc_helper.compute()
else:
Expand Down
22 changes: 12 additions & 10 deletions tests/finetune/test_finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def download_alpaca():
True, # run_validation
True, # use_peft
"qaic", # device
0.0043353, # expected_train_loss
1.0043447, # expected_train_metric
0.0117334, # expected_eval_loss
1.0118025, # expected_eval_metric
1.5427961, # expected_train_loss
4.6776514, # expected_train_metric
1.2898713, # expected_eval_loss
3.6323189, # expected_eval_metric
id="llama_config_gsm8k", # config name
),
pytest.param(
Expand All @@ -68,10 +68,10 @@ def download_alpaca():
True, # run_validation
True, # use_peft
"qaic", # device
0.0006099, # expected_train_loss
1.0006101, # expected_train_metric
0.0065296, # expected_eval_loss
1.0065510, # expected_eval_metric
1.4348667, # expected_train_loss
4.1990857, # expected_train_metric
1.5941212, # expected_eval_loss
4.9239997, # expected_eval_metric
id="llama_config_alpaca", # config name
),
pytest.param(
Expand All @@ -86,15 +86,16 @@ def download_alpaca():
True, # run_validation
False, # use_peft
"qaic", # device
0.00052981, # expected_train_loss
0.63060283, # expected_train_loss
0.55554199, # expected_train_metric
0.00738618, # expected_eval_loss
0.61503016, # expected_eval_loss
0.70825195, # expected_eval_metric
id="bert_config_imdb", # config name
),
]


@pytest.mark.skip() # remove when it's clear why diff val_step_loss values are observed in diff runs on existing code (even without PR #478 changes)
@pytest.mark.cli
@pytest.mark.on_qaic
@pytest.mark.finetune
Expand Down Expand Up @@ -149,6 +150,7 @@ def test_finetune_llama(
download_alpaca()

results = finetune(**kwargs)

assert np.allclose(results["avg_train_loss"], expected_train_loss, atol=1e-3), "Train loss is not matching."
assert np.allclose(results["avg_train_metric"], expected_train_metric, atol=1e-3), "Train metric is not matching."
assert np.allclose(results["avg_eval_loss"], expected_eval_loss, atol=1e-3), "Eval loss is not matching."
Expand Down
Loading