File size: 4,684 Bytes
d3b61f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from contextlib import nullcontext

import torch


def collate_query_groups(groups, pad_token_id):
    if not groups:
        raise ValueError('Provide at least one query group')
    max_length = max(group['input_ids'].shape[1] for group in groups)
    max_options = max(group['pair_indices'].shape[1] for group in groups)
    input_ids = torch.full((len(groups), max_length), pad_token_id, dtype=torch.long)
    token_types = torch.zeros_like(input_ids)
    attention_mask = torch.zeros_like(input_ids, dtype=torch.bool)
    pairs, masks, queries, tasks, sample_indices = [], [], [], [], []
    for sample_index, group in enumerate(groups):
        if group['input_ids'].shape[0] != 1 or group['video_grid_thw'].shape[0] != 1:
            raise ValueError('Each input group must contain exactly one video')
        length = group['input_ids'].shape[1]
        input_ids[sample_index, :length] = group['input_ids'][0]
        token_types[sample_index, :length] = group['mm_token_type_ids'][0]
        attention_mask[sample_index, :length] = True
        padding = max_options - group['pair_indices'].shape[1]
        pairs.append(torch.nn.functional.pad(group['pair_indices'], (0, padding)))
        masks.append(torch.nn.functional.pad(group['score_mask'], (0, padding)))
        queries.append(group['query_indices'])
        tasks.append(group['decision_tasks'])
        sample_indices.extend([sample_index] * len(group['query_indices']))
    return dict(input_ids=input_ids, mm_token_type_ids=token_types, attention_mask=attention_mask,
                video_grid_thw=torch.cat([group['video_grid_thw'] for group in groups]),
                pair_indices=torch.cat(pairs), query_indices=torch.cat(queries),
                score_mask=torch.cat(masks), decision_tasks=torch.cat(tasks),
                sample_indices=torch.tensor(sample_indices))


def forward_states(native, groups, features):
    if not groups or len(groups) != len(features):
        raise ValueError('Each screenshot needs its own group and features')
    if any(value.is_inference() for value in features):
        raise ValueError('Clone visual features outside inference_mode before the language forward')
    batch = collate_query_groups(groups, 0)
    device = native.get_input_embeddings().weight.device
    batch = {key: value.to(device) for key, value in batch.items()}
    amp = torch.autocast('cuda', dtype=torch.bfloat16) if device.type == 'cuda' else nullcontext()
    with amp:
        embeds = native.get_input_embeddings()(batch['input_ids'])
        if getattr(native, 'full_parameter_training', False) and device.type == 'cuda':
            embeds = embeds.to(torch.bfloat16)
        video_token = native.config.backbone_config.video_token_id
        all_slots = batch['input_ids'] == video_token
        for index, value in enumerate(features):
            if int(all_slots[index].sum()) != len(value):
                raise ValueError('Visual feature count differs from state tokens')
        embeds = embeds.masked_scatter(all_slots[..., None].expand_as(embeds), torch.cat(features).to(embeds))
        if native.is_moe:
            positions, _ = native.backbone.get_rope_index(input_ids=batch['input_ids'],
                mm_token_type_ids=batch['mm_token_type_ids'], video_grid_thw=batch['video_grid_thw'],
                attention_mask=batch['attention_mask'])
        else:
            positions = native.backbone.compute_3d_position_ids(input_ids=batch['input_ids'], inputs_embeds=embeds,
                video_grid_thw=batch['video_grid_thw'], attention_mask=batch['attention_mask'],
                mm_token_type_ids=batch['mm_token_type_ids'])
        hidden = native.backbone.language_model(inputs_embeds=embeds, position_ids=positions,
            attention_mask=batch['attention_mask'], use_cache=False).last_hidden_state
    with torch.autocast(device_type=device.type, enabled=False):
        indices = batch['sample_indices']
        logits = native.head(hidden[indices[:, None], batch['pair_indices']], hidden[indices, batch['query_indices']])
        mask = batch['score_mask']
        probabilities = torch.where((batch['decision_tasks'] == 1)[:, None],
            logits.masked_fill(~mask, -torch.inf).softmax(-1), logits.sigmoid()) * mask
        items, offset = [], 0
        for group in groups:
            count, options = len(group['query_indices']), group['pair_indices'].shape[1]
            items.append(dict(logits=logits[offset:offset + count, :options],
                probabilities=probabilities[offset:offset + count, :options],
                score_mask=mask[offset:offset + count, :options]))
            offset += count
    return dict(items=items)