File size: 15,155 Bytes
89a4256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""HydraGemma4: one model, many heads.

    model = HydraGemma4.from_pretrained(
        base_model="google/gemma-4-E2B-it",
        adapter_path="./colgemma4_adapter",
    )

    # Retrieval -- LoRA ON, bidirectional attention
    embeddings = model.embed(images)           # [B, N, 128]

    # Generation -- LoRA OFF, causal attention
    text = model.generate(image, "Describe this document.")

One ColGemma4 + LoRA + lm_head. Attention mode and LoRA toggled per call.

Gemma 4 E2B layer structure:
    35 layers total: 28 sliding_attention + 7 full_attention
    full_attention at indices: [4, 9, 14, 19, 24, 29, 34]
    Only full_attention layers are patched for bidirectional mode.
    Sliding layers stay causal always.

Key Gemma 4 differences from Qwen3.5:
    - Layer forward signature includes `per_layer_input` (Per-Layer Embeddings)
    - Layer type via config.text_config.layer_types list (not layer.layer_type attribute)
    - Vision tower at self.vision_tower (not self.visual)
    - final_logit_softcapping for logit capping
    - image_position_ids (not image_grid_thw)
"""

import re
from typing import List, Union

import torch
import torch.nn as nn
from PIL import Image
from peft import PeftModel
from transformers import AutoProcessor, Gemma4ForConditionalGeneration
from transformers.models.gemma4.modeling_gemma4 import Gemma4Model as _Gemma4Base

from colgemma4 import ColGemma4, ColGemma4Processor


class HydraGemma4:
    """One ColGemma4 model with LoRA + lm_head. Two faces via toggling.

    Retrieval face:  LoRA ON,  bidirectional attention -> custom_text_proj -> 128-dim
    Generation face: LoRA OFF, causal attention -> lm_head -> autoregressive text
    """

    def __init__(self, model, lm_head, emb_processor, gen_processor, attn_fns, config):
        self.model = model
        self.lm_head = lm_head
        self.emb_processor = emb_processor
        self.gen_processor = gen_processor
        self._attn_fns = attn_fns  # {layer_idx: (causal_fn, bidir_fn)}
        self._config = config
        self._base = self._unwrap(model)

    @staticmethod
    def _unwrap(model):
        m = model
        if hasattr(m, "module"):
            m = m.module
        if hasattr(m, "base_model"):
            m = m.base_model
        if hasattr(m, "model"):
            m = m.model
        return m

    def _set_bidirectional(self):
        for idx, (_, bidir_fn) in self._attn_fns.items():
            self._base.language_model.layers[idx].forward = bidir_fn

    def _set_causal(self):
        for idx, (causal_fn, _) in self._attn_fns.items():
            self._base.language_model.layers[idx].forward = causal_fn

    @classmethod
    def from_pretrained(
        cls,
        base_model: str = "google/gemma-4-E2B-it",
        adapter_path: str = "/tmp/colgemma4_adapter",
        torch_dtype=torch.bfloat16,
        max_visual_tokens: int = 560,
        device: str = "cuda",
    ):
        """Load single model from base + adapter + lm_head."""
        from pathlib import Path

        print(f"Loading ColGemma4 from {base_model}...", flush=True)
        config = Gemma4ForConditionalGeneration.config_class.from_pretrained(
            base_model, trust_remote_code=True
        )
        config.text_config.use_cache = False

        model = ColGemma4.from_pretrained(
            base_model,
            config=config,
            torch_dtype=torch_dtype,
            attn_implementation="sdpa",
            ignore_mismatched_sizes=True,
        )

        # Store causal/bidirectional forwards for full-attention layers
        layer_types = config.text_config.layer_types
        attn_fns = {}
        for idx, layer in enumerate(model.language_model.layers):
            if layer_types[idx] == "full_attention":
                causal_fn = layer.forward

                def make_bidir(orig):
                    def bidir(
                        hidden_states,
                        per_layer_input=None,
                        position_embeddings=None,
                        attention_mask=None,
                        **kw,
                    ):
                        if (
                            attention_mask is not None
                            and attention_mask.ndim == 4
                            and attention_mask.dtype.is_floating_point
                        ):
                            min_dtype = torch.finfo(attention_mask.dtype).min
                            diag = torch.diagonal(attention_mask, dim1=-2, dim2=-1)
                            is_valid = diag > (min_dtype / 2)
                            bidir_mask = torch.where(
                                is_valid.unsqueeze(-1) & is_valid.unsqueeze(-2),
                                attention_mask.new_zeros(1),
                                attention_mask.new_full((1,), min_dtype),
                            )
                            attention_mask = bidir_mask
                        return orig(
                            hidden_states,
                            per_layer_input=per_layer_input,
                            position_embeddings=position_embeddings,
                            attention_mask=attention_mask,
                            **kw,
                        )

                    return bidir

                bidir_fn = make_bidir(causal_fn)
                attn_fns[idx] = (causal_fn, bidir_fn)
                layer.forward = bidir_fn

        print(f"Patched {len(attn_fns)} full-attention layers", flush=True)

        print(f"Loading LoRA adapter from {adapter_path}...", flush=True)
        model = PeftModel.from_pretrained(model, adapter_path)
        model = model.to(device).eval()

        # Load lm_head
        print("Loading lm_head...", flush=True)
        lm_head_path = Path(adapter_path) / "lm_head.pt"
        if lm_head_path.exists():
            base_cfg = Gemma4ForConditionalGeneration.config_class.from_pretrained(base_model)
            lm_head = nn.Linear(
                base_cfg.text_config.hidden_size,
                base_cfg.text_config.vocab_size,
                bias=False,
            )
            lm_head.load_state_dict(torch.load(lm_head_path, map_location="cpu"))
        else:
            base = Gemma4ForConditionalGeneration.from_pretrained(
                base_model, torch_dtype=torch_dtype
            )
            lm_head = base.lm_head
            del base
            torch.cuda.empty_cache()

        lm_head = lm_head.to(device).to(torch_dtype)

        emb_processor = ColGemma4Processor.from_pretrained(
            base_model, max_num_visual_tokens=max_visual_tokens
        )
        gen_processor = AutoProcessor.from_pretrained(base_model, trust_remote_code=True)

        params = sum(p.numel() for p in model.parameters()) / 1e6
        lm_params = sum(p.numel() for p in lm_head.parameters()) / 1e6
        print(
            f"Ready. Single model: {params:.0f}M + lm_head: {lm_params:.0f}M",
            flush=True,
        )

        return cls(model, lm_head, emb_processor, gen_processor, attn_fns, config)

    def embed(self, images: Union[Image.Image, List[Image.Image]]) -> torch.Tensor:
        """Embed images for retrieval. LoRA ON, bidirectional attention."""
        if isinstance(images, Image.Image):
            images = [images]
        self.model.enable_adapter_layers()
        self._set_bidirectional()
        inputs = self.emb_processor.process_images(images)
        device = next(self.model.parameters()).device
        inputs = {k: v.to(device) for k, v in inputs.items()}
        with torch.no_grad(), torch.amp.autocast("cuda", dtype=torch.bfloat16):
            return self.model(**inputs)

    def embed_queries(self, queries: Union[str, List[str]]) -> torch.Tensor:
        """Embed queries for retrieval. LoRA ON, bidirectional attention."""
        if isinstance(queries, str):
            queries = [queries]
        self.model.enable_adapter_layers()
        self._set_bidirectional()
        inputs = self.emb_processor.process_queries(queries)
        device = next(self.model.parameters()).device
        inputs = {k: v.to(device) for k, v in inputs.items()}
        with torch.no_grad(), torch.amp.autocast("cuda", dtype=torch.bfloat16):
            return self.model(**inputs)

    @torch.no_grad()
    def generate(
        self,
        image: Image.Image,
        prompt: str,
        max_new_tokens: int = 4096,
        system_prompt: str = None,
        temperature: float = 1.0,
        top_p: float = 0.95,
        top_k: int = 64,
    ) -> str:
        """Generate text given image + prompt. LoRA OFF, causal attention."""
        self.model.disable_adapter_layers()
        self._set_causal()
        try:
            return self._generate_with_kv_cache(
                image, prompt, max_new_tokens, system_prompt,
                temperature=temperature, top_p=top_p, top_k=top_k,
            )
        except Exception:
            return self._generate_no_cache(
                image, prompt, max_new_tokens, system_prompt,
                temperature=temperature, top_p=top_p, top_k=top_k,
            )
        finally:
            self.model.enable_adapter_layers()
            self._set_bidirectional()

    def _prepare_inputs(self, image, prompt, system_prompt=None):
        msgs = []
        if system_prompt:
            msgs.append({"role": "system", "content": system_prompt})
        msgs.append({
            "role": "user",
            "content": [
                {"type": "image", "image": image},
                {"type": "text", "text": prompt},
            ],
        })
        txt = self.gen_processor.apply_chat_template(
            msgs, tokenize=False, add_generation_prompt=True,
        )
        inp = self.gen_processor(
            text=[txt], images=[image], return_tensors="pt", padding=True
        )
        device = next(self.model.parameters()).device
        return {k: v.to(device) for k, v in inp.items()}

    def _decode(self, generated):
        if not generated:
            return ""
        gen_ids = torch.cat(generated, dim=1)
        return self.gen_processor.tokenizer.decode(gen_ids[0], skip_special_tokens=True).strip()

    def _is_eos(self, token_id):
        eos = self.gen_processor.tokenizer.eos_token_id
        if isinstance(eos, list):
            return token_id in eos
        return token_id == eos

    @staticmethod
    def _sample_token(logits, temperature, top_p, top_k):
        if temperature <= 0:
            return logits.argmax(dim=-1)
        logits_1d = logits[0, 0] / temperature
        if top_k > 0:
            topk_vals, _ = torch.topk(logits_1d, min(top_k, logits_1d.size(-1)))
            logits_1d[logits_1d < topk_vals[-1]] = float("-inf")
        probs = torch.softmax(logits_1d, dim=-1)
        if 0 < top_p < 1.0:
            sorted_probs, sorted_idx = torch.sort(probs, descending=True)
            cumsum = torch.cumsum(sorted_probs, dim=-1)
            mask = cumsum - sorted_probs > top_p
            sorted_probs[mask] = 0
            probs = torch.zeros_like(probs).scatter_(0, sorted_idx, sorted_probs)
        probs = probs / probs.sum()
        token_id = torch.multinomial(probs, num_samples=1)
        return token_id.reshape(1, 1)

    def _generate_with_kv_cache(
        self, image, prompt, max_new_tokens, system_prompt=None,
        temperature=1.0, top_p=0.95, top_k=64,
    ):
        """KV-cache generation. Pixel values only on first step."""
        inp = self._prepare_inputs(image, prompt, system_prompt)
        input_ids = inp["input_ids"]
        attn_mask = inp["attention_mask"]
        mm_ids = inp.get("mm_token_type_ids")
        past_key_values = None
        generated = []

        final_softcap = self._config.text_config.final_logit_softcapping

        for step in range(max_new_tokens):
            if step == 0:
                kw = {
                    "input_ids": input_ids,
                    "attention_mask": attn_mask,
                    "pixel_values": inp.get("pixel_values"),
                    "image_position_ids": inp.get("image_position_ids"),
                    "use_cache": True,
                    "output_hidden_states": True,
                    "return_dict": True,
                }
                if mm_ids is not None:
                    kw["mm_token_type_ids"] = mm_ids
            else:
                kw = {
                    "input_ids": next_token,
                    "attention_mask": attn_mask,
                    "past_key_values": past_key_values,
                    "use_cache": True,
                    "output_hidden_states": True,
                    "return_dict": True,
                }

            outputs = _Gemma4Base.forward(self._base, **kw)
            past_key_values = outputs.past_key_values
            logits = self.lm_head(outputs.last_hidden_state[:, -1:, :])

            if final_softcap is not None:
                logits = logits / final_softcap
                logits = torch.tanh(logits)
                logits = logits * final_softcap

            next_token = self._sample_token(logits, temperature, top_p, top_k)
            generated.append(next_token)

            if self._is_eos(next_token.item()):
                break

            attn_mask = torch.cat([attn_mask, torch.ones_like(next_token)], dim=1)

        return self._decode(generated)

    def _generate_no_cache(
        self, image, prompt, max_new_tokens, system_prompt=None,
        temperature=1.0, top_p=0.95, top_k=64,
    ):
        """No-cache fallback. Passes all inputs every step."""
        inp = self._prepare_inputs(image, prompt, system_prompt)
        input_ids = inp["input_ids"]
        attn_mask = inp["attention_mask"]
        mm_ids = inp.get("mm_token_type_ids")
        generated = []

        final_softcap = self._config.text_config.final_logit_softcapping

        for step in range(max_new_tokens):
            kw = {
                "input_ids": input_ids,
                "attention_mask": attn_mask,
                "pixel_values": inp.get("pixel_values"),
                "image_position_ids": inp.get("image_position_ids"),
            }
            if mm_ids is not None:
                kw["mm_token_type_ids"] = mm_ids

            hidden = _Gemma4Base.forward(
                self._base, **kw,
                use_cache=False, output_hidden_states=True, return_dict=True,
            ).last_hidden_state

            logits = self.lm_head(hidden[:, -1:, :])
            if final_softcap is not None:
                logits = logits / final_softcap
                logits = torch.tanh(logits)
                logits = logits * final_softcap

            next_token = self._sample_token(logits, temperature, top_p, top_k)
            generated.append(next_token)

            if self._is_eos(next_token.item()):
                break

            input_ids = torch.cat([input_ids, next_token], dim=1)
            attn_mask = torch.cat([attn_mask, torch.ones_like(next_token)], dim=1)
            if mm_ids is not None:
                mm_ids = torch.cat([mm_ids, torch.zeros_like(next_token)], dim=1)

        return self._decode(generated)