forked from baidu-baige/LoongForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvlm_task_encoder.py
More file actions
848 lines (759 loc) · 32.4 KB
/
Copy pathvlm_task_encoder.py
File metadata and controls
848 lines (759 loc) · 32.4 KB
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
# Copyright 2026 The LoongForge Authors.
# SPDX-License-Identifier: Apache-2.0
"""VLMTaskEncoder class."""
import torch
from typing import Dict, List, Optional, Tuple, Union
from typing_extensions import override
from dataclasses import dataclass
from megatron.energon import (
CaptioningSample,
VQASample,
)
from importlib.metadata import version
if version('megatron-energon') < "7.0.0":
from megatron.energon.flavors.webdataset import VideoData as AVData
_ENERGON_NEEDS_SUBFLAVOR = True
else:
from megatron.energon.flavors.webdataset import AVData
_ENERGON_NEEDS_SUBFLAVOR = False
from megatron.energon.task_encoder.base import stateless
from transformers import AutoProcessor
from loongforge.utils import constants, get_chat_template
from qwen_vl_utils.vision_process import smart_nframes, smart_resize
from torchvision import transforms
from torchvision.transforms import InterpolationMode
from .base.task_encoder import (
BaseTaskEncoder,
BaseTaskSample,
BaseTaskSamplePacked,
BaseTaskBatchPacked,
_parse_messages,
)
from loongforge.data.chat_template import HFChatTemplate
from loongforge.data.multimodal import (
MultiMixQASample,
PackedCaptioningSample,
PackedVQASample,
PackedMultiMixQASample,
PackedChatMixSample,
ChatMixSample,
)
IGNORE_INDEX = -100 # ID for labels that should be ignored.
IMAGE_TOKEN = "<|image_pad|>"
VIDEO_TOKEN = "<|video_pad|>"
VISION_TAGS = ["<|vision_start|>", "<|vision_end|>"]
IMAGE_TOKEN_WITH_TAGS = VISION_TAGS[0] + IMAGE_TOKEN + VISION_TAGS[1]
VIDEO_TOKEN_WITH_TAGS = VISION_TAGS[0] + VIDEO_TOKEN + VISION_TAGS[1]
@dataclass
class VLMTaskSample(BaseTaskSample):
"""An image task sample with a grid of tokens and their corresponding pixel values."""
image_grid_thw: Optional[torch.Tensor] = None
video_grid_thw: Optional[torch.Tensor] = None
def __init__(self, image_grid_thw: str, video_grid_thw=None, **kwargs):
super().__init__(**kwargs)
self.image_grid_thw = image_grid_thw
self.video_grid_thw = video_grid_thw
@dataclass
class VLMTaskSamplePacked(BaseTaskSamplePacked):
"""An image task sample with a grid of tokens and their corresponding pixel values."""
image_grid_thw: Optional[torch.Tensor] = None
video_grid_thw: Optional[torch.Tensor] = None
def __init__(
self, sample: BaseTaskSample, image_grid_thw: str, video_grid_thw=None
):
init_args = vars(sample).copy()
init_args.update({
'__key__': sample.__key__,
'__restore_key__': sample.__restore_key__,
'__subflavors__': sample.__subflavors__
})
super().__init__(**init_args)
self.image_grid_thw = image_grid_thw
self.video_grid_thw = video_grid_thw
def __repr__(self):
base = super().__repr__() if hasattr(super(), "__repr__") else ""
grid_str = ""
if self.image_grid_thw is not None:
grid_str += (
f", image_grid_thw“="
f"{tuple(self.image_grid_thw.shape) if hasattr(self.image_grid_thw, 'shape') else self.image_grid_thw}"
)
if self.video_grid_thw is not None:
grid_str += (
f", video_grid_thw="
f"{tuple(self.video_grid_thw.shape) if hasattr(self.video_grid_thw, 'shape') else self.video_grid_thw}"
)
return base[:-1] + grid_str + ")"
@dataclass
class VLMTaskBatchPacked(BaseTaskBatchPacked):
"""An image task sample with a grid of tokens and their corresponding pixel values."""
image_grid_thw: Optional[torch.Tensor] = None
video_grid_thw: Optional[torch.Tensor] = None
def __init__(
self, sample: BaseTaskSample, image_grid_thw: str, video_grid_thw=None
):
init_args = vars(sample).copy()
init_args.update({
'__key__': sample.__key__,
'__restore_key__': sample.__restore_key__,
'__subflavors__': sample.__subflavors__
})
super().__init__(**init_args)
self.image_grid_thw = image_grid_thw
self.video_grid_thw = video_grid_thw
class VLMTaskEncoder(BaseTaskEncoder):
"""A simple task encoder for VLMs."""
def __init__(self, args):
super().__init__()
if args.training_phase in ['sft']:
self.chat_template = get_chat_template()
self.processor = AutoProcessor.from_pretrained(self.args.hf_tokenizer_path, trust_remote_code=True)
if args.image_resolution:
setattr(self.processor, "image_resolution", args.image_resolution)
# video
self.frame_min_pixels = args.frame_min_pixels
self.frame_max_pixels = args.frame_max_pixels
self.video_max_pixels = args.video_max_pixels
self.fps = args.fps
self.fps_min_frames = args.fps_min_frames
self.fps_max_frames = args.fps_max_frames
# image
self.min_pixels = args.min_pixels
self.max_pixels = args.max_pixels
def _resize_video(self, vision: AVData, image_factor=28, frame_factor=2):
"""Resize video: frame number, height, width"""
if _ENERGON_NEEDS_SUBFLAVOR:
total_frames = len(vision.frames)
video_fps = vision.info["video_fps"]
vision.info["fps"] = self.fps
vision.info["min_frames"] = self.fps_min_frames
vision.info["max_frames"] = self.fps_max_frames
nframes = smart_nframes(
vision.info, total_frames=total_frames, video_fps=video_fps
)
idx = torch.linspace(0, total_frames - 1, nframes).round().long()
video = vision.frames[idx]
else:
_, total_frames = vision.get_video_duration(get_frame_count=True)
video_fps = vision.get_video_fps()
if not hasattr(vision, "info") or vision.info is None:
vision.info = {}
vision.info["video_fps"] = video_fps
vision.info["fps"] = self.fps
vision.info["min_frames"] = self.fps_min_frames
vision.info["max_frames"] = self.fps_max_frames
# resize frame
nframes = smart_nframes(
vision.info, total_frames=total_frames, video_fps=video_fps
)
idx = torch.linspace(0, total_frames - 1, nframes).round().long()
frame_ranges = [(int(i), int(i) + 1) for i in idx.tolist()]
clips = vision.get_clips(video_clip_ranges=frame_ranges, video_unit="frames")
video = torch.stack([clip[0] for clip in clips.video_clips], dim=0)
# resize height, width
nframes, _, height, width = video.shape
resized_height, resized_width = smart_resize(
height,
width,
factor=image_factor,
min_pixels=int(self.frame_min_pixels * 1.05),
max_pixels=min(
self.frame_max_pixels, self.video_max_pixels / nframes * frame_factor
),
)
video = transforms.functional.resize(
video,
[resized_height, resized_width],
interpolation=InterpolationMode.BICUBIC,
antialias=True,
).float()
return video
def _resize_image(self, image, size_factor=28):
resized_height, resized_width = smart_resize(
image.height,
image.width,
factor=size_factor,
min_pixels=self.min_pixels,
max_pixels=self.max_pixels,
)
image = image.resize((resized_width, resized_height))
return image
def _process(self, image, text):
""" " Process the data to get the model's input"""
inputs = self.processor(
text=text,
images=image,
padding=True,
return_tensors="pt",
)
input_ids = inputs["input_ids"][0]
attn_mask = inputs["attention_mask"][0].logical_not()
image_grid_thw = None
pixel = []
if image is not None:
image_grid_thw = inputs["image_grid_thw"] # [t,h,w]
pixel = [inputs["pixel_values"]] # [hw, 2*3*14*14]
target = input_ids.clone()
vision_start_id, img_pad_id, vision_end_id = (
self.tokenizer.convert_tokens_to_ids(
[VISION_TAGS[0], IMAGE_TOKEN, VISION_TAGS[1]]
)
)
target[target == vision_start_id] = IGNORE_INDEX
target[target == img_pad_id] = IGNORE_INDEX
target[target == vision_end_id] = IGNORE_INDEX
return input_ids, target, pixel, image_grid_thw, attn_mask
def process_sft_vqa(self, context, answer, image):
"""process the data for sft vqa"""
text = self.processor.apply_chat_template(
[
{"role": "user", "content": context},
{"role": "assistant", "content": answer},
],
tokenize=False,
).replace("<image>", IMAGE_TOKEN_WITH_TAGS)
if text[-1] == "\n":
text = text[:-1]
input_ids, _, imgs, image_grid_thw, attn_mask = self._process(image, text)
target = torch.ones_like(input_ids) * IGNORE_INDEX
answer_ids = self.tokenizer.tokenize(answer)
target[-len(answer_ids) - 1 : -1] = torch.tensor(answer_ids)
target[-1] = input_ids[-1]
return input_ids, target, attn_mask, imgs, image_grid_thw
def process_sft_qa(
self, messages: list, system: str, raw_video: list, raw_image: list, tools=None
):
"""process the data for sft qa"""
video_grid_thw = None
pixel_values_videos = []
image_grid_thw = None
pixel_values_images = []
video = []
image = []
if raw_image is not None:
for i in raw_image:
image.append(self._resize_image(i))
if raw_video is not None:
for v in raw_video:
video.append(self._resize_video(v))
messages, mm_inputs = self.chat_template.mm_plugin.process_messages(
messages,
image if image is not None else [],
video if raw_video is not None else [],
self.processor,
)
if raw_video is not None:
video_grid_thw = mm_inputs["video_grid_thw"]
pixel_values_videos = [mm_inputs["pixel_values_videos"]]
if raw_image is not None:
image_grid_thw = mm_inputs["image_grid_thw"]
pixel_values_images = [mm_inputs["pixel_values"]]
encode_pairs = self.chat_template.encode_multiturn(
tokenizer=self.tokenizer,
messages=messages,
system=system,
)
input_ids, target = [], []
for turn_idx, (source_ids, target_ids) in enumerate(encode_pairs):
input_ids += source_ids + target_ids
target += [IGNORE_INDEX] * len(source_ids) + target_ids
input_ids = torch.tensor(input_ids)
target = torch.tensor(target)
attn_mask = torch.zeros_like(input_ids).bool()
return (
input_ids,
target,
attn_mask,
pixel_values_images,
image_grid_thw,
pixel_values_videos,
video_grid_thw,
)
def _make_sample_from(self, sample, *, cls=None, key=None, **fields):
"""Derive a new sample from ``sample``, carrying its energon meta.
Forwards the source's ``__key__`` / ``__restore_key__`` /
``__subflavors__`` (and ``__subflavor__`` on energon < 7.0) into a
freshly constructed ``cls`` instance. Used both for the final task
sample (``cls`` defaults to ``VLMTaskSample``) and for upstream
flavor samples re-fed into another encoder (e.g. ``ChatMixSample``).
Pass ``key`` to override ``sample.__key__`` (e.g. per-turn sub-keys).
"""
if cls is None:
cls = VLMTaskSample
meta = {
"__key__": key if key is not None else sample.__key__,
"__restore_key__": sample.__restore_key__,
"__subflavors__": sample.__subflavors__,
}
if _ENERGON_NEEDS_SUBFLAVOR:
meta["__subflavor__"] = None
return cls(**meta, **fields)
def encode_captioning(self, sample: CaptioningSample) -> BaseTaskSample:
"""Encode CaptioningSample."""
"""Preprocessing function for datasets like COCO, containing image-caption pairs.
See Energon codebase for more details on CaptioningSample.
https://github.com/NVIDIA/Megatron-Energon/blob/develop/src/megatron/energon/flavors/captioning.py
"""
assert self.args.training_phase == constants.TrainingPhase.PRETRAIN, "Only support PRETRAIN phase"
text = (
IMAGE_TOKEN_WITH_TAGS + sample.caption + self.tokenizer.tokenizer.eos_token
)
input_ids, target, imgs, image_grid_thw, attn_mask = self._process(
sample.image, text
)
num_tiles = [len(image_grid_thw)]
if self.args.enable_discard_sample:
assert len(input_ids) <= self.args.seq_length, f"{sample.__key__} input length {len(input_ids)}"
else:
assert image_grid_thw.prod() / 4 <= self.args.seq_length, f"{sample.__key__} thw {image_grid_thw}"
return self._make_sample_from(
sample,
imgs=imgs,
image_grid_thw=image_grid_thw,
num_tiles=num_tiles,
tokens=input_ids,
labels=target,
attn_mask=attn_mask,
total_len=len(input_ids),
)
def encode_vqa(self, sample: VQASample) -> BaseTaskSample:
"""Encode pretrain sample in Qwen2VL style."""
if self.args.training_phase == constants.TrainingPhase.PRETRAIN:
if self.args.add_question_in_pretrain:
text = (sample.context + sample.answers).replace(
"<image>", IMAGE_TOKEN_WITH_TAGS
)
else:
text = IMAGE_TOKEN_WITH_TAGS + sample.answers
text = text + self.tokenizer.tokenizer.eos_token
input_ids, target, imgs, image_grid_thw, attn_mask = self._process(sample.image, text)
elif self.args.training_phase == constants.TrainingPhase.SFT:
input_ids, target, attn_mask, imgs, image_grid_thw = self.process_sft_vqa(sample.context, \
sample.answers, sample.image)
else:
raise NotImplementedError(f"Unknown training phase {self.args.training_phase}")
num_tiles = [len(image_grid_thw)]
if self.args.enable_discard_sample:
assert len(input_ids) <= self.args.seq_length, f"{sample.__key__} input length {len(input_ids)}"
else:
assert image_grid_thw.prod() / 4 <= self.args.seq_length, f"{sample.__key__} grid_thw: {image_grid_thw}"
return self._make_sample_from(
sample,
imgs=imgs,
image_grid_thw=image_grid_thw,
num_tiles=num_tiles,
tokens=input_ids,
labels=target,
attn_mask=attn_mask,
total_len=len(input_ids),
)
def encode_multi_vid_qa(self, sample: VQASample) -> BaseTaskSample:
"""Encode sample in Qwen2VL style."""
if self.args.training_phase == constants.TrainingPhase.SFT:
(
input_ids,
target,
attn_mask,
imgs,
image_grid_thw,
video,
video_grid_thw,
) = self.process_sft_qa(
sample.messages,
sample.system,
sample.video,
None,
tools=getattr(sample, "tools", None),
)
else:
raise NotImplementedError(
f"Unknown training phase {self.args.training_phase}"
)
if self.args.enable_discard_sample:
assert (
len(input_ids) <= self.args.seq_length
), f"{sample.__key__} input length {len(input_ids)}"
else:
assert (
video_grid_thw.prod(dim=-1).sum() / 4 <= self.args.seq_length
), f"{sample.__key__} grid_thw: {video_grid_thw}"
return self._make_sample_from(
sample,
imgs=imgs,
image_grid_thw=image_grid_thw,
pixel_values_videos=video,
video_grid_thw=video_grid_thw,
num_tiles=[len(video_grid_thw)],
tokens=input_ids,
labels=target,
attn_mask=attn_mask,
total_len=len(input_ids),
)
def encode_multi_mix_qa(self, sample: MultiMixQASample) -> BaseTaskSample:
"""Encode sample in Qwen2VL style."""
if self.args.training_phase == constants.TrainingPhase.SFT:
num_tiles = []
(
input_ids,
target,
attn_mask,
imgs,
image_grid_thw,
pixel_values_videos,
video_grid_thw,
) = self.process_sft_qa(
sample.messages,
sample.system,
sample.video,
sample.image,
tools=getattr(sample, "tools", None),
)
if sample.video is not None:
num_tiles = [len(video_grid_thw)]
elif sample.image is not None:
num_tiles = [len(image_grid_thw)]
else:
raise NotImplementedError(
f"Unknown training phase {self.args.training_phase}"
)
if self.args.enable_discard_sample:
assert (
len(input_ids) <= self.args.seq_length
), f"{sample.__key__} input length {len(input_ids)}"
elif sample.video is not None:
assert (
video_grid_thw.prod(dim=-1).sum() / 4 <= self.args.seq_length
), f"{sample.__key__} grid_thw: {video_grid_thw}"
elif sample.image is not None:
assert (
image_grid_thw.prod(dim=-1).sum() / 4 <= self.args.seq_length
), f"{sample.__key__} grid_thw: {image_grid_thw}"
return self._make_sample_from(
sample,
imgs=imgs,
image_grid_thw=image_grid_thw,
pixel_values_videos=pixel_values_videos,
video_grid_thw=video_grid_thw,
num_tiles=num_tiles,
tokens=input_ids,
labels=target,
attn_mask=attn_mask,
total_len=len(input_ids),
)
def encode_chat_mix(self, sample: ChatMixSample) -> Optional[BaseTaskSample]:
"""Encode chat-format multimodal sample (with optional tool calling)."""
if self.args.training_phase != constants.TrainingPhase.SFT:
raise NotImplementedError(
f"encode_chat_mix only supports SFT, got {self.args.training_phase}"
)
(
input_ids,
target,
attn_mask,
imgs,
image_grid_thw,
pixel_values_videos,
video_grid_thw,
) = self.process_sft_qa(
sample.messages,
sample.system,
sample.video,
sample.image,
tools=sample.tools,
)
num_tiles = []
if sample.video is not None:
num_tiles = [len(video_grid_thw)]
elif sample.image is not None:
num_tiles = [len(image_grid_thw)]
if self.args.enable_discard_sample:
assert (
len(input_ids) <= self.args.seq_length
), f"{sample.__key__} input length {len(input_ids)}"
elif sample.video is not None:
assert (
video_grid_thw.prod(dim=-1).sum() / 4 <= self.args.seq_length
), f"{sample.__key__} grid_thw: {video_grid_thw}"
elif sample.image is not None:
assert (
image_grid_thw.prod(dim=-1).sum() / 4 <= self.args.seq_length
), f"{sample.__key__} grid_thw: {image_grid_thw}"
return self._make_sample_from(
sample,
imgs=imgs,
image_grid_thw=image_grid_thw,
pixel_values_videos=pixel_values_videos,
video_grid_thw=video_grid_thw,
num_tiles=num_tiles,
tokens=input_ids,
labels=target,
attn_mask=attn_mask,
total_len=len(input_ids),
)
def encode_packed_captioning(
self, sample: PackedCaptioningSample
) -> BaseTaskSample:
"""Generates an encoded multimodal packed captioning sample from a raw sample."""
n_orig_sample = len(sample.images)
l_VLMTaskSample = []
for idx in range(n_orig_sample):
if _ENERGON_NEEDS_SUBFLAVOR:
cur_capsample = CaptioningSample(
__key__=f"{sample.__key__}.img{idx:03d}_jpg",
__restore_key__=sample.__restore_key__,
__subflavor__=None,
__subflavors__=sample.__subflavors__,
image=sample.images[idx],
caption=sample.captions[idx],
)
else:
cur_capsample = CaptioningSample(
__key__=f"{sample.__key__}.img{idx:03d}_jpg",
__restore_key__=sample.__restore_key__,
__subflavors__=sample.__subflavors__,
image=sample.images[idx],
caption=sample.captions[idx],
)
l_VLMTaskSample.append(self.encode_captioning(cur_capsample))
l_sample_packed = self.pack_selected_samples(l_VLMTaskSample)
self.is_packing_enabled = True
return l_sample_packed
def encode_packed_vqa(self, sample: PackedVQASample) -> BaseTaskSample:
"""Generates an encoded multimodal packed vqa sample from a raw sample."""
n_orig_sample = len(sample.images)
l_VLMTaskSample = []
for idx in range(n_orig_sample):
if _ENERGON_NEEDS_SUBFLAVOR:
cur_capsample = VQASample(
__key__=f"{sample.__key__}.img{idx:03d}_jpg",
__restore_key__=sample.__restore_key__,
__subflavor__=None,
__subflavors__=sample.__subflavors__,
image=sample.images[idx],
answers=sample.answers[idx],
context=sample.contexts[idx],
)
else:
cur_capsample = VQASample(
__key__=f"{sample.__key__}.img{idx:03d}_jpg",
__restore_key__=sample.__restore_key__,
__subflavors__=sample.__subflavors__,
image=sample.images[idx],
answers=sample.answers[idx],
context=sample.contexts[idx],
)
l_VLMTaskSample.append(self.encode_vqa(cur_capsample))
l_sample_packed = self.pack_selected_samples(l_VLMTaskSample)
self.is_packing_enabled = True
return l_sample_packed
def encode_packed_multi_mix_qa(
self, sample: PackedMultiMixQASample
) -> BaseTaskSample:
"""Generates an encoded multimodal packed multi mix qa sample from a raw sample."""
n_orig_sample = len(sample.contexts)
l_VLMTaskSample = []
images = sample.images if sample.images is not None else []
videos = sample.videos if sample.videos is not None else []
has_images = len(images) > 0
has_videos = len(videos) > 0
if has_images and has_videos:
raise ValueError(
f"encode_packed_multi_mix_qa: cannot mix images and videos in same sample for key={sample.__key__}"
)
has_text_only = not has_images and not has_videos
media_list = images if has_images else videos
media_type = "image" if has_images else ("video" if has_videos else "text")
if not has_text_only and len(media_list) != n_orig_sample:
raise ValueError(
f"encode_packed_multi_mix_qa: media count ({len(media_list)}) "
f"!= context count ({n_orig_sample}) for key={sample.__key__}"
)
for idx in range(n_orig_sample):
context = sample.contexts[idx] # str
media_group = None if has_text_only else media_list[idx] # List[Tensor] or List[AVData]
answer_group = sample.answers[idx] if sample.answers else [] # List[str]
if isinstance(answer_group, list):
answer = "\n\n".join(answer_group) if answer_group else ""
else:
answer = answer_group or ""
system = None
messages = [
{"role": "user", "content": context},
{"role": "assistant", "content": answer},
]
if has_images:
init_kwargs = {
"__key__": f"{sample.__key__}.q{idx:03d}",
"__restore_key__": sample.__restore_key__,
"__subflavors__": sample.__subflavors__,
"messages": messages,
"image": media_group,
"video": None,
"system": system,
}
if _ENERGON_NEEDS_SUBFLAVOR:
init_kwargs["__subflavor__"] = None
cur_sample = MultiMixQASample(**init_kwargs)
elif has_videos:
init_kwargs = {
"__key__": f"{sample.__key__}.q{idx:03d}",
"__restore_key__": sample.__restore_key__,
"__subflavors__": sample.__subflavors__,
"messages": messages,
"image": None,
"video": media_group, # List[AVData]
"system": system,
}
if _ENERGON_NEEDS_SUBFLAVOR:
init_kwargs["__subflavor__"] = None
cur_sample = MultiMixQASample(**init_kwargs)
else:
init_kwargs = {
"__key__": f"{sample.__key__}.q{idx:03d}",
"__restore_key__": sample.__restore_key__,
"__subflavors__": sample.__subflavors__,
"messages": messages,
"image": None,
"video": None,
"system": system,
}
if _ENERGON_NEEDS_SUBFLAVOR:
init_kwargs["__subflavor__"] = None
cur_sample = MultiMixQASample(**init_kwargs)
l_VLMTaskSample.append(self.encode_multi_mix_qa(cur_sample))
l_sample_packed = self.pack_selected_samples(l_VLMTaskSample)
self.is_packing_enabled = True
return l_sample_packed
def encode_packed_chat_mix(
self,
sample: PackedChatMixSample,
) -> BaseTaskSamplePacked:
"""Encode an offline-packed chat/tool-calling sample.
Generic skeleton: unpacks the offline artifact, re-encodes each turn
through ``self.encode_chat_mix``, and re-packs via
``pack_selected_samples``. Subclasses customise per-turn encoding by
overriding ``encode_chat_mix``; tool-calling support requires the
active chat template to be ``HFChatTemplate``.
"""
n_orig_sample = len(sample.packed_messages)
images = sample.packed_images if sample.packed_images is not None else []
videos = sample.packed_videos if sample.packed_videos is not None else []
has_images = len(images) > 0
has_videos = len(videos) > 0
if has_images and has_videos:
raise ValueError(
f"encode_packed_chat_mix: cannot mix images and videos "
f"in same sample for key={sample.__key__}"
)
has_text_only = not has_images and not has_videos
media_list = images if has_images else videos
if not has_text_only and len(media_list) != n_orig_sample:
raise ValueError(
f"encode_packed_chat_mix: media count ({len(media_list)}) "
f"!= messages count ({n_orig_sample}) for key={sample.__key__}"
)
encoded_members = []
for idx, raw_sample in enumerate(sample.packed_messages):
raw_sample = raw_sample or {}
raw_messages = raw_sample.get("messages") or raw_sample.get("texts")
if raw_messages is None:
raise ValueError(
f"packed_chat_mix sample {sample.__key__}.q{idx:03d} "
"has neither `messages` nor `texts`."
)
messages, system = _parse_messages(raw_messages)
tools = raw_sample.get("tools")
if tools and not isinstance(self.chat_template, HFChatTemplate):
raise ValueError(
f"packed_chat_mix turn {sample.__key__}.q{idx:03d} carries "
f"tool definitions but the active chat template "
f"({type(self.chat_template).__name__}) cannot render "
f"tool_calls. Use HFChatTemplate or strip tools from the data."
)
media_group = None if has_text_only else media_list[idx]
cur_sample = self._make_sample_from(
sample,
cls=ChatMixSample,
key=f"{sample.__key__}.q{idx:03d}",
messages=messages,
image=media_group if has_images else None,
video=media_group if has_videos else None,
system=system,
tools=tools if tools else None,
)
encoded = self.encode_chat_mix(cur_sample)
if encoded is None:
raise ValueError(
f"encode_packed_chat_mix: member {cur_sample.__key__} was "
f"dropped during encode_chat_mix. Offline packed artifacts "
f"must pre-validate that every member fits within seq_length."
)
encoded_members.append(encoded)
l_sample_packed = self.pack_selected_samples(encoded_members)
self.is_packing_enabled = True
return l_sample_packed
def process_samples_grid(self, samples):
"""concat grid_thw for image and video"""
image_grid_thw = [
x.image_grid_thw for x in samples if x.image_grid_thw is not None
]
video_grid_thw = [
x.video_grid_thw for x in samples if x.video_grid_thw is not None
]
if len(image_grid_thw) > 0:
image_grid_thw = torch.cat(image_grid_thw).to(dtype=torch.int32)
else:
image_grid_thw = None
if len(video_grid_thw) > 0:
video_grid_thw = torch.cat(video_grid_thw).to(dtype=torch.int32)
else:
video_grid_thw = None
return image_grid_thw, video_grid_thw
@override
@stateless
def pack_selected_samples(
self, samples: List[VLMTaskSample]
) -> List[VLMTaskSamplePacked]:
"""Pack selected samples into one big sample."""
image_grid_thw, video_grid_thw = self.process_samples_grid(samples)
return VLMTaskSamplePacked(
super().pack_selected_samples(samples),
image_grid_thw=image_grid_thw,
video_grid_thw=video_grid_thw,
)
@override
def batch(
self, samples: List[Union[VLMTaskSample, VLMTaskSamplePacked]]
) -> VLMTaskBatchPacked:
"""Batch samples together"""
image_grid_thw, video_grid_thw = self.process_samples_grid(samples)
return VLMTaskBatchPacked(
super().batch(samples),
image_grid_thw=image_grid_thw,
video_grid_thw=video_grid_thw,
)
@override
def process_images(
self, samples: List[Union[VLMTaskSample, VLMTaskSamplePacked]]
) -> torch.Tensor:
""" " Process the data to get the model's input"""
imgs = [img for s in samples if s.imgs is not None for img in s.imgs]
if len(imgs) > 0:
return torch.cat(imgs)
else:
return torch.tensor([[0]], dtype=torch.float32)
@override
def process_videos(
self, samples: List[Union[VLMTaskSample, VLMTaskSamplePacked]]
) -> torch.Tensor:
""" " Process the data to get the model's input"""
pixel_values_videos = [
pixel_values_video
for s in samples
if s.pixel_values_videos is not None
for pixel_values_video in s.pixel_values_videos
]
if len(pixel_values_videos) > 0:
return torch.cat(pixel_values_videos)
else:
return torch.tensor([[0]], dtype=torch.float32)