File size: 23,962 Bytes
26afca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/env python3
import argparse
import json
from pathlib import Path

import coremltools as ct
import numpy as np
import torch
from PIL import Image, ImageDraw
from qwen_vl_utils import process_vision_info
from transformers import AutoConfig, AutoProcessor, Qwen3_5ForConditionalGeneration
from transformers.models.qwen3_5.modeling_qwen3_5 import (
    get_vision_bilinear_indices_and_weights,
    get_vision_cu_seqlens,
    get_vision_position_ids,
)


PROMPT = (
    "OCR this image. Return only the exact text visible in the image, preserving "
    "Persian, numbers, line breaks, and punctuation. Do not explain."
)


def layer_type(layer: torch.nn.Module) -> str:
    """Bridge the Qwen 3.5 decoder API used by Transformers 5.3 and 5.13+."""
    value = getattr(layer, "layer_type", None)
    if value is None:
        value = getattr(layer, "block_type", None)
    if value not in {"linear_attention", "full_attention"}:
        raise ValueError(f"Unsupported Qwen 3.5 decoder layer type: {value!r}")
    return value


class VisionCoreMLWrapper(torch.nn.Module):
    def __init__(self, visual, image_grid_thw, pixel_values_shape):
        super().__init__()
        self.visual = visual
        self.register_buffer("image_grid_thw", image_grid_thw)
        bilinear_indices, bilinear_weights = get_vision_bilinear_indices_and_weights(
            image_grid_thw,
            num_grid_per_side=visual.num_grid_per_side,
            spatial_merge_size=visual.config.spatial_merge_size,
            kwargs={},
        )
        position_ids = get_vision_position_ids(image_grid_thw, visual.spatial_merge_size, kwargs={})
        cu_seqlens = get_vision_cu_seqlens(image_grid_thw, kwargs={})
        self.seq_len = int(position_ids.shape[0])
        self.patch_rows = int(pixel_values_shape[0])
        self.patch_embed_dim = int(visual.patch_embed.embed_dim)
        self.merger_hidden_size = int(visual.merger.hidden_size)
        self.merger_rows = int(self.seq_len // (visual.spatial_merge_size**2))
        self.register_buffer("bilinear_indices", bilinear_indices.to(torch.long))
        self.register_buffer("bilinear_weights", bilinear_weights.to(torch.float32))
        self.register_buffer("position_ids", position_ids.to(torch.long))
        self.register_buffer("cu_seqlens", cu_seqlens.to(torch.int32))

    def patch_embed_forward(self, pixel_values):
        patch_embed = self.visual.patch_embed
        hidden_states = pixel_values.reshape(
            self.patch_rows,
            patch_embed.in_channels,
            patch_embed.temporal_patch_size,
            patch_embed.patch_size,
            patch_embed.patch_size,
        )
        hidden_states = patch_embed.proj(hidden_states.to(dtype=patch_embed.proj.weight.dtype))
        return hidden_states.reshape(self.seq_len, self.patch_embed_dim)

    def attention_forward(self, attn, hidden_states, position_embeddings):
        query_states, key_states, value_states = (
            attn.qkv(hidden_states)
            .reshape(self.seq_len, 3, attn.num_heads, -1)
            .permute(1, 0, 2, 3)
            .unbind(0)
        )
        cos, sin = position_embeddings
        head_dim = attn.qkv.out_features // (3 * attn.num_heads)
        half_dim = head_dim // 2
        cos = cos.unsqueeze(-2).float()
        sin = sin.unsqueeze(-2).float()

        query_float = query_states.float()
        key_float = key_states.float()
        query_rot = torch.cat(
            (-query_float.narrow(-1, half_dim, half_dim), query_float.narrow(-1, 0, half_dim)),
            dim=-1,
        )
        key_rot = torch.cat(
            (-key_float.narrow(-1, half_dim, half_dim), key_float.narrow(-1, 0, half_dim)),
            dim=-1,
        )
        query_states = ((query_float * cos) + (query_rot * sin)).to(query_states.dtype)
        key_states = ((key_float * cos) + (key_rot * sin)).to(key_states.dtype)

        query_states = query_states.transpose(0, 1).unsqueeze(0)
        key_states = key_states.transpose(0, 1).unsqueeze(0)
        value_states = value_states.transpose(0, 1).unsqueeze(0)

        attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) * attn.scaling
        attn_weights = torch.nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
        attn_output = torch.matmul(attn_weights, value_states)
        attn_output = attn_output.transpose(1, 2).contiguous()
        attn_output = attn_output.reshape(self.seq_len, -1).contiguous()
        return attn.proj(attn_output)

    def forward(self, pixel_values):
        hidden_states = self.patch_embed_forward(pixel_values)
        pos_embeds = (self.visual.pos_embed(self.bilinear_indices) * self.bilinear_weights[:, :, None]).sum(0)
        hidden_states = hidden_states + pos_embeds.to(hidden_states.dtype)
        rotary_pos_emb = self.visual.rotary_pos_emb(self.position_ids)

        hidden_states = hidden_states.reshape(self.seq_len, -1)
        rotary_pos_emb = rotary_pos_emb.reshape(self.seq_len, -1)
        emb = torch.cat((rotary_pos_emb, rotary_pos_emb), dim=-1)
        position_embeddings = (emb.cos(), emb.sin())

        for block in self.visual.blocks:
            hidden_states = hidden_states + self.attention_forward(
                block.attn,
                block.norm1(hidden_states),
                position_embeddings=position_embeddings,
            )
            hidden_states = hidden_states + block.mlp(block.norm2(hidden_states))

        merger = self.visual.merger
        if merger.use_postshuffle_norm:
            hidden_states = hidden_states.reshape(self.merger_rows, self.merger_hidden_size)
        hidden_states = merger.norm(hidden_states).reshape(self.merger_rows, self.merger_hidden_size)
        return merger.linear_fc2(merger.act_fn(merger.linear_fc1(hidden_states)))


class FullLastLogitsCoreMLWrapper(torch.nn.Module):
    def __init__(self, model, image_grid_thw):
        super().__init__()
        self.model = model
        self.register_buffer("image_grid_thw", image_grid_thw)

    def forward(self, input_ids, attention_mask, pixel_values, mm_token_type_ids):
        out = self.model(
            input_ids=input_ids,
            attention_mask=attention_mask,
            pixel_values=pixel_values,
            image_grid_thw=self.image_grid_thw,
            mm_token_type_ids=mm_token_type_ids,
            use_cache=False,
            return_dict=True,
        )
        return out.logits[:, -1:, :]


class LanguageLastLogitsCoreMLWrapper(torch.nn.Module):
    def __init__(self, language_model, lm_head, position_embeddings, seq_len):
        super().__init__()
        self.language_model = language_model
        self.lm_head = lm_head
        cos, sin = position_embeddings
        self.register_buffer("cos", cos)
        self.register_buffer("sin", sin)
        mask = torch.full((1, 1, seq_len, seq_len), torch.finfo(torch.float32).min)
        mask = torch.triu(mask, diagonal=1)
        self.register_buffer("causal_mask", mask)

    @staticmethod
    def rotate_half_static(x, rotary_dim):
        half_dim = rotary_dim // 2
        x1 = x.narrow(-1, 0, half_dim)
        x2 = x.narrow(-1, half_dim, half_dim)
        return torch.cat((-x2, x1), dim=-1)

    def apply_rotary_static(self, q, k):
        cos = self.cos.unsqueeze(1)
        sin = self.sin.unsqueeze(1)
        rotary_dim = int(self.cos.shape[-1])
        q_rot = q.narrow(-1, 0, rotary_dim)
        q_pass = q.narrow(-1, rotary_dim, int(q.shape[-1]) - rotary_dim)
        k_rot = k.narrow(-1, 0, rotary_dim)
        k_pass = k.narrow(-1, rotary_dim, int(k.shape[-1]) - rotary_dim)
        q_embed = (q_rot * cos) + (self.rotate_half_static(q_rot, rotary_dim) * sin)
        k_embed = (k_rot * cos) + (self.rotate_half_static(k_rot, rotary_dim) * sin)
        return torch.cat((q_embed, q_pass), dim=-1), torch.cat((k_embed, k_pass), dim=-1)

    @staticmethod
    def repeat_kv_static(hidden_states, n_rep):
        if n_rep == 1:
            return hidden_states
        batch = int(hidden_states.shape[0])
        num_key_value_heads = int(hidden_states.shape[1])
        seq_len = int(hidden_states.shape[2])
        head_dim = int(hidden_states.shape[3])
        hidden_states = hidden_states[:, :, None, :, :].expand(
            batch,
            num_key_value_heads,
            n_rep,
            seq_len,
            head_dim,
        )
        return hidden_states.reshape(batch, num_key_value_heads * n_rep, seq_len, head_dim)

    def full_attention_forward(self, attn, hidden_states):
        query_states, gate = torch.chunk(
            attn.q_proj(hidden_states).view(1, 300, -1, attn.head_dim * 2),
            2,
            dim=-1,
        )
        gate = gate.reshape(1, 300, -1)

        query_states = attn.q_norm(query_states.view(1, 300, -1, attn.head_dim)).transpose(1, 2)
        key_states = attn.k_norm(attn.k_proj(hidden_states).view(1, 300, -1, attn.head_dim)).transpose(1, 2)
        value_states = attn.v_proj(hidden_states).view(1, 300, -1, attn.head_dim).transpose(1, 2)

        query_states, key_states = self.apply_rotary_static(query_states, key_states)
        key_states = self.repeat_kv_static(key_states, attn.num_key_value_groups)
        value_states = self.repeat_kv_static(value_states, attn.num_key_value_groups)

        attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) * attn.scaling
        attn_weights = attn_weights + self.causal_mask.to(attn_weights.dtype)
        attn_weights = torch.nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
        attn_output = torch.matmul(attn_weights, value_states)
        attn_output = attn_output.transpose(1, 2).contiguous()
        attn_output = attn_output.reshape(1, 300, -1).contiguous()
        attn_output = attn_output * torch.sigmoid(gate)
        return attn.o_proj(attn_output)

    @staticmethod
    def l2norm_static(x):
        return x * torch.rsqrt((x * x).sum(dim=-1, keepdim=True) + 1e-6)

    def chunk_gated_delta_rule_static(self, query, key, value, g, beta):
        chunk_size = 64
        sequence_length = 300
        total_sequence_length = 320
        pad_size = 20

        initial_dtype = query.dtype
        query = self.l2norm_static(query)
        key = self.l2norm_static(key)
        query = query.transpose(1, 2).contiguous().float()
        key = key.transpose(1, 2).contiguous().float()
        value = value.transpose(1, 2).contiguous().float()
        beta = beta.transpose(1, 2).contiguous().float()
        g = g.transpose(1, 2).contiguous().float()

        query = torch.nn.functional.pad(query, (0, 0, 0, pad_size))
        key = torch.nn.functional.pad(key, (0, 0, 0, pad_size))
        value = torch.nn.functional.pad(value, (0, 0, 0, pad_size))
        beta = torch.nn.functional.pad(beta, (0, pad_size))
        g = torch.nn.functional.pad(g, (0, pad_size))

        scale = 1 / (128**0.5)
        query = query * scale

        v_beta = value * beta.unsqueeze(-1)
        k_beta = key * beta.unsqueeze(-1)
        query = query.reshape(1, 16, 5, chunk_size, 128)
        key = key.reshape(1, 16, 5, chunk_size, 128)
        value = value.reshape(1, 16, 5, chunk_size, 128)
        k_beta = k_beta.reshape(1, 16, 5, chunk_size, 128)
        v_beta = v_beta.reshape(1, 16, 5, chunk_size, 128)
        g = g.reshape(1, 16, 5, chunk_size)

        tri0 = torch.triu(torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), diagonal=0)
        tri1 = torch.triu(torch.ones(chunk_size, chunk_size, dtype=torch.bool, device=query.device), diagonal=1)
        g_cum = g.cumsum(dim=-1)
        decay_mask = ((g_cum.unsqueeze(-1) - g_cum.unsqueeze(-2)).tril().exp().float()).tril()
        attn = -((k_beta @ key.transpose(-1, -2)) * decay_mask).masked_fill(tri0, 0)
        # Fixed-size triangular recurrence from torch_chunk_gated_delta_rule.
        for i in range(1, chunk_size):
            row = attn[..., i, :i].clone()
            sub = attn[..., :i, :i].clone()
            update = row + (row.unsqueeze(-1) * sub).sum(-2)
            suffix = attn[..., i, i:]
            new_row = torch.cat((update, suffix), dim=-1)
            before = attn[..., :i, :]
            after = attn[..., i + 1 :, :]
            attn = torch.cat((before, new_row.unsqueeze(-2), after), dim=-2)
        attn = attn + torch.eye(chunk_size, dtype=attn.dtype, device=query.device)
        value = attn @ v_beta
        k_cumdecay = attn @ (k_beta * g_cum.exp().unsqueeze(-1))

        last_recurrent_state = torch.zeros(1, 16, 128, 128, dtype=value.dtype, device=value.device)
        outs = []
        for i in range(5):
            q_i = query[:, :, i]
            k_i = key[:, :, i]
            v_i = value[:, :, i]
            attn_i = (q_i @ k_i.transpose(-1, -2) * decay_mask[:, :, i]).masked_fill(tri1, 0)
            v_prime = k_cumdecay[:, :, i] @ last_recurrent_state
            v_new = v_i - v_prime
            attn_inter = (q_i * g_cum[:, :, i, :, None].exp()) @ last_recurrent_state
            outs.append(attn_inter + attn_i @ v_new)
            last_recurrent_state = (
                last_recurrent_state * g_cum[:, :, i, -1, None, None].exp()
                + (k_i * (g_cum[:, :, i, -1, None] - g_cum[:, :, i]).exp()[..., None]).transpose(-1, -2)
                @ v_new
            )

        core_attn_out = torch.stack(outs, dim=2)
        core_attn_out = core_attn_out.reshape(1, 16, total_sequence_length, 128)
        core_attn_out = core_attn_out[:, :, :sequence_length]
        return core_attn_out.transpose(1, 2).contiguous().to(initial_dtype)

    def gated_delta_forward_static(self, linear_attn, hidden_states):
        batch_size = 1
        seq_len = 300
        mixed_qkv = linear_attn.in_proj_qkv(hidden_states).transpose(1, 2)
        z = linear_attn.in_proj_z(hidden_states).reshape(batch_size, seq_len, -1, linear_attn.head_v_dim)
        b = linear_attn.in_proj_b(hidden_states)
        a = linear_attn.in_proj_a(hidden_states)

        mixed_qkv = torch.nn.functional.silu(linear_attn.conv1d(mixed_qkv)[:, :, :seq_len])
        mixed_qkv = mixed_qkv.transpose(1, 2)
        query, key, value = torch.split(
            mixed_qkv,
            [
                linear_attn.key_dim,
                linear_attn.key_dim,
                linear_attn.value_dim,
            ],
            dim=-1,
        )
        query = query.reshape(batch_size, seq_len, -1, linear_attn.head_k_dim)
        key = key.reshape(batch_size, seq_len, -1, linear_attn.head_k_dim)
        value = value.reshape(batch_size, seq_len, -1, linear_attn.head_v_dim)
        beta = b.sigmoid()
        g = -linear_attn.A_log.float().exp() * torch.nn.functional.softplus(a.float() + linear_attn.dt_bias)

        core_attn_out = self.chunk_gated_delta_rule_static(query, key, value, g, beta)
        core_attn_out = core_attn_out.reshape(-1, linear_attn.head_v_dim)
        z = z.reshape(-1, linear_attn.head_v_dim)
        normed = linear_attn.norm(core_attn_out, z)
        normed = normed.reshape(batch_size, seq_len, -1)
        return linear_attn.out_proj(normed)

    def forward(self, inputs_embeds):
        hidden_states = inputs_embeds
        for layer in self.language_model.layers:
            residual = hidden_states
            hidden_states = layer.input_layernorm(hidden_states)
            if layer_type(layer) == "linear_attention":
                hidden_states = self.gated_delta_forward_static(layer.linear_attn, hidden_states)
            else:
                hidden_states = self.full_attention_forward(layer.self_attn, hidden_states)
            hidden_states = residual + hidden_states

            residual = hidden_states
            hidden_states = layer.post_attention_layernorm(hidden_states)
            hidden_states = layer.mlp(hidden_states)
            hidden_states = residual + hidden_states

        hidden_states = self.language_model.norm(hidden_states)
        hidden = hidden_states[:, -1:, :]
        return self.lm_head(hidden)


def load_model(model_id: str, dtype: torch.dtype):
    config = AutoConfig.from_pretrained(model_id, trust_remote_code=True)
    config._attn_implementation = "eager"
    if hasattr(config, "text_config"):
        config.text_config._attn_implementation = "eager"
    if hasattr(config, "vision_config"):
        config.vision_config._attn_implementation = "eager"
    model = Qwen3_5ForConditionalGeneration.from_pretrained(
        model_id,
        config=config,
        torch_dtype=dtype,
        device_map="cpu",
        low_cpu_mem_usage=True,
        trust_remote_code=True,
        attn_implementation="eager",
    ).eval()
    model.config._attn_implementation = "eager"
    if hasattr(model.config, "text_config"):
        model.config.text_config._attn_implementation = "eager"
    return model


def build_sample(processor):
    image = Image.new("RGB", (512, 512), "white")
    draw = ImageDraw.Draw(image)
    draw.text((40, 80), "Invoice 123\nTotal $42.00", fill="black")
    messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": PROMPT}]}]
    text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    image_inputs, video_inputs = process_vision_info(messages)
    return processor(text=[text], images=image_inputs, videos=video_inputs, return_tensors="pt")


def main() -> None:
    parser = argparse.ArgumentParser(description="Export Bina 0.1 split/full CoreML canaries from source BF16 weights.")
    parser.add_argument("--model-id", default="Reza2kn/Bina-0.1-Koochik")
    parser.add_argument("--output-dir", required=True)
    parser.add_argument("--mode", choices=["vision", "full", "language"], default="vision")
    parser.add_argument("--dtype", choices=["float16", "float32"], default="float16")
    parser.add_argument(
        "--compute-precision",
        choices=["float16", "float32"],
        default="float16",
        help="CoreML ML Program compute precision; the source checkpoint remains BF16.",
    )
    args = parser.parse_args()

    output_dir = Path(args.output_dir).expanduser().resolve()
    output_dir.mkdir(parents=True, exist_ok=True)
    dtype = torch.float16 if args.dtype == "float16" else torch.float32
    np_dtype = np.float16 if args.dtype == "float16" else np.float32
    compute_precision = (
        ct.precision.FLOAT16 if args.compute_precision == "float16" else ct.precision.FLOAT32
    )
    precision_tag = "fp16" if args.compute_precision == "float16" else "fp32"

    processor = AutoProcessor.from_pretrained(args.model_id, trust_remote_code=True)
    model = load_model(args.model_id, dtype)
    sample = build_sample(processor)
    input_ids = sample["input_ids"].to(torch.int64)
    attention_mask = sample["attention_mask"].to(torch.int64)
    mm_token_type_ids = sample["mm_token_type_ids"].to(torch.int64)
    pixel_values = sample["pixel_values"].to(dtype)
    image_grid_thw = sample["image_grid_thw"].to(torch.int64)

    if args.mode == "vision":
        wrapper = VisionCoreMLWrapper(model.model.visual, image_grid_thw, tuple(pixel_values.shape)).eval()
        example = (pixel_values,)
        traced = torch.jit.trace(wrapper, example, strict=False)
        package_path = output_dir / f"surya_vision_{precision_tag}.mlpackage"
        mlmodel = ct.convert(
            traced,
            convert_to="mlprogram",
            minimum_deployment_target=ct.target.macOS14,
            compute_precision=compute_precision,
            inputs=[ct.TensorType(name="pixel_values", shape=tuple(pixel_values.shape), dtype=np_dtype)],
            outputs=[ct.TensorType(name="image_embeds")],
        )
    elif args.mode == "full":
        wrapper = FullLastLogitsCoreMLWrapper(model, image_grid_thw).eval()
        example = (input_ids, attention_mask, pixel_values, mm_token_type_ids)
        traced = torch.jit.trace(wrapper, example, strict=False)
        package_path = output_dir / f"surya_full_last_logits_{precision_tag}.mlpackage"
        mlmodel = ct.convert(
            traced,
            convert_to="mlprogram",
            minimum_deployment_target=ct.target.macOS14,
            compute_precision=compute_precision,
            inputs=[
                ct.TensorType(name="input_ids", shape=tuple(input_ids.shape), dtype=np.int32),
                ct.TensorType(name="attention_mask", shape=tuple(attention_mask.shape), dtype=np.int32),
                ct.TensorType(name="pixel_values", shape=tuple(pixel_values.shape), dtype=np_dtype),
                ct.TensorType(name="mm_token_type_ids", shape=tuple(mm_token_type_ids.shape), dtype=np.int32),
            ],
            outputs=[ct.TensorType(name="logits")],
        )
    else:
        with torch.no_grad():
            inputs_embeds = model.model.get_input_embeddings()(input_ids)
            image_outputs = model.model.get_image_features(pixel_values, image_grid_thw, return_dict=True)
            image_embeds = torch.cat(image_outputs.pooler_output, dim=0).to(inputs_embeds.device, inputs_embeds.dtype)
            image_mask, _ = model.model.get_placeholder_mask(
                input_ids,
                inputs_embeds=inputs_embeds,
                image_features=image_embeds,
            )
            inputs_embeds = inputs_embeds.masked_scatter(image_mask, image_embeds)
            position_ids = model.model.compute_3d_position_ids(
                input_ids=input_ids,
                image_grid_thw=image_grid_thw,
                video_grid_thw=None,
                inputs_embeds=inputs_embeds,
                attention_mask=attention_mask,
                past_key_values=None,
                mm_token_type_ids=mm_token_type_ids,
            )

        position_embeddings = model.model.language_model.rotary_emb(inputs_embeds, position_ids)
        wrapper = LanguageLastLogitsCoreMLWrapper(
            model.model.language_model,
            model.lm_head,
            position_embeddings,
            int(inputs_embeds.shape[1]),
        ).eval()
        example = (inputs_embeds,)
        traced = torch.jit.trace(wrapper, example, strict=False)
        package_path = output_dir / f"surya_language_last_logits_{precision_tag}.mlpackage"
        mlmodel = ct.convert(
            traced,
            convert_to="mlprogram",
            minimum_deployment_target=ct.target.macOS14,
            compute_precision=compute_precision,
            inputs=[
                ct.TensorType(name="inputs_embeds", shape=tuple(inputs_embeds.shape), dtype=np_dtype),
            ],
            outputs=[ct.TensorType(name="logits")],
        )

    mlmodel.save(str(package_path))
    processor.save_pretrained(output_dir / "processor")
    (output_dir / "export_config.json").write_text(
        json.dumps(
            {
                "model_id": args.model_id,
                "mode": args.mode,
                "source_dtype": "bf16",
                "coreml_compute_precision": precision_tag,
                "sample_shapes": {
                    "input_ids": list(input_ids.shape),
                    "attention_mask": list(attention_mask.shape),
                    "mm_token_type_ids": list(mm_token_type_ids.shape),
                    "pixel_values": list(pixel_values.shape),
                    "image_grid_thw": list(image_grid_thw.shape),
                    **(
                        {
                            "inputs_embeds": list(inputs_embeds.shape),
                            "position_ids": list(position_ids.shape),
                        }
                        if args.mode == "language"
                        else {}
                    ),
                },
                "package": str(package_path),
            },
            indent=2,
        )
        + "\n",
        encoding="utf-8",
    )
    print(json.dumps({"package": str(package_path), "mode": args.mode}, indent=2), flush=True)


if __name__ == "__main__":
    main()