File size: 15,695 Bytes
b5d99f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Jerboa Multimodal Architecture (Vision + Audio + Language).

Key Features:
- Plug-and-play Lightweight Vision Encoder (built-in ViT or SigLIP compatible)
- 2x2 Spatial Token Compression (downsamples visual tokens 4x for ultra-low memory & fast prefill)
- Audio Log-Mel Spectrogram Encoder (Whisper-compatible downsampled 1D Conv + Transformer)
- Multimodal Projectors (2-layer MLP with SiLU and RMSNorm)
- Seamless placeholder replacement (<|image|>, <|audio|>) into LLM token embeddings
- End-to-end training and inference on Apple Silicon MPS
"""

from typing import List, Optional, Tuple, Union

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.modeling_outputs import CausalLMOutputWithPast

try:
    from .config import JerboaConfig
    from .modeling import JerboaForCausalLM, JerboaRMSNorm
except (ImportError, ValueError):
    from config import JerboaConfig
    from modeling import JerboaForCausalLM, JerboaRMSNorm


class VisionPatchEmbed(nn.Module):
    """2D Image to Patch Embedding."""

    def __init__(self, img_size: int = 224, patch_size: int = 16, in_chans: int = 3, embed_dim: int = 384):
        super().__init__()
        self.img_size = img_size
        self.patch_size = patch_size
        self.grid_size = img_size // patch_size
        self.num_patches = self.grid_size * self.grid_size

        self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
        self.norm = nn.LayerNorm(embed_dim)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # x: [B, C, H, W] -> [B, D, grid_h, grid_w] -> [B, N, D]
        x = self.proj(x).flatten(2).transpose(1, 2)
        x = self.norm(x)
        return x


class VisionTransformerBlock(nn.Module):
    """Lightweight Vision Transformer Block."""

    def __init__(self, dim: int, num_heads: int = 6, mlp_ratio: float = 4.0):
        super().__init__()
        self.norm1 = nn.LayerNorm(dim)
        self.attn = nn.MultiheadAttention(dim, num_heads, dropout=0.0, batch_first=True)
        self.norm2 = nn.LayerNorm(dim)
        mlp_hidden_dim = int(dim * mlp_ratio)
        self.mlp = nn.Sequential(
            nn.Linear(dim, mlp_hidden_dim),
            nn.GELU(),
            nn.Linear(mlp_hidden_dim, dim),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        norm_x = self.norm1(x)
        attn_out, _ = self.attn(norm_x, norm_x, norm_x)
        x = x + attn_out
        x = x + self.mlp(self.norm2(x))
        return x


class LightweightVisionEncoder(nn.Module):
    """Compact Vision Encoder (ViT-Nano ~15M params) for fast multimodal learning."""

    def __init__(
        self,
        img_size: int = 224,
        patch_size: int = 16,
        in_chans: int = 3,
        embed_dim: int = 384,
        depth: int = 6,
        num_heads: int = 6,
    ):
        super().__init__()
        self.patch_embed = VisionPatchEmbed(img_size, patch_size, in_chans, embed_dim)
        self.num_patches = self.patch_embed.num_patches
        self.pos_embed = nn.Parameter(torch.zeros(1, self.num_patches, embed_dim))
        self.blocks = nn.ModuleList([VisionTransformerBlock(embed_dim, num_heads) for _ in range(depth)])
        self.norm = nn.LayerNorm(embed_dim)
        nn.init.trunc_normal_(self.pos_embed, std=0.02)

    def forward(self, pixel_values: torch.Tensor) -> torch.Tensor:
        # pixel_values: [B, C, H, W]
        x = self.patch_embed(pixel_values)
        x = x + self.pos_embed
        for block in self.blocks:
            x = block(x)
        x = self.norm(x)
        return x  # [B, num_patches, embed_dim]


class EmbeddingGemmaVisionEncoder(nn.Module):
    """Vision Encoder wrapper backed by Google DeepMind's EmbeddingGemma 2.

    Extracts rich 768-dimensional visual token representations (256 tokens per image)
    aligned with Gemma 4's multimodal embedding space.
    """

    def __init__(self, model_id: str = "google/embeddinggemma-2", freeze: bool = True):
        super().__init__()
        from transformers import AutoModel, AutoProcessor

        self.model_id = model_id
        print(f"[EmbeddingGemma] Loading vision backbone from '{model_id}'...")
        self.processor = AutoProcessor.from_pretrained(model_id)
        self.embedding_model = AutoModel.from_pretrained(model_id, dtype=torch.float32)

        if freeze:
            for p in self.embedding_model.parameters():
                p.requires_grad = False
            self.embedding_model.eval()

        self.embed_dim = 768

    def forward(self, images_or_pixel_values) -> torch.Tensor:
        """Forward images through EmbeddingGemma 2 and extract visual token sequence."""
        device = next(self.embedding_model.parameters()).device
        dtype = next(self.embedding_model.parameters()).dtype

        if isinstance(images_or_pixel_values, torch.Tensor):
            # Already tensor: convert to PIL for processor if needed or pass directly
            # If standard [B, C, H, W] tensor in [0, 1]
            from torchvision.transforms.functional import to_pil_image
            b = images_or_pixel_values.shape[0]
            pil_images = [to_pil_image(images_or_pixel_values[i].cpu().float()) for i in range(b)]
            inputs = self.processor(text=["<|image|>"] * b, images=pil_images, return_tensors="pt")
        elif isinstance(images_or_pixel_values, list):
            b = len(images_or_pixel_values)
            inputs = self.processor(text=["<|image|>"] * b, images=images_or_pixel_values, return_tensors="pt")
        else:
            inputs = self.processor(text="<|image|>", images=images_or_pixel_values, return_tensors="pt")

        inputs = {k: v.to(device) for k, v in inputs.items()}

        with torch.no_grad():
            outputs = self.embedding_model(**inputs)
            # last_hidden_state: [B, 260, 768]
            # token 0: BOS, 1: BOI, 2..257: 256 visual tokens, 258: EOI, 259: EOS
            visual_tokens = outputs.last_hidden_state[:, 2:258, :].to(dtype)

        return visual_tokens  # [B, 256, 768]


class VisionProjector(nn.Module):
    """Vision-to-Language Projector with 2x2 Spatial Token Compression."""

    def __init__(self, vision_dim: int, llm_dim: int, spatial_merge_size: int = 2):
        super().__init__()
        self.spatial_merge_size = spatial_merge_size
        in_features = vision_dim * (spatial_merge_size * spatial_merge_size)
        self.linear_1 = nn.Linear(in_features, llm_dim, bias=True)
        self.act = nn.SiLU()
        self.linear_2 = nn.Linear(llm_dim, llm_dim, bias=True)
        self.norm = JerboaRMSNorm(llm_dim)

    def forward(self, visual_features: torch.Tensor, grid_h: int = 14, grid_w: int = 14) -> torch.Tensor:
        # visual_features: [B, H*W, D]
        b, n, d = visual_features.shape
        m = self.spatial_merge_size
        if n == grid_h * grid_w and grid_h % m == 0 and grid_w % m == 0:
            # Reshape into 2D grid: [B, grid_h, grid_w, D]
            feat = visual_features.view(b, grid_h, grid_w, d)
            # Rearrange into 2x2 spatial blocks: [B, grid_h//2, grid_w//2, 4*D]
            feat = feat.view(b, grid_h // m, m, grid_w // m, m, d)
            feat = feat.permute(0, 1, 3, 2, 4, 5).contiguous()
            feat = feat.view(b, (grid_h // m) * (grid_w // m), m * m * d)
        else:
            feat = visual_features

        x = self.act(self.linear_1(feat))
        x = self.norm(self.linear_2(x))
        return x


class LightweightAudioEncoder(nn.Module):
    """Compact 1D Conv + Transformer Audio Encoder for Mel-Spectrograms."""

    def __init__(self, num_mel_bins: int = 80, embed_dim: int = 256, depth: int = 4, num_heads: int = 4):
        super().__init__()
        # Downsample temporal dimension 4x using 2 strided Conv1d layers
        self.conv1 = nn.Conv1d(num_mel_bins, embed_dim, kernel_size=3, stride=2, padding=1)
        self.conv2 = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=2, padding=1)
        self.norm = nn.LayerNorm(embed_dim)
        self.layers = nn.ModuleList([
            nn.TransformerEncoderLayer(d_model=embed_dim, nhead=num_heads, dropout=0.0, batch_first=True)
            for _ in range(depth)
        ])

    def forward(self, mel_features: torch.Tensor) -> torch.Tensor:
        # mel_features: [B, Mel_bins, T]
        x = F.gelu(self.conv1(mel_features))
        x = F.gelu(self.conv2(x))
        x = x.transpose(1, 2)  # [B, T//4, embed_dim]
        x = self.norm(x)
        for layer in self.layers:
            x = layer(x)
        return x


class AudioProjector(nn.Module):
    """Audio-to-Language Projector."""

    def __init__(self, audio_dim: int, llm_dim: int):
        super().__init__()
        self.linear_1 = nn.Linear(audio_dim, llm_dim, bias=True)
        self.act = nn.SiLU()
        self.linear_2 = nn.Linear(llm_dim, llm_dim, bias=True)
        self.norm = JerboaRMSNorm(llm_dim)

    def forward(self, audio_features: torch.Tensor) -> torch.Tensor:
        x = self.act(self.linear_1(audio_features))
        x = self.norm(self.linear_2(x))
        return x


class JerboaVLForConditionalGeneration(nn.Module):
    """Jerboa Multimodal Model uniting Vision, Audio, and Language."""

    def __init__(
        self,
        config: JerboaConfig,
        vision_encoder: Optional[nn.Module] = None,
        audio_encoder: Optional[nn.Module] = None,
        vision_dim: Optional[int] = None,
        audio_dim: int = 256,
        use_embedding_gemma: bool = False,
    ):
        super().__init__()
        self.config = config
        self.language_model = JerboaForCausalLM(config)
        self.use_embedding_gemma = use_embedding_gemma

        # Vision Encoder & Projector
        if vision_encoder is not None:
            self.vision_encoder = vision_encoder
            effective_v_dim = vision_dim or 384
        elif use_embedding_gemma:
            self.vision_encoder = EmbeddingGemmaVisionEncoder(freeze=True)
            effective_v_dim = 768
        else:
            effective_v_dim = vision_dim or 384
            self.vision_encoder = VisionPatchEmbed(img_size=224, patch_size=16, embed_dim=effective_v_dim)

        # Vision Projector connects 768-dim EmbeddingGemma or ViT to LLM hidden_size (768)
        self.vision_projector = VisionProjector(
            vision_dim=effective_v_dim,
            llm_dim=config.hidden_size,
            spatial_merge_size=1 if use_embedding_gemma else config.vision_spatial_merge_size,
        )

        # Audio Encoder & Projector
        self.audio_encoder = audio_encoder if audio_encoder is not None else LightweightAudioEncoder(
            num_mel_bins=80, embed_dim=audio_dim, depth=4
        )
        self.audio_projector = AudioProjector(
            audio_dim=audio_dim,
            llm_dim=config.hidden_size,
        )

        self.image_token_id = config.image_token_id
        self.audio_token_id = config.audio_token_id

    def forward(
        self,
        input_ids: torch.LongTensor,
        pixel_values: Optional[torch.FloatTensor] = None,
        audio_values: Optional[torch.FloatTensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        labels: Optional[torch.LongTensor] = None,
        **kwargs,
    ) -> CausalLMOutputWithPast:
        # Get base text token embeddings
        inputs_embeds = self.language_model.model.embed_tokens(input_ids)
        bsz, seq_len, hidden_dim = inputs_embeds.shape

        # Process and inject visual tokens
        if pixel_values is not None and self.image_token_id is not None:
            vision_feats = self.vision_encoder(pixel_values)
            proj_vision = self.vision_projector(vision_feats)  # [B_img, N_vis, hidden_dim]

            for b in range(bsz):
                img_mask = input_ids[b] == self.image_token_id
                img_count = img_mask.sum().item()
                if img_count > 0:
                    vis_tokens = proj_vision[min(b, proj_vision.shape[0] - 1)]
                    if len(vis_tokens) >= img_count:
                        inputs_embeds[b, img_mask] = vis_tokens[:img_count]
                    else:
                        indices = torch.where(img_mask)[0][: len(vis_tokens)]
                        inputs_embeds[b, indices] = vis_tokens

        # Process and inject audio tokens
        if audio_values is not None and self.audio_token_id is not None:
            audio_feats = self.audio_encoder(audio_values)
            proj_audio = self.audio_projector(audio_feats)  # [B_aud, N_aud, hidden_dim]

            for b in range(bsz):
                aud_mask = input_ids[b] == self.audio_token_id
                aud_count = aud_mask.sum().item()
                if aud_count > 0:
                    aud_tokens = proj_audio[min(b, proj_audio.shape[0] - 1)]
                    if len(aud_tokens) >= aud_count:
                        inputs_embeds[b, aud_mask] = aud_tokens[:aud_count]
                    else:
                        indices = torch.where(aud_mask)[0][: len(aud_tokens)]
                        inputs_embeds[b, indices] = aud_tokens

        # Mask labels on image and audio placeholder tokens so loss is computed on response text
        if labels is not None:
            labels = labels.clone()
            if self.image_token_id is not None:
                labels[input_ids == self.image_token_id] = -100
            if self.audio_token_id is not None:
                labels[input_ids == self.audio_token_id] = -100

        return self.language_model(
            inputs_embeds=inputs_embeds,
            attention_mask=attention_mask,
            labels=labels,
            **kwargs,
        )

    def generate(
        self,
        input_ids: torch.LongTensor,
        pixel_values: Optional[torch.FloatTensor] = None,
        audio_values: Optional[torch.FloatTensor] = None,
        max_new_tokens: int = 50,
        temperature: float = 0.7,
        top_p: float = 0.9,
        do_sample: bool = False,
        **kwargs,
    ):
        """Multimodal text generation using fast KV cache decoding."""
        # 1. Prefill step with vision and audio embedding injection
        outputs = self(
            input_ids=input_ids,
            pixel_values=pixel_values,
            audio_values=audio_values,
            use_cache=True,
        )
        past_key_values = outputs.past_key_values
        next_token_logits = outputs.logits[:, -1, :]

        if do_sample and temperature > 0:
            probs = torch.softmax(next_token_logits / temperature, dim=-1)
            next_token = torch.multinomial(probs, num_samples=1)
        else:
            next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)

        generated = [next_token]
        cur_input_ids = next_token

        # 2. Autoregressive decoding with KV cache
        eos_id = kwargs.get("eos_token_id", getattr(self.config, "eos_token_id", None))
        for _ in range(max_new_tokens - 1):
            outputs = self.language_model(
                input_ids=cur_input_ids,
                past_key_values=past_key_values,
                use_cache=True,
            )
            past_key_values = outputs.past_key_values
            next_token_logits = outputs.logits[:, -1, :]

            if do_sample and temperature > 0:
                probs = torch.softmax(next_token_logits / temperature, dim=-1)
                cur_input_ids = torch.multinomial(probs, num_samples=1)
            else:
                cur_input_ids = torch.argmax(next_token_logits, dim=-1, keepdim=True)

            generated.append(cur_input_ids)
            if eos_id is not None and (cur_input_ids == eos_id).all():
                break

        return torch.cat([input_ids] + generated, dim=-1)