File size: 12,157 Bytes
431ffa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Multimodal tokenizer processor."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any

from Tokenizer.unified.encoded import EncodedToken

from .image_placeholders import (
    image_patch_count,
    image_placeholder_tokens,
)
from .video_placeholders import (
    video_patch_count,
    video_placeholder_tokens,
)
from .tokens import IMAGE_PLACEHOLDER, VIDEO_PLACEHOLDER


@dataclass
class MultimodalEncoding:
    input_ids: list[int]
    attention_mask: list[int]
    tokens: list[EncodedToken]
    image_token_spans: list[tuple[int, int]]
    video_token_spans: list[tuple[int, int]] = field(default_factory=list)
    images: list[Any] = field(default_factory=list)
    videos: list[Any] = field(default_factory=list)
    pixel_values: Any = None


@dataclass(frozen=True)
class _PlaceholderRecord:
    start: int
    end: int
    patch_count: int


class MultimodalProcessor:
    def __init__(
        self,
        tokenizer,
        image_processor=None,
        patch_size: int = 14,
        merge_size: int = 2,
        temporal_patch_size: int = 2,
    ):
        self.tokenizer = tokenizer
        self.image_processor = image_processor
        self.patch_size = patch_size
        self.merge_size = merge_size
        self.temporal_patch_size = temporal_patch_size

    def __call__(
        self,
        text: str,
        images: list[Any] | None = None,
        image_sizes: list[Any] | None = None,
        videos: list[Any] | None = None,
        video_sizes: list[Any] | None = None,
        add_bos: bool = False,
        add_eos: bool = False,
    ) -> MultimodalEncoding:
        image_list = list(images or [])
        sizes = list(image_sizes or [])
        video_list = list(videos or [])
        vsizes = list(video_sizes or [])

        n_img_holes = text.count(IMAGE_PLACEHOLDER)
        n_vid_holes = text.count(VIDEO_PLACEHOLDER)

        if len(image_list) != n_img_holes:
            raise ValueError(
                f"<image> placeholder count ({n_img_holes}) does not match "
                f"images length ({len(image_list)})"
            )
        if len(video_list) != n_vid_holes:
            raise ValueError(
                f"<video> placeholder count ({n_vid_holes}) does not match "
                f"videos length ({len(video_list)})"
            )
        if sizes and len(sizes) != n_img_holes:
            raise ValueError(
                f"<image> placeholder count ({n_img_holes}) does not match "
                f"image_sizes length ({len(sizes)})"
            )
        if vsizes and len(vsizes) != n_vid_holes:
            raise ValueError(
                f"<video> placeholder count ({n_vid_holes}) does not match "
                f"video_sizes length ({len(vsizes)})"
            )

        # Default sizes for missing entries: assume single-patch image/video.
        if not sizes and n_img_holes:
            sizes = [None] * n_img_holes
        if not vsizes and n_vid_holes:
            vsizes = [None] * n_vid_holes

        expanded, image_records, video_records, boundary_map = self._expand_placeholders(
            text, sizes, vsizes
        )

        result = self.tokenizer.encode_with_spans(
            expanded, add_bos=add_bos, add_eos=add_eos
        )
        tokens = self._annotate_patches(
            result.tokens, image_records, video_records, boundary_map
        )
        img_spans = self._collect_spans(
            tokens, "<image_start>", "<image_patch>", "<image_end>", image_records
        )
        vid_spans = self._collect_spans(
            tokens, "<video_start>", "<video_patch>", "<video_end>", video_records
        )

        # Invariant: attention_mask aligns with input_ids
        assert len(result.attention_mask) == len(result.input_ids)

        processed_images = image_list
        if self.image_processor is not None and image_list:
            processed_images = self.image_processor(image_list)

        return MultimodalEncoding(
            input_ids=result.input_ids,
            attention_mask=result.attention_mask,
            tokens=tokens,
            image_token_spans=img_spans,
            video_token_spans=vid_spans,
            images=processed_images,
            videos=video_list,
        )

    # ---- expansion ----

    def _expand_placeholders(
        self, text: str, image_sizes: list[Any], video_sizes: list[Any]
    ) -> tuple[str, list[_PlaceholderRecord], list[_PlaceholderRecord], list[int]]:
        out: list[str] = []
        boundary_map = [0]
        image_records: list[_PlaceholderRecord] = []
        video_records: list[_PlaceholderRecord] = []
        img_idx = 0
        vid_idx = 0
        i = 0
        while i < len(text):
            if text.startswith(IMAGE_PLACEHOLDER, i):
                n = self._patches_image(image_sizes[img_idx])
                end = i + len(IMAGE_PLACEHOLDER)
                image_records.append(_PlaceholderRecord(i, end, n))
                expanded = "".join(image_placeholder_tokens(n))
                out.append(expanded)
                boundary_map.extend([end] * len(expanded))
                img_idx += 1
                i = end
                continue
            if text.startswith(VIDEO_PLACEHOLDER, i):
                n = self._patches_video(video_sizes[vid_idx])
                end = i + len(VIDEO_PLACEHOLDER)
                video_records.append(_PlaceholderRecord(i, end, n))
                expanded = "".join(video_placeholder_tokens(n))
                out.append(expanded)
                boundary_map.extend([end] * len(expanded))
                vid_idx += 1
                i = end
                continue
            out.append(text[i])
            boundary_map.append(i + 1)
            i += 1
        return "".join(out), image_records, video_records, boundary_map

    def _expand_images(self, text: str, sizes: list[Any]) -> str:
        parts = text.split(IMAGE_PLACEHOLDER)
        if len(parts) == 1:
            return text
        out = [parts[0]]
        for i, size in enumerate(sizes):
            n = self._patches_image(size)
            out.append("".join(image_placeholder_tokens(n)))
            out.append(parts[i + 1])
        return "".join(out)

    def _expand_videos(self, text: str, sizes: list[Any]) -> str:
        parts = text.split(VIDEO_PLACEHOLDER)
        if len(parts) == 1:
            return text
        out = [parts[0]]
        for i, size in enumerate(sizes):
            n = self._patches_video(size)
            out.append("".join(video_placeholder_tokens(n)))
            out.append(parts[i + 1])
        return "".join(out)

    def _patches_image(self, size: Any) -> int:
        if size is None:
            return 1
        if isinstance(size, dict):
            w = int(size.get("width") or size.get("w"))
            h = int(size.get("height") or size.get("h"))
        else:
            w, h = int(size[0]), int(size[1])
        return image_patch_count(w, h, self.patch_size, self.merge_size)

    def _patches_video(self, size: Any) -> int:
        if size is None:
            return 1
        if isinstance(size, dict):
            f = int(size.get("frames") or size.get("num_frames"))
            w = int(size.get("width") or size.get("w"))
            h = int(size.get("height") or size.get("h"))
        else:
            f, w, h = int(size[0]), int(size[1]), int(size[2])
        return video_patch_count(
            f, w, h, self.patch_size, self.temporal_patch_size, self.merge_size
        )

    # ---- annotation & spans ----

    def _annotate_patches(
        self,
        tokens: list[EncodedToken],
        image_records: list[_PlaceholderRecord],
        video_records: list[_PlaceholderRecord],
        boundary_map: list[int],
    ) -> list[EncodedToken]:
        """Attach image_index/video_index metadata to patch tokens."""
        out: list[EncodedToken] = []
        img_idx = -1
        vid_idx = -1
        in_img = False
        in_vid = False
        for t in tokens:
            meta = t.metadata
            start, end = self._remap_offsets(t.start, t.end, boundary_map)
            if t.token == "<image_start>":
                img_idx += 1
                in_img = True
                meta = self._placeholder_metadata(meta, image_records, img_idx, "image_index")
            elif t.token == "<image_end>":
                meta = self._placeholder_metadata(meta, image_records, img_idx, "image_index")
                in_img = False
            elif t.token == "<image_patch>" and in_img:
                meta = self._placeholder_metadata(meta, image_records, img_idx, "image_index")
            elif t.token == "<video_start>":
                vid_idx += 1
                in_vid = True
                meta = self._placeholder_metadata(meta, video_records, vid_idx, "video_index")
            elif t.token == "<video_end>":
                meta = self._placeholder_metadata(meta, video_records, vid_idx, "video_index")
                in_vid = False
            elif t.token == "<video_patch>" and in_vid:
                meta = self._placeholder_metadata(meta, video_records, vid_idx, "video_index")
            if meta is not t.metadata:
                source_span = meta.get("source_span") if meta else None
                if source_span is not None:
                    start, end = int(source_span[0]), int(source_span[1])
                out.append(
                    EncodedToken(
                        t.id, t.token, t.track, start, end, t.surface, meta
                    )
                )
            elif start != t.start or end != t.end:
                out.append(
                    EncodedToken(
                        t.id, t.token, t.track, start, end, t.surface, t.metadata
                    )
                )
            else:
                out.append(t)
        return out

    def _remap_offsets(
        self, start: int, end: int, boundary_map: list[int]
    ) -> tuple[int, int]:
        if start == -1 and end == -1:
            return start, end
        if not boundary_map:
            return start, end
        start_idx = min(max(start, 0), len(boundary_map) - 1)
        end_idx = min(max(end, 0), len(boundary_map) - 1)
        return boundary_map[start_idx], boundary_map[end_idx]

    def _placeholder_metadata(
        self,
        meta: dict | None,
        records: list[_PlaceholderRecord],
        index: int,
        index_key: str,
    ) -> dict:
        merged = {**(meta or {}), index_key: index}
        if 0 <= index < len(records):
            record = records[index]
            merged["source_span"] = [record.start, record.end]
        return merged

    def _collect_spans(
        self,
        tokens: list[EncodedToken],
        start_tok: str,
        patch_tok: str,
        end_tok: str,
        records: list[_PlaceholderRecord],
    ) -> list[tuple[int, int]]:
        spans: list[tuple[int, int]] = []
        start: int | None = None
        for idx, t in enumerate(tokens):
            if t.token == start_tok:
                start = idx
            elif t.token == end_tok and start is not None:
                spans.append((start, idx + 1))
                start = None
        if len(spans) != len(records):
            raise ValueError(
                f"{start_tok}/{end_tok} span count ({len(spans)}) does not match "
                f"placeholder count ({len(records)})"
            )
        for i, (span_start, span_end) in enumerate(spans):
            patch_count = sum(1 for token in tokens[span_start:span_end] if token.token == patch_tok)
            if patch_count != records[i].patch_count:
                raise ValueError(
                    f"{patch_tok} count in span {i} is {patch_count}, "
                    f"expected {records[i].patch_count}"
                )
            if span_end - span_start != records[i].patch_count + 2:
                raise ValueError(
                    f"{start_tok}/{end_tok} span length for placeholder {i} is "
                    f"{span_end - span_start}, expected {records[i].patch_count + 2}"
                )
        return spans