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,416 +0,0 @@
|
|
|
1
|
-
import inspect
|
|
2
|
-
from dataclasses import dataclass, field
|
|
3
|
-
from typing import Dict, List, Optional, Tuple, 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 = "mllama"
|
|
19
|
-
vocab_size: int = 32000
|
|
20
|
-
hidden_size: int = 4096
|
|
21
|
-
intermediate_size: int = 14336
|
|
22
|
-
num_hidden_layers: int = 40
|
|
23
|
-
num_attention_heads: int = 32
|
|
24
|
-
num_key_value_heads: int = 8
|
|
25
|
-
hidden_act: str = "silu"
|
|
26
|
-
max_position_embeddings: int = 131072
|
|
27
|
-
initializer_range: float = 0.02
|
|
28
|
-
rms_norm_eps: float = 1e-6
|
|
29
|
-
tie_word_embeddings: bool = False
|
|
30
|
-
rope_theta: float = 10000.0
|
|
31
|
-
rope_traditional: bool = False
|
|
32
|
-
rope_scaling: Optional[Dict[str, Union[float, str]]] = None
|
|
33
|
-
cross_attention_layers: List[int] = field(
|
|
34
|
-
default_factory=lambda: [3, 8, 13, 18, 23, 28, 33, 38]
|
|
35
|
-
)
|
|
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 MllamaTextCrossAttention(nn.Module):
|
|
53
|
-
def __init__(self, config: TextConfig, layer_idx: Optional[int] = None):
|
|
54
|
-
super().__init__()
|
|
55
|
-
self.config = config
|
|
56
|
-
self.hidden_size = config.hidden_size
|
|
57
|
-
self.num_heads = config.num_attention_heads
|
|
58
|
-
self.head_dim = self.hidden_size // self.num_heads
|
|
59
|
-
self.num_key_value_heads = config.num_key_value_heads
|
|
60
|
-
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
|
61
|
-
self.layer_idx = layer_idx
|
|
62
|
-
self.scale = self.head_dim**-0.5
|
|
63
|
-
self.q_proj = nn.Linear(
|
|
64
|
-
self.hidden_size, self.num_heads * self.head_dim, bias=False
|
|
65
|
-
)
|
|
66
|
-
self.k_proj = nn.Linear(
|
|
67
|
-
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
|
68
|
-
)
|
|
69
|
-
self.v_proj = nn.Linear(
|
|
70
|
-
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
|
71
|
-
)
|
|
72
|
-
self.o_proj = nn.Linear(
|
|
73
|
-
self.num_heads * self.head_dim, self.hidden_size, bias=False
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
self.q_norm = nn.RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
|
77
|
-
self.k_norm = nn.RMSNorm(self.head_dim, eps=config.rms_norm_eps)
|
|
78
|
-
|
|
79
|
-
def __call__(
|
|
80
|
-
self,
|
|
81
|
-
hidden_states: mx.array,
|
|
82
|
-
cross_attention_states: Optional[mx.array] = None,
|
|
83
|
-
attention_mask: Optional[mx.array] = None,
|
|
84
|
-
cache: Optional[KVCache] = None,
|
|
85
|
-
) -> mx.array:
|
|
86
|
-
|
|
87
|
-
bsz, q_len, _ = hidden_states.shape
|
|
88
|
-
query = (
|
|
89
|
-
self.q_proj(hidden_states)
|
|
90
|
-
.reshape(bsz, q_len, self.num_heads, self.head_dim)
|
|
91
|
-
.transpose(0, 2, 1, 3)
|
|
92
|
-
)
|
|
93
|
-
query_states = self.q_norm(query)
|
|
94
|
-
|
|
95
|
-
if cross_attention_states is not None:
|
|
96
|
-
key_states = (
|
|
97
|
-
self.k_proj(cross_attention_states)
|
|
98
|
-
.reshape(bsz, -1, self.num_key_value_heads, self.head_dim)
|
|
99
|
-
.transpose(0, 2, 1, 3)
|
|
100
|
-
)
|
|
101
|
-
value_states = (
|
|
102
|
-
self.v_proj(cross_attention_states)
|
|
103
|
-
.reshape(bsz, -1, self.num_key_value_heads, self.head_dim)
|
|
104
|
-
.transpose(0, 2, 1, 3)
|
|
105
|
-
)
|
|
106
|
-
key_states = self.k_norm(key_states)
|
|
107
|
-
elif cache is not None and cache.offset > 0:
|
|
108
|
-
key_states, value_states = cache.fetch()
|
|
109
|
-
else:
|
|
110
|
-
key_states, value_states = mx.split(query, 2, axis=1)
|
|
111
|
-
key_states = self.k_norm(key_states)
|
|
112
|
-
|
|
113
|
-
attn_output = scaled_dot_product_attention(
|
|
114
|
-
query_states,
|
|
115
|
-
key_states,
|
|
116
|
-
value_states,
|
|
117
|
-
cache,
|
|
118
|
-
scale=self.scale,
|
|
119
|
-
mask=attention_mask, # add a dim for batch processing
|
|
120
|
-
)
|
|
121
|
-
attn_output = attn_output.transpose(0, 2, 1, 3).reshape(
|
|
122
|
-
bsz, q_len, self.hidden_size
|
|
123
|
-
)
|
|
124
|
-
return self.o_proj(attn_output)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
class MllamaTextSelfAttention(nn.Module):
|
|
128
|
-
def __init__(self, config: TextConfig, layer_idx: int):
|
|
129
|
-
super().__init__()
|
|
130
|
-
self.config = config
|
|
131
|
-
self.hidden_size = config.hidden_size
|
|
132
|
-
self.num_heads = config.num_attention_heads
|
|
133
|
-
self.head_dim = self.hidden_size // self.num_heads
|
|
134
|
-
self.num_key_value_heads = config.num_key_value_heads
|
|
135
|
-
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
|
136
|
-
self.scale = self.head_dim**-0.5
|
|
137
|
-
self.layer_idx = layer_idx
|
|
138
|
-
|
|
139
|
-
self.q_proj = nn.Linear(
|
|
140
|
-
self.hidden_size, self.num_heads * self.head_dim, bias=False
|
|
141
|
-
)
|
|
142
|
-
self.k_proj = nn.Linear(
|
|
143
|
-
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
|
144
|
-
)
|
|
145
|
-
self.v_proj = nn.Linear(
|
|
146
|
-
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
|
|
147
|
-
)
|
|
148
|
-
self.o_proj = nn.Linear(
|
|
149
|
-
self.num_heads * self.head_dim, self.hidden_size, bias=False
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
self.rope = nn.RoPE(
|
|
153
|
-
self.head_dim,
|
|
154
|
-
traditional=config.rope_traditional,
|
|
155
|
-
base=config.rope_theta,
|
|
156
|
-
scale=1,
|
|
157
|
-
)
|
|
158
|
-
|
|
159
|
-
def __call__(
|
|
160
|
-
self,
|
|
161
|
-
x: mx.array,
|
|
162
|
-
mask: Optional[mx.array] = None,
|
|
163
|
-
cache: Optional[KVCache] = None,
|
|
164
|
-
) -> mx.array:
|
|
165
|
-
bsz, q_len, _ = x.shape
|
|
166
|
-
query_states = (
|
|
167
|
-
self.q_proj(x).reshape(bsz, q_len, self.num_heads, -1).transpose(0, 2, 1, 3)
|
|
168
|
-
)
|
|
169
|
-
key_states = (
|
|
170
|
-
self.k_proj(x)
|
|
171
|
-
.reshape(bsz, q_len, self.num_key_value_heads, -1)
|
|
172
|
-
.transpose(0, 2, 1, 3)
|
|
173
|
-
)
|
|
174
|
-
value_states = (
|
|
175
|
-
self.v_proj(x)
|
|
176
|
-
.reshape(bsz, q_len, self.num_key_value_heads, -1)
|
|
177
|
-
.transpose(0, 2, 1, 3)
|
|
178
|
-
)
|
|
179
|
-
|
|
180
|
-
if cache is not None:
|
|
181
|
-
query_states = self.rope(query_states, offset=cache.offset)
|
|
182
|
-
key_states = self.rope(key_states, offset=cache.offset)
|
|
183
|
-
key_states, value_states = cache.update_and_fetch(key_states, value_states)
|
|
184
|
-
else:
|
|
185
|
-
query_states = self.rope(query_states)
|
|
186
|
-
key_states = self.rope(key_states)
|
|
187
|
-
|
|
188
|
-
attn_output = scaled_dot_product_attention(
|
|
189
|
-
query_states, key_states, value_states, cache, scale=self.scale, mask=mask
|
|
190
|
-
)
|
|
191
|
-
attn_output = attn_output.transpose(0, 2, 1, 3).reshape(
|
|
192
|
-
bsz, q_len, self.hidden_size
|
|
193
|
-
)
|
|
194
|
-
return self.o_proj(attn_output)
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
class MllamaTextMLP(nn.Module):
|
|
198
|
-
def __init__(self, config: TextConfig):
|
|
199
|
-
super().__init__()
|
|
200
|
-
self.gate_proj = nn.Linear(
|
|
201
|
-
config.hidden_size, config.intermediate_size, bias=False
|
|
202
|
-
)
|
|
203
|
-
self.up_proj = nn.Linear(
|
|
204
|
-
config.hidden_size, config.intermediate_size, bias=False
|
|
205
|
-
)
|
|
206
|
-
self.down_proj = nn.Linear(
|
|
207
|
-
config.intermediate_size, config.hidden_size, bias=False
|
|
208
|
-
)
|
|
209
|
-
self.act_fn = lambda x: x * mx.sigmoid(x)
|
|
210
|
-
|
|
211
|
-
def __call__(self, x):
|
|
212
|
-
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
class MllamaSelfAttentionDecoderLayer(nn.Module):
|
|
216
|
-
def __init__(self, config: TextConfig, layer_idx: int):
|
|
217
|
-
super().__init__()
|
|
218
|
-
self.hidden_size = config.hidden_size
|
|
219
|
-
self.self_attn = MllamaTextSelfAttention(config, layer_idx=layer_idx)
|
|
220
|
-
self.mlp = MllamaTextMLP(config)
|
|
221
|
-
self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
222
|
-
self.post_attention_layernorm = nn.RMSNorm(
|
|
223
|
-
config.hidden_size, eps=config.rms_norm_eps
|
|
224
|
-
)
|
|
225
|
-
|
|
226
|
-
def __call__(
|
|
227
|
-
self,
|
|
228
|
-
hidden_states: mx.array,
|
|
229
|
-
mask: Optional[mx.array] = None,
|
|
230
|
-
cache: Optional[KVCache] = None,
|
|
231
|
-
) -> mx.array:
|
|
232
|
-
residual = hidden_states
|
|
233
|
-
hidden_states = self.input_layernorm(hidden_states)
|
|
234
|
-
hidden_states = self.self_attn(
|
|
235
|
-
x=hidden_states,
|
|
236
|
-
mask=mask,
|
|
237
|
-
cache=cache,
|
|
238
|
-
)
|
|
239
|
-
hidden_states = residual + hidden_states
|
|
240
|
-
|
|
241
|
-
residual = hidden_states
|
|
242
|
-
hidden_states = self.post_attention_layernorm(hidden_states)
|
|
243
|
-
hidden_states = self.mlp(hidden_states)
|
|
244
|
-
hidden_states = residual + hidden_states
|
|
245
|
-
|
|
246
|
-
return hidden_states
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
class MllamaCrossAttentionDecoderLayer(nn.Module):
|
|
250
|
-
def __init__(self, config: TextConfig, layer_idx: int):
|
|
251
|
-
super().__init__()
|
|
252
|
-
self.hidden_size = config.hidden_size
|
|
253
|
-
self.cross_attn = MllamaTextCrossAttention(config, layer_idx=layer_idx)
|
|
254
|
-
self.mlp = MllamaTextMLP(config)
|
|
255
|
-
self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
256
|
-
self.post_attention_layernorm = nn.RMSNorm(
|
|
257
|
-
config.hidden_size, eps=config.rms_norm_eps
|
|
258
|
-
)
|
|
259
|
-
self.cross_attn_attn_gate = mx.zeros(1)
|
|
260
|
-
self.cross_attn_mlp_gate = mx.zeros(1)
|
|
261
|
-
|
|
262
|
-
def __call__(
|
|
263
|
-
self,
|
|
264
|
-
hidden_states: mx.array,
|
|
265
|
-
cross_attention_states: mx.array,
|
|
266
|
-
attention_mask: Optional[mx.array] = None,
|
|
267
|
-
full_text_row_masked_out_mask: Optional[mx.array] = None,
|
|
268
|
-
cache: Optional[KVCache] = None,
|
|
269
|
-
) -> mx.array:
|
|
270
|
-
residual = hidden_states
|
|
271
|
-
hidden_states = self.input_layernorm(hidden_states)
|
|
272
|
-
hidden_states = self.cross_attn(
|
|
273
|
-
hidden_states=hidden_states,
|
|
274
|
-
cross_attention_states=cross_attention_states,
|
|
275
|
-
attention_mask=attention_mask,
|
|
276
|
-
cache=cache,
|
|
277
|
-
)
|
|
278
|
-
hidden_states = residual + mx.tanh(self.cross_attn_attn_gate) * hidden_states
|
|
279
|
-
|
|
280
|
-
residual = hidden_states
|
|
281
|
-
hidden_states = self.post_attention_layernorm(hidden_states)
|
|
282
|
-
hidden_states = self.mlp(hidden_states)
|
|
283
|
-
if full_text_row_masked_out_mask is not None:
|
|
284
|
-
hidden_states = full_text_row_masked_out_mask[:, 0] * hidden_states
|
|
285
|
-
hidden_states = residual + mx.tanh(self.cross_attn_mlp_gate) * hidden_states
|
|
286
|
-
|
|
287
|
-
return hidden_states
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
class MllamaTextModel(nn.Module):
|
|
291
|
-
def __init__(self, config: TextConfig):
|
|
292
|
-
super().__init__()
|
|
293
|
-
self.config = config
|
|
294
|
-
self.vocab_size = config.vocab_size
|
|
295
|
-
self.hidden_size = config.hidden_size
|
|
296
|
-
|
|
297
|
-
self.embed_tokens = nn.Embedding(config.vocab_size + 8, config.hidden_size)
|
|
298
|
-
self.layers = [
|
|
299
|
-
(
|
|
300
|
-
MllamaCrossAttentionDecoderLayer(config, layer_idx)
|
|
301
|
-
if layer_idx in config.cross_attention_layers
|
|
302
|
-
else MllamaSelfAttentionDecoderLayer(config, layer_idx)
|
|
303
|
-
)
|
|
304
|
-
for layer_idx in range(config.num_hidden_layers)
|
|
305
|
-
]
|
|
306
|
-
self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
|
307
|
-
|
|
308
|
-
def __call__(
|
|
309
|
-
self,
|
|
310
|
-
input_ids: Optional[mx.array] = None,
|
|
311
|
-
mask: Optional[mx.array] = None,
|
|
312
|
-
position_ids: Optional[mx.array] = None,
|
|
313
|
-
cross_attention_states: Optional[mx.array] = None,
|
|
314
|
-
cross_attention_mask: Optional[mx.array] = None,
|
|
315
|
-
full_text_row_masked_out_mask: Optional[mx.array] = None,
|
|
316
|
-
inputs_embeds: Optional[mx.array] = None,
|
|
317
|
-
cache: Optional[KVCache] = None,
|
|
318
|
-
) -> mx.array:
|
|
319
|
-
if input_ids is not None and inputs_embeds is not None:
|
|
320
|
-
raise ValueError(
|
|
321
|
-
"You cannot specify both input_ids and inputs_embeds at the same time"
|
|
322
|
-
)
|
|
323
|
-
elif input_ids is not None:
|
|
324
|
-
batch_size, seq_length = input_ids.shape
|
|
325
|
-
inputs_embeds = self.embed_tokens(input_ids)
|
|
326
|
-
elif inputs_embeds is not None:
|
|
327
|
-
batch_size, seq_length, _ = inputs_embeds.shape
|
|
328
|
-
else:
|
|
329
|
-
raise ValueError("You have to specify either input_ids or inputs_embeds")
|
|
330
|
-
|
|
331
|
-
if position_ids is None:
|
|
332
|
-
position_ids = mx.expand_dims(mx.arange(seq_length), 0)
|
|
333
|
-
position_ids = mx.repeat(position_ids, batch_size, axis=0)
|
|
334
|
-
|
|
335
|
-
hidden_states = inputs_embeds
|
|
336
|
-
|
|
337
|
-
if cache is None:
|
|
338
|
-
cache = [None] * len(self.layers)
|
|
339
|
-
|
|
340
|
-
if mask is None:
|
|
341
|
-
mask = create_attention_mask(hidden_states, cache)
|
|
342
|
-
|
|
343
|
-
for idx, (decoder_layer, c) in enumerate(zip(self.layers, cache)):
|
|
344
|
-
if idx in self.config.cross_attention_layers:
|
|
345
|
-
layer_outputs = decoder_layer(
|
|
346
|
-
hidden_states,
|
|
347
|
-
cross_attention_states=cross_attention_states,
|
|
348
|
-
attention_mask=cross_attention_mask,
|
|
349
|
-
full_text_row_masked_out_mask=full_text_row_masked_out_mask,
|
|
350
|
-
cache=c,
|
|
351
|
-
)
|
|
352
|
-
else:
|
|
353
|
-
layer_outputs = decoder_layer(
|
|
354
|
-
hidden_states,
|
|
355
|
-
mask=mask,
|
|
356
|
-
cache=c,
|
|
357
|
-
)
|
|
358
|
-
hidden_states = layer_outputs
|
|
359
|
-
|
|
360
|
-
hidden_states = self.norm(hidden_states)
|
|
361
|
-
|
|
362
|
-
return hidden_states
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
class LanguageModel(nn.Module):
|
|
366
|
-
def __init__(self, config: TextConfig):
|
|
367
|
-
super().__init__()
|
|
368
|
-
self.config = config
|
|
369
|
-
self.model = MllamaTextModel(config)
|
|
370
|
-
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
|
371
|
-
|
|
372
|
-
def __call__(
|
|
373
|
-
self,
|
|
374
|
-
input_ids: Optional[mx.array] = None,
|
|
375
|
-
mask: Optional[mx.array] = None,
|
|
376
|
-
cross_attention_states: Optional[mx.array] = None,
|
|
377
|
-
cross_attention_mask: Optional[mx.array] = None,
|
|
378
|
-
full_text_row_masked_out_mask: Optional[mx.array] = None,
|
|
379
|
-
inputs_embeds: Optional[mx.array] = None,
|
|
380
|
-
cache: Optional[KVCache] = None,
|
|
381
|
-
) -> Tuple[mx.array, Optional[mx.array]]:
|
|
382
|
-
|
|
383
|
-
hidden_states = self.model(
|
|
384
|
-
input_ids=input_ids,
|
|
385
|
-
mask=mask,
|
|
386
|
-
cross_attention_states=cross_attention_states,
|
|
387
|
-
cross_attention_mask=cross_attention_mask,
|
|
388
|
-
full_text_row_masked_out_mask=full_text_row_masked_out_mask,
|
|
389
|
-
inputs_embeds=inputs_embeds,
|
|
390
|
-
cache=cache,
|
|
391
|
-
)
|
|
392
|
-
|
|
393
|
-
logits = self.lm_head(hidden_states)
|
|
394
|
-
|
|
395
|
-
return LanguageModelOutput(
|
|
396
|
-
logits=logits, cross_attention_states=cross_attention_states
|
|
397
|
-
)
|
|
398
|
-
|
|
399
|
-
@staticmethod
|
|
400
|
-
def sanitize(weights):
|
|
401
|
-
# Remove unused precomputed rotary freqs
|
|
402
|
-
return {
|
|
403
|
-
k: v for k, v in weights.items() if "self_attn.rotary_emb.inv_freq" not in k
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
@property
|
|
407
|
-
def layers(self):
|
|
408
|
-
return self.model.layers
|
|
409
|
-
|
|
410
|
-
@property
|
|
411
|
-
def head_dim(self):
|
|
412
|
-
return self.config.hidden_size // self.config.num_attention_heads
|
|
413
|
-
|
|
414
|
-
@property
|
|
415
|
-
def n_kv_heads(self):
|
|
416
|
-
return self.config.num_key_value_heads
|
|
@@ -1,172 +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, Tuple
|
|
7
|
-
|
|
8
|
-
import mlx.core as mx
|
|
9
|
-
import mlx.nn as nn
|
|
10
|
-
from huggingface_hub import snapshot_download
|
|
11
|
-
|
|
12
|
-
from ..cache import KVCache
|
|
13
|
-
from .language import LanguageModel, TextConfig
|
|
14
|
-
from .vision import VisionConfig, VisionModel
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@dataclass
|
|
18
|
-
class ModelConfig:
|
|
19
|
-
text_config: TextConfig
|
|
20
|
-
vision_config: VisionConfig
|
|
21
|
-
model_type: str
|
|
22
|
-
ignore_index: int = -100
|
|
23
|
-
image_token_index: int = 128256
|
|
24
|
-
vision_feature_select_strategy: str = "default"
|
|
25
|
-
vision_feature_layer: int = -2
|
|
26
|
-
vocab_size: int = 32000
|
|
27
|
-
eos_token_id: Optional[List[int]] = None
|
|
28
|
-
|
|
29
|
-
@classmethod
|
|
30
|
-
def from_dict(cls, params):
|
|
31
|
-
return cls(
|
|
32
|
-
**{
|
|
33
|
-
k: v
|
|
34
|
-
for k, v in params.items()
|
|
35
|
-
if k in inspect.signature(cls).parameters
|
|
36
|
-
}
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
class Model(nn.Module):
|
|
41
|
-
def __init__(self, config: ModelConfig):
|
|
42
|
-
super().__init__()
|
|
43
|
-
self.config = config
|
|
44
|
-
self.vision_tower = VisionModel(config.vision_config)
|
|
45
|
-
self.language_model = LanguageModel(config.text_config)
|
|
46
|
-
self.multi_modal_projector = nn.Linear(
|
|
47
|
-
config.vision_config.vision_output_dim,
|
|
48
|
-
config.text_config.hidden_size,
|
|
49
|
-
bias=True,
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
@property
|
|
53
|
-
def layers(self):
|
|
54
|
-
return self.language_model.model.layers
|
|
55
|
-
|
|
56
|
-
def __call__(
|
|
57
|
-
self,
|
|
58
|
-
input_ids: mx.array,
|
|
59
|
-
pixel_values: mx.array,
|
|
60
|
-
mask: mx.array,
|
|
61
|
-
cache: Optional[KVCache] = None,
|
|
62
|
-
**kwargs,
|
|
63
|
-
) -> Tuple[mx.array, Optional[mx.array]]:
|
|
64
|
-
|
|
65
|
-
aspect_ratio_ids = kwargs.pop("aspect_ratio_ids", None)
|
|
66
|
-
aspect_ratio_mask = kwargs.pop("aspect_ratio_mask", None)
|
|
67
|
-
cross_attention_mask = kwargs.pop("cross_attention_mask", None)
|
|
68
|
-
|
|
69
|
-
inputs_embeds = None
|
|
70
|
-
|
|
71
|
-
# Process vision input if provided
|
|
72
|
-
if pixel_values is not None:
|
|
73
|
-
if aspect_ratio_ids is None:
|
|
74
|
-
raise ValueError(
|
|
75
|
-
"`aspect_ratio_ids` must be provided if `pixel_values` is provided"
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
vision_outputs = self.vision_tower(
|
|
79
|
-
pixel_values=pixel_values,
|
|
80
|
-
aspect_ratio_ids=aspect_ratio_ids,
|
|
81
|
-
aspect_ratio_mask=aspect_ratio_mask,
|
|
82
|
-
)
|
|
83
|
-
cross_attention_states = vision_outputs[0]
|
|
84
|
-
|
|
85
|
-
cross_attention_states = self.multi_modal_projector(
|
|
86
|
-
cross_attention_states
|
|
87
|
-
).reshape(
|
|
88
|
-
-1,
|
|
89
|
-
cross_attention_states.shape[-2],
|
|
90
|
-
self.config.text_config.hidden_size,
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
else:
|
|
94
|
-
cross_attention_states = None
|
|
95
|
-
|
|
96
|
-
# Prepare cross attention mask
|
|
97
|
-
if cross_attention_mask is not None:
|
|
98
|
-
cross_attention_mask, full_text_row_masked_out_mask = (
|
|
99
|
-
self._prepare_cross_attention_mask(
|
|
100
|
-
cross_attention_mask,
|
|
101
|
-
num_vision_tokens=(
|
|
102
|
-
self.config.vision_config.image_size
|
|
103
|
-
// self.config.vision_config.patch_size
|
|
104
|
-
)
|
|
105
|
-
** 2
|
|
106
|
-
+ 1,
|
|
107
|
-
)
|
|
108
|
-
)
|
|
109
|
-
else:
|
|
110
|
-
full_text_row_masked_out_mask = None
|
|
111
|
-
|
|
112
|
-
if cross_attention_mask is not None:
|
|
113
|
-
cache_position = mx.arange(input_ids.shape[1], dtype=mx.int32)
|
|
114
|
-
cross_attention_mask = cross_attention_mask[:, :, cache_position]
|
|
115
|
-
full_text_row_masked_out_mask = full_text_row_masked_out_mask[
|
|
116
|
-
:, :, cache_position
|
|
117
|
-
]
|
|
118
|
-
|
|
119
|
-
# Process language input
|
|
120
|
-
outputs = self.language_model(
|
|
121
|
-
input_ids=input_ids,
|
|
122
|
-
mask=mask,
|
|
123
|
-
cross_attention_states=cross_attention_states,
|
|
124
|
-
cross_attention_mask=cross_attention_mask,
|
|
125
|
-
full_text_row_masked_out_mask=full_text_row_masked_out_mask,
|
|
126
|
-
inputs_embeds=inputs_embeds,
|
|
127
|
-
cache=cache,
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
return outputs
|
|
131
|
-
|
|
132
|
-
def _prepare_cross_attention_mask(
|
|
133
|
-
self,
|
|
134
|
-
cross_attention_mask: mx.array,
|
|
135
|
-
num_vision_tokens: int,
|
|
136
|
-
) -> Tuple[mx.array, mx.array]:
|
|
137
|
-
batch_size, text_total_length, *_ = cross_attention_mask.shape
|
|
138
|
-
cross_attention_mask = mx.repeat(
|
|
139
|
-
cross_attention_mask, num_vision_tokens, axis=3
|
|
140
|
-
)
|
|
141
|
-
cross_attention_mask = cross_attention_mask.reshape(
|
|
142
|
-
batch_size, text_total_length, -1
|
|
143
|
-
)
|
|
144
|
-
cross_attention_mask = mx.expand_dims(cross_attention_mask, 1)
|
|
145
|
-
|
|
146
|
-
# Invert the mask
|
|
147
|
-
inverted_cross_attn_mask = 1.0 - cross_attention_mask
|
|
148
|
-
fill_array = mx.array(-1e9)
|
|
149
|
-
fill_array = mx.broadcast_to(fill_array, inverted_cross_attn_mask.shape)
|
|
150
|
-
cross_attention_mask = mx.where(
|
|
151
|
-
inverted_cross_attn_mask,
|
|
152
|
-
fill_array,
|
|
153
|
-
cross_attention_mask,
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
# Apply full-row bias
|
|
157
|
-
full_text_row_masked_out_mask = mx.any(
|
|
158
|
-
cross_attention_mask != -1e9,
|
|
159
|
-
axis=-1,
|
|
160
|
-
keepdims=True,
|
|
161
|
-
)
|
|
162
|
-
cross_attention_mask *= full_text_row_masked_out_mask
|
|
163
|
-
|
|
164
|
-
return cross_attention_mask, full_text_row_masked_out_mask
|
|
165
|
-
|
|
166
|
-
def sanitize(self, weights):
|
|
167
|
-
def transform_key(key):
|
|
168
|
-
if "vision_tower" not in key:
|
|
169
|
-
key = key.replace("vision_model", "vision_tower")
|
|
170
|
-
return key
|
|
171
|
-
|
|
172
|
-
return {transform_key(k): v for k, v in weights.items()}
|