File size: 7,355 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
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
from __future__ import annotations

import json
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Iterable

import torch

from .config import LoraSettings
from .constants import DEFAULT_MODEL_ID, DEFAULT_MODEL_REVISION, SUPPORTED_LORA_MODULES


# PEFT's card names the base by its resolved cache path, which the Hub's metadata check rejects.
_SNAPSHOT_PATH = re.compile(r"[^\s:]*models--[^/\s]+/snapshots/[0-9a-f]+")

_COMPACT_TARGETS = {
    "q": "q_proj",
    "k": "k_proj",
    "v": "v_proj",
    "o": "o_proj",
}


def resolve_lora_targets(specification: str) -> tuple[str, ...]:
    """Resolve a portable LoRA target preset into projection suffixes."""
    normalized = specification.strip().lower()
    if normalized == "all":
        return SUPPORTED_LORA_MODULES
    if normalized and all(character in _COMPACT_TARGETS for character in normalized):
        if len(set(normalized)) != len(normalized):
            raise ValueError(f"LoRA target shorthand contains duplicates: {specification!r}.")
        return tuple(_COMPACT_TARGETS[character] for character in normalized)

    targets = tuple(part.strip() for part in specification.split(",") if part.strip())
    if not targets:
        raise ValueError("LoRA targets cannot be empty.")
    unsupported = sorted(set(targets).difference(SUPPORTED_LORA_MODULES))
    if unsupported:
        raise ValueError(
            "Unsupported LoRA target(s): "
            f"{unsupported}. Supported projection names are {list(SUPPORTED_LORA_MODULES)}."
        )
    if len(set(targets)) != len(targets):
        raise ValueError(f"LoRA target list contains duplicates: {specification!r}.")
    return targets


def validate_target_modules(model, targets: Iterable[str]) -> dict[str, int]:
    """Require exactly one requested projection per transformer layer."""
    targets = tuple(targets)
    num_layers = getattr(model.config, "num_hidden_layers", None)
    if not isinstance(num_layers, int) or num_layers <= 0:
        raise RuntimeError("The base model must declare config.num_hidden_layers.")

    counts = {
        target: sum(name.endswith(f".{target}") for name, _ in model.named_modules())
        for target in targets
    }
    invalid = {target: count for target, count in counts.items() if count != num_layers}
    if invalid:
        raise RuntimeError(
            "Requested LoRA projections are not present exactly once per transformer "
            f"layer (expected {num_layers} each): {invalid}."
        )
    return counts


def create_lora_model(model, settings: LoraSettings):
    """Attach the only trainable weights used by public Uno training."""
    from peft import LoraConfig, TaskType, get_peft_model

    settings.validate()
    targets = resolve_lora_targets(settings.target)
    validate_target_modules(model, targets)
    config = LoraConfig(
        task_type=TaskType.CAUSAL_LM,
        inference_mode=False,
        r=settings.rank,
        lora_alpha=settings.resolved_alpha,
        lora_dropout=settings.dropout,
        target_modules=list(targets),
        bias="none",
    )
    peft_model = get_peft_model(model, config)
    for name, parameter in peft_model.named_parameters():
        if parameter.requires_grad:
            if "lora_A" not in name and "lora_B" not in name:
                raise RuntimeError(
                    "Uno is LoRA-only, but PEFT exposed a non-LoRA trainable tensor: "
                    f"{name}."
                )
            parameter.data = parameter.data.float()
    validate_only_lora_trainable(peft_model)
    return peft_model, targets


def disable_lora_input_upcast(model) -> int:
    """Training runs under bf16 autocast, so PEFT's per-layer fp32 input copies are pure overhead."""
    from peft.tuners.tuners_utils import BaseTunerLayer

    count = 0
    for module in model.modules():
        if isinstance(module, BaseTunerLayer):
            module.cast_input_dtype_enabled = False
            count += 1
    return count


def validate_only_lora_trainable(model) -> tuple[int, int]:
    trainable = []
    total_trainable = 0
    total_parameters = 0
    for name, parameter in model.named_parameters():
        total_parameters += parameter.numel()
        if parameter.requires_grad:
            trainable.append(name)
            total_trainable += parameter.numel()
    if not trainable:
        raise RuntimeError("No trainable LoRA tensors were found.")
    invalid = [
        name for name in trainable if "lora_A" not in name and "lora_B" not in name
    ]
    if invalid:
        raise RuntimeError(
            "Only LoRA A/B tensors may be trainable; found " f"{invalid[:5]}."
        )
    return total_trainable, total_parameters


def make_saved_adapter_portable(adapter_dir: str | Path) -> None:
    """Replace resolved cache paths in PEFT metadata with the pinned Hub source."""
    adapter_dir = Path(adapter_dir)
    config_path = adapter_dir / "adapter_config.json"
    config = json.loads(config_path.read_text())
    config["base_model_name_or_path"] = DEFAULT_MODEL_ID
    config["revision"] = DEFAULT_MODEL_REVISION
    config_path.write_text(json.dumps(config, indent=2, sort_keys=True) + "\n")
    make_readme_portable(adapter_dir / "README.md")


def make_readme_portable(readme_path: Path) -> None:
    if not readme_path.is_file():
        return
    text = readme_path.read_text()
    portable = _SNAPSHOT_PATH.sub(DEFAULT_MODEL_ID, text)
    if portable != text:
        readme_path.write_text(portable)


@dataclass
class TokenwiseLoraRouter:
    """Mask PEFT's low-rank update on clean teacher token rows."""

    model: object

    def __post_init__(self) -> None:
        self.token_mask: torch.Tensor | None = None
        self.handles = []
        hooked_modules: set[int] = set()
        validate_only_lora_trainable(self.model)
        for module in self.model.modules():
            lora_a_layers = getattr(module, "lora_A", None)
            if lora_a_layers is None:
                continue
            if any(getattr(module, "use_dora", {}).values()):
                raise RuntimeError("Token-conditional Uno routing does not support DoRA.")
            for lora_a in lora_a_layers.values():
                if id(lora_a) in hooked_modules:
                    continue
                hooked_modules.add(id(lora_a))
                self.handles.append(lora_a.register_forward_hook(self._mask_output))
        if not self.handles:
            raise RuntimeError("No PEFT LoRA-A layers were found for conditional routing.")

    def set_token_mask(self, token_mask: torch.Tensor) -> None:
        self.token_mask = token_mask

    def _mask_output(self, module, inputs, output):
        del module, inputs
        if self.token_mask is None:
            raise RuntimeError("Set the Uno LoRA token mask before the model forward.")
        if output.shape[:-1] != self.token_mask.shape:
            raise RuntimeError(
                "Uno LoRA mask and activation shapes differ: "
                f"mask={tuple(self.token_mask.shape)}, output={tuple(output.shape)}."
            )
        mask = self.token_mask.to(device=output.device, dtype=output.dtype)
        return output * mask.unsqueeze(-1)

    def close(self) -> None:
        for handle in self.handles:
            handle.remove()
        self.handles.clear()