File size: 5,229 Bytes
4ab04b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import sys
from types import MethodType

import torch


def unwrap_base_model(model):
    while hasattr(model, "module"):
        model = model.module
    return model.get_base_model() if hasattr(model, "get_base_model") else model


def validate_uno_model(model) -> None:
    base_model = unwrap_base_model(model)
    required = ("prepare_for_bd_training", "model", "lm_head")
    missing = [name for name in required if not hasattr(base_model, name)]
    if missing:
        raise RuntimeError(
            "The selected HF model is not a Uno-prepared SDAR model. Missing "
            f"required interface member(s): {missing}."
        )
    _get_sdar_helpers(base_model)
    if not hasattr(base_model.config, "block_size"):
        raise RuntimeError("The Uno model config must define block_size.")


def configure_uniform_noise_config(config):
    configured_noise = getattr(config, "noise", None)
    if configured_noise not in (None, "uniform"):
        raise RuntimeError(
            "Public Uno training requires uniform noise, but the model config "
            f"declares noise={configured_noise!r}."
        )
    config.noise = "uniform"
    return config


def configure_uniform_noise(model) -> None:
    base_model = unwrap_base_model(model)
    validate_uno_model(base_model)
    base_model.noise = "uniform"
    configure_uniform_noise_config(base_model.config)


def _get_sdar_helpers(base_model):
    modeling_module = sys.modules.get(base_model.__class__.__module__)
    if modeling_module is None:
        raise RuntimeError(
            f"Could not resolve modeling module for {base_model.__class__.__name__}."
        )
    modify_position_ids = getattr(
        modeling_module, "modify_padded_position_ids_2d", None
    )
    calculate_token_nums = getattr(modeling_module, "calculate_token_nums", None)
    if modify_position_ids is None or calculate_token_nums is None:
        raise RuntimeError(
            "The Uno model module must provide modify_padded_position_ids_2d and "
            "calculate_token_nums."
        )
    return modify_position_ids, calculate_token_nums


def _build_noisy_region_mask(
    num_tokens: list[torch.Tensor], sequence_length: int
) -> torch.Tensor:
    rows = []
    for lengths in num_tokens:
        parts = []
        for length in lengths.tolist():
            parts.append(torch.ones(length, dtype=torch.bool, device=lengths.device))
            parts.append(torch.zeros(length, dtype=torch.bool, device=lengths.device))
        row = torch.cat(parts)
        if row.numel() != 2 * sequence_length:
            raise RuntimeError(
                "Uno noisy/clean layout has an invalid length: "
                f"{row.numel()} for sequence_length={sequence_length}."
            )
        rows.append(row)
    return torch.stack(rows)


def prepare_uno_layout(
    base_model,
    input_ids: torch.Tensor,
    position_ids: torch.Tensor | None,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    batch_size, sequence_length = input_ids.shape
    if position_ids is None:
        position_ids = torch.arange(
            sequence_length, device=input_ids.device
        ).unsqueeze(0).expand(batch_size, -1)
    modify_position_ids, calculate_token_nums = _get_sdar_helpers(base_model)
    model_position_ids = modify_position_ids(position_ids)
    token_counts = calculate_token_nums(model_position_ids)
    noisy_region_mask = _build_noisy_region_mask(token_counts, sequence_length)
    return position_ids, model_position_ids, noisy_region_mask


def extract_uno_regions(
    hidden_states: torch.Tensor,
    noisy_region_mask: torch.Tensor,
    sequence_length: int,
) -> tuple[torch.Tensor, torch.Tensor]:
    batch_size, _, hidden_size = hidden_states.shape
    noisy = hidden_states[noisy_region_mask].reshape(
        batch_size, sequence_length, hidden_size
    )
    clean = hidden_states[~noisy_region_mask].reshape(
        batch_size, sequence_length, hidden_size
    )
    return noisy, clean


def run_prepared_uno_forward(
    model,
    base_model,
    inputs: dict[str, torch.Tensor],
    concatenated_input_ids: torch.Tensor,
    concatenated_position_ids: torch.Tensor,
    flex_attention_mask,
) -> torch.Tensor:
    """Run the paired noisy/clean layout through wrappers in one transformer pass."""
    decoder = getattr(base_model, "model", None)
    if decoder is None:
        raise RuntimeError("The Uno model must expose its decoder as .model.")
    original_forward = base_model.forward

    def prepared_forward(self, *args, **kwargs):
        del self, args, kwargs
        outputs = decoder(
            input_ids=concatenated_input_ids,
            attention_mask=flex_attention_mask,
            position_ids=concatenated_position_ids,
            output_attentions=False,
            output_hidden_states=False,
            return_dict=True,
            use_cache=False,
        )
        return {"hidden_states": (outputs.last_hidden_state,)}

    base_model.forward = MethodType(prepared_forward, base_model)
    try:
        outputs = model(**inputs, output_hidden_states=True)
    finally:
        base_model.forward = original_forward
    return outputs["hidden_states"][-1]