File size: 25,611 Bytes
1737ebf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 | import json
import random
import logging
import re
import time
import itertools
from dataclasses import dataclass, field
from typing import Dict, Optional, Sequence, List, Tuple, Any
from collections.abc import Sequence
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import Dataset
import transformers
from . import data_list
from .rope2d import get_rope_index_25, get_rope_index_2, get_rope_index_3
IGNORE_INDEX = -100
IMAGE_TOKEN_INDEX = 151655
VIDEO_TOKEN_INDEX = 151656
DEFAULT_IMAGE_TOKEN = "<image>"
DEFAULT_VIDEO_TOKEN = "<video>"
local_rank = None
def rank0_print(*args):
if local_rank == 0:
print(*args)
def read_jsonl(path):
with open(path, "r") as f:
return [json.loads(line) for line in f]
def _make_abs_paths(base: Path, files: str) -> str:
return f"{(base / files).resolve()}"
def update_processor_pixels(processor, data_args):
logger = logging.getLogger(__name__)
# --- Image Processor ---
ip = processor.image_processor
rank0_print("=== BEFORE IMAGE PROCESSOR PARAMETERS ===")
rank0_print(f"Image min_pixels: {getattr(ip, 'min_pixels', 'N/A')}")
rank0_print(f"Image max_pixels: {getattr(ip, 'max_pixels', 'N/A')}")
rank0_print(f"ip.size: {ip.size}")
rank0_print(f"Image size (shortest_edge): {ip.size.get('shortest_edge', 'N/A')}")
rank0_print(f"Image size (longest_edge): {ip.size.get('longest_edge', 'N/A')}")
if hasattr(ip, "min_pixels") and hasattr(ip, "max_pixels"):
ip.min_pixels = data_args.min_pixels
ip.max_pixels = data_args.max_pixels
rank0_print(f"✅ Updated image_processor min_pixels to {data_args.min_pixels}")
rank0_print(f"✅ Updated image_processor max_pixels to {data_args.max_pixels}")
if hasattr(ip, "size") and isinstance(ip.size, dict):
ip.size["shortest_edge"] = data_args.min_pixels
ip.size["longest_edge"] = data_args.max_pixels
rank0_print(
f"✅ Updated image_processor size['shortest_edge'] to {data_args.min_pixels}"
)
rank0_print(
f"✅ Updated image_processor size['longest_edge'] to {data_args.max_pixels}"
)
rank0_print("=== AFTER IMAGE PROCESSOR PARAMETERS ===")
rank0_print(f"Image min_pixels: {getattr(ip, 'min_pixels', 'N/A')}")
rank0_print(f"Image max_pixels: {getattr(ip, 'max_pixels', 'N/A')}")
rank0_print(f"Image size (shortest_edge): {ip.size.get('shortest_edge', 'N/A')}")
rank0_print(f"Image size (longest_edge): {ip.size.get('longest_edge', 'N/A')}")
# --- Video Processor ---
if hasattr(processor, "video_processor") and processor.video_processor is not None:
vp = processor.video_processor
rank0_print("\n=== BEFORE VIDEO PROCESSOR PARAMETERS ===")
rank0_print(f"Video min_pixels: {getattr(vp, 'min_pixels', 'N/A')}")
rank0_print(f"Video max_pixels: {getattr(vp, 'max_pixels', 'N/A')}")
rank0_print(f"Video min_frames: {getattr(vp, 'min_frames', 'N/A')}")
rank0_print(f"Video max_frames: {getattr(vp, 'max_frames', 'N/A')}")
rank0_print(f"Video fps: {getattr(vp, 'fps', 'N/A')}")
rank0_print(
f"Video size (shortest_edge): {vp.size.get('shortest_edge', 'N/A')}"
)
rank0_print(f"Video size (longest_edge): {vp.size.get('longest_edge', 'N/A')}")
if hasattr(vp, "min_pixels") and hasattr(vp, "max_pixels"):
vp.min_pixels = data_args.video_min_pixels
vp.max_pixels = data_args.video_max_pixels
rank0_print(
f"✅ Updated Qwen2-VL video_processor min_pixels to {data_args.video_min_pixels}"
)
rank0_print(
f"✅ Updated Qwen2-VL video_processor max_pixels to {data_args.video_max_pixels}"
)
if hasattr(vp, "min_frames") and hasattr(vp, "max_frames"):
vp.min_frames = data_args.video_min_frames
vp.max_frames = data_args.video_max_frames
rank0_print(
f"✅ Updated video_processor min_frames to {data_args.video_min_frames}"
)
rank0_print(
f"✅ Updated video_processor max_frames to {data_args.video_max_frames}"
)
if hasattr(vp, "fps"):
vp.fps = data_args.video_fps
rank0_print(f"✅ Updated video_processor fps to {data_args.video_fps}")
if hasattr(vp, "size") and isinstance(vp.size, dict):
vp.size["shortest_edge"] = data_args.video_min_pixels
vp.size["longest_edge"] = data_args.video_max_pixels
rank0_print(
f"✅ Updated Video size (shortest_edge): {vp.size.get('shortest_edge', 'N/A')}"
)
rank0_print(
f"✅ Updated Video size (longest_edge): {vp.size.get('longest_edge', 'N/A')}"
)
rank0_print("=== AFTER VIDEO PROCESSOR PARAMETERS ===")
rank0_print(f"Video min_pixels: {getattr(vp, 'min_pixels', 'N/A')}")
rank0_print(f"Video max_pixels: {getattr(vp, 'max_pixels', 'N/A')}")
rank0_print(f"Video min_frames: {getattr(vp, 'min_frames', 'N/A')}")
rank0_print(f"Video max_frames: {getattr(vp, 'max_frames', 'N/A')}")
rank0_print(f"Video fps: {getattr(vp, 'fps', 'N/A')}")
rank0_print(
f"Video size (shortest_edge): {vp.size.get('shortest_edge', 'N/A')}"
)
rank0_print(f"Video size (longest_edge): {vp.size.get('longest_edge', 'N/A')}")
return processor
def _build_messages(item: Dict[str, Any], base_path: Path) -> List[Dict[str, Any]]:
# Extract and normalize images and videos
images = item.get("image") or []
if isinstance(images, str):
images = [images]
videos = item.get("video") or []
if isinstance(videos, str):
videos = [videos]
# Build media pools with absolute paths
image_pool = [
{"type": "image", "image": _make_abs_paths(base_path, img)} for img in images
]
video_pool = [
{"type": "video", "video": _make_abs_paths(base_path, vid)} for vid in videos
]
messages = []
for turn in item["conversations"]:
role = "user" if turn["from"] == "human" else "assistant"
text: str = turn["value"]
if role == "user":
content = []
# Split text by <image> or <video> placeholders while keeping delimiters
text_parts = re.split(r"(<image>|<video>)", text)
for seg in text_parts:
if seg == "<image>":
if not image_pool:
raise ValueError(
"Number of <image> placeholders exceeds the number of provided images"
)
content.append(image_pool.pop(0))
elif seg == "<video>":
if not video_pool:
raise ValueError(
"Number of <video> placeholders exceeds the number of provided videos"
)
content.append(video_pool.pop(0))
elif seg.strip():
content.append({"type": "text", "text": seg.strip()})
messages.append({"role": role, "content": content})
else:
# Assistant messages contain only text
messages.append({"role": role, "content": [{"type": "text", "text": text}]})
# Check for unused media files
if image_pool:
raise ValueError(
f"{len(image_pool)} image(s) remain unused (not consumed by placeholders)"
)
if video_pool:
raise ValueError(
f"{len(video_pool)} video(s) remain unused (not consumed by placeholders)"
)
return messages
def preprocess_qwen_visual(
sources,
processor,
) -> Dict:
if len(sources) != 1:
raise ValueError(f"Expected 1 source, got {len(sources)}")
source = sources[0]
base_path = Path(source.get("data_path", ""))
messages = _build_messages(source, base_path)
full_result = processor.apply_chat_template(
messages, tokenize=True, return_dict=True, return_tensors="pt"
)
input_ids = full_result["input_ids"]
if isinstance(input_ids, list):
input_ids = torch.tensor(input_ids).unsqueeze(0)
labels = torch.full_like(input_ids, IGNORE_INDEX)
input_ids_flat = input_ids[0].tolist()
L = len(input_ids_flat)
pos = 0
while pos < L:
if input_ids_flat[pos] == 77091:
ans_start = pos + 2
ans_end = ans_start
while ans_end < L and input_ids_flat[ans_end] != 151645:
ans_end += 1
if ans_end < L:
labels[0, ans_start : ans_end + 2] = input_ids[
0, ans_start : ans_end + 2
]
pos = ans_end
pos += 1
full_result["labels"] = labels
full_result["input_ids"] = input_ids
return full_result
class LazySupervisedDataset(Dataset):
"""Dataset for supervised fine-tuning."""
def __init__(self, processor, data_args):
super(LazySupervisedDataset, self).__init__()
dataset = data_args.dataset_use.split(",")
dataset_list = data_list(dataset)
rank0_print(f"Loading datasets: {dataset_list}")
self.video_max_total_pixels = getattr(
data_args, "video_max_total_pixels", 1664 * 28 * 28
)
self.video_min_total_pixels = getattr(
data_args, "video_min_total_pixels", 256 * 28 * 28
)
self.model_type = data_args.model_type
if data_args.model_type == "qwen3vl":
self.get_rope_index = get_rope_index_3
elif data_args.model_type == "qwen2.5vl":
self.get_rope_index = get_rope_index_25
elif data_args.model_type == "qwen2vl":
self.get_rope_index = get_rope_index_2
else:
raise ValueError(f"model_type: {data_args.model_type} not supported")
list_data_dict = []
for data in dataset_list:
file_format = data["annotation_path"].split(".")[-1]
if file_format == "jsonl":
annotations = read_jsonl(data["annotation_path"])
else:
annotations = json.load(open(data["annotation_path"], "r"))
sampling_rate = data.get("sampling_rate", 1.0)
if sampling_rate < 1.0:
annotations = random.sample(
annotations, int(len(annotations) * sampling_rate)
)
rank0_print(f"sampling {len(annotations)} examples from dataset {data}")
else:
rank0_print(f"dataset name: {data}")
for ann in annotations:
if isinstance(ann, list):
for sub_ann in ann:
sub_ann["data_path"] = data["data_path"]
else:
ann["data_path"] = data["data_path"]
list_data_dict += annotations
rank0_print(f"Total training samples: {len(list_data_dict)}")
rank0_print("Formatting inputs...Skip in lazy mode")
processor = update_processor_pixels(processor, data_args)
self.processor = processor
self.tokenizer = processor.tokenizer
self.data_args = data_args
self.merge_size = getattr(processor.image_processor, "merge_size", 2)
self.list_data_dict = list_data_dict
if data_args.data_packing:
self.item_fn = self._get_packed_item
else:
self.item_fn = self._get_item
def __len__(self):
return len(self.list_data_dict)
@property
def lengths(self):
length_list = []
for sample in self.list_data_dict:
img_tokens = 128 if "image" in sample else 0
length_list.append(
sum(len(conv["value"].split()) for conv in sample["conversations"])
+ img_tokens
)
return length_list
@property
def modality_lengths(self):
length_list = []
for sample in self.list_data_dict:
cur_len = sum(
len(conv["value"].split()) for conv in sample["conversations"]
)
cur_len = (
cur_len if ("image" in sample) or ("video" in sample) else -cur_len
)
length_list.append(cur_len)
return length_list
@property
def pre_calculated_length(self):
if "num_tokens" in self.list_data_dict[0]:
length_list = [sample["num_tokens"] for sample in self.list_data_dict]
return np.array(length_list)
else:
print("No pre-calculated length available.")
return np.array([1] * len(self.list_data_dict))
def __getitem__(self, i) -> Dict[str, torch.Tensor]:
num_base_retries = 3
num_final_retries = 30
# try the current sample first
for attempt_idx in range(num_base_retries):
try:
sources = self.list_data_dict[i]
if isinstance(sources, dict):
sources = [sources]
sample = self.item_fn(sources)
return sample
except Exception as e:
# sleep 1s in case it is a cloud disk issue
print(f"[Try #{attempt_idx}] Failed to fetch sample {i}. Exception:", e)
time.sleep(1)
# try other samples, in case it is file corruption issue
for attempt_idx in range(num_base_retries):
try:
next_index = min(i + 1, len(self.list_data_dict) - 1)
sources = self.list_data_dict[next_index]
if isinstance(sources, dict):
sources = [sources]
sample = self.item_fn(sources)
return sample
except Exception as e:
# no need to sleep
print(
f"[Try other #{attempt_idx}] Failed to fetch sample {next_index}. Exception:",
e,
)
pass
try:
sources = self.list_data_dict[i]
if isinstance(sources, dict):
sources = [sources]
sample = self.item_fn(sources)
return sample
except Exception as e:
raise e
def _get_item(self, sources) -> Dict[str, torch.Tensor]:
data_dict = preprocess_qwen_visual(
sources,
self.processor,
)
seq_len = data_dict["input_ids"][0].size(0)
if "image_grid_thw" in data_dict:
grid_thw = data_dict.get("image_grid_thw")
if not isinstance(grid_thw, Sequence):
grid_thw = [grid_thw]
else:
grid_thw = None
if "video_grid_thw" in data_dict:
video_grid_thw = data_dict.get("video_grid_thw")
if not isinstance(video_grid_thw, Sequence):
video_grid_thw = [video_grid_thw]
second_per_grid_ts = [
self.processor.video_processor.temporal_patch_size
/ self.processor.video_processor.fps
] * len(video_grid_thw)
else:
video_grid_thw = None
second_per_grid_ts = None
position_ids, _ = self.get_rope_index(
self.merge_size,
data_dict["input_ids"],
image_grid_thw=torch.cat(grid_thw, dim=0) if grid_thw else None,
video_grid_thw=(
torch.cat(video_grid_thw, dim=0) if video_grid_thw else None
),
second_per_grid_ts=second_per_grid_ts if second_per_grid_ts else None,
)
data_dict["position_ids"] = position_ids
data_dict["attention_mask"] = [seq_len]
text = self.processor.tokenizer.decode(
data_dict["input_ids"][0], skip_special_tokens=False
)
labels = data_dict["labels"][0]
labels = [
tid if tid != -100 else self.processor.tokenizer.pad_token_id
for tid in labels
]
label = self.processor.tokenizer.decode(labels, skip_special_tokens=False)
return data_dict
def _get_packed_item(self, sources) -> Dict[str, torch.Tensor]:
if isinstance(sources, dict):
if isinstance(source, dict):
sources = [sources]
assert len(sources) == 1, "Don't know why it is wrapped to a list" # FIXME
return self._get_item(sources)
if isinstance(sources, list):
data_list = []
new_data_dict = {}
for source in sources:
if isinstance(source, dict):
source = [source]
assert (
len(source) == 1
), f"Don't know why it is wrapped to a list.\n {source}" # FIXME
data_list.append(self._get_item(source))
input_ids = torch.cat([d["input_ids"] for d in data_list], dim=1)
labels = torch.cat([d["labels"] for d in data_list], dim=1)
position_ids = torch.cat([d["position_ids"] for d in data_list], dim=2)
attention_mask = [
d["attention_mask"][0] for d in data_list if "attention_mask" in d
]
new_data_dict = {
"input_ids": input_ids,
"labels": labels,
"position_ids": position_ids,
"attention_mask": attention_mask if attention_mask else None,
}
if any("pixel_values" in d for d in data_list):
new_data_dict.update(
{
"pixel_values": torch.cat(
[
d["pixel_values"]
for d in data_list
if "pixel_values" in d
],
dim=0,
),
"image_grid_thw": torch.cat(
[
d["image_grid_thw"]
for d in data_list
if "image_grid_thw" in d
],
dim=0,
),
}
)
if any("pixel_values_videos" in d for d in data_list):
new_data_dict.update(
{
"pixel_values_videos": torch.cat(
[
d["pixel_values_videos"]
for d in data_list
if "pixel_values_videos" in d
],
dim=0,
),
"video_grid_thw": torch.cat(
[
d["video_grid_thw"]
for d in data_list
if "video_grid_thw" in d
],
dim=0,
),
}
)
return new_data_dict
def pad_and_cat(tensor_list):
max_length = max(tensor.shape[2] for tensor in tensor_list)
padded_tensors = []
for tensor in tensor_list:
pad_length = max_length - tensor.shape[2]
padded_tensor = torch.nn.functional.pad(tensor, (0, pad_length), "constant", 1)
padded_tensors.append(padded_tensor)
stacked_tensor = torch.cat(padded_tensors, dim=1)
return stacked_tensor
@dataclass
class DataCollatorForSupervisedDataset(object):
"""Collate examples for supervised fine-tuning."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels, position_ids = tuple(
[instance[key] for instance in instances]
for key in ("input_ids", "labels", "position_ids")
)
input_ids = [ids.squeeze(0) for ids in input_ids]
labels = [ids.squeeze(0) for ids in labels]
input_ids = torch.nn.utils.rnn.pad_sequence(
input_ids, batch_first=True, padding_value=self.tokenizer.pad_token_id
)
labels = torch.nn.utils.rnn.pad_sequence(
labels, batch_first=True, padding_value=IGNORE_INDEX
)
position_ids = pad_and_cat(position_ids)
input_ids = input_ids[:, : self.tokenizer.model_max_length]
labels = labels[:, : self.tokenizer.model_max_length]
position_ids = position_ids[:, :, : self.tokenizer.model_max_length]
batch = dict(
input_ids=input_ids,
labels=labels,
attention_mask=input_ids.ne(self.tokenizer.pad_token_id),
)
images = list(
instance["pixel_values"]
for instance in instances
if "pixel_values" in instance
)
videos = list(
instance["pixel_values_videos"]
for instance in instances
if "pixel_values_videos" in instance
)
if len(images) != 0:
concat_images = torch.cat([image for image in images], dim=0)
grid_thw = [
instance["image_grid_thw"]
for instance in instances
if "image_grid_thw" in instance
]
grid_thw = torch.cat(grid_thw, dim=0)
else:
concat_images = None
grid_thw = None
if len(videos) != 0:
concat_videos = torch.cat([video for video in videos], dim=0)
video_grid_thw = [
instance["video_grid_thw"]
for instance in instances
if "video_grid_thw" in instance
]
video_grid_thw = torch.cat(video_grid_thw, dim=0)
else:
concat_videos = None
video_grid_thw = None
batch["pixel_values"] = concat_images
batch["image_grid_thw"] = grid_thw
batch["pixel_values_videos"] = concat_videos
batch["video_grid_thw"] = video_grid_thw
batch["position_ids"] = position_ids
return batch
@dataclass
class FlattenedDataCollatorForSupervisedDataset(DataCollatorForSupervisedDataset):
"""Collate examples into packed sequence with multi-modal support."""
tokenizer: transformers.PreTrainedTokenizer
def __call__(self, instances: Sequence[Dict]) -> Dict[str, torch.Tensor]:
input_ids, labels, position_ids, attention_mask = tuple(
[instance[key] for instance in instances]
for key in ("input_ids", "labels", "position_ids", "attention_mask")
)
attention_mask = list(
itertools.chain(
*(
instance["attention_mask"]
for instance in instances
if "attention_mask" in instance
)
)
)
seq_lens = torch.tensor([0] + attention_mask, dtype=torch.int32)
cumsum_seq_lens = torch.cumsum(seq_lens, dim=0, dtype=torch.int32)
input_ids = torch.cat(input_ids, dim=1)
labels = torch.cat(labels, dim=1)
position_ids = torch.cat(position_ids, dim=2)
batch = dict(
input_ids=input_ids,
labels=labels,
attention_mask=cumsum_seq_lens,
position_ids=position_ids,
)
images = list(
instance["pixel_values"]
for instance in instances
if "pixel_values" in instance
)
videos = list(
instance["pixel_values_videos"]
for instance in instances
if "pixel_values_videos" in instance
)
if len(images) != 0:
concat_images = torch.cat([image for image in images], dim=0)
grid_thw = [
instance["image_grid_thw"]
for instance in instances
if "image_grid_thw" in instance
]
grid_thw = torch.cat(grid_thw, dim=0)
else:
concat_images = None
grid_thw = None
if len(videos) != 0:
concat_videos = torch.cat([video for video in videos], dim=0)
video_grid_thw = [
instance["video_grid_thw"]
for instance in instances
if "video_grid_thw" in instance
]
video_grid_thw = torch.cat(video_grid_thw, dim=0)
else:
concat_videos = None
video_grid_thw = None
batch["pixel_values"] = concat_images
batch["image_grid_thw"] = grid_thw
batch["pixel_values_videos"] = concat_videos
batch["video_grid_thw"] = video_grid_thw
return batch
def make_supervised_data_module(processor, data_args) -> Dict:
"""Make dataset and collator for supervised fine-tuning."""
train_dataset = LazySupervisedDataset(processor, data_args=data_args)
if data_args.data_flatten or data_args.data_packing:
data_collator = FlattenedDataCollatorForSupervisedDataset(processor.tokenizer)
return dict(
train_dataset=train_dataset, eval_dataset=None, data_collator=data_collator
)
data_collator = DataCollatorForSupervisedDataset(processor.tokenizer)
return dict(
train_dataset=train_dataset, eval_dataset=None, data_collator=data_collator
)
if __name__ == "__main__":
pass
|