nexaai 1.0.19rc5__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc7__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/binds/nexa_llama_cpp/libggml-base.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libggml-cpu.so +0 -0
- nexaai/binds/nexa_llama_cpp/libggml-metal.so +0 -0
- nexaai/binds/nexa_llama_cpp/libggml.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libllama.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libmtmd.dylib +0 -0
- nexaai/binds/nexa_llama_cpp/libnexa_plugin.dylib +0 -0
- nexaai/binds/nexa_mlx/libnexa_plugin.dylib +0 -0
- nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +12 -0
- nexaai/binds/nexa_mlx/py-lib/asr/interface.py +122 -0
- nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/common/utils.py +25 -0
- nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/cv/generate.py +195 -0
- nexaai/binds/nexa_mlx/py-lib/cv/interface.py +151 -0
- nexaai/binds/nexa_mlx/py-lib/cv/main.py +81 -0
- nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +1736 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +333 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +617 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/main.py +173 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +399 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +1 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +244 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +82 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +281 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +306 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +116 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +65 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +386 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +105 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +100 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +460 -0
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +274 -0
- nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/llm/generate.py +149 -0
- nexaai/binds/nexa_mlx/py-lib/llm/interface.py +764 -0
- nexaai/binds/nexa_mlx/py-lib/llm/main.py +68 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +174 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +287 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/main.py +127 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +330 -0
- nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +1 -0
- nexaai/binds/nexa_mlx/py-lib/sd/interface.py +362 -0
- nexaai/binds/nexa_mlx/py-lib/sd/main.py +286 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +306 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +116 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +65 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +385 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +105 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +100 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +460 -0
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +274 -0
- nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +12 -0
- nexaai/binds/nexa_mlx/py-lib/tts/interface.py +276 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +3 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +572 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +294 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +276 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +504 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/main.py +320 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +68 -0
- 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 +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +193 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +186 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +233 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +503 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +202 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +230 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +10 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +264 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +472 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +591 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +526 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +356 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +366 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +488 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +591 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +213 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +315 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +238 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +2 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +1038 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +139 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +322 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +629 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +1022 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +9 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +294 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +191 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +267 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +175 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +192 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +233 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +9 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +140 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +220 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +393 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +293 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +307 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +143 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +509 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +522 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +386 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +138 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +560 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +240 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +153 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +259 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +9 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +236 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +256 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +303 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +230 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +160 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +243 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +283 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +416 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +172 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +499 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +243 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +133 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +465 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +10 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +230 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +385 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +557 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +526 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +282 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +160 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +242 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +21 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +243 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +71 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +324 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +229 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +161 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +320 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +2 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +108 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +490 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +168 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +414 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +2 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +104 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +490 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +167 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +312 -0
- 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 +117 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +531 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +701 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +255 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +303 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +407 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +476 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +1223 -0
- 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 +117 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +531 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +701 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +255 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +303 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +407 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +476 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +1309 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +210 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +8 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +62 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +209 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +215 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +474 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +39 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +344 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +9 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +70 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +296 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +160 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +928 -0
- nexaai/binds/nexa_nexaml/libggml-base.dylib +0 -0
- nexaai/binds/nexa_nexaml/libggml-cpu.so +0 -0
- nexaai/binds/nexa_nexaml/libggml-metal.so +0 -0
- nexaai/binds/nexa_nexaml/libggml.dylib +0 -0
- nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +276 -0
- nexaai/mlx_backend/vlm/interface.py +21 -4
- nexaai/mlx_backend/vlm/main.py +6 -2
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +117 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +531 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +701 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +255 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +303 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +407 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/processor.py +476 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +1309 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/switch_layers.py +210 -0
- {nexaai-1.0.19rc5.dist-info → nexaai-1.0.19rc7.dist-info}/METADATA +1 -1
- {nexaai-1.0.19rc5.dist-info → nexaai-1.0.19rc7.dist-info}/RECORD +221 -21
- {nexaai-1.0.19rc5.dist-info → nexaai-1.0.19rc7.dist-info}/WHEEL +0 -0
- {nexaai-1.0.19rc5.dist-info → nexaai-1.0.19rc7.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
from .interface import VLM
|
|
2
|
+
from ml import GenerationConfig, SamplerConfig, ChatMessage
|
|
3
|
+
import re
|
|
4
|
+
import os
|
|
5
|
+
import codecs
|
|
6
|
+
import argparse
|
|
7
|
+
|
|
8
|
+
def parse_media_from_input(user_input):
|
|
9
|
+
"""Parse quoted media files from user input and return prompt and media paths"""
|
|
10
|
+
# Find all quoted strings (both single and double quotes)
|
|
11
|
+
quoted_pattern = r'["\']([^"\']*)["\']'
|
|
12
|
+
quoted_matches = re.findall(quoted_pattern, user_input)
|
|
13
|
+
|
|
14
|
+
# Remove quoted strings from the input to get the actual prompt
|
|
15
|
+
prompt = re.sub(quoted_pattern, '', user_input).strip()
|
|
16
|
+
|
|
17
|
+
# Separate image and audio files based on extensions
|
|
18
|
+
image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp'}
|
|
19
|
+
audio_extensions = {'.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a'}
|
|
20
|
+
|
|
21
|
+
image_paths = []
|
|
22
|
+
audio_paths = []
|
|
23
|
+
|
|
24
|
+
for quoted_file in quoted_matches:
|
|
25
|
+
if quoted_file: # Skip empty quotes
|
|
26
|
+
# Expand user path if it starts with ~
|
|
27
|
+
if quoted_file.startswith('~'):
|
|
28
|
+
quoted_file = os.path.expanduser(quoted_file)
|
|
29
|
+
|
|
30
|
+
# Check if file exists
|
|
31
|
+
if not os.path.exists(quoted_file):
|
|
32
|
+
print(f"Warning: File '{quoted_file}' not found")
|
|
33
|
+
continue
|
|
34
|
+
|
|
35
|
+
file_ext = os.path.splitext(quoted_file.lower())[1]
|
|
36
|
+
if file_ext in image_extensions:
|
|
37
|
+
image_paths.append(quoted_file)
|
|
38
|
+
elif file_ext in audio_extensions:
|
|
39
|
+
audio_paths.append(quoted_file)
|
|
40
|
+
|
|
41
|
+
return prompt, image_paths if image_paths else None, audio_paths if audio_paths else None
|
|
42
|
+
|
|
43
|
+
def parse_arguments():
|
|
44
|
+
"""Parse command line arguments for the VLM main function."""
|
|
45
|
+
parser = argparse.ArgumentParser(
|
|
46
|
+
description="Interactive VLM (Vision-Language Model) conversation interface."
|
|
47
|
+
)
|
|
48
|
+
parser.add_argument(
|
|
49
|
+
"--model_path",
|
|
50
|
+
type=str,
|
|
51
|
+
default="mlx-community/gemma-3-4b-it-8bit",
|
|
52
|
+
help="The path to the local model directory or Hugging Face repo."
|
|
53
|
+
)
|
|
54
|
+
parser.add_argument(
|
|
55
|
+
"--model_name",
|
|
56
|
+
type=str,
|
|
57
|
+
default="",
|
|
58
|
+
help="Specific model name/type (e.g., 'qwen3vl', 'qwen3vl-moe', 'gemma3'). If empty, auto-detect from model_path."
|
|
59
|
+
)
|
|
60
|
+
parser.add_argument(
|
|
61
|
+
"--context_length",
|
|
62
|
+
type=int,
|
|
63
|
+
default=2048,
|
|
64
|
+
help="Context length for the model (default: 2048)."
|
|
65
|
+
)
|
|
66
|
+
parser.add_argument(
|
|
67
|
+
"--temperature",
|
|
68
|
+
type=float,
|
|
69
|
+
default=0.7,
|
|
70
|
+
help="Sampling temperature (default: 0.7)."
|
|
71
|
+
)
|
|
72
|
+
parser.add_argument(
|
|
73
|
+
"--top_p",
|
|
74
|
+
type=float,
|
|
75
|
+
default=0.9,
|
|
76
|
+
help="Top-p sampling parameter (default: 0.9)."
|
|
77
|
+
)
|
|
78
|
+
parser.add_argument(
|
|
79
|
+
"--max_tokens",
|
|
80
|
+
type=int,
|
|
81
|
+
default=512,
|
|
82
|
+
help="Maximum tokens to generate (default: 512)."
|
|
83
|
+
)
|
|
84
|
+
return parser.parse_args()
|
|
85
|
+
|
|
86
|
+
def main():
|
|
87
|
+
"""Main function for interactive VLM conversation."""
|
|
88
|
+
args = parse_arguments()
|
|
89
|
+
|
|
90
|
+
# Auto-detect model name if not provided
|
|
91
|
+
model_name = args.model_name
|
|
92
|
+
|
|
93
|
+
# TODO: avoid such hardcoded model name detection
|
|
94
|
+
if not model_name:
|
|
95
|
+
if "qwen3vl-30B" in args.model_path.lower():
|
|
96
|
+
model_name = "qwen3vl-moe"
|
|
97
|
+
elif "qwen3" in args.model_path.lower():
|
|
98
|
+
model_name = "qwen3vl"
|
|
99
|
+
elif "gemma" in args.model_path.lower():
|
|
100
|
+
model_name = "gemma3"
|
|
101
|
+
else:
|
|
102
|
+
model_name = ""
|
|
103
|
+
|
|
104
|
+
# Load the VLM instance
|
|
105
|
+
vlm = VLM(
|
|
106
|
+
model_name=model_name,
|
|
107
|
+
model_path=args.model_path,
|
|
108
|
+
mmproj_path=None, # Not needed for this model
|
|
109
|
+
context_length=args.context_length,
|
|
110
|
+
device=None
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
# Configure sampler
|
|
114
|
+
sampler_config = SamplerConfig(
|
|
115
|
+
temperature=args.temperature,
|
|
116
|
+
top_p=args.top_p
|
|
117
|
+
)
|
|
118
|
+
vlm.set_sampler(sampler_config)
|
|
119
|
+
|
|
120
|
+
# Chat history using ChatMessage objects
|
|
121
|
+
chat = []
|
|
122
|
+
|
|
123
|
+
print("VLM Multi-round conversation started. Type 'quit' or 'exit' to end.")
|
|
124
|
+
print("Include images/audios in quotes, e.g.: 'describe \"image1.jpg\" \"image2.png\"'")
|
|
125
|
+
print("You can also use single quotes: 'describe '/path/to/image.jpg''")
|
|
126
|
+
print("=" * 50)
|
|
127
|
+
|
|
128
|
+
def on_token(text_chunk):
|
|
129
|
+
"""Token callback for streaming"""
|
|
130
|
+
print(text_chunk, end="", flush=True)
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
while True:
|
|
134
|
+
# Get user input
|
|
135
|
+
user_input = input("\nUser: ").strip()
|
|
136
|
+
|
|
137
|
+
# Check for exit commands
|
|
138
|
+
if user_input.lower() in ["quit", "exit", "q"]:
|
|
139
|
+
print("Goodbye!")
|
|
140
|
+
break
|
|
141
|
+
|
|
142
|
+
if not user_input:
|
|
143
|
+
continue
|
|
144
|
+
|
|
145
|
+
# Parse media files and prompt from user input
|
|
146
|
+
prompt_text, image_paths, audio_paths = parse_media_from_input(user_input)
|
|
147
|
+
|
|
148
|
+
# If no text prompt after parsing, use the original input
|
|
149
|
+
if not prompt_text.strip():
|
|
150
|
+
prompt_text = user_input
|
|
151
|
+
image_paths = None
|
|
152
|
+
audio_paths = None
|
|
153
|
+
|
|
154
|
+
# Add user message to chat history using ChatMessage
|
|
155
|
+
chat.append(ChatMessage(role="user", content=prompt_text))
|
|
156
|
+
|
|
157
|
+
# Calculate number of images and audios for chat template
|
|
158
|
+
num_images = len(image_paths) if image_paths else 0
|
|
159
|
+
num_audios = len(audio_paths) if audio_paths else 0
|
|
160
|
+
|
|
161
|
+
# Apply chat template with image/audio token insertion
|
|
162
|
+
try:
|
|
163
|
+
formatted_prompt = vlm.apply_chat_template_with_media(chat, num_images=num_images, num_audios=num_audios)
|
|
164
|
+
except (NotImplementedError, AttributeError):
|
|
165
|
+
# Fallback to manual formatting if chat template is not implemented
|
|
166
|
+
formatted_prompt = ""
|
|
167
|
+
for msg in chat:
|
|
168
|
+
formatted_prompt += f"{msg.role}: {msg.content}\n"
|
|
169
|
+
formatted_prompt += "Assistant: "
|
|
170
|
+
|
|
171
|
+
# Generation config with media paths
|
|
172
|
+
generation_config = GenerationConfig(
|
|
173
|
+
max_tokens=args.max_tokens,
|
|
174
|
+
sampler_config=sampler_config,
|
|
175
|
+
image_paths=image_paths,
|
|
176
|
+
audio_paths=audio_paths
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# Generate response
|
|
180
|
+
print("Assistant: ", end="", flush=True)
|
|
181
|
+
|
|
182
|
+
try:
|
|
183
|
+
# Use streaming generation with callback
|
|
184
|
+
response_text = ""
|
|
185
|
+
|
|
186
|
+
def token_callback(text_chunk):
|
|
187
|
+
nonlocal response_text
|
|
188
|
+
print(text_chunk, end="", flush=True)
|
|
189
|
+
response_text += text_chunk
|
|
190
|
+
return True
|
|
191
|
+
|
|
192
|
+
# Use generate_stream method for streaming generation
|
|
193
|
+
response = vlm.generate_stream(
|
|
194
|
+
prompt=formatted_prompt,
|
|
195
|
+
config=generation_config,
|
|
196
|
+
on_token=token_callback
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
print() # New line after streaming
|
|
200
|
+
|
|
201
|
+
# Add assistant response to chat history using ChatMessage
|
|
202
|
+
chat.append(ChatMessage(role="assistant", content=response_text))
|
|
203
|
+
|
|
204
|
+
except Exception as e:
|
|
205
|
+
print(f"Error generating response: {e}")
|
|
206
|
+
print()
|
|
207
|
+
|
|
208
|
+
# Clean up
|
|
209
|
+
vlm.destroy()
|
|
210
|
+
|
|
211
|
+
def test_vlm_generate_stream(model_path, model_name):
|
|
212
|
+
# Specify the checkpoint
|
|
213
|
+
context_length = 2048
|
|
214
|
+
|
|
215
|
+
# Load the corresponding model and VLM instance
|
|
216
|
+
vlm = VLM(
|
|
217
|
+
model_name=model_name,
|
|
218
|
+
model_path=model_path,
|
|
219
|
+
mmproj_path=None, # Not needed for this model
|
|
220
|
+
context_length=context_length,
|
|
221
|
+
device=None
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
# Configure sampler
|
|
225
|
+
sampler_config = SamplerConfig(
|
|
226
|
+
temperature=0.7,
|
|
227
|
+
top_p=0.9
|
|
228
|
+
)
|
|
229
|
+
vlm.set_sampler(sampler_config)
|
|
230
|
+
|
|
231
|
+
# Chat history using ChatMessage objects (following ml.py API)
|
|
232
|
+
chat = []
|
|
233
|
+
|
|
234
|
+
print("Multi-round VLM conversation started. Type 'quit' or 'exit' to end.")
|
|
235
|
+
print("Include images/audios in quotes, e.g.: 'describe \"image1.jpg\" \"image2.png\"'")
|
|
236
|
+
print("You can also use single quotes: 'describe '/path/to/image.jpg''")
|
|
237
|
+
print("=" * 50)
|
|
238
|
+
|
|
239
|
+
def on_token(text_chunk, user_data):
|
|
240
|
+
"""Token callback for streaming"""
|
|
241
|
+
print(text_chunk, end="", flush=True)
|
|
242
|
+
if user_data is not None:
|
|
243
|
+
user_data["response"] += text_chunk
|
|
244
|
+
return True
|
|
245
|
+
|
|
246
|
+
while True:
|
|
247
|
+
# Get user input
|
|
248
|
+
user_input = input("\nUser: ").strip()
|
|
249
|
+
|
|
250
|
+
# Check for exit commands
|
|
251
|
+
if user_input.lower() in ["quit", "exit", "q"]:
|
|
252
|
+
print("Goodbye!")
|
|
253
|
+
break
|
|
254
|
+
|
|
255
|
+
if not user_input:
|
|
256
|
+
continue
|
|
257
|
+
|
|
258
|
+
# Parse media files and prompt from user input
|
|
259
|
+
prompt_text, image_paths, audio_paths = parse_media_from_input(user_input)
|
|
260
|
+
|
|
261
|
+
# If no text prompt after parsing, use the original input
|
|
262
|
+
if not prompt_text.strip():
|
|
263
|
+
prompt_text = user_input
|
|
264
|
+
image_paths = None
|
|
265
|
+
audio_paths = None
|
|
266
|
+
|
|
267
|
+
# Add user message to chat history using ChatMessage (following ml.py API)
|
|
268
|
+
chat.append(ChatMessage(role="user", content=prompt_text))
|
|
269
|
+
|
|
270
|
+
# Calculate number of images and audios for chat template
|
|
271
|
+
num_images = len(image_paths) if image_paths else 0
|
|
272
|
+
num_audios = len(audio_paths) if audio_paths else 0
|
|
273
|
+
|
|
274
|
+
# Apply chat template with image/audio token insertion
|
|
275
|
+
try:
|
|
276
|
+
formatted_prompt = vlm.apply_chat_template_with_media(chat, num_images=num_images, num_audios=num_audios)
|
|
277
|
+
except (NotImplementedError, AttributeError):
|
|
278
|
+
# Fallback to manual formatting if chat template is not implemented
|
|
279
|
+
formatted_prompt = ""
|
|
280
|
+
for msg in chat:
|
|
281
|
+
formatted_prompt += f"{msg.role}: {msg.content}\n"
|
|
282
|
+
formatted_prompt += "Assistant: "
|
|
283
|
+
|
|
284
|
+
# Generation config with media paths
|
|
285
|
+
generation_config = GenerationConfig(
|
|
286
|
+
max_tokens=512,
|
|
287
|
+
sampler_config=sampler_config,
|
|
288
|
+
image_paths=image_paths,
|
|
289
|
+
audio_paths=audio_paths
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
# Generate response
|
|
293
|
+
print("Assistant: ", end="", flush=True)
|
|
294
|
+
|
|
295
|
+
try:
|
|
296
|
+
# Use streaming generation with callback - single method handles all cases
|
|
297
|
+
user_data = {"response": ""}
|
|
298
|
+
|
|
299
|
+
# Always use the unified generate_stream method
|
|
300
|
+
response = vlm.generate_stream(
|
|
301
|
+
prompt=formatted_prompt,
|
|
302
|
+
config=generation_config,
|
|
303
|
+
on_token=on_token,
|
|
304
|
+
user_data=user_data
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
print() # New line after streaming
|
|
308
|
+
|
|
309
|
+
# Add assistant response to chat history using ChatMessage
|
|
310
|
+
chat.append(ChatMessage(role="assistant", content=user_data["response"]))
|
|
311
|
+
|
|
312
|
+
except Exception as e:
|
|
313
|
+
print(f"Error generating response: {e}")
|
|
314
|
+
print()
|
|
315
|
+
|
|
316
|
+
# Clean up
|
|
317
|
+
vlm.destroy()
|
|
318
|
+
|
|
319
|
+
if __name__ == "__main__":
|
|
320
|
+
main()
|
|
File without changes
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Copyright © 2023-2024 Apple Inc.
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
|
|
5
|
+
from .utils import MODEL_CONVERSION_DTYPES, convert
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def configure_parser() -> argparse.ArgumentParser:
|
|
9
|
+
"""
|
|
10
|
+
Configures and returns the argument parser for the script.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
argparse.ArgumentParser: Configured argument parser.
|
|
14
|
+
"""
|
|
15
|
+
parser = argparse.ArgumentParser(
|
|
16
|
+
description="Convert Hugging Face model to MLX format"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
parser.add_argument("--hf-path", type=str, help="Path to the Hugging Face model.")
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--mlx-path", type=str, default="mlx_model", help="Path to save the MLX model."
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"-q", "--quantize", help="Generate a quantized model.", action="store_true"
|
|
25
|
+
)
|
|
26
|
+
parser.add_argument(
|
|
27
|
+
"--q-group-size", help="Group size for quantization.", type=int, default=64
|
|
28
|
+
)
|
|
29
|
+
parser.add_argument(
|
|
30
|
+
"--q-bits", help="Bits per weight for quantization.", type=int, default=4
|
|
31
|
+
)
|
|
32
|
+
parser.add_argument(
|
|
33
|
+
"--dtype",
|
|
34
|
+
help="Type to save the parameter. Defaults to config.json's `torch_dtype` or the current model weights dtype",
|
|
35
|
+
type=str,
|
|
36
|
+
choices=MODEL_CONVERSION_DTYPES,
|
|
37
|
+
default=None,
|
|
38
|
+
)
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--upload-repo",
|
|
41
|
+
help="The Hugging Face repo to upload the model to.",
|
|
42
|
+
type=str,
|
|
43
|
+
default=None,
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"-d",
|
|
47
|
+
"--dequantize",
|
|
48
|
+
help="Dequantize a quantized model.",
|
|
49
|
+
action="store_true",
|
|
50
|
+
default=False,
|
|
51
|
+
)
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"--skip-vision",
|
|
54
|
+
help="Skip vision module quantization.",
|
|
55
|
+
action="store_true",
|
|
56
|
+
default=False,
|
|
57
|
+
)
|
|
58
|
+
return parser
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def main():
|
|
62
|
+
parser = configure_parser()
|
|
63
|
+
args = parser.parse_args()
|
|
64
|
+
convert(**vars(args))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
main()
|
|
File without changes
|
|
@@ -0,0 +1,193 @@
|
|
|
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 mlx_lm.utils import snapshot_download
|
|
12
|
+
|
|
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
|
+
image_token_index: int = 255036
|
|
23
|
+
max_splits_per_img: int = 12
|
|
24
|
+
downsample_factor: int = 2
|
|
25
|
+
alignment_intermediate_size: int = 28672
|
|
26
|
+
adapter_layer_norm_eps: float = 1e-06
|
|
27
|
+
vision_feature_layer: int = -1
|
|
28
|
+
vision_feature_select_strategy: str = "full"
|
|
29
|
+
eos_token_id: Optional[List[int]] = None
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def from_dict(cls, params):
|
|
33
|
+
return cls(
|
|
34
|
+
**{
|
|
35
|
+
k: v
|
|
36
|
+
for k, v in params.items()
|
|
37
|
+
if k in inspect.signature(cls).parameters
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class AyaVisionMultiModalProjector(nn.Module):
|
|
43
|
+
def __init__(self, config: ModelConfig):
|
|
44
|
+
super().__init__()
|
|
45
|
+
self.config = config
|
|
46
|
+
self.downsample_factor = config.downsample_factor
|
|
47
|
+
self.alignment_intermediate_size = getattr(
|
|
48
|
+
config, "alignment_intermediate_size", config.text_config.hidden_size
|
|
49
|
+
)
|
|
50
|
+
self.layernorm = nn.LayerNorm(
|
|
51
|
+
config.vision_config.hidden_size * (config.downsample_factor**2),
|
|
52
|
+
eps=config.adapter_layer_norm_eps,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
self.linear_1 = nn.Linear(
|
|
56
|
+
config.vision_config.hidden_size * (config.downsample_factor**2),
|
|
57
|
+
self.alignment_intermediate_size,
|
|
58
|
+
bias=True,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
self.act = nn.SiLU() # SwiGLU uses SiLU activation
|
|
62
|
+
|
|
63
|
+
# For SwiGLU, project down to half size since we split intermediate dim
|
|
64
|
+
self.linear_2 = nn.Linear(
|
|
65
|
+
self.alignment_intermediate_size // 2,
|
|
66
|
+
config.text_config.hidden_size,
|
|
67
|
+
bias=True,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def __call__(self, image_features):
|
|
71
|
+
image_features = self.pixel_shuffle(image_features)
|
|
72
|
+
image_features = self.layernorm(image_features)
|
|
73
|
+
hidden_states = self.linear_1(image_features)
|
|
74
|
+
|
|
75
|
+
# Split along last dimension and apply SwiGLU
|
|
76
|
+
x, gate = mx.split(hidden_states, 2, axis=-1)
|
|
77
|
+
hidden_states = self.act(gate) * x
|
|
78
|
+
|
|
79
|
+
hidden_states = self.linear_2(hidden_states)
|
|
80
|
+
return hidden_states
|
|
81
|
+
|
|
82
|
+
def pixel_shuffle(self, image_features): # B, S, D
|
|
83
|
+
batch_size, seq_length, feature_dim = image_features.shape
|
|
84
|
+
height = width = int(seq_length**0.5)
|
|
85
|
+
image_features = image_features.reshape(
|
|
86
|
+
image_features.shape[0], width, height, -1
|
|
87
|
+
)
|
|
88
|
+
channels = image_features.shape[-1]
|
|
89
|
+
image_features = image_features.reshape(
|
|
90
|
+
batch_size,
|
|
91
|
+
width,
|
|
92
|
+
int(height / self.downsample_factor),
|
|
93
|
+
int(channels * self.downsample_factor),
|
|
94
|
+
)
|
|
95
|
+
image_features = image_features.transpose(0, 2, 1, 3)
|
|
96
|
+
image_features = image_features.reshape(
|
|
97
|
+
batch_size,
|
|
98
|
+
int(height / self.downsample_factor),
|
|
99
|
+
int(width / self.downsample_factor),
|
|
100
|
+
-1,
|
|
101
|
+
)
|
|
102
|
+
image_features = image_features.transpose(0, 2, 1, 3)
|
|
103
|
+
return image_features
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Model(nn.Module):
|
|
107
|
+
def __init__(self, config: ModelConfig):
|
|
108
|
+
super().__init__()
|
|
109
|
+
self.config = config
|
|
110
|
+
self.vision_tower = VisionModel(config.vision_config)
|
|
111
|
+
self.language_model = LanguageModel(config.text_config)
|
|
112
|
+
self.multi_modal_projector = AyaVisionMultiModalProjector(config)
|
|
113
|
+
self.vision_feature_layer = config.vision_feature_layer
|
|
114
|
+
self.vision_feature_select_strategy = config.vision_feature_select_strategy
|
|
115
|
+
|
|
116
|
+
def get_input_embeddings(
|
|
117
|
+
self,
|
|
118
|
+
input_ids: Optional[mx.array] = None,
|
|
119
|
+
pixel_values: Optional[mx.array] = None,
|
|
120
|
+
**kwargs,
|
|
121
|
+
):
|
|
122
|
+
if pixel_values is None:
|
|
123
|
+
return self.language_model.model.embed_tokens(input_ids)
|
|
124
|
+
|
|
125
|
+
# Get the input embeddings from the language model
|
|
126
|
+
inputs_embeds = self.language_model.model.embed_tokens(input_ids)
|
|
127
|
+
|
|
128
|
+
spatial_shapes = kwargs.get("spatial_shapes", None)
|
|
129
|
+
# Get the ouptut hidden states from the vision model
|
|
130
|
+
*_, hidden_states = self.vision_tower(
|
|
131
|
+
pixel_values.transpose(0, 2, 3, 1),
|
|
132
|
+
spatial_shapes=spatial_shapes,
|
|
133
|
+
output_hidden_states=True,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# Select the hidden states from the desired layer
|
|
137
|
+
selected_image_feature = hidden_states[self.vision_feature_layer]
|
|
138
|
+
|
|
139
|
+
if self.vision_feature_select_strategy == "default":
|
|
140
|
+
selected_image_feature = selected_image_feature[:, 1:]
|
|
141
|
+
elif self.vision_feature_select_strategy == "full":
|
|
142
|
+
selected_image_feature = selected_image_feature
|
|
143
|
+
else:
|
|
144
|
+
raise ValueError(
|
|
145
|
+
"Unexpected feature selection strategy: "
|
|
146
|
+
f"{self.vision_feature_select_strategy}"
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Pass image features through the multi-modal projector
|
|
150
|
+
image_features = self.multi_modal_projector(selected_image_feature)
|
|
151
|
+
|
|
152
|
+
# Insert special image tokens in the input_ids
|
|
153
|
+
final_inputs_embeds = self._merge_input_ids_with_image_features(
|
|
154
|
+
image_features, inputs_embeds, input_ids
|
|
155
|
+
)
|
|
156
|
+
return final_inputs_embeds
|
|
157
|
+
|
|
158
|
+
def _merge_input_ids_with_image_features(
|
|
159
|
+
self, image_features, inputs_embeds, input_ids
|
|
160
|
+
):
|
|
161
|
+
image_token_index = self.config.image_token_index
|
|
162
|
+
|
|
163
|
+
# Positions of <image> tokens in input_ids, assuming batch size is 1
|
|
164
|
+
image_positions = np.where(input_ids[0] == image_token_index)[0].tolist()
|
|
165
|
+
num_images, _, _, vision_hidden_size = image_features.shape
|
|
166
|
+
|
|
167
|
+
reshaped_image_hidden_states = image_features.reshape(-1, vision_hidden_size)
|
|
168
|
+
|
|
169
|
+
# cast to the dtype of the input_embeds to support quantized models
|
|
170
|
+
reshaped_image_hidden_states = reshaped_image_hidden_states.astype(
|
|
171
|
+
inputs_embeds.dtype
|
|
172
|
+
)
|
|
173
|
+
inputs_embeds[:, image_positions, :] = reshaped_image_hidden_states
|
|
174
|
+
return inputs_embeds
|
|
175
|
+
|
|
176
|
+
@property
|
|
177
|
+
def layers(self):
|
|
178
|
+
return self.language_model.model.layers
|
|
179
|
+
|
|
180
|
+
def __call__(
|
|
181
|
+
self,
|
|
182
|
+
input_ids: mx.array,
|
|
183
|
+
pixel_values: mx.array,
|
|
184
|
+
mask: mx.array,
|
|
185
|
+
cache=None,
|
|
186
|
+
**kwargs,
|
|
187
|
+
):
|
|
188
|
+
|
|
189
|
+
input_embddings = self.get_input_embeddings(input_ids, pixel_values, **kwargs)
|
|
190
|
+
logits = self.language_model(
|
|
191
|
+
input_ids, cache=cache, inputs_embeds=input_embddings
|
|
192
|
+
)
|
|
193
|
+
return logits
|