nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc8__cp310-cp310-macosx_14_0_universal2.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nexaai might be problematic. Click here for more details.
- nexaai/_stub.cpython-310-darwin.so +0 -0
- nexaai/_version.py +1 -1
- nexaai/binds/libnexa_bridge.dylib +0 -0
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/METADATA +1 -1
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/RECORD +7 -196
- nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
- nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
- nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
- nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
- nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
- nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
- nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
- nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
- nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
- nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
- nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
- nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
- nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
- nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
- nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
- nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
- nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
- nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
- nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
- nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
- nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
- nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
- nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
- nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
- nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
- nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
- nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
- nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
- nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/WHEEL +0 -0
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/top_level.txt +0 -0
|
@@ -1,526 +0,0 @@
|
|
|
1
|
-
import copy
|
|
2
|
-
import inspect
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from functools import partial
|
|
5
|
-
from math import sqrt
|
|
6
|
-
from typing import Dict, Optional, Union
|
|
7
|
-
|
|
8
|
-
import cv2
|
|
9
|
-
import mlx.core as mx
|
|
10
|
-
import mlx.nn as nn
|
|
11
|
-
import numpy as np
|
|
12
|
-
|
|
13
|
-
from .sam import SAMEncoder
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@dataclass
|
|
17
|
-
class VisionConfig:
|
|
18
|
-
model_type: str
|
|
19
|
-
num_hidden_layers: int = 24
|
|
20
|
-
hidden_size: int = 1024
|
|
21
|
-
intermediate_size: int = 4096
|
|
22
|
-
num_attention_heads: int = 16
|
|
23
|
-
image_size: int = 384
|
|
24
|
-
patch_size: int = 16
|
|
25
|
-
num_channels: int = 3
|
|
26
|
-
layer_norm_eps: float = 1e-5
|
|
27
|
-
cls: str = None
|
|
28
|
-
params: dict = None
|
|
29
|
-
|
|
30
|
-
def __post_init__(self):
|
|
31
|
-
if "high_res_cfg" in self.params:
|
|
32
|
-
self.image_size = self.params["high_res_cfg"]["image_size"]
|
|
33
|
-
|
|
34
|
-
@classmethod
|
|
35
|
-
def from_dict(cls, params):
|
|
36
|
-
return cls(
|
|
37
|
-
**{
|
|
38
|
-
k: v
|
|
39
|
-
for k, v in params.items()
|
|
40
|
-
if k in inspect.signature(cls).parameters
|
|
41
|
-
}
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
@dataclass
|
|
46
|
-
class MLPConfig:
|
|
47
|
-
hidden_size: int
|
|
48
|
-
intermediate_size: int
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def check_array_shape(arr):
|
|
52
|
-
shape = arr.shape
|
|
53
|
-
|
|
54
|
-
# Check if the shape has 4 dimensions
|
|
55
|
-
if len(shape) != 4:
|
|
56
|
-
return False
|
|
57
|
-
|
|
58
|
-
out_channels, kH, KW, _ = shape
|
|
59
|
-
|
|
60
|
-
# Check if out_channels is the largest, and kH and KW are the same
|
|
61
|
-
if (out_channels >= kH) and (out_channels >= KW) and (kH == KW):
|
|
62
|
-
return True
|
|
63
|
-
else:
|
|
64
|
-
return False
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class AttentionPoolLatent(nn.Module):
|
|
68
|
-
"""Attention pooling w/ latent query"""
|
|
69
|
-
|
|
70
|
-
def __init__(
|
|
71
|
-
self,
|
|
72
|
-
in_features: int,
|
|
73
|
-
out_features: int = None,
|
|
74
|
-
embed_dim: int = None,
|
|
75
|
-
num_heads: int = 8,
|
|
76
|
-
mlp_ratio: float = 4.0,
|
|
77
|
-
qkv_bias: bool = True,
|
|
78
|
-
qk_norm: bool = False,
|
|
79
|
-
latent_len: int = 1,
|
|
80
|
-
latent_dim: int = None,
|
|
81
|
-
pos_embed: str = "",
|
|
82
|
-
pool_type: str = "token",
|
|
83
|
-
norm_layer: Optional[nn.Module] = None,
|
|
84
|
-
drop: float = 0.0,
|
|
85
|
-
):
|
|
86
|
-
super().__init__()
|
|
87
|
-
|
|
88
|
-
embed_dim = embed_dim or in_features
|
|
89
|
-
out_features = out_features or in_features
|
|
90
|
-
assert embed_dim % num_heads == 0
|
|
91
|
-
self.num_heads = num_heads
|
|
92
|
-
self.head_dim = embed_dim // num_heads
|
|
93
|
-
self.scale = self.head_dim**-0.5
|
|
94
|
-
self.pool = pool_type
|
|
95
|
-
|
|
96
|
-
self.latent_dim = latent_dim or embed_dim
|
|
97
|
-
self.latent_len = latent_len
|
|
98
|
-
self.latent = mx.zeros((self.latent_len, embed_dim))[None, :]
|
|
99
|
-
|
|
100
|
-
self.q = nn.Linear(embed_dim, embed_dim, bias=qkv_bias)
|
|
101
|
-
self.kv = nn.Linear(embed_dim, embed_dim * 2, bias=qkv_bias)
|
|
102
|
-
self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
|
103
|
-
self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
|
|
104
|
-
self.proj = nn.Linear(embed_dim, embed_dim)
|
|
105
|
-
self.proj_drop = nn.Dropout(drop)
|
|
106
|
-
|
|
107
|
-
if pos_embed == "abs":
|
|
108
|
-
spatial_len = self.feat_size
|
|
109
|
-
self.pos_embed = mx.zeros((spatial_len, in_features))
|
|
110
|
-
else:
|
|
111
|
-
self.pos_embed = None
|
|
112
|
-
|
|
113
|
-
self.norm = nn.LayerNorm(out_features)
|
|
114
|
-
config = MLPConfig(
|
|
115
|
-
hidden_size=embed_dim, intermediate_size=int(embed_dim * mlp_ratio)
|
|
116
|
-
)
|
|
117
|
-
self.mlp = MLP(config)
|
|
118
|
-
|
|
119
|
-
def __call__(self, x: mx.array):
|
|
120
|
-
B, N, C = x.shape
|
|
121
|
-
|
|
122
|
-
if self.pos_embed is not None:
|
|
123
|
-
x = x + self.pos_embed.unsqueeze(0).to(x.dtype)
|
|
124
|
-
|
|
125
|
-
q_latent = mx.array(self.latent)
|
|
126
|
-
|
|
127
|
-
q = (
|
|
128
|
-
self.q(q_latent)
|
|
129
|
-
.reshape(B, self.latent_len, self.num_heads, self.head_dim)
|
|
130
|
-
.transpose(0, 2, 1, 3)
|
|
131
|
-
)
|
|
132
|
-
|
|
133
|
-
kv = (
|
|
134
|
-
self.kv(x)
|
|
135
|
-
.reshape(B, N, 2, self.num_heads, self.head_dim)
|
|
136
|
-
.transpose(2, 0, 3, 1, 4)
|
|
137
|
-
)
|
|
138
|
-
k, v = mx.split(kv, 2, axis=0)
|
|
139
|
-
|
|
140
|
-
q, k = self.q_norm(q), self.k_norm(k)
|
|
141
|
-
|
|
142
|
-
x = mx.fast.scaled_dot_product_attention(
|
|
143
|
-
q, k[0], v[0], scale=(1.0 / sqrt(q.shape[-1])), mask=None
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
x = x.transpose(0, 2, 1, 3).reshape(B, self.latent_len, C)
|
|
147
|
-
x = self.proj(x)
|
|
148
|
-
x = self.proj_drop(x)
|
|
149
|
-
|
|
150
|
-
x = x + self.mlp(self.norm(x))
|
|
151
|
-
|
|
152
|
-
# optional pool if latent seq_len > 1 and pooled output is desired
|
|
153
|
-
if self.pool == "token":
|
|
154
|
-
x = x[:, 0]
|
|
155
|
-
elif self.pool == "avg":
|
|
156
|
-
x = x.mean(1)
|
|
157
|
-
return x
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
class Attention(nn.Module):
|
|
161
|
-
def __init__(
|
|
162
|
-
self,
|
|
163
|
-
dims: int,
|
|
164
|
-
num_heads: int,
|
|
165
|
-
qkv_bias: bool = False,
|
|
166
|
-
):
|
|
167
|
-
super().__init__()
|
|
168
|
-
|
|
169
|
-
if (dims % num_heads) != 0:
|
|
170
|
-
raise ValueError(
|
|
171
|
-
"The input feature dimensions should be divisible by the "
|
|
172
|
-
f"number of heads ({dims} % {num_heads}) != 0"
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
self.num_heads = num_heads = num_heads
|
|
176
|
-
head_dim = dims // num_heads
|
|
177
|
-
self.scale = head_dim**-0.5
|
|
178
|
-
|
|
179
|
-
self.qkv = nn.Linear(dims, dims * 3, bias=qkv_bias)
|
|
180
|
-
self.proj = nn.Linear(dims, dims, bias=True)
|
|
181
|
-
|
|
182
|
-
def __call__(self, x, mask=None):
|
|
183
|
-
qkv = self.qkv(x)
|
|
184
|
-
queries, keys, values = mx.split(qkv, 3, axis=-1)
|
|
185
|
-
|
|
186
|
-
num_heads = self.num_heads
|
|
187
|
-
B, L, D = queries.shape
|
|
188
|
-
_, S, _ = keys.shape
|
|
189
|
-
queries = queries.reshape(B, L, num_heads, -1).transpose(0, 2, 1, 3)
|
|
190
|
-
keys = keys.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
|
|
191
|
-
values = values.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
|
|
192
|
-
|
|
193
|
-
output = mx.fast.scaled_dot_product_attention(
|
|
194
|
-
queries, keys, values, scale=self.scale, mask=mask
|
|
195
|
-
)
|
|
196
|
-
output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
|
|
197
|
-
|
|
198
|
-
return self.proj(output)
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
class FastGELUActivation(nn.Module):
|
|
202
|
-
"""
|
|
203
|
-
Applies GELU approximation that is slower than QuickGELU but more accurate. See: https://github.com/hendrycks/GELUs
|
|
204
|
-
"""
|
|
205
|
-
|
|
206
|
-
def __call__(self, input: mx.array) -> mx.array:
|
|
207
|
-
return (
|
|
208
|
-
0.5
|
|
209
|
-
* input
|
|
210
|
-
* (1.0 + mx.tanh(np.sqrt(2 / np.pi) * (input + 0.044715 * (input**3))))
|
|
211
|
-
).astype(input.dtype)
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
class MLP(nn.Module):
|
|
215
|
-
def __init__(self, config: Union[VisionConfig, Dict], bias: bool = True):
|
|
216
|
-
super().__init__()
|
|
217
|
-
self.activation_fn = FastGELUActivation()
|
|
218
|
-
self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size, bias=bias)
|
|
219
|
-
self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size, bias=bias)
|
|
220
|
-
|
|
221
|
-
def __call__(self, x: mx.array) -> mx.array:
|
|
222
|
-
x = self.activation_fn(self.fc1(x))
|
|
223
|
-
x = self.fc2(x)
|
|
224
|
-
return x
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
class EncoderLayer(nn.Module):
|
|
228
|
-
def __init__(self, config: VisionConfig):
|
|
229
|
-
super().__init__()
|
|
230
|
-
self.embed_dim = config.hidden_size
|
|
231
|
-
self.attn = Attention(
|
|
232
|
-
config.hidden_size, config.num_attention_heads, qkv_bias=True
|
|
233
|
-
)
|
|
234
|
-
self.norm1 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
|
|
235
|
-
self.mlp = MLP(config)
|
|
236
|
-
self.norm2 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
|
|
237
|
-
|
|
238
|
-
def __call__(self, x: mx.array, mask: Optional[mx.array] = None) -> mx.array:
|
|
239
|
-
y = self.norm1(x)
|
|
240
|
-
y = self.attn(y, mask)
|
|
241
|
-
x = x + y
|
|
242
|
-
y = self.norm2(x)
|
|
243
|
-
y = self.mlp(y)
|
|
244
|
-
return x + y
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
class VisionEmbeddings(nn.Module):
|
|
248
|
-
def __init__(self, config: VisionConfig, norm_layer: bool = False):
|
|
249
|
-
super().__init__()
|
|
250
|
-
self.config = config
|
|
251
|
-
self.embed_dim = config.hidden_size
|
|
252
|
-
self.image_size = config.image_size
|
|
253
|
-
self.patch_size = config.patch_size
|
|
254
|
-
|
|
255
|
-
self.proj = nn.Conv2d(
|
|
256
|
-
in_channels=config.num_channels,
|
|
257
|
-
out_channels=self.embed_dim,
|
|
258
|
-
kernel_size=self.patch_size,
|
|
259
|
-
stride=self.patch_size,
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
self.num_patches = (self.image_size // self.patch_size) ** 2
|
|
263
|
-
self.num_positions = self.num_patches
|
|
264
|
-
|
|
265
|
-
self.norm = (
|
|
266
|
-
nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
|
|
267
|
-
if norm_layer
|
|
268
|
-
else nn.Identity()
|
|
269
|
-
)
|
|
270
|
-
|
|
271
|
-
def __call__(self, x: mx.array) -> mx.array:
|
|
272
|
-
patch_embeddings = self.proj(x)
|
|
273
|
-
patch_embeddings = mx.flatten(patch_embeddings, start_axis=1, end_axis=2)
|
|
274
|
-
return self.norm(patch_embeddings)
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
class SigLipVisionModel(nn.Module):
|
|
278
|
-
def __init__(
|
|
279
|
-
self,
|
|
280
|
-
config: VisionConfig,
|
|
281
|
-
ignore_head: bool,
|
|
282
|
-
pre_norm: bool = False,
|
|
283
|
-
no_embed_class: bool = True,
|
|
284
|
-
):
|
|
285
|
-
super().__init__()
|
|
286
|
-
self.num_prefix_tokens = 1
|
|
287
|
-
self.no_embed_class = False
|
|
288
|
-
self.dynamic_img_size = False
|
|
289
|
-
self.ignore_head = ignore_head
|
|
290
|
-
self.cls_token = None
|
|
291
|
-
self.reg_token = None
|
|
292
|
-
self.patch_embed = VisionEmbeddings(config)
|
|
293
|
-
self.norm_pre = nn.LayerNorm(config.hidden_size) if pre_norm else nn.Identity()
|
|
294
|
-
self.blocks = [EncoderLayer(config) for _ in range(config.num_hidden_layers)]
|
|
295
|
-
self.norm = nn.LayerNorm(config.hidden_size)
|
|
296
|
-
num_patches = self.patch_embed.num_patches
|
|
297
|
-
embed_len = (
|
|
298
|
-
num_patches if no_embed_class else num_patches + self.num_prefix_tokens
|
|
299
|
-
)
|
|
300
|
-
self.pos_embed = mx.random.normal((embed_len, config.hidden_size))[None, :]
|
|
301
|
-
|
|
302
|
-
norm_layer = partial(nn.LayerNorm, eps=1e-5)
|
|
303
|
-
self.attn_pool = AttentionPoolLatent(
|
|
304
|
-
config.hidden_size,
|
|
305
|
-
num_heads=config.num_attention_heads,
|
|
306
|
-
norm_layer=norm_layer,
|
|
307
|
-
)
|
|
308
|
-
|
|
309
|
-
def __call__(
|
|
310
|
-
self,
|
|
311
|
-
x: mx.array,
|
|
312
|
-
output_hidden_states: Optional[bool] = None,
|
|
313
|
-
) -> mx.array:
|
|
314
|
-
x = self.patch_embed(x)
|
|
315
|
-
x += self.pos_embed
|
|
316
|
-
x = self.norm_pre(x)
|
|
317
|
-
|
|
318
|
-
encoder_states = (x,) if output_hidden_states else None
|
|
319
|
-
for l in self.blocks:
|
|
320
|
-
x = l(x, mask=None)
|
|
321
|
-
if output_hidden_states:
|
|
322
|
-
encoder_states = encoder_states + (x,)
|
|
323
|
-
|
|
324
|
-
pooler_output = self.norm(x)
|
|
325
|
-
|
|
326
|
-
if not self.ignore_head:
|
|
327
|
-
pooler_output = self.attn_pool(pooler_output)
|
|
328
|
-
return pooler_output, x, encoder_states
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
class HybridVisionModel(nn.Module):
|
|
332
|
-
def __init__(self, config: VisionConfig, resolution: str, ignore_head: bool = True):
|
|
333
|
-
super().__init__()
|
|
334
|
-
|
|
335
|
-
self.model_type = config.model_type
|
|
336
|
-
self.resolution = resolution
|
|
337
|
-
if self.model_type != "vision":
|
|
338
|
-
raise ValueError(f"Unsupported model type: {self.model_type}")
|
|
339
|
-
|
|
340
|
-
if resolution == "high":
|
|
341
|
-
self.vision_tower = SAMEncoder()
|
|
342
|
-
else:
|
|
343
|
-
self.vision_tower = SigLipVisionModel(config, ignore_head)
|
|
344
|
-
|
|
345
|
-
def __call__(self, x: mx.array) -> mx.array:
|
|
346
|
-
if self.resolution == "high":
|
|
347
|
-
return self.vision_tower(x)
|
|
348
|
-
else:
|
|
349
|
-
return self.vision_tower(x)[0]
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
# def resize_image(image, size, antialias=True):
|
|
353
|
-
# """
|
|
354
|
-
# Resize an image using scipy.ndimage.zoom with an option for bicubic interpolation.
|
|
355
|
-
|
|
356
|
-
# Args:
|
|
357
|
-
# image (numpy.ndarray): The input image array.
|
|
358
|
-
# size (tuple): The target size as (width, height).
|
|
359
|
-
# antialias (bool): True to use bicubic interpolation, False to use nearest neighbor.
|
|
360
|
-
|
|
361
|
-
# Returns:
|
|
362
|
-
# numpy.ndarray: The resized image array.
|
|
363
|
-
# """
|
|
364
|
-
# # Ensure the image is an array and remove singleton dimensions
|
|
365
|
-
# image = np.array(image[0])
|
|
366
|
-
|
|
367
|
-
# # Calculate zoom factors for the spatial dimensions
|
|
368
|
-
# # Note: size is expected as (width, height) but image.shape gives (height, width)
|
|
369
|
-
# current_height, current_width = image.shape[:2]
|
|
370
|
-
# width_factor = size[0] / current_width
|
|
371
|
-
# height_factor = size[1] / current_height
|
|
372
|
-
# zoom_factors = (height_factor, width_factor) # Apply zoom to height and width
|
|
373
|
-
|
|
374
|
-
# # Choose the interpolation order: 3 for bicubic, 0 for nearest
|
|
375
|
-
# order = 3 if antialias else 0
|
|
376
|
-
|
|
377
|
-
# # Apply zoom to the image. Handle both grayscale and color images.
|
|
378
|
-
# if image.ndim == 2: # Grayscale image
|
|
379
|
-
# resized_image = zoom(image, zoom_factors, order=order)
|
|
380
|
-
# elif image.ndim == 3: # Color image
|
|
381
|
-
# # Apply zoom separately for each channel
|
|
382
|
-
# resized_channels = [
|
|
383
|
-
# zoom(image[:, :, i], zoom_factors, order=order)
|
|
384
|
-
# for i in range(image.shape[2])
|
|
385
|
-
# ]
|
|
386
|
-
# resized_image = np.stack(resized_channels, axis=2)
|
|
387
|
-
|
|
388
|
-
# return resized_image
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
# TODO: Match the output of scipy.ndimage.zoom
|
|
392
|
-
def resize_image(image, size, antialias=True):
|
|
393
|
-
"""
|
|
394
|
-
Resize an image with OpenCV.
|
|
395
|
-
|
|
396
|
-
Args:
|
|
397
|
-
image (numpy.ndarray): The input image array. Supports H × W or H × W × C.
|
|
398
|
-
If you pass in a batch (N × H × W × C) just slice the
|
|
399
|
-
element you want, e.g. image[0].
|
|
400
|
-
size (tuple): Target size as (width, height) — exactly the same order that
|
|
401
|
-
cv2.resize expects.
|
|
402
|
-
antialias (bool):
|
|
403
|
-
* True → high‑quality interpolation (bicubic for upscaling, area for downscaling)
|
|
404
|
-
* False → nearest‑neighbor (fast, blocky)
|
|
405
|
-
|
|
406
|
-
Returns:
|
|
407
|
-
numpy.ndarray: The resized image array.
|
|
408
|
-
"""
|
|
409
|
-
img = np.ascontiguousarray(np.asarray(image))
|
|
410
|
-
if img.ndim == 4 and img.shape[0] == 1: # squeeze stray batch dim
|
|
411
|
-
img = img[0]
|
|
412
|
-
h0, w0 = img.shape[:2]
|
|
413
|
-
|
|
414
|
-
# --- work out dsize vs fx/fy ---------------------------------------------
|
|
415
|
-
dsize = None
|
|
416
|
-
fx = fy = 0.0
|
|
417
|
-
if isinstance(size, (int, float)): # uniform scale
|
|
418
|
-
fx = fy = float(size)
|
|
419
|
-
elif isinstance(size, (tuple, list)) and len(size) == 2:
|
|
420
|
-
a, b = size
|
|
421
|
-
# Heuristic: treat "small" floats as scale factors
|
|
422
|
-
if all(isinstance(x, (int, float)) and x < 10 for x in (a, b)):
|
|
423
|
-
fx, fy = float(a), float(b) # scale factors
|
|
424
|
-
else:
|
|
425
|
-
dsize = (int(a), int(b)) # absolute pixels
|
|
426
|
-
else:
|
|
427
|
-
raise ValueError("target must be scalar or a 2‑tuple")
|
|
428
|
-
|
|
429
|
-
# Guard against zeros after int‑casting
|
|
430
|
-
if dsize:
|
|
431
|
-
if dsize[0] <= 0 or dsize[1] <= 0:
|
|
432
|
-
raise ValueError(f"dsize became {dsize}")
|
|
433
|
-
else:
|
|
434
|
-
if fx <= 0 or fy <= 0:
|
|
435
|
-
raise ValueError(f"fx,fy became {(fx, fy)}")
|
|
436
|
-
|
|
437
|
-
# --- choose interpolation -------------------------------------------------
|
|
438
|
-
if antialias:
|
|
439
|
-
# Use Lanczos interpolation for potentially better detail preservation
|
|
440
|
-
interp = cv2.INTER_LANCZOS4
|
|
441
|
-
else:
|
|
442
|
-
interp = cv2.INTER_NEAREST
|
|
443
|
-
|
|
444
|
-
# --- call OpenCV ----------------------------------------------------------
|
|
445
|
-
return mx.array(cv2.resize(img, dsize=dsize, fx=fx, fy=fy, interpolation=interp))
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
class VisionModel(nn.Module):
|
|
449
|
-
def __init__(self, config: VisionConfig, ignore_head: bool = True):
|
|
450
|
-
super().__init__()
|
|
451
|
-
|
|
452
|
-
self.model_type = config.model_type
|
|
453
|
-
self.config = config
|
|
454
|
-
if self.model_type != "vision":
|
|
455
|
-
raise ValueError(f"Unsupported model type: {self.model_type}")
|
|
456
|
-
|
|
457
|
-
if config.cls == "HybridVisionTower":
|
|
458
|
-
self.high_layer_norm = nn.LayerNorm(
|
|
459
|
-
config.params["high_res_cfg"]["output_dim"]
|
|
460
|
-
)
|
|
461
|
-
self.low_layer_norm = nn.LayerNorm(
|
|
462
|
-
config.params["low_res_cfg"]["output_dim"]
|
|
463
|
-
)
|
|
464
|
-
|
|
465
|
-
high_res_cfg = copy.deepcopy(config)
|
|
466
|
-
high_res_cfg.image_size = config.params["high_res_cfg"]["image_size"]
|
|
467
|
-
self.vision_tower_high = HybridVisionModel(
|
|
468
|
-
high_res_cfg, "high", ignore_head
|
|
469
|
-
)
|
|
470
|
-
|
|
471
|
-
low_res_cfg = copy.deepcopy(config)
|
|
472
|
-
low_res_cfg.image_size = config.params["low_res_cfg"]["image_size"]
|
|
473
|
-
|
|
474
|
-
self.vision_tower_low = HybridVisionModel(low_res_cfg, "low", ignore_head)
|
|
475
|
-
self.low_res_size = config.params["low_res_cfg"]["image_size"]
|
|
476
|
-
self.resize = lambda image: resize_image(
|
|
477
|
-
image, (self.low_res_size, self.low_res_size), antialias=True
|
|
478
|
-
)
|
|
479
|
-
|
|
480
|
-
else:
|
|
481
|
-
self.vision_tower = SigLipVisionModel(config, ignore_head)
|
|
482
|
-
|
|
483
|
-
def __call__(
|
|
484
|
-
self, x: mx.array, output_hidden_states: Optional[bool] = None
|
|
485
|
-
) -> mx.array:
|
|
486
|
-
if self.config.cls == "HybridVisionTower":
|
|
487
|
-
high_images = x
|
|
488
|
-
low_images = mx.array(self.resize(np.array(x)))[None, :]
|
|
489
|
-
|
|
490
|
-
high_res = self.vision_tower_high(high_images)
|
|
491
|
-
low_res = self.vision_tower_low(low_images)
|
|
492
|
-
|
|
493
|
-
return (high_res, low_res)
|
|
494
|
-
else:
|
|
495
|
-
return self.vision_tower(x, output_hidden_states)
|
|
496
|
-
|
|
497
|
-
def sanitize(self, weights):
|
|
498
|
-
sanitized_weights = {}
|
|
499
|
-
weight_keys = {
|
|
500
|
-
"neck.0.weight",
|
|
501
|
-
"neck.2.weight",
|
|
502
|
-
"neck_hd.0.weight",
|
|
503
|
-
"neck_hd.2.weight",
|
|
504
|
-
"downsamples.0.weight",
|
|
505
|
-
"downsamples.1.weight",
|
|
506
|
-
"patch_embed.proj.weight",
|
|
507
|
-
}
|
|
508
|
-
for k, v in weights.items():
|
|
509
|
-
if "position_ids" in k:
|
|
510
|
-
# Remove unused position_ids
|
|
511
|
-
continue
|
|
512
|
-
|
|
513
|
-
elif ".".join(k.split(".")[-3:]) in weight_keys:
|
|
514
|
-
# PyTorch conv2d weight tensors have shape:
|
|
515
|
-
# [out_channels, in_channels, kH, KW]
|
|
516
|
-
# MLX conv2d expects the weight be of shape:
|
|
517
|
-
# [out_channels, kH, KW, in_channels]
|
|
518
|
-
if check_array_shape(v):
|
|
519
|
-
sanitized_weights[k] = v
|
|
520
|
-
else:
|
|
521
|
-
sanitized_weights[k] = v.transpose(0, 2, 3, 1)
|
|
522
|
-
|
|
523
|
-
else:
|
|
524
|
-
sanitized_weights[k] = v
|
|
525
|
-
|
|
526
|
-
return sanitized_weights
|