File size: 8,551 Bytes
6efaeab
 
 
 
 
 
 
c6d0c2e
6efaeab
ac59af7
 
 
c6d0c2e
6efaeab
 
 
 
 
 
 
c6d0c2e
 
 
 
 
 
 
6efaeab
 
 
 
 
 
 
c6d0c2e
 
 
 
 
 
6efaeab
c6d0c2e
6efaeab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d0c2e
 
 
 
 
6efaeab
 
 
 
c6d0c2e
6efaeab
 
c6d0c2e
6efaeab
 
 
 
c6d0c2e
 
 
 
 
 
 
 
 
 
 
 
 
 
6efaeab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d0c2e
6efaeab
f552828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6efaeab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6d0c2e
 
6efaeab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""OgmaModel — top-level model wrapping any architecture variant."""

from __future__ import annotations

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import PreTrainedModel

from .config import OgmaConfig, TaskToken, VariantType
from .embeddings import TokenEmbedding
from .pooling import create_pooling
from .transformer import TransformerVariant

__all__ = ["OgmaModel"]

MAX_PARAMS = 10_000_000


def _build_variant(config: OgmaConfig) -> nn.Module:
    """Instantiate the released Ogma architecture variant."""
    if config.variant != VariantType.TRANSFORMER:
        raise ValueError(f"This HF release supports transformer checkpoints, got {config.variant}")
    return TransformerVariant(config)


class OgmaModel(PreTrainedModel):
    """Ogma embedding model.

    Wraps any architecture variant with shared embedding, pooling, and
    normalization. Produces L2-normalized embeddings at d_output dimensions,
    Matryoshka-compatible at configured sub-dimensions.
    """

    config_class = OgmaConfig
    base_model_prefix = "ogma"
    supports_gradient_checkpointing = False
    _tied_weights_keys: list[str] = []
    all_tied_weights_keys: dict[str, str] = {}

    def __init__(self, config: OgmaConfig) -> None:
        super().__init__(config)
        self.config = config
        self.embedding = TokenEmbedding(config)
        self.variant = _build_variant(config)
        self.pooling = create_pooling(config)

        # Output projection if variant output != d_output
        needs_proj = (
            config.variant == VariantType.DEEP_NARROW
            and config.d_model != config.d_output
        )
        # DeepNarrowVariant already has output_proj, so no extra needed here
        if not needs_proj and config.d_model != config.d_output:
            self.output_proj: nn.Module = nn.Linear(
                config.d_model, config.d_output
            )
        else:
            self.output_proj = nn.Identity()

    def forward(
        self,
        input_ids: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
        task_token_ids: torch.Tensor | None = None,
        token_ids: torch.Tensor | None = None,
        **_: object,
    ) -> torch.Tensor:
        """Forward pass producing L2-normalized embeddings.

        Args:
            input_ids: (B, S) token IDs, Hugging Face style.
            attention_mask: (B, S) attention mask (1=valid, 0=pad).
            task_token_ids: (B,) task token IDs (4=QRY, 5=DOC, 6=SYM).
            token_ids: Backward-compatible alias for input_ids.

        Returns:
            (B, d_output) L2-normalized embeddings.
        """
        if input_ids is None:
            input_ids = token_ids
        if input_ids is None:
            raise ValueError("input_ids or token_ids must be provided")
        if attention_mask is None:
            attention_mask = torch.ones_like(input_ids)
        if task_token_ids is None:
            task_token_ids = torch.full(
                (input_ids.shape[0],),
                self.config.sym_id,
                device=input_ids.device,
                dtype=torch.long,
            )
        token_ids = input_ids
        # Embed tokens with task token prepended -> (B, S+1, d_model)
        x = self.embedding(token_ids, task_token_ids)

        # Extend attention mask for prepended task token
        task_mask = torch.ones(
            attention_mask.shape[0], 1,
            device=attention_mask.device,
            dtype=attention_mask.dtype,
        )
        extended_mask = torch.cat([task_mask, attention_mask], dim=1)

        # Run through variant
        x = self.variant(x, extended_mask)

        # Pool
        x = self.pooling(x, extended_mask)

        # Project if needed
        x = self.output_proj(x)

        # L2 normalize
        return F.normalize(x, p=2, dim=-1)

    def encode(
        self,
        token_ids: torch.Tensor,
        attention_mask: torch.Tensor,
        task: TaskToken = TaskToken.SYM,
    ) -> torch.Tensor:
        """Encode tokens with a specified task mode.

        Args:
            token_ids: (B, S) token IDs.
            attention_mask: (B, S) attention mask.
            task: Task token to use.

        Returns:
            (B, d_output) L2-normalized embeddings.
        """
        task_ids = torch.full(
            (token_ids.shape[0],),
            self.config.task_token_id(task),
            device=token_ids.device,
            dtype=torch.long,
        )
        return self.forward(input_ids=token_ids, attention_mask=attention_mask, task_token_ids=task_ids)

    # TaskToken re-exported as a class attribute for clean external access
    TaskToken = TaskToken  # noqa: F821 (imported at module top)

    @torch.no_grad()
    def embed(
        self,
        texts,
        task: str = "sym",
        tokenizer=None,
        batch_size: int = 32,
        max_length: int = 1024,
    ) -> "torch.Tensor":
        """High-level text → L2-normalized embeddings.

        Args:
            texts: str or list[str] to encode.
            task: "qry" / "doc" / "sym" (or a TaskToken enum member).
            tokenizer: OgmaTokenizerFast instance. If None, loaded
                automatically from self.name_or_path (requires the model to
                have been loaded via AutoModel.from_pretrained).
            batch_size: Texts per forward pass.
            max_length: Token cap per text (default 1024).

        Returns:
            (len(texts), d_output) tensor of L2-normalized embeddings on the
            same device as the model.
        """
        if isinstance(texts, str):
            texts = [texts]
        if isinstance(task, str):
            task = self.TaskToken[task.upper()]
        if tokenizer is None:
            from transformers import AutoTokenizer
            tokenizer = AutoTokenizer.from_pretrained(
                self.name_or_path, trust_remote_code=True
            )
        device = next(self.parameters()).device
        outs = []
        for i in range(0, len(texts), batch_size):
            enc = tokenizer(
                texts[i : i + batch_size],
                return_tensors="pt",
                padding=True,
                truncation=True,
                max_length=max_length,
            ).to(device)
            outs.append(self.encode(enc["input_ids"], enc["attention_mask"], task=task))
        return torch.cat(outs, dim=0)


    def param_count(self) -> int:
        """Count total trainable parameters."""
        return sum(p.numel() for p in self.parameters() if p.requires_grad)

    def assert_param_budget(self) -> None:
        """Assert model is under the 10M parameter budget."""
        count = self.param_count()
        assert count < MAX_PARAMS, (
            f"Model has {count:,} params, exceeds {MAX_PARAMS:,} budget"
        )

    @classmethod
    def from_config(cls, config: OgmaConfig) -> OgmaModel:
        """Factory method to build a model from config."""
        model = cls(config)
        if model.param_count() < MAX_PARAMS:
            model.assert_param_budget()
        return model

    @classmethod
    def from_checkpoint(
        cls,
        path: str,
        device: str = "cpu",
    ) -> OgmaModel:
        """Load model from a checkpoint directory.

        Args:
            path: Path to checkpoint directory containing config.yaml
                and model.pt.
            device: Device to load model to.

        Returns:
            Loaded OgmaModel.
        """
        from pathlib import Path

        import yaml

        ckpt_path = Path(path)
        with open(ckpt_path / "config.yaml") as f:
            config_dict = yaml.safe_load(f)
        config = OgmaConfig.from_dict(config_dict)

        model = cls(config)
        state_dict = torch.load(
            ckpt_path / "model.pt",
            map_location=device,
            weights_only=True,
        )
        model.load_state_dict(state_dict)
        model.to(device)
        model.eval()
        return model

    def save_checkpoint(self, path: str) -> None:
        """Save model checkpoint.

        Args:
            path: Directory to save config.yaml and model.pt.
        """
        from pathlib import Path

        import yaml

        ckpt_path = Path(path)
        ckpt_path.mkdir(parents=True, exist_ok=True)
        with open(ckpt_path / "config.yaml", "w") as f:
            yaml.dump(self.config.to_dict(), f, default_flow_style=False)
        torch.save(self.state_dict(), ckpt_path / "model.pt")