nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc9__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/mlx_backend/vlm/generate_qwen3_vl.py +14 -31
- nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +15 -32
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/qwen3vl.py +7 -23
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +8 -24
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/METADATA +1 -1
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/RECORD +11 -200
- 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.19rc9.dist-info}/WHEEL +0 -0
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/top_level.txt +0 -0
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import inspect
|
|
2
|
-
from dataclasses import dataclass
|
|
3
|
-
from typing import Optional
|
|
4
|
-
|
|
5
|
-
import mlx.core as mx
|
|
6
|
-
import mlx.nn as nn
|
|
7
|
-
import numpy as np
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
@dataclass
|
|
11
|
-
class VisionConfig:
|
|
12
|
-
model_type: str
|
|
13
|
-
hidden_size: int
|
|
14
|
-
num_attention_heads: int
|
|
15
|
-
patch_size: int
|
|
16
|
-
num_hidden_layers: int = 12
|
|
17
|
-
intermediate_size: int = 3072
|
|
18
|
-
image_size: int = 224
|
|
19
|
-
num_channels: int = 3
|
|
20
|
-
layer_norm_eps: float = 1e-6
|
|
21
|
-
|
|
22
|
-
@classmethod
|
|
23
|
-
def from_dict(cls, params):
|
|
24
|
-
return cls(
|
|
25
|
-
**{
|
|
26
|
-
k: v
|
|
27
|
-
for k, v in params.items()
|
|
28
|
-
if k in inspect.signature(cls).parameters
|
|
29
|
-
}
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def check_array_shape(arr):
|
|
34
|
-
shape = arr.shape
|
|
35
|
-
|
|
36
|
-
# Check if the shape has 4 dimensions
|
|
37
|
-
if len(shape) != 4:
|
|
38
|
-
return False
|
|
39
|
-
|
|
40
|
-
out_channels, kH, KW, _ = shape
|
|
41
|
-
|
|
42
|
-
# Check if out_channels is the largest, and kH and KW are the same
|
|
43
|
-
if (out_channels >= kH) and (out_channels >= KW) and (kH == KW):
|
|
44
|
-
return True
|
|
45
|
-
else:
|
|
46
|
-
return False
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
class Attention(nn.Module):
|
|
50
|
-
def __init__(
|
|
51
|
-
self,
|
|
52
|
-
dims: int,
|
|
53
|
-
num_heads: int,
|
|
54
|
-
query_input_dims: Optional[int] = None,
|
|
55
|
-
key_input_dims: Optional[int] = None,
|
|
56
|
-
value_input_dims: Optional[int] = None,
|
|
57
|
-
value_dims: Optional[int] = None,
|
|
58
|
-
value_output_dims: Optional[int] = None,
|
|
59
|
-
bias: bool = True,
|
|
60
|
-
):
|
|
61
|
-
super().__init__()
|
|
62
|
-
|
|
63
|
-
if (dims % num_heads) != 0:
|
|
64
|
-
raise ValueError(
|
|
65
|
-
"The input feature dimensions should be divisible by the "
|
|
66
|
-
f"number of heads ({dims} % {num_heads}) != 0"
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
query_input_dims = query_input_dims or dims
|
|
70
|
-
key_input_dims = key_input_dims or dims
|
|
71
|
-
value_input_dims = value_input_dims or key_input_dims
|
|
72
|
-
value_dims = value_dims or dims
|
|
73
|
-
value_output_dims = value_output_dims or dims
|
|
74
|
-
|
|
75
|
-
self.num_heads = num_heads
|
|
76
|
-
head_dim = dims // num_heads
|
|
77
|
-
self.scale = head_dim**-0.5
|
|
78
|
-
|
|
79
|
-
self.q_proj = nn.Linear(query_input_dims, dims, bias=bias)
|
|
80
|
-
self.k_proj = nn.Linear(key_input_dims, dims, bias=bias)
|
|
81
|
-
self.v_proj = nn.Linear(value_input_dims, value_dims, bias=bias)
|
|
82
|
-
self.out_proj = nn.Linear(value_dims, value_output_dims, bias=bias)
|
|
83
|
-
|
|
84
|
-
def __call__(self, x, mask=None):
|
|
85
|
-
queries = self.q_proj(x)
|
|
86
|
-
keys = self.k_proj(x)
|
|
87
|
-
values = self.v_proj(x)
|
|
88
|
-
|
|
89
|
-
num_heads = self.num_heads
|
|
90
|
-
B, L, D = queries.shape
|
|
91
|
-
_, S, _ = keys.shape
|
|
92
|
-
queries = queries.reshape(B, L, num_heads, -1).transpose(0, 2, 1, 3)
|
|
93
|
-
keys = keys.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
|
|
94
|
-
values = values.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
|
|
95
|
-
|
|
96
|
-
output = mx.fast.scaled_dot_product_attention(
|
|
97
|
-
queries, keys, values, scale=self.scale, mask=mask
|
|
98
|
-
)
|
|
99
|
-
output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
|
|
100
|
-
return self.out_proj(output)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
class MLP(nn.Module):
|
|
104
|
-
def __init__(self, config: VisionConfig):
|
|
105
|
-
super().__init__()
|
|
106
|
-
self.activation_fn = nn.GELU(approx="precise")
|
|
107
|
-
self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size, bias=True)
|
|
108
|
-
self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size, bias=True)
|
|
109
|
-
|
|
110
|
-
def __call__(self, x: mx.array) -> mx.array:
|
|
111
|
-
x = self.fc1(x)
|
|
112
|
-
x = self.activation_fn(x)
|
|
113
|
-
x = self.fc2(x)
|
|
114
|
-
return x
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
class EncoderLayer(nn.Module):
|
|
118
|
-
def __init__(self, config: VisionConfig):
|
|
119
|
-
super().__init__()
|
|
120
|
-
self.embed_dim = config.hidden_size
|
|
121
|
-
self.self_attn = Attention(
|
|
122
|
-
config.hidden_size, config.num_attention_heads, bias=True
|
|
123
|
-
)
|
|
124
|
-
self.layer_norm1 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
|
|
125
|
-
self.mlp = MLP(config)
|
|
126
|
-
self.layer_norm2 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
|
|
127
|
-
|
|
128
|
-
def __call__(self, x: mx.array, mask: Optional[mx.array] = None) -> mx.array:
|
|
129
|
-
r = self.self_attn(self.layer_norm1(x), mask)
|
|
130
|
-
h = x + r
|
|
131
|
-
r = self.mlp(self.layer_norm2(h))
|
|
132
|
-
return h + r
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
class Encoder(nn.Module):
|
|
136
|
-
def __init__(self, config: VisionConfig):
|
|
137
|
-
super().__init__()
|
|
138
|
-
self.layers = [EncoderLayer(config) for _ in range(config.num_hidden_layers)]
|
|
139
|
-
|
|
140
|
-
def __call__(
|
|
141
|
-
self,
|
|
142
|
-
x: mx.array,
|
|
143
|
-
output_hidden_states: Optional[bool] = None,
|
|
144
|
-
mask: Optional[mx.array] = None,
|
|
145
|
-
) -> mx.array:
|
|
146
|
-
encoder_states = (x,) if output_hidden_states else None
|
|
147
|
-
h = x
|
|
148
|
-
for l in self.layers:
|
|
149
|
-
x = l(x, mask=mask)
|
|
150
|
-
if output_hidden_states:
|
|
151
|
-
encoder_states = encoder_states + (x,)
|
|
152
|
-
|
|
153
|
-
h = x
|
|
154
|
-
|
|
155
|
-
return (h, encoder_states)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
class VisionEmbeddings(nn.Module):
|
|
159
|
-
def __init__(self, config: VisionConfig):
|
|
160
|
-
super().__init__()
|
|
161
|
-
self.config = config
|
|
162
|
-
self.embed_dim = config.hidden_size
|
|
163
|
-
self.image_size = config.image_size
|
|
164
|
-
self.patch_size = config.patch_size
|
|
165
|
-
|
|
166
|
-
self.patch_embedding = nn.Conv2d(
|
|
167
|
-
in_channels=config.num_channels,
|
|
168
|
-
out_channels=self.embed_dim,
|
|
169
|
-
kernel_size=self.patch_size,
|
|
170
|
-
stride=self.patch_size,
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
self.num_patches = (self.image_size // self.patch_size) ** 2
|
|
174
|
-
self.num_positions = self.num_patches
|
|
175
|
-
self.position_embedding = nn.Embedding(self.num_positions, self.embed_dim)
|
|
176
|
-
|
|
177
|
-
def __call__(self, x: mx.array) -> mx.array:
|
|
178
|
-
patch_embeddings = self.patch_embedding(x)
|
|
179
|
-
patch_embeddings = mx.flatten(patch_embeddings, start_axis=1, end_axis=2)
|
|
180
|
-
position_ids = mx.array(mx.arange(self.num_positions)[None, :])
|
|
181
|
-
embeddings = patch_embeddings
|
|
182
|
-
embeddings += self.position_embedding(position_ids)
|
|
183
|
-
return embeddings
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
class VisionModel(nn.Module):
|
|
187
|
-
def __init__(self, config: VisionConfig):
|
|
188
|
-
super().__init__()
|
|
189
|
-
self.model_type = config.model_type
|
|
190
|
-
if self.model_type not in [
|
|
191
|
-
"siglip_vision_model",
|
|
192
|
-
"idefics3",
|
|
193
|
-
"idefics3_vision",
|
|
194
|
-
"smolvlm_vision",
|
|
195
|
-
]:
|
|
196
|
-
raise ValueError(f"Unsupported model type: {self.model_type}")
|
|
197
|
-
|
|
198
|
-
self.embeddings = VisionEmbeddings(config)
|
|
199
|
-
self.encoder = Encoder(config)
|
|
200
|
-
self.post_layernorm = nn.LayerNorm(config.hidden_size)
|
|
201
|
-
|
|
202
|
-
def __call__(
|
|
203
|
-
self,
|
|
204
|
-
x: mx.array,
|
|
205
|
-
output_hidden_states: Optional[bool] = None,
|
|
206
|
-
) -> mx.array:
|
|
207
|
-
x = self.embeddings(x)
|
|
208
|
-
x = x.astype(self.embeddings.patch_embedding.weight.dtype)
|
|
209
|
-
encoder_outputs = self.encoder(
|
|
210
|
-
x=x, output_hidden_states=output_hidden_states, mask=None
|
|
211
|
-
)
|
|
212
|
-
pooler_output = self.post_layernorm(encoder_outputs[0])
|
|
213
|
-
return pooler_output, x, encoder_outputs[-1]
|
|
214
|
-
|
|
215
|
-
def sanitize(self, weights):
|
|
216
|
-
sanitized_weights = {}
|
|
217
|
-
for k, v in weights.items():
|
|
218
|
-
if "position_ids" in k:
|
|
219
|
-
# Remove unused position_ids
|
|
220
|
-
continue
|
|
221
|
-
elif "patch_embedding.weight" in k:
|
|
222
|
-
# PyTorch conv2d weight tensors have shape:
|
|
223
|
-
# [out_channels, in_channels, kH, KW]
|
|
224
|
-
# MLX conv2d expects the weight be of shape:
|
|
225
|
-
# [out_channels, kH, KW, in_channels]
|
|
226
|
-
if check_array_shape(v):
|
|
227
|
-
sanitized_weights[k] = v
|
|
228
|
-
else:
|
|
229
|
-
sanitized_weights[k] = v.transpose(0, 2, 3, 1)
|
|
230
|
-
else:
|
|
231
|
-
sanitized_weights[k] = v
|
|
232
|
-
|
|
233
|
-
return sanitized_weights
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import glob
|
|
2
|
-
import inspect
|
|
3
|
-
import json
|
|
4
|
-
from dataclasses import dataclass
|
|
5
|
-
from pathlib import Path
|
|
6
|
-
from typing import List, Optional
|
|
7
|
-
|
|
8
|
-
import mlx.core as mx
|
|
9
|
-
import mlx.nn as nn
|
|
10
|
-
import numpy as np
|
|
11
|
-
from huggingface_hub import snapshot_download
|
|
12
|
-
|
|
13
|
-
from ..base import pixel_shuffle
|
|
14
|
-
from .language import LanguageModel, TextConfig
|
|
15
|
-
from .vision import VisionConfig, VisionModel
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
@dataclass
|
|
19
|
-
class ModelConfig:
|
|
20
|
-
text_config: TextConfig
|
|
21
|
-
vision_config: VisionConfig
|
|
22
|
-
model_type: str
|
|
23
|
-
ignore_index: int = -100
|
|
24
|
-
image_token_index: int = 151667
|
|
25
|
-
video_token_index: int = 151656
|
|
26
|
-
vision_feature_select_strategy: str = "default"
|
|
27
|
-
vision_feature_layer: int = -1
|
|
28
|
-
vocab_size: int = 32000
|
|
29
|
-
downsample_ratio: float = 0.5
|
|
30
|
-
eos_token_id: Optional[List[int]] = None
|
|
31
|
-
|
|
32
|
-
@classmethod
|
|
33
|
-
def from_dict(cls, params):
|
|
34
|
-
return cls(
|
|
35
|
-
**{
|
|
36
|
-
k: v
|
|
37
|
-
for k, v in params.items()
|
|
38
|
-
if k in inspect.signature(cls).parameters
|
|
39
|
-
}
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
class Model(nn.Module):
|
|
44
|
-
def __init__(self, config: ModelConfig):
|
|
45
|
-
super().__init__()
|
|
46
|
-
self.config = config
|
|
47
|
-
self.vision_model = VisionModel(config.vision_config)
|
|
48
|
-
self.language_model = LanguageModel(config.text_config)
|
|
49
|
-
|
|
50
|
-
self.downsample_ratio = config.downsample_ratio
|
|
51
|
-
|
|
52
|
-
vit_hidden_size = self.config.vision_config.hidden_size
|
|
53
|
-
llm_hidden_size = self.config.text_config.hidden_size
|
|
54
|
-
|
|
55
|
-
self.mlp1 = [
|
|
56
|
-
nn.LayerNorm(vit_hidden_size * int(1 / self.downsample_ratio) ** 2),
|
|
57
|
-
nn.Linear(
|
|
58
|
-
vit_hidden_size * int(1 / self.downsample_ratio) ** 2, llm_hidden_size
|
|
59
|
-
),
|
|
60
|
-
nn.GELU(),
|
|
61
|
-
nn.Linear(llm_hidden_size, llm_hidden_size),
|
|
62
|
-
]
|
|
63
|
-
|
|
64
|
-
def get_input_embeddings(
|
|
65
|
-
self,
|
|
66
|
-
input_ids: Optional[mx.array] = None,
|
|
67
|
-
pixel_values: Optional[mx.array] = None,
|
|
68
|
-
):
|
|
69
|
-
|
|
70
|
-
if pixel_values is None:
|
|
71
|
-
return self.language_model.model.embed_tokens(input_ids)
|
|
72
|
-
|
|
73
|
-
dtype = self.vision_model.embeddings.patch_embedding.weight.dtype
|
|
74
|
-
pixel_values = pixel_values.astype(dtype)
|
|
75
|
-
|
|
76
|
-
# TODO: Remove this after transformers implementation is merged
|
|
77
|
-
if pixel_values.ndim == 5:
|
|
78
|
-
pixel_values = pixel_values[0]
|
|
79
|
-
|
|
80
|
-
# Get the input embeddings from the language model
|
|
81
|
-
inputs_embeds = self.language_model.model.embed_tokens(input_ids)
|
|
82
|
-
|
|
83
|
-
# Get the ouptut hidden states from the vision model
|
|
84
|
-
hidden_states, _, _ = self.vision_model(
|
|
85
|
-
pixel_values.transpose(0, 2, 3, 1), output_hidden_states=True
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
# Extract vision embeddings, removing the class token (first token)
|
|
89
|
-
hidden_states = hidden_states[:, 1:, :]
|
|
90
|
-
|
|
91
|
-
# Apply pixel shuffle with downsampling
|
|
92
|
-
hidden_states = pixel_shuffle(
|
|
93
|
-
hidden_states, shuffle_ratio=self.downsample_ratio
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
# Apply MLP transformation
|
|
97
|
-
for layer in self.mlp1:
|
|
98
|
-
hidden_states = layer(hidden_states)
|
|
99
|
-
|
|
100
|
-
# Insert special image tokens in the input_ids
|
|
101
|
-
final_inputs_embeds = self._merge_input_ids_with_image_features(
|
|
102
|
-
hidden_states, inputs_embeds, input_ids
|
|
103
|
-
)
|
|
104
|
-
return final_inputs_embeds
|
|
105
|
-
|
|
106
|
-
def _merge_input_ids_with_image_features(
|
|
107
|
-
self, image_features, inputs_embeds, input_ids
|
|
108
|
-
):
|
|
109
|
-
B, N, C = inputs_embeds.shape
|
|
110
|
-
image_token_index = self.config.image_token_index
|
|
111
|
-
video_token_index = self.config.video_token_index
|
|
112
|
-
|
|
113
|
-
# Positions of <image> tokens in input_ids, assuming batch size is 1
|
|
114
|
-
image_positions = input_ids == image_token_index
|
|
115
|
-
if mx.sum(image_positions) == 0:
|
|
116
|
-
image_positions = input_ids == video_token_index
|
|
117
|
-
|
|
118
|
-
image_indices = np.where(image_positions)[1].tolist()
|
|
119
|
-
|
|
120
|
-
image_features = image_features.reshape(-1, image_features.shape[-1])
|
|
121
|
-
|
|
122
|
-
inputs_embeds[:, image_indices, :] = image_features
|
|
123
|
-
|
|
124
|
-
return inputs_embeds.reshape(B, N, C)
|
|
125
|
-
|
|
126
|
-
@property
|
|
127
|
-
def layers(self):
|
|
128
|
-
return self.language_model.model.layers
|
|
129
|
-
|
|
130
|
-
def __call__(
|
|
131
|
-
self,
|
|
132
|
-
input_ids: mx.array,
|
|
133
|
-
pixel_values: mx.array,
|
|
134
|
-
mask: mx.array,
|
|
135
|
-
cache=None,
|
|
136
|
-
**kwargs,
|
|
137
|
-
):
|
|
138
|
-
input_embddings = self.get_input_embeddings(input_ids, pixel_values)
|
|
139
|
-
logits = self.language_model(None, cache=cache, inputs_embeds=input_embddings)
|
|
140
|
-
return logits
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import inspect
|
|
2
|
-
from dataclasses import dataclass
|
|
3
|
-
from typing import Dict, Optional, Union
|
|
4
|
-
|
|
5
|
-
import mlx.core as mx
|
|
6
|
-
import mlx.nn as nn
|
|
7
|
-
|
|
8
|
-
from ..base import (
|
|
9
|
-
LanguageModelOutput,
|
|
10
|
-
create_attention_mask,
|
|
11
|
-
scaled_dot_product_attention,
|
|
12
|
-
)
|
|
13
|
-
from ..cache import KVCache
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@dataclass
|
|
17
|
-
class TextConfig:
|
|
18
|
-
model_type: str
|
|
19
|
-
hidden_size: int
|
|
20
|
-
num_hidden_layers: int
|
|
21
|
-
intermediate_size: int
|
|
22
|
-
num_attention_heads: int
|
|
23
|
-
rms_norm_eps: float
|
|
24
|
-
vocab_size: int
|
|
25
|
-
max_window_layers: int
|
|
26
|
-
hidden_act: str
|
|
27
|
-
num_key_value_heads: Optional[int] = 8
|
|
28
|
-
max_position_embeddings: Optional[int] = 40960
|
|
29
|
-
rope_theta: float = 1000000.0
|
|
30
|
-
rope_traditional: bool = False
|
|
31
|
-
rope_scaling: Optional[Dict[str, Union[float, str]]] = None
|
|
32
|
-
tie_word_embeddings: bool = False
|
|
33
|
-
sliding_window: int = 32768
|
|
34
|
-
use_sliding_window: bool = False
|
|
35
|
-
use_cache: bool = True
|
|
36
|
-
|
|
37
|
-
def __post_init__(self):
|
|
38
|
-
if self.num_key_value_heads is None:
|
|
39
|
-
self.num_key_value_heads = self.num_attention_heads
|
|
40
|
-
|
|
41
|
-
@classmethod
|
|
42
|
-
def from_dict(cls, params):
|
|
43
|
-
return cls(
|
|
44
|
-
**{
|
|
45
|
-
k: v
|
|
46
|
-
for k, v in params.items()
|
|
47
|
-
if k in inspect.signature(cls).parameters
|
|
48
|
-
}
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
class Attention(nn.Module):
|
|
53
|
-
def __init__(self, args: TextConfig):
|
|
54
|
-
super().__init__()
|
|
55
|
-
|
|
56
|
-
dim = args.hidden_size
|
|
57
|
-
self.n_heads = n_heads = args.num_attention_heads
|
|
58
|
-
assert args.num_key_value_heads is not None
|
|
59
|
-
self.n_kv_heads = n_kv_heads = args.num_key_value_heads
|
|
60
|
-
|
|
61
|
-
self.head_dim = head_dim = args.hidden_size // n_heads
|
|
62
|
-
self.scale = head_dim**-0.5
|
|
63
|
-
|
|
64
|
-
self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=True)
|
|
65
|
-
self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=True)
|
|
66
|
-
self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=True)
|
|
67
|
-
self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=False)
|
|
68
|
-
|
|
69
|
-
self.rotary_emb = nn.RoPE(
|
|
70
|
-
head_dim,
|
|
71
|
-
base=args.rope_theta,
|
|
72
|
-
traditional=args.rope_traditional,
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
def __call__(
|
|
76
|
-
self,
|
|
77
|
-
x: mx.array,
|
|
78
|
-
mask: Optional[mx.array] = None,
|
|
79
|
-
cache: Optional[KVCache] = None,
|
|
80
|
-
) -> mx.array:
|
|
81
|
-
B, L, D = x.shape
|
|
82
|
-
|
|
83
|
-
queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
|
|
84
|
-
|
|
85
|
-
# Prepare the queries, keys and values for the attention computation
|
|
86
|
-
queries = queries.reshape(B, L, self.n_heads, self.head_dim).transpose(
|
|
87
|
-
0, 2, 1, 3
|
|
88
|
-
)
|
|
89
|
-
keys = keys.reshape(B, L, self.n_kv_heads, self.head_dim).transpose(0, 2, 1, 3)
|
|
90
|
-
values = values.reshape(B, L, self.n_kv_heads, self.head_dim).transpose(
|
|
91
|
-
0, 2, 1, 3
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
offset = cache.offset if cache else 0
|
|
95
|
-
|
|
96
|
-
if mask is not None and isinstance(mask, mx.array):
|
|
97
|
-
mask = mask[..., : keys.shape[-2]]
|
|
98
|
-
|
|
99
|
-
queries = self.rotary_emb(queries, offset=offset)
|
|
100
|
-
keys = self.rotary_emb(keys, offset=offset)
|
|
101
|
-
|
|
102
|
-
if cache is not None:
|
|
103
|
-
keys, values = cache.update_and_fetch(keys, values)
|
|
104
|
-
|
|
105
|
-
output = scaled_dot_product_attention(
|
|
106
|
-
queries, keys, values, cache, scale=self.scale, mask=mask
|
|
107
|
-
)
|
|
108
|
-
output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
|
|
109
|
-
return self.o_proj(output)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
class MLP(nn.Module):
|
|
113
|
-
def __init__(self, dim, hidden_dim):
|
|
114
|
-
super().__init__()
|
|
115
|
-
self.gate_proj = nn.Linear(dim, hidden_dim, bias=False)
|
|
116
|
-
self.down_proj = nn.Linear(hidden_dim, dim, bias=False)
|
|
117
|
-
self.up_proj = nn.Linear(dim, hidden_dim, bias=False)
|
|
118
|
-
|
|
119
|
-
def __call__(self, x) -> mx.array:
|
|
120
|
-
return self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x))
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
class Qwen2VLDecoderLayer(nn.Module):
|
|
124
|
-
def __init__(self, args: TextConfig):
|
|
125
|
-
super().__init__()
|
|
126
|
-
self.num_attention_heads = args.num_attention_heads
|
|
127
|
-
self.hidden_size = args.hidden_size
|
|
128
|
-
self.self_attn = Attention(args)
|
|
129
|
-
self.mlp = MLP(args.hidden_size, args.intermediate_size)
|
|
130
|
-
self.input_layernorm = nn.RMSNorm(args.hidden_size, eps=args.rms_norm_eps)
|
|
131
|
-
self.post_attention_layernorm = nn.RMSNorm(
|
|
132
|
-
args.hidden_size, eps=args.rms_norm_eps
|
|
133
|
-
)
|
|
134
|
-
self.args = args
|
|
135
|
-
|
|
136
|
-
def __call__(
|
|
137
|
-
self,
|
|
138
|
-
x: mx.array,
|
|
139
|
-
mask: Optional[mx.array] = None,
|
|
140
|
-
cache: Optional[KVCache] = None,
|
|
141
|
-
) -> mx.array:
|
|
142
|
-
r = self.self_attn(self.input_layernorm(x), mask, cache)
|
|
143
|
-
h = x + r
|
|
144
|
-
r = self.mlp(self.post_attention_layernorm(h))
|
|
145
|
-
out = h + r
|
|
146
|
-
return out
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
class Qwen2Model(nn.Module):
|
|
150
|
-
def __init__(self, args: TextConfig):
|
|
151
|
-
super().__init__()
|
|
152
|
-
self.args = args
|
|
153
|
-
self.vocab_size = args.vocab_size
|
|
154
|
-
self.num_hidden_layers = args.num_hidden_layers
|
|
155
|
-
assert self.vocab_size > 0
|
|
156
|
-
self.embed_tokens = nn.Embedding(args.vocab_size, args.hidden_size)
|
|
157
|
-
self.layers = [
|
|
158
|
-
Qwen2VLDecoderLayer(args=args) for _ in range(args.num_hidden_layers)
|
|
159
|
-
]
|
|
160
|
-
self.norm = nn.RMSNorm(args.hidden_size, eps=args.rms_norm_eps)
|
|
161
|
-
|
|
162
|
-
def __call__(
|
|
163
|
-
self,
|
|
164
|
-
inputs: mx.array,
|
|
165
|
-
inputs_embeds: Optional[mx.array] = None,
|
|
166
|
-
mask: Optional[mx.array] = None,
|
|
167
|
-
cache=None,
|
|
168
|
-
):
|
|
169
|
-
if inputs_embeds is None:
|
|
170
|
-
h = self.embed_tokens(inputs)
|
|
171
|
-
else:
|
|
172
|
-
h = inputs_embeds
|
|
173
|
-
|
|
174
|
-
if cache is None:
|
|
175
|
-
cache = [None] * len(self.layers)
|
|
176
|
-
|
|
177
|
-
if mask is None:
|
|
178
|
-
mask = create_attention_mask(h, cache)
|
|
179
|
-
|
|
180
|
-
for layer, c in zip(self.layers, cache):
|
|
181
|
-
h = layer(h, mask, c)
|
|
182
|
-
|
|
183
|
-
return self.norm(h)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
class LanguageModel(nn.Module):
|
|
187
|
-
def __init__(self, args: TextConfig):
|
|
188
|
-
super().__init__()
|
|
189
|
-
self.args = args
|
|
190
|
-
self.model_type = args.model_type
|
|
191
|
-
self.model = Qwen2Model(args)
|
|
192
|
-
|
|
193
|
-
if not args.tie_word_embeddings:
|
|
194
|
-
self.lm_head = nn.Linear(args.hidden_size, args.vocab_size, bias=False)
|
|
195
|
-
|
|
196
|
-
def __call__(
|
|
197
|
-
self,
|
|
198
|
-
inputs: mx.array,
|
|
199
|
-
inputs_embeds: Optional[mx.array] = None,
|
|
200
|
-
mask: Optional[mx.array] = None,
|
|
201
|
-
cache=None,
|
|
202
|
-
):
|
|
203
|
-
out = self.model(inputs, cache=cache, inputs_embeds=inputs_embeds)
|
|
204
|
-
if self.args.tie_word_embeddings:
|
|
205
|
-
out = self.model.embed_tokens.as_linear(out)
|
|
206
|
-
else:
|
|
207
|
-
out = self.lm_head(out)
|
|
208
|
-
return LanguageModelOutput(logits=out)
|
|
209
|
-
|
|
210
|
-
@property
|
|
211
|
-
def layers(self):
|
|
212
|
-
return self.model.layers
|
|
213
|
-
|
|
214
|
-
@property
|
|
215
|
-
def head_dim(self):
|
|
216
|
-
return self.args.hidden_size // self.args.num_attention_heads
|
|
217
|
-
|
|
218
|
-
@property
|
|
219
|
-
def n_kv_heads(self):
|
|
220
|
-
return self.args.num_key_value_heads
|