-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QEff. Finetune] : Sort dataset based on length for DDP (#211)
Sorting in dataset, sample based on length for DDP fix for run_validation = False --------- Signed-off-by: Mamta Singh <[email protected]>
- Loading branch information
1 parent
2fed663
commit de97e39
Showing
5 changed files
with
110 additions
and
32 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,62 @@ | ||
# ----------------------------------------------------------------------------- | ||
# | ||
# Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# | ||
# ----------------------------------------------------------------------------- | ||
|
||
import random | ||
from itertools import islice | ||
|
||
import numpy as np | ||
import torch | ||
|
||
|
||
class LengthBasedBatchSampler(torch.utils.data.BatchSampler): | ||
def __init__(self, data_source, batch_size: int, drop_last: bool, shuffle: bool = True) -> None: | ||
if isinstance(next(iter(data_source)), dict): | ||
first_key = next(iter(next(iter(data_source)).keys())) | ||
self.lengths = [len(d[first_key]) for d in data_source] | ||
else: | ||
self.lengths = [len(d) for d in data_source] | ||
self.batch_size = batch_size | ||
self.drop_last = drop_last | ||
self.shuffle = shuffle | ||
|
||
def __iter__(self): | ||
ids = np.argsort(self.lengths, kind="mergesort") | ||
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) | ||
|
||
for b in batches: | ||
yield b | ||
|
||
def __len__(self): | ||
if self.drop_last: | ||
return len(self.lengths) // self.batch_size | ||
else: | ||
return len(self.lengths) // self.batch_size + (len(self.lengths) % self.batch_size > 0) | ||
|
||
|
||
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 | ||
) -> None: | ||
random.seed(seed) | ||
self.batch_sampler = LengthBasedBatchSampler( | ||
data_source, batch_size=batch_size, drop_last=True, shuffle=shuffle | ||
) | ||
self.num_replicas = num_replicas | ||
self.rank = rank | ||
|
||
def __iter__(self): | ||
max_length = len(self.batch_sampler) // self.num_replicas * self.num_replicas | ||
return islice(self.batch_sampler, self.rank, max_length, self.num_replicas) | ||
|
||
def __len__(self): | ||
return len(self.batch_sampler) // self.num_replicas |
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