File size: 8,013 Bytes
810f8b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Geometry helpers shared by Qwen3.5 True MGVB and multimodal M-RoPE."""

from __future__ import annotations

from dataclasses import dataclass

import torch


@dataclass(frozen=True, order=True)
class SpatialCell:
    temporal: int
    top: int
    left: int
    bottom: int
    right: int

    @property
    def area(self) -> int:
        return (self.bottom - self.top) * (self.right - self.left)

    @property
    def height(self) -> int:
        return self.bottom - self.top

    @property
    def width(self) -> int:
        return self.right - self.left


def merged_grid_shape(grid_thw, spatial_merge_size: int) -> tuple[int, int, int]:
    values = grid_thw.detach().to(device="cpu", dtype=torch.long).tolist() if isinstance(grid_thw, torch.Tensor) else list(grid_thw)
    if len(values) != 3:
        raise ValueError(f"grid_thw must contain exactly three values, got {values!r}")
    temporal, height, width = (int(value) for value in values)
    merge = int(spatial_merge_size)
    if temporal < 1 or height < 1 or width < 1 or merge < 1:
        raise ValueError(f"invalid visual grid={values!r}, spatial_merge_size={merge}")
    if height % merge or width % merge:
        raise ValueError(f"visual grid {values!r} is not divisible by spatial_merge_size={merge}")
    return temporal, height // merge, width // merge


def native_visual_token_count(grid_thw, spatial_merge_size: int) -> int:
    temporal, height, width = merged_grid_shape(grid_thw, spatial_merge_size)
    return temporal * height * width


def compact_visual_token_count(grid_thw, spatial_merge_size: int, token_budget: int) -> int:
    budget = int(token_budget)
    if budget < 1:
        raise ValueError(f"MGVB token budget must be positive, got {budget}")
    return min(native_visual_token_count(grid_thw, spatial_merge_size), budget)


def native_visual_positions(grid_thw, spatial_merge_size: int, *, device=None) -> torch.Tensor:
    temporal, height, width = merged_grid_shape(grid_thw, spatial_merge_size)
    t = torch.arange(temporal, device=device, dtype=torch.long).view(-1, 1, 1)
    h = torch.arange(height, device=device, dtype=torch.long).view(1, -1, 1)
    w = torch.arange(width, device=device, dtype=torch.long).view(1, 1, -1)
    return torch.stack(
        [
            t.expand(temporal, height, width),
            h.expand(temporal, height, width),
            w.expand(temporal, height, width),
        ],
        dim=-1,
    ).reshape(-1, 3)


def partition_spatial_grid(
    temporal: int,
    height: int,
    width: int,
    cell_count: int,
) -> list[SpatialCell]:
    """Split a THW grid into exactly ``cell_count`` deterministic non-empty rectangles."""

    temporal, height, width, cell_count = map(int, (temporal, height, width, cell_count))
    token_count = temporal * height * width
    if temporal < 1 or height < 1 or width < 1:
        raise ValueError(f"invalid merged grid {(temporal, height, width)!r}")
    if cell_count < 1 or cell_count > token_count:
        raise ValueError(f"cell_count={cell_count} must be in [1, {token_count}]")

    cells = [SpatialCell(t, 0, 0, height, width) for t in range(temporal)]
    if cell_count < len(cells):
        raise ValueError(
            f"cell_count={cell_count} cannot preserve temporal slices for temporal={temporal}"
        )

    while len(cells) < cell_count:
        candidates = [
            (cell.area, max(cell.height, cell.width), -index, index)
            for index, cell in enumerate(cells)
            if cell.area > 1
        ]
        if not candidates:
            raise RuntimeError(
                f"cannot split merged grid {(temporal, height, width)} into {cell_count} cells"
            )
        _, _, _, split_index = max(candidates)
        cell = cells.pop(split_index)
        if cell.width >= cell.height and cell.width > 1:
            middle = cell.left + cell.width // 2
            children = [
                SpatialCell(cell.temporal, cell.top, cell.left, cell.bottom, middle),
                SpatialCell(cell.temporal, cell.top, middle, cell.bottom, cell.right),
            ]
        else:
            middle = cell.top + cell.height // 2
            children = [
                SpatialCell(cell.temporal, cell.top, cell.left, middle, cell.right),
                SpatialCell(cell.temporal, middle, cell.left, cell.bottom, cell.right),
            ]
        cells.extend(children)

    return sorted(cells)


def cell_center_positions(cells: list[SpatialCell], *, device=None) -> torch.Tensor:
    return torch.tensor(
        [
            [
                cell.temporal,
                (cell.top + cell.bottom - 1) // 2,
                (cell.left + cell.right - 1) // 2,
            ]
            for cell in cells
        ],
        dtype=torch.long,
        device=device,
    )


def cell_edges(
    cells: list[SpatialCell],
    height: int,
    width: int,
    *,
    halo: int = 0,
    device=None,
) -> tuple[torch.Tensor, torch.Tensor]:
    """Return flattened token indices and their destination cell ids."""

    token_indices: list[int] = []
    cell_indices: list[int] = []
    halo = max(0, int(halo))
    for cell_id, cell in enumerate(cells):
        top = max(0, cell.top - halo)
        left = max(0, cell.left - halo)
        bottom = min(height, cell.bottom + halo)
        right = min(width, cell.right + halo)
        base = cell.temporal * height * width
        for row in range(top, bottom):
            start = base + row * width + left
            count = right - left
            token_indices.extend(range(start, start + count))
            cell_indices.extend([cell_id] * count)
    return (
        torch.tensor(token_indices, dtype=torch.long, device=device),
        torch.tensor(cell_indices, dtype=torch.long, device=device),
    )


def cell_assignment(
    cells: list[SpatialCell],
    height: int,
    width: int,
    *,
    device=None,
) -> torch.Tensor:
    if not cells:
        raise ValueError("cannot build a cell assignment from an empty cell list")
    temporal = max(cell.temporal for cell in cells) + 1
    assignment = torch.full(
        (temporal, height, width), -1, dtype=torch.long, device=device
    )
    for cell_id, cell in enumerate(cells):
        assignment[cell.temporal, cell.top : cell.bottom, cell.left : cell.right] = cell_id
    if torch.any(assignment < 0):
        raise RuntimeError("spatial cells do not cover the complete visual grid")
    return assignment.reshape(-1)


def build_mgvb_visual_positions(
    grid_thw,
    *,
    spatial_merge_size: int,
    token_budget: int,
    global_tokens: int,
    region_tokens: int,
    device=None,
) -> torch.Tensor:
    """Build positions in the exact order emitted by True MGVB."""

    temporal, height, width = merged_grid_shape(grid_thw, spatial_merge_size)
    native_count = temporal * height * width
    compact_count = min(native_count, int(token_budget))
    if compact_count == native_count:
        return native_visual_positions(grid_thw, spatial_merge_size, device=device)

    global_count = int(global_tokens)
    region_count = int(region_tokens)
    fine_count = compact_count - global_count - region_count
    if global_count < 1 or region_count < 1 or fine_count < 1:
        raise ValueError(
            "compressed MGVB budget must leave positive global, region, and fine counts; "
            f"got total={compact_count}, global={global_count}, region={region_count}, fine={fine_count}"
        )

    center = torch.tensor(
        [[(temporal - 1) // 2, (height - 1) // 2, (width - 1) // 2]],
        dtype=torch.long,
        device=device,
    ).expand(global_count, -1)
    region_cells = partition_spatial_grid(temporal, height, width, region_count)
    fine_cells = partition_spatial_grid(temporal, height, width, fine_count)
    return torch.cat(
        [
            center,
            cell_center_positions(region_cells, device=device),
            cell_center_positions(fine_cells, device=device),
        ],
        dim=0,
    )