nexaai 1.0.19rc6__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/utils/manifest_utils.py +222 -15
- nexaai/utils/model_manager.py +83 -7
- nexaai/utils/model_types.py +2 -0
- {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/METADATA +1 -1
- {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/RECORD +224 -24
- {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/WHEEL +0 -0
- {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
import os
|
|
5
|
+
import mlx.core as mx
|
|
6
|
+
import mlx.nn as nn
|
|
7
|
+
import time
|
|
8
|
+
from PIL import Image
|
|
9
|
+
import requests
|
|
10
|
+
import numpy as np
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from huggingface_hub import snapshot_download
|
|
13
|
+
|
|
14
|
+
# Add current directory to path for imports
|
|
15
|
+
curr_dir = os.path.dirname(os.path.abspath(__file__))
|
|
16
|
+
sys.path.append(curr_dir)
|
|
17
|
+
sys.path.append(os.path.dirname(curr_dir))
|
|
18
|
+
|
|
19
|
+
# Add the qwen3vl model directory to path
|
|
20
|
+
qwen3vl_dir = os.path.join(curr_dir, "modeling", "models", "qwen3_vl")
|
|
21
|
+
sys.path.append(qwen3vl_dir)
|
|
22
|
+
|
|
23
|
+
# Import required modules for quantized loading
|
|
24
|
+
from transformers import AutoTokenizer
|
|
25
|
+
|
|
26
|
+
# Try relative imports first, fallback to sys.path approach for Nuitka compatibility
|
|
27
|
+
try:
|
|
28
|
+
from .modeling.models.qwen3_vl.llm_common.generate import nexa_generate_step
|
|
29
|
+
from .modeling.models.qwen3_vl.llm_common.cache import make_prompt_cache
|
|
30
|
+
from .modeling.models.qwen3_vl.qwen3vl import (
|
|
31
|
+
VEGModel, LLMModel, ModelArgs, VisionConfig, TextConfig, handle_multimodal_embeds
|
|
32
|
+
)
|
|
33
|
+
from .modeling.models.qwen3_vl.processor import Qwen3VLProcessor
|
|
34
|
+
except ImportError:
|
|
35
|
+
# Fallback for Nuitka compiled environment - use sys.path approach
|
|
36
|
+
from llm_common.generate import nexa_generate_step
|
|
37
|
+
from llm_common.cache import make_prompt_cache
|
|
38
|
+
from qwen3vl import VEGModel, LLMModel, ModelArgs, VisionConfig, TextConfig, handle_multimodal_embeds
|
|
39
|
+
from processor import Qwen3VLProcessor
|
|
40
|
+
|
|
41
|
+
from ml import ChatMessage
|
|
42
|
+
from dataclasses import dataclass
|
|
43
|
+
from typing import Any, Generator, List, Optional, Sequence, Tuple, Union
|
|
44
|
+
from .generate import GenerationResult
|
|
45
|
+
|
|
46
|
+
# Custom exception for context length exceeded
|
|
47
|
+
class ContextLengthExceededError(Exception):
|
|
48
|
+
"""Raised when input context length exceeds model's maximum context size"""
|
|
49
|
+
pass
|
|
50
|
+
|
|
51
|
+
@dataclass
|
|
52
|
+
class Qwen3VLBundledModel:
|
|
53
|
+
"""Container for Qwen3-VL vision and language models."""
|
|
54
|
+
vision_model: VEGModel
|
|
55
|
+
llm_model: LLMModel
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _ensure_list(x: Union[str, List[str], None]) -> Optional[List[str]]:
|
|
59
|
+
if x is None:
|
|
60
|
+
return None
|
|
61
|
+
return x if isinstance(x, list) else [x]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def load_qwen3_vl(
|
|
65
|
+
path_or_repo: str,
|
|
66
|
+
adapter_path: Optional[str] = None,
|
|
67
|
+
lazy: bool = False,
|
|
68
|
+
revision: Optional[str] = None,
|
|
69
|
+
**kwargs,
|
|
70
|
+
) -> Tuple[Qwen3VLBundledModel, Qwen3VLProcessor]:
|
|
71
|
+
"""Load Qwen3-VL quantized models and processor.
|
|
72
|
+
|
|
73
|
+
Parameters are aligned with .generate.load for compatibility.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
model_path = Path(path_or_repo)
|
|
77
|
+
if not model_path.exists():
|
|
78
|
+
if "/" in path_or_repo:
|
|
79
|
+
model_path = Path(snapshot_download(
|
|
80
|
+
repo_id=path_or_repo, repo_type="model", revision=revision))
|
|
81
|
+
else:
|
|
82
|
+
# Fallback to local modelfiles directory
|
|
83
|
+
model_path = Path(qwen3vl_dir) / "modelfiles"
|
|
84
|
+
if not model_path.exists():
|
|
85
|
+
model_path = Path(curr_dir) / "modelfiles"
|
|
86
|
+
|
|
87
|
+
# Model configs (kept identical to main)
|
|
88
|
+
vision_config = VisionConfig(
|
|
89
|
+
hidden_size=1024,
|
|
90
|
+
intermediate_size=4096,
|
|
91
|
+
num_heads=16,
|
|
92
|
+
num_hidden_layers=24,
|
|
93
|
+
patch_size=16,
|
|
94
|
+
temporal_patch_size=2,
|
|
95
|
+
in_channels=3,
|
|
96
|
+
hidden_act="gelu",
|
|
97
|
+
spatial_merge_size=2,
|
|
98
|
+
out_hidden_size=2560,
|
|
99
|
+
num_position_embeddings=2304,
|
|
100
|
+
deepstack_visual_indexes=[5, 11, 17],
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
text_config = TextConfig(
|
|
104
|
+
model_type="qwen3vl",
|
|
105
|
+
hidden_size=2560,
|
|
106
|
+
num_hidden_layers=36,
|
|
107
|
+
intermediate_size=9728,
|
|
108
|
+
num_attention_heads=32,
|
|
109
|
+
num_key_value_heads=8,
|
|
110
|
+
rms_norm_eps=1e-6,
|
|
111
|
+
vocab_size=151936,
|
|
112
|
+
max_position_embeddings=32768,
|
|
113
|
+
rope_theta=5000000.0,
|
|
114
|
+
head_dim=128,
|
|
115
|
+
tie_word_embeddings=True,
|
|
116
|
+
attention_bias=False,
|
|
117
|
+
attention_dropout=0.0,
|
|
118
|
+
rope_scaling={"mrope_section": [24, 20, 20],
|
|
119
|
+
"rope_type": "default", "type": "default"},
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
vision_model = VEGModel(vision_config)
|
|
123
|
+
llm_model = LLMModel(text_config)
|
|
124
|
+
|
|
125
|
+
# Try to load LLM model from available files in order of preference
|
|
126
|
+
preferred_order = [
|
|
127
|
+
("qwen3vl-llm-4B-q4_0.safetensors", 4),
|
|
128
|
+
("qwen3vl-llm-4B-q8_0.safetensors", 8),
|
|
129
|
+
("qwen3vl-llm-4B-f32.safetensors", 32)
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
llm_weights_path = None
|
|
133
|
+
quantization_bits = None
|
|
134
|
+
|
|
135
|
+
# Try loading in order of preference
|
|
136
|
+
for filename, bits in preferred_order:
|
|
137
|
+
candidate_path = model_path / filename
|
|
138
|
+
if candidate_path.exists():
|
|
139
|
+
llm_weights_path = candidate_path
|
|
140
|
+
quantization_bits = bits
|
|
141
|
+
break
|
|
142
|
+
|
|
143
|
+
if llm_weights_path is None:
|
|
144
|
+
# Fallback to original hardcoded path for backward compatibility
|
|
145
|
+
llm_weights_path = model_path / "qwen3vl-llm-4B-q4_0.safetensors"
|
|
146
|
+
quantization_bits = 4
|
|
147
|
+
|
|
148
|
+
vision_weights_path = model_path / "qwen3vl-vision-4B-f16.safetensors"
|
|
149
|
+
|
|
150
|
+
if not vision_weights_path.exists() or not llm_weights_path.exists():
|
|
151
|
+
raise FileNotFoundError(
|
|
152
|
+
f"Missing safetensors. Vision: {vision_weights_path}, LLM: {llm_weights_path}"
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Load weights (vision fp16, llm with detected quantization)
|
|
156
|
+
vision_model.set_dtype(mx.float16)
|
|
157
|
+
vision_model.load_weights(str(vision_weights_path), strict=True)
|
|
158
|
+
|
|
159
|
+
# Apply quantization if needed and load LLM weights
|
|
160
|
+
if quantization_bits in [4, 8]:
|
|
161
|
+
nn.quantize(llm_model, bits=quantization_bits, group_size=64,
|
|
162
|
+
class_predicate=quant_predicate)
|
|
163
|
+
|
|
164
|
+
llm_model.load_weights(str(llm_weights_path), strict=True)
|
|
165
|
+
|
|
166
|
+
# Tokenizer and processor
|
|
167
|
+
tokenizer = AutoTokenizer.from_pretrained(path_or_repo)
|
|
168
|
+
processor = Qwen3VLProcessor(tokenizer=tokenizer)
|
|
169
|
+
|
|
170
|
+
return Qwen3VLBundledModel(vision_model=vision_model, llm_model=llm_model), processor
|
|
171
|
+
|
|
172
|
+
def apply_chat_template_qwen3_vl(messages: Sequence[ChatMessage], num_images: int = 0, num_audios: int = 0, tools: Optional[str] = None, enable_thinking: bool = False) -> str:
|
|
173
|
+
"""Apply chat template: serialize messages with content as a list of typed items."""
|
|
174
|
+
|
|
175
|
+
messages_dict = []
|
|
176
|
+
for i, msg in enumerate(messages):
|
|
177
|
+
content_items = [{"type": "text", "text": msg.content}]
|
|
178
|
+
messages_dict.append({"role": msg.role, "content": content_items})
|
|
179
|
+
|
|
180
|
+
result = json.dumps(messages_dict)
|
|
181
|
+
|
|
182
|
+
return result
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def stream_generate_qwen3_vl(
|
|
186
|
+
model: Qwen3VLBundledModel,
|
|
187
|
+
processor: Qwen3VLProcessor,
|
|
188
|
+
prompt: str,
|
|
189
|
+
image: Union[str, List[str]] = None,
|
|
190
|
+
audio: Union[str, List[str]] = None,
|
|
191
|
+
max_tokens: int = 512,
|
|
192
|
+
**kwargs,
|
|
193
|
+
|
|
194
|
+
) -> Generator[Any, None, None]:
|
|
195
|
+
"""Stream generation yielding .generate.GenerationResult-compatible chunks."""
|
|
196
|
+
|
|
197
|
+
try:
|
|
198
|
+
messages = json.loads(prompt)
|
|
199
|
+
except json.JSONDecodeError as e:
|
|
200
|
+
raise
|
|
201
|
+
|
|
202
|
+
if image is not None:
|
|
203
|
+
image_list = image if isinstance(image, list) else [image]
|
|
204
|
+
pil_images = []
|
|
205
|
+
for i, p in enumerate(image_list):
|
|
206
|
+
try:
|
|
207
|
+
img = Image.open(p)
|
|
208
|
+
pil_images.append(img)
|
|
209
|
+
except Exception as e:
|
|
210
|
+
continue
|
|
211
|
+
|
|
212
|
+
contents = [{"type": "image", "image": img} for img in pil_images]
|
|
213
|
+
if messages:
|
|
214
|
+
if "content" not in messages[-1] or not isinstance(messages[-1]["content"], list):
|
|
215
|
+
messages[-1]["content"] = []
|
|
216
|
+
messages[-1]["content"].extend(contents)
|
|
217
|
+
|
|
218
|
+
raw_text, processed_images = processor.messages_to_text(
|
|
219
|
+
messages, add_generation_prompt=True)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
inputs = processor.text_to_input_ids(
|
|
223
|
+
raw_text, images=processed_images, return_tensors="mlx")
|
|
224
|
+
|
|
225
|
+
input_ids = inputs["input_ids"]
|
|
226
|
+
pixel_values = inputs.get("pixel_values")
|
|
227
|
+
image_grid_thw = inputs.get("image_grid_thw")
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
# Check if input context exceeds KV cache size and raise error
|
|
231
|
+
max_kv_size = 4096 # This should match the max_kv_size used in make_prompt_cache and nexa_generate_step
|
|
232
|
+
if input_ids.size > max_kv_size:
|
|
233
|
+
error_msg = f"Input context length ({input_ids.size} tokens) exceeds maximum supported context size ({max_kv_size} tokens). Please reduce the input length."
|
|
234
|
+
raise ContextLengthExceededError(error_msg)
|
|
235
|
+
|
|
236
|
+
inputs_embeds, deepstack_visual_embeds, visual_pos_masks, cos, sin, rope_deltas = handle_multimodal_embeds(
|
|
237
|
+
model.vision_model, model.llm_model, input_ids, pixel_values, image_grid_thw
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
prompt_cache = make_prompt_cache(model.llm_model, max_kv_size=4096)
|
|
242
|
+
tokenizer = processor.tokenizer
|
|
243
|
+
|
|
244
|
+
# Rough prompt TPS estimation based on input size
|
|
245
|
+
prompt_start = time.perf_counter()
|
|
246
|
+
prompt_tps = input_ids.size / max(1e-6, (time.perf_counter() - prompt_start))
|
|
247
|
+
|
|
248
|
+
gen_count = 0
|
|
249
|
+
tic = time.perf_counter()
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
try:
|
|
253
|
+
for token, logprobs in nexa_generate_step(
|
|
254
|
+
model=model.llm_model,
|
|
255
|
+
prompt=None,
|
|
256
|
+
input_embeddings=inputs_embeds,
|
|
257
|
+
max_tokens=max_tokens,
|
|
258
|
+
max_kv_size=4096,
|
|
259
|
+
prompt_cache=prompt_cache,
|
|
260
|
+
visual_pos_masks=visual_pos_masks,
|
|
261
|
+
deepstack_visual_embeds=deepstack_visual_embeds,
|
|
262
|
+
cos=cos,
|
|
263
|
+
sin=sin,
|
|
264
|
+
rope_deltas=rope_deltas,
|
|
265
|
+
):
|
|
266
|
+
if token == tokenizer.eos_token_id:
|
|
267
|
+
break
|
|
268
|
+
|
|
269
|
+
text_piece = tokenizer.decode([token])
|
|
270
|
+
gen_count += 1
|
|
271
|
+
|
|
272
|
+
current_tps = gen_count / max(1e-6, (time.perf_counter() - tic))
|
|
273
|
+
|
|
274
|
+
yield GenerationResult(
|
|
275
|
+
text=text_piece,
|
|
276
|
+
token=token,
|
|
277
|
+
logprobs=logprobs,
|
|
278
|
+
prompt_tokens=int(input_ids.size),
|
|
279
|
+
generation_tokens=gen_count,
|
|
280
|
+
prompt_tps=float(prompt_tps),
|
|
281
|
+
generation_tps=float(current_tps),
|
|
282
|
+
peak_memory=float(mx.get_peak_memory() / 1e9),
|
|
283
|
+
)
|
|
284
|
+
except Exception as e:
|
|
285
|
+
import traceback
|
|
286
|
+
traceback.print_exc()
|
|
287
|
+
raise
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def quant_predicate(path: str, mod: nn.Module) -> bool:
|
|
291
|
+
"""Quantization predicate to exclude certain layers from quantization."""
|
|
292
|
+
if path.endswith("lm_head") or "norm" in path.lower() or "embed" in path.lower():
|
|
293
|
+
return False
|
|
294
|
+
return isinstance(mod, (nn.Linear, nn.Embedding))
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import json
|
|
3
|
+
import sys
|
|
4
|
+
import os
|
|
5
|
+
import mlx.core as mx
|
|
6
|
+
import mlx.nn as nn
|
|
7
|
+
import time
|
|
8
|
+
from PIL import Image
|
|
9
|
+
import requests
|
|
10
|
+
import numpy as np
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from huggingface_hub import snapshot_download
|
|
13
|
+
|
|
14
|
+
# Add current directory to path for imports
|
|
15
|
+
curr_dir = os.path.dirname(os.path.abspath(__file__))
|
|
16
|
+
sys.path.append(curr_dir)
|
|
17
|
+
sys.path.append(os.path.dirname(curr_dir))
|
|
18
|
+
|
|
19
|
+
# Add the qwen3vl model directory to path
|
|
20
|
+
qwen3vl_dir = os.path.join(curr_dir, "modeling", "models", "qwen3vl_moe")
|
|
21
|
+
sys.path.append(qwen3vl_dir)
|
|
22
|
+
|
|
23
|
+
# Import required modules for quantized loading
|
|
24
|
+
from transformers import AutoTokenizer
|
|
25
|
+
|
|
26
|
+
# Try relative imports first, fallback to sys.path approach for Nuitka compatibility
|
|
27
|
+
try:
|
|
28
|
+
from .modeling.models.qwen3_vl_moe.llm_common.generate import nexa_generate_step
|
|
29
|
+
from .modeling.models.qwen3_vl_moe.llm_common.cache import make_prompt_cache
|
|
30
|
+
from .modeling.models.qwen3_vl_moe.qwen3vl_moe import (
|
|
31
|
+
VEGModel, LLMModel, ModelArgs, VisionConfig, TextConfig, handle_multimodal_embeds
|
|
32
|
+
)
|
|
33
|
+
from .modeling.models.qwen3_vl_moe.processor import Qwen3VLProcessor
|
|
34
|
+
except ImportError:
|
|
35
|
+
# Fallback for Nuitka compiled environment - use sys.path approach
|
|
36
|
+
from llm_common.generate import nexa_generate_step
|
|
37
|
+
from llm_common.cache import make_prompt_cache
|
|
38
|
+
from qwen3vl_moe import VEGModel, LLMModel, ModelArgs, VisionConfig, TextConfig, handle_multimodal_embeds
|
|
39
|
+
from processor import Qwen3VLProcessor
|
|
40
|
+
|
|
41
|
+
from ml import ChatMessage
|
|
42
|
+
from dataclasses import dataclass
|
|
43
|
+
from typing import Any, Generator, List, Optional, Sequence, Tuple, Union
|
|
44
|
+
from .generate import GenerationResult
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class Qwen3VLBundledModel:
|
|
48
|
+
"""Container for Qwen3-VL MoE vision and language models."""
|
|
49
|
+
vision_model: VEGModel
|
|
50
|
+
llm_model: LLMModel
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _ensure_list(x: Union[str, List[str], None]) -> Optional[List[str]]:
|
|
54
|
+
if x is None:
|
|
55
|
+
return None
|
|
56
|
+
return x if isinstance(x, list) else [x]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def load_qwen3_vl(
|
|
60
|
+
path_or_repo: str,
|
|
61
|
+
adapter_path: Optional[str] = None,
|
|
62
|
+
lazy: bool = False,
|
|
63
|
+
revision: Optional[str] = None,
|
|
64
|
+
**kwargs,
|
|
65
|
+
) -> Tuple[Qwen3VLBundledModel, Qwen3VLProcessor]:
|
|
66
|
+
"""Load Qwen3-VL MoE quantized models and processor.
|
|
67
|
+
|
|
68
|
+
Parameters are aligned with .generate.load for compatibility.
|
|
69
|
+
"""
|
|
70
|
+
model_path = Path(path_or_repo)
|
|
71
|
+
if not model_path.exists():
|
|
72
|
+
if "/" in path_or_repo:
|
|
73
|
+
model_path = Path(snapshot_download(
|
|
74
|
+
repo_id=path_or_repo, repo_type="model", revision=revision))
|
|
75
|
+
else:
|
|
76
|
+
# Fallback to local modelfiles directory
|
|
77
|
+
model_path = Path(qwen3vl_dir) / "modelfiles"
|
|
78
|
+
if not model_path.exists():
|
|
79
|
+
model_path = Path(curr_dir) / "modelfiles"
|
|
80
|
+
|
|
81
|
+
# Model configs - Updated to match Qwen3VL-MoE specifications
|
|
82
|
+
vision_config = VisionConfig(
|
|
83
|
+
hidden_size=1152,
|
|
84
|
+
intermediate_size=4304,
|
|
85
|
+
num_heads=16,
|
|
86
|
+
num_hidden_layers=27,
|
|
87
|
+
patch_size=16,
|
|
88
|
+
temporal_patch_size=2,
|
|
89
|
+
in_channels=3,
|
|
90
|
+
hidden_act="gelu_pytorch_tanh",
|
|
91
|
+
spatial_merge_size=2,
|
|
92
|
+
out_hidden_size=2048,
|
|
93
|
+
num_position_embeddings=2304,
|
|
94
|
+
deepstack_visual_indexes=[8, 16, 24],
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
text_config = TextConfig(
|
|
98
|
+
model_type="qwen3_vl_moe_text",
|
|
99
|
+
hidden_size=2048,
|
|
100
|
+
num_hidden_layers=48,
|
|
101
|
+
intermediate_size=6144,
|
|
102
|
+
num_attention_heads=32,
|
|
103
|
+
num_key_value_heads=4,
|
|
104
|
+
rms_norm_eps=1e-6,
|
|
105
|
+
vocab_size=152064,
|
|
106
|
+
max_position_embeddings=128000,
|
|
107
|
+
rope_theta=1000000.0,
|
|
108
|
+
head_dim=128,
|
|
109
|
+
tie_word_embeddings=False,
|
|
110
|
+
attention_bias=False,
|
|
111
|
+
attention_dropout=0.0,
|
|
112
|
+
rope_scaling={
|
|
113
|
+
"mrope_interleaved": True,
|
|
114
|
+
"mrope_section": [24, 20, 20],
|
|
115
|
+
"rope_type": "default"
|
|
116
|
+
},
|
|
117
|
+
# MoE specific parameters
|
|
118
|
+
num_experts=128,
|
|
119
|
+
num_experts_per_tok=8,
|
|
120
|
+
moe_intermediate_size=768,
|
|
121
|
+
shared_expert_intermediate_size=0,
|
|
122
|
+
norm_topk_prob=True,
|
|
123
|
+
decoder_sparse_step=1,
|
|
124
|
+
max_window_layers=48,
|
|
125
|
+
sliding_window=32768,
|
|
126
|
+
mlp_only_layers=[],
|
|
127
|
+
use_qk_norm=True,
|
|
128
|
+
layer_types=[],
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
vision_model = VEGModel(vision_config)
|
|
132
|
+
llm_model = LLMModel(text_config)
|
|
133
|
+
|
|
134
|
+
# Try to load LLM model from available files in order of preference
|
|
135
|
+
preferred_order = [
|
|
136
|
+
("qwen3vl-moe-llm-30B-A3B-q4_0.safetensors", 4),
|
|
137
|
+
("qwen3vl-moe-llm-30B-A3B-q8_0.safetensors", 8),
|
|
138
|
+
("qwen3vl-moe-llm-30B-A3B-f32.safetensors", 32),
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
llm_weights_path = None
|
|
142
|
+
quantization_bits = None
|
|
143
|
+
|
|
144
|
+
# Try loading in order of preference
|
|
145
|
+
for filename, bits in preferred_order:
|
|
146
|
+
candidate_path = model_path / filename
|
|
147
|
+
if candidate_path.exists():
|
|
148
|
+
llm_weights_path = candidate_path
|
|
149
|
+
quantization_bits = bits
|
|
150
|
+
break
|
|
151
|
+
|
|
152
|
+
if llm_weights_path is None:
|
|
153
|
+
# Fallback to original hardcoded path for backward compatibility
|
|
154
|
+
llm_weights_path = model_path / "qwen3vl-moe-llm-30B-A3B-q4_0.safetensors"
|
|
155
|
+
quantization_bits = 4
|
|
156
|
+
|
|
157
|
+
vision_weights_path = model_path / "qwen3vl-moe-vision-30B-A3B-f16.safetensors"
|
|
158
|
+
|
|
159
|
+
if not vision_weights_path.exists():
|
|
160
|
+
raise FileNotFoundError(
|
|
161
|
+
f"Missing vision weights: {vision_weights_path}"
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
# Load weights (vision fp16, llm with detected quantization)
|
|
165
|
+
vision_model.set_dtype(mx.float16)
|
|
166
|
+
vision_model.load_weights(str(vision_weights_path), strict=True)
|
|
167
|
+
|
|
168
|
+
# Apply quantization if needed and load LLM weights
|
|
169
|
+
if quantization_bits in [4, 8]:
|
|
170
|
+
nn.quantize(llm_model, bits=quantization_bits, group_size=64,
|
|
171
|
+
class_predicate=quant_predicate)
|
|
172
|
+
# For f32 (32-bit), no quantization needed
|
|
173
|
+
|
|
174
|
+
llm_model.load_weights(str(llm_weights_path), strict=True)
|
|
175
|
+
|
|
176
|
+
# Tokenizer and processor
|
|
177
|
+
tokenizer = AutoTokenizer.from_pretrained(path_or_repo)
|
|
178
|
+
processor = Qwen3VLProcessor(tokenizer=tokenizer)
|
|
179
|
+
|
|
180
|
+
return Qwen3VLBundledModel(vision_model=vision_model, llm_model=llm_model), processor
|
|
181
|
+
|
|
182
|
+
def apply_chat_template_qwen3_vl(messages: Sequence[ChatMessage], num_images: int = 0, num_audios: int = 0, tools: Optional[str] = None, enable_thinking: bool = False) -> str:
|
|
183
|
+
"""Apply chat template: serialize messages with content as a list of typed items."""
|
|
184
|
+
messages_dict = []
|
|
185
|
+
for msg in messages:
|
|
186
|
+
content_items = [{"type": "text", "text": msg.content}]
|
|
187
|
+
messages_dict.append({"role": msg.role, "content": content_items})
|
|
188
|
+
return json.dumps(messages_dict)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def stream_generate_qwen3_vl(
|
|
192
|
+
model: Qwen3VLBundledModel,
|
|
193
|
+
processor: Qwen3VLProcessor,
|
|
194
|
+
prompt: str,
|
|
195
|
+
image: Union[str, List[str]] = None,
|
|
196
|
+
audio: Union[str, List[str]] = None,
|
|
197
|
+
max_tokens: int = 512,
|
|
198
|
+
**kwargs,
|
|
199
|
+
|
|
200
|
+
) -> Generator[Any, None, None]:
|
|
201
|
+
"""Stream generation yielding .generate.GenerationResult-compatible chunks."""
|
|
202
|
+
messages = json.loads(prompt)
|
|
203
|
+
if image is not None:
|
|
204
|
+
image_list = image if isinstance(image, list) else [image]
|
|
205
|
+
pil_images = []
|
|
206
|
+
for p in image_list:
|
|
207
|
+
try:
|
|
208
|
+
pil_images.append(Image.open(p))
|
|
209
|
+
except Exception:
|
|
210
|
+
continue
|
|
211
|
+
contents = [{"type": "image", "image": img} for img in pil_images]
|
|
212
|
+
if messages:
|
|
213
|
+
if "content" not in messages[-1] or not isinstance(messages[-1]["content"], list):
|
|
214
|
+
messages[-1]["content"] = []
|
|
215
|
+
messages[-1]["content"].extend(contents)
|
|
216
|
+
|
|
217
|
+
raw_text, processed_images = processor.messages_to_text(
|
|
218
|
+
messages, add_generation_prompt=True)
|
|
219
|
+
|
|
220
|
+
inputs = processor.text_to_input_ids(
|
|
221
|
+
raw_text, images=processed_images, return_tensors="mlx")
|
|
222
|
+
|
|
223
|
+
input_ids = inputs["input_ids"]
|
|
224
|
+
pixel_values = inputs.get("pixel_values")
|
|
225
|
+
image_grid_thw = inputs.get("image_grid_thw")
|
|
226
|
+
|
|
227
|
+
inputs_embeds, deepstack_visual_embeds, visual_pos_masks, cos, sin, rope_deltas = handle_multimodal_embeds(
|
|
228
|
+
model.vision_model, model.llm_model, input_ids, pixel_values, image_grid_thw
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
prompt_cache = make_prompt_cache(model.llm_model, max_kv_size=4096)
|
|
232
|
+
tokenizer = processor.tokenizer
|
|
233
|
+
|
|
234
|
+
# Rough prompt TPS estimation based on input size
|
|
235
|
+
prompt_start = time.perf_counter()
|
|
236
|
+
prompt_tps = input_ids.size / max(1e-6, (time.perf_counter() - prompt_start))
|
|
237
|
+
|
|
238
|
+
gen_count = 0
|
|
239
|
+
tic = time.perf_counter()
|
|
240
|
+
|
|
241
|
+
for token, logprobs in nexa_generate_step(
|
|
242
|
+
model=model.llm_model,
|
|
243
|
+
prompt=None,
|
|
244
|
+
input_embeddings=inputs_embeds,
|
|
245
|
+
max_tokens=max_tokens,
|
|
246
|
+
max_kv_size=4096,
|
|
247
|
+
prompt_cache=prompt_cache,
|
|
248
|
+
visual_pos_masks=visual_pos_masks,
|
|
249
|
+
deepstack_visual_embeds=deepstack_visual_embeds,
|
|
250
|
+
cos=cos,
|
|
251
|
+
sin=sin,
|
|
252
|
+
rope_deltas=rope_deltas,
|
|
253
|
+
):
|
|
254
|
+
if token == tokenizer.eos_token_id:
|
|
255
|
+
break
|
|
256
|
+
|
|
257
|
+
text_piece = tokenizer.decode([token])
|
|
258
|
+
gen_count += 1
|
|
259
|
+
|
|
260
|
+
yield GenerationResult(
|
|
261
|
+
text=text_piece,
|
|
262
|
+
token=token,
|
|
263
|
+
logprobs=logprobs,
|
|
264
|
+
prompt_tokens=int(input_ids.size),
|
|
265
|
+
generation_tokens=gen_count,
|
|
266
|
+
prompt_tps=float(prompt_tps),
|
|
267
|
+
generation_tps=float(
|
|
268
|
+
gen_count / max(1e-6, (time.perf_counter() - tic))),
|
|
269
|
+
peak_memory=float(mx.get_peak_memory() / 1e9),
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def quant_predicate(path: str, mod: nn.Module) -> bool:
|
|
273
|
+
"""Quantization predicate to exclude certain layers from quantization."""
|
|
274
|
+
if path.endswith("lm_head") or "norm" in path.lower() or "embed" in path.lower():
|
|
275
|
+
return False
|
|
276
|
+
return isinstance(mod, (nn.Linear, nn.Embedding))
|