Upload rl_code/verl/trainer/data_loader.py with huggingface_hub
Browse files
rl_code/verl/trainer/data_loader.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
from typing import Optional
|
| 16 |
+
|
| 17 |
+
import torch
|
| 18 |
+
from torch.utils.data import RandomSampler, SequentialSampler
|
| 19 |
+
from torchdata.stateful_dataloader import StatefulDataLoader
|
| 20 |
+
from transformers import PreTrainedTokenizer, ProcessorMixin
|
| 21 |
+
|
| 22 |
+
from ..utils.dataset import RLHFDataset, collate_fn
|
| 23 |
+
from .config import DataConfig
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def create_dataloader(config: DataConfig, tokenizer: PreTrainedTokenizer, processor: Optional[ProcessorMixin]) -> None:
|
| 27 |
+
train_dataset = RLHFDataset(
|
| 28 |
+
data_path=config.train_files,
|
| 29 |
+
tokenizer=tokenizer,
|
| 30 |
+
processor=processor,
|
| 31 |
+
prompt_key=config.prompt_key,
|
| 32 |
+
answer_key=config.answer_key,
|
| 33 |
+
image_key=config.image_key,
|
| 34 |
+
video_key=config.video_key,
|
| 35 |
+
image_dir=config.image_dir,
|
| 36 |
+
video_fps=config.video_fps,
|
| 37 |
+
max_prompt_length=config.max_prompt_length,
|
| 38 |
+
truncation="right",
|
| 39 |
+
format_prompt=config.format_prompt,
|
| 40 |
+
min_pixels=config.min_pixels,
|
| 41 |
+
max_pixels=config.max_pixels,
|
| 42 |
+
filter_overlong_prompts=config.filter_overlong_prompts,
|
| 43 |
+
filter_overlong_prompts_workers=config.filter_overlong_prompts_workers,
|
| 44 |
+
)
|
| 45 |
+
# use sampler for better ckpt resume
|
| 46 |
+
if config.shuffle:
|
| 47 |
+
train_dataloader_generator = torch.Generator()
|
| 48 |
+
train_dataloader_generator.manual_seed(config.seed)
|
| 49 |
+
sampler = RandomSampler(data_source=train_dataset, generator=train_dataloader_generator)
|
| 50 |
+
else:
|
| 51 |
+
sampler = SequentialSampler(data_source=train_dataset)
|
| 52 |
+
|
| 53 |
+
if config.mini_rollout_batch_size is not None:
|
| 54 |
+
train_batch_size = config.mini_rollout_batch_size
|
| 55 |
+
else:
|
| 56 |
+
train_batch_size = config.rollout_batch_size
|
| 57 |
+
|
| 58 |
+
train_dataloader = StatefulDataLoader(
|
| 59 |
+
dataset=train_dataset,
|
| 60 |
+
batch_size=train_batch_size,
|
| 61 |
+
sampler=sampler,
|
| 62 |
+
num_workers=8,
|
| 63 |
+
collate_fn=collate_fn,
|
| 64 |
+
pin_memory=False,
|
| 65 |
+
drop_last=True,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
val_dataset = RLHFDataset(
|
| 69 |
+
data_path=config.val_files,
|
| 70 |
+
tokenizer=tokenizer,
|
| 71 |
+
processor=processor,
|
| 72 |
+
prompt_key=config.prompt_key,
|
| 73 |
+
answer_key=config.answer_key,
|
| 74 |
+
image_key=config.image_key,
|
| 75 |
+
image_dir=config.image_dir,
|
| 76 |
+
max_prompt_length=config.max_prompt_length,
|
| 77 |
+
truncation="right",
|
| 78 |
+
format_prompt=config.format_prompt,
|
| 79 |
+
min_pixels=config.min_pixels,
|
| 80 |
+
max_pixels=config.max_pixels,
|
| 81 |
+
filter_overlong_prompts=config.filter_overlong_prompts,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
if config.val_batch_size == -1:
|
| 85 |
+
val_batch_size = len(val_dataset)
|
| 86 |
+
else:
|
| 87 |
+
val_batch_size = config.val_batch_size
|
| 88 |
+
|
| 89 |
+
val_dataloader = StatefulDataLoader(
|
| 90 |
+
dataset=val_dataset,
|
| 91 |
+
batch_size=val_batch_size,
|
| 92 |
+
shuffle=False,
|
| 93 |
+
num_workers=8,
|
| 94 |
+
collate_fn=collate_fn,
|
| 95 |
+
pin_memory=False,
|
| 96 |
+
drop_last=False,
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
assert len(train_dataloader) >= 1
|
| 100 |
+
assert len(val_dataloader) >= 1
|
| 101 |
+
print(f"Size of train dataloader: {len(train_dataloader)}")
|
| 102 |
+
print(f"Size of val dataloader: {len(val_dataloader)}")
|
| 103 |
+
return train_dataloader, val_dataloader
|