nexaai 1.0.29__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.
- nexaai/__init__.py +99 -0
- nexaai/_stub.cpython-310-darwin.so +0 -0
- nexaai/_version.py +4 -0
- nexaai/asr.py +68 -0
- nexaai/asr_impl/__init__.py +0 -0
- nexaai/asr_impl/mlx_asr_impl.py +93 -0
- nexaai/asr_impl/pybind_asr_impl.py +127 -0
- nexaai/base.py +39 -0
- nexaai/binds/__init__.py +7 -0
- nexaai/binds/asr_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/common_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/cpu_gpu/libggml-base.dylib +0 -0
- nexaai/binds/cpu_gpu/libggml-cpu.so +0 -0
- nexaai/binds/cpu_gpu/libggml-metal.so +0 -0
- nexaai/binds/cpu_gpu/libggml.dylib +0 -0
- nexaai/binds/cpu_gpu/libmtmd.dylib +0 -0
- nexaai/binds/cpu_gpu/libnexa_cpu_gpu.dylib +0 -0
- nexaai/binds/cpu_gpu/libnexa_plugin.dylib +0 -0
- nexaai/binds/cv_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/diarize_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/embedder_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/libnexa_bridge.dylib +0 -0
- nexaai/binds/llm_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/metal/libnexa_plugin.dylib +0 -0
- nexaai/binds/metal/py-lib/ml.py +888 -0
- nexaai/binds/metal/py-lib/mlx_audio/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/__init__.py +5 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/activation.py +51 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/amp.py +96 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/bigvgan.py +149 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/conv.py +114 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/bigvgan/resample.py +177 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/base.py +228 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/dac.py +285 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/nn/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/nn/layers.py +129 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/descript/nn/quantize.py +149 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/encodec/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/encodec/encodec.py +777 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/mimi.py +286 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/__init__.py +20 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/conv.py +398 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/kv_cache.py +199 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/quantization.py +179 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/seanet.py +314 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/mimi/modules/transformer.py +256 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/s3/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/s3/model.py +260 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/s3/model_v2.py +383 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/s3/utils.py +122 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/snac/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/snac/attention.py +97 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/snac/layers.py +306 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/snac/snac.py +154 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/snac/vq.py +135 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/vocos/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/vocos/mel.py +33 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/models/vocos/vocos.py +359 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_bigvgan.py +54 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_descript.py +109 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_encodec.py +58 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_mimi.py +22 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_s3.py +25 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_snac.py +40 -0
- nexaai/binds/metal/py-lib/mlx_audio/codec/tests/test_vocos.py +93 -0
- nexaai/binds/metal/py-lib/mlx_audio/server.py +525 -0
- nexaai/binds/metal/py-lib/mlx_audio/sts/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/sts/tests/test_voice_pipeline.py +156 -0
- nexaai/binds/metal/py-lib/mlx_audio/sts/voice_pipeline.py +327 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/generate.py +174 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/alignment.py +248 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/attention.py +187 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/audio.py +76 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/conformer.py +331 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/ctc.py +34 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/parakeet.py +604 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/rnnt.py +157 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/parakeet/tokenizer.py +2 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/wav2vec/feature_extractor.py +757 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/wav2vec/wav2vec.py +738 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/audio.py +82 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/decoding.py +742 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/timing.py +329 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/tokenizer.py +398 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/whisper.py +862 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/models/whisper/writers.py +268 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/tests/test_models.py +381 -0
- nexaai/binds/metal/py-lib/mlx_audio/stt/utils.py +195 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/audio_player.py +120 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/convert.py +71 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/generate.py +449 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/bark/__init__.py +4 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/bark/bark.py +528 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/bark/isftnet.py +12 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/bark/pipeline.py +442 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/base.py +84 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/dia/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/dia/audio.py +287 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/dia/config.py +256 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/dia/dia.py +592 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/dia/layers.py +870 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/__init__.py +3 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/attention.py +180 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/bigvgan.py +124 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/conformer.py +247 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/ecapa_tdnn/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/ecapa_tdnn/asp.py +59 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/ecapa_tdnn/ecapa_tdnn.py +91 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/ecapa_tdnn/se_res2net.py +132 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/ecapa_tdnn/tdnn.py +42 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/gpt2.py +38 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/indextts.py +412 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/mel.py +37 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/normalize.py +294 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/indextts/perceiver.py +62 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/interpolate.py +108 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/__init__.py +4 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/istftnet.py +979 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/kokoro.py +331 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/modules.py +659 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/pipeline.py +453 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/kokoro/voice.py +113 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/llama/__init__.py +3 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/llama/llama.py +324 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/audio_processor.py +351 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/dac_interface.py +162 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/outetts.py +255 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/prompt_processor.py +181 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/outetts/tokens.py +36 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/sesame/__init__.py +3 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/sesame/attention.py +195 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/sesame/sesame.py +633 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/sesame/watermarking.py +105 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/audio_tokenizer.py +138 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/bicodec.py +269 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/blocks/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/blocks/sampler.py +111 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/encoder_decoder/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/encoder_decoder/feat_decoder.py +120 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/encoder_decoder/feat_encoder.py +136 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/encoder_decoder/wave_generator.py +113 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/finite_scalar_quantization.py +238 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/residual.py +209 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/residual_fsq.py +309 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/speaker/__init__.py +1 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/speaker/ecapa_tdnn.py +283 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/speaker/perceiver_encoder.py +326 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/speaker/pooling_layers.py +297 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/modules/speaker/speaker_encoder.py +155 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/spark.py +382 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/utils/audio.py +220 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/utils/file.py +221 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/models/spark/utils/token_parser.py +181 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/tests/__init__.py +0 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/tests/test_base.py +66 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/tests/test_convert.py +173 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/tests/test_interpolate.py +88 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/tests/test_models.py +974 -0
- nexaai/binds/metal/py-lib/mlx_audio/tts/utils.py +337 -0
- nexaai/binds/metal/py-lib/mlx_audio/utils.py +237 -0
- nexaai/binds/metal/py-lib/mlx_audio/version.py +1 -0
- nexaai/binds/metal/py-lib/profiling.py +239 -0
- nexaai/binds/nexaml/libfftw3.3.dylib +0 -0
- nexaai/binds/nexaml/libfftw3f.3.dylib +0 -0
- nexaai/binds/nexaml/libggml-base.dylib +0 -0
- nexaai/binds/nexaml/libggml-cpu.so +0 -0
- nexaai/binds/nexaml/libggml-metal.so +0 -0
- nexaai/binds/nexaml/libggml.dylib +0 -0
- nexaai/binds/nexaml/libmp3lame.0.dylib +0 -0
- nexaai/binds/nexaml/libmpg123.0.dylib +0 -0
- nexaai/binds/nexaml/libnexa-mm-process.dylib +0 -0
- nexaai/binds/nexaml/libnexa-sampling.dylib +0 -0
- nexaai/binds/nexaml/libnexa_plugin.dylib +0 -0
- nexaai/binds/nexaml/libnexaproc.dylib +0 -0
- nexaai/binds/nexaml/libomp.dylib +0 -0
- nexaai/binds/nexaml/libqwen3-vl.dylib +0 -0
- nexaai/binds/nexaml/libqwen3vl-vision.dylib +0 -0
- nexaai/binds/rerank_bind.cpython-310-darwin.so +0 -0
- nexaai/binds/vlm_bind.cpython-310-darwin.so +0 -0
- nexaai/common.py +106 -0
- nexaai/cv.py +95 -0
- nexaai/cv_impl/__init__.py +0 -0
- nexaai/cv_impl/mlx_cv_impl.py +91 -0
- nexaai/cv_impl/pybind_cv_impl.py +124 -0
- nexaai/diarize.py +80 -0
- nexaai/diarize_impl/__init__.py +1 -0
- nexaai/diarize_impl/pybind_diarize_impl.py +125 -0
- nexaai/embedder.py +73 -0
- nexaai/embedder_impl/__init__.py +0 -0
- nexaai/embedder_impl/mlx_embedder_impl.py +118 -0
- nexaai/embedder_impl/pybind_embedder_impl.py +96 -0
- nexaai/image_gen.py +141 -0
- nexaai/image_gen_impl/__init__.py +0 -0
- nexaai/image_gen_impl/mlx_image_gen_impl.py +292 -0
- nexaai/image_gen_impl/pybind_image_gen_impl.py +85 -0
- nexaai/llm.py +98 -0
- nexaai/llm_impl/__init__.py +0 -0
- nexaai/llm_impl/mlx_llm_impl.py +271 -0
- nexaai/llm_impl/pybind_llm_impl.py +238 -0
- nexaai/log.py +92 -0
- nexaai/mlx_backend/asr/__init__.py +12 -0
- nexaai/mlx_backend/asr/interface.py +122 -0
- nexaai/mlx_backend/common/__init__.py +0 -0
- nexaai/mlx_backend/common/utils.py +25 -0
- nexaai/mlx_backend/cv/__init__.py +0 -0
- nexaai/mlx_backend/cv/generate.py +195 -0
- nexaai/mlx_backend/cv/interface.py +162 -0
- nexaai/mlx_backend/cv/main.py +81 -0
- nexaai/mlx_backend/cv/modeling/pp_ocr_v4.py +1736 -0
- nexaai/mlx_backend/embedding/__init__.py +0 -0
- nexaai/mlx_backend/embedding/generate.py +333 -0
- nexaai/mlx_backend/embedding/interface.py +617 -0
- nexaai/mlx_backend/embedding/main.py +173 -0
- nexaai/mlx_backend/embedding/modeling/__init__.py +0 -0
- nexaai/mlx_backend/embedding/modeling/nexa_jina_v2.py +399 -0
- nexaai/mlx_backend/image_gen/__init__.py +1 -0
- nexaai/mlx_backend/image_gen/generate_sd.py +244 -0
- nexaai/mlx_backend/image_gen/interface.py +82 -0
- nexaai/mlx_backend/image_gen/main.py +281 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/__init__.py +306 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/clip.py +116 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/config.py +65 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/model_io.py +386 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/sampler.py +105 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/tokenizer.py +100 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/unet.py +460 -0
- nexaai/mlx_backend/image_gen/stable_diffusion/vae.py +274 -0
- nexaai/mlx_backend/llm/__init__.py +0 -0
- nexaai/mlx_backend/llm/generate.py +149 -0
- nexaai/mlx_backend/llm/interface.py +764 -0
- nexaai/mlx_backend/llm/main.py +68 -0
- nexaai/mlx_backend/ml.py +888 -0
- nexaai/mlx_backend/mlx_audio/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/codec/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/__init__.py +5 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/activation.py +51 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/amp.py +96 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/bigvgan.py +149 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/conv.py +114 -0
- nexaai/mlx_backend/mlx_audio/codec/models/bigvgan/resample.py +177 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/base.py +228 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/dac.py +285 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/nn/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/nn/layers.py +129 -0
- nexaai/mlx_backend/mlx_audio/codec/models/descript/nn/quantize.py +149 -0
- nexaai/mlx_backend/mlx_audio/codec/models/encodec/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/encodec/encodec.py +777 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/mimi.py +286 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/__init__.py +20 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/conv.py +398 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/kv_cache.py +199 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/quantization.py +179 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/seanet.py +314 -0
- nexaai/mlx_backend/mlx_audio/codec/models/mimi/modules/transformer.py +256 -0
- nexaai/mlx_backend/mlx_audio/codec/models/s3/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/s3/model.py +260 -0
- nexaai/mlx_backend/mlx_audio/codec/models/s3/model_v2.py +383 -0
- nexaai/mlx_backend/mlx_audio/codec/models/s3/utils.py +122 -0
- nexaai/mlx_backend/mlx_audio/codec/models/snac/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/snac/attention.py +97 -0
- nexaai/mlx_backend/mlx_audio/codec/models/snac/layers.py +306 -0
- nexaai/mlx_backend/mlx_audio/codec/models/snac/snac.py +154 -0
- nexaai/mlx_backend/mlx_audio/codec/models/snac/vq.py +135 -0
- nexaai/mlx_backend/mlx_audio/codec/models/vocos/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/codec/models/vocos/mel.py +33 -0
- nexaai/mlx_backend/mlx_audio/codec/models/vocos/vocos.py +359 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_bigvgan.py +54 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_descript.py +109 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_encodec.py +58 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_mimi.py +22 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_s3.py +25 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_snac.py +40 -0
- nexaai/mlx_backend/mlx_audio/codec/tests/test_vocos.py +93 -0
- nexaai/mlx_backend/mlx_audio/server.py +525 -0
- nexaai/mlx_backend/mlx_audio/sts/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/sts/tests/test_voice_pipeline.py +156 -0
- nexaai/mlx_backend/mlx_audio/sts/voice_pipeline.py +327 -0
- nexaai/mlx_backend/mlx_audio/stt/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/stt/generate.py +174 -0
- nexaai/mlx_backend/mlx_audio/stt/models/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/alignment.py +248 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/attention.py +187 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/audio.py +76 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/conformer.py +331 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/ctc.py +34 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/parakeet.py +604 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/rnnt.py +157 -0
- nexaai/mlx_backend/mlx_audio/stt/models/parakeet/tokenizer.py +2 -0
- nexaai/mlx_backend/mlx_audio/stt/models/wav2vec/feature_extractor.py +757 -0
- nexaai/mlx_backend/mlx_audio/stt/models/wav2vec/wav2vec.py +738 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/audio.py +82 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/decoding.py +742 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/timing.py +329 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/tokenizer.py +398 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/whisper.py +862 -0
- nexaai/mlx_backend/mlx_audio/stt/models/whisper/writers.py +268 -0
- nexaai/mlx_backend/mlx_audio/stt/tests/test_models.py +381 -0
- nexaai/mlx_backend/mlx_audio/stt/utils.py +195 -0
- nexaai/mlx_backend/mlx_audio/tts/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/tts/audio_player.py +120 -0
- nexaai/mlx_backend/mlx_audio/tts/convert.py +71 -0
- nexaai/mlx_backend/mlx_audio/tts/generate.py +449 -0
- nexaai/mlx_backend/mlx_audio/tts/models/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/models/bark/__init__.py +4 -0
- nexaai/mlx_backend/mlx_audio/tts/models/bark/bark.py +528 -0
- nexaai/mlx_backend/mlx_audio/tts/models/bark/isftnet.py +12 -0
- nexaai/mlx_backend/mlx_audio/tts/models/bark/pipeline.py +442 -0
- nexaai/mlx_backend/mlx_audio/tts/models/base.py +84 -0
- nexaai/mlx_backend/mlx_audio/tts/models/dia/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/tts/models/dia/audio.py +287 -0
- nexaai/mlx_backend/mlx_audio/tts/models/dia/config.py +256 -0
- nexaai/mlx_backend/mlx_audio/tts/models/dia/dia.py +592 -0
- nexaai/mlx_backend/mlx_audio/tts/models/dia/layers.py +870 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/__init__.py +3 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/attention.py +180 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/bigvgan.py +124 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/conformer.py +247 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/ecapa_tdnn/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/ecapa_tdnn/asp.py +59 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/ecapa_tdnn/ecapa_tdnn.py +91 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/ecapa_tdnn/se_res2net.py +132 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/ecapa_tdnn/tdnn.py +42 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/gpt2.py +38 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/indextts.py +412 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/mel.py +37 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/normalize.py +294 -0
- nexaai/mlx_backend/mlx_audio/tts/models/indextts/perceiver.py +62 -0
- nexaai/mlx_backend/mlx_audio/tts/models/interpolate.py +108 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/__init__.py +4 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/istftnet.py +979 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/kokoro.py +331 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/modules.py +659 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/pipeline.py +453 -0
- nexaai/mlx_backend/mlx_audio/tts/models/kokoro/voice.py +113 -0
- nexaai/mlx_backend/mlx_audio/tts/models/llama/__init__.py +3 -0
- nexaai/mlx_backend/mlx_audio/tts/models/llama/llama.py +324 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/audio_processor.py +351 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/dac_interface.py +162 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/default_speaker.json +461 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/outetts.py +255 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/prompt_processor.py +181 -0
- nexaai/mlx_backend/mlx_audio/tts/models/outetts/tokens.py +36 -0
- nexaai/mlx_backend/mlx_audio/tts/models/sesame/__init__.py +3 -0
- nexaai/mlx_backend/mlx_audio/tts/models/sesame/attention.py +195 -0
- nexaai/mlx_backend/mlx_audio/tts/models/sesame/sesame.py +633 -0
- nexaai/mlx_backend/mlx_audio/tts/models/sesame/watermarking.py +105 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/audio_tokenizer.py +138 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/bicodec.py +269 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/blocks/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/blocks/sampler.py +111 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/encoder_decoder/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/encoder_decoder/feat_decoder.py +120 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/encoder_decoder/feat_encoder.py +136 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/encoder_decoder/wave_generator.py +113 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/finite_scalar_quantization.py +238 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/residual.py +209 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/residual_fsq.py +309 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/speaker/__init__.py +1 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/speaker/ecapa_tdnn.py +283 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/speaker/perceiver_encoder.py +326 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/speaker/pooling_layers.py +297 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/modules/speaker/speaker_encoder.py +155 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/spark.py +382 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/utils/audio.py +220 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/utils/file.py +221 -0
- nexaai/mlx_backend/mlx_audio/tts/models/spark/utils/token_parser.py +181 -0
- nexaai/mlx_backend/mlx_audio/tts/tests/__init__.py +0 -0
- nexaai/mlx_backend/mlx_audio/tts/tests/test_base.py +66 -0
- nexaai/mlx_backend/mlx_audio/tts/tests/test_convert.py +173 -0
- nexaai/mlx_backend/mlx_audio/tts/tests/test_interpolate.py +88 -0
- nexaai/mlx_backend/mlx_audio/tts/tests/test_models.py +974 -0
- nexaai/mlx_backend/mlx_audio/tts/utils.py +337 -0
- nexaai/mlx_backend/mlx_audio/utils.py +237 -0
- nexaai/mlx_backend/mlx_audio/version.py +1 -0
- nexaai/mlx_backend/profiling.py +239 -0
- nexaai/mlx_backend/rerank/__init__.py +0 -0
- nexaai/mlx_backend/rerank/generate.py +174 -0
- nexaai/mlx_backend/rerank/interface.py +287 -0
- nexaai/mlx_backend/rerank/main.py +127 -0
- nexaai/mlx_backend/rerank/modeling/__init__.py +0 -0
- nexaai/mlx_backend/rerank/modeling/nexa_jina_rerank.py +330 -0
- nexaai/mlx_backend/sd/__init__.py +1 -0
- nexaai/mlx_backend/sd/interface.py +362 -0
- nexaai/mlx_backend/sd/main.py +286 -0
- nexaai/mlx_backend/sd/modeling/__init__.py +306 -0
- nexaai/mlx_backend/sd/modeling/clip.py +116 -0
- nexaai/mlx_backend/sd/modeling/config.py +65 -0
- nexaai/mlx_backend/sd/modeling/model_io.py +385 -0
- nexaai/mlx_backend/sd/modeling/sampler.py +105 -0
- nexaai/mlx_backend/sd/modeling/tokenizer.py +100 -0
- nexaai/mlx_backend/sd/modeling/unet.py +460 -0
- nexaai/mlx_backend/sd/modeling/vae.py +274 -0
- nexaai/mlx_backend/tts/__init__.py +12 -0
- nexaai/mlx_backend/tts/interface.py +276 -0
- nexaai/mlx_backend/vlm/__init__.py +3 -0
- nexaai/mlx_backend/vlm/generate.py +572 -0
- nexaai/mlx_backend/vlm/generate_qwen3_vl.py +374 -0
- nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +259 -0
- nexaai/mlx_backend/vlm/interface.py +559 -0
- nexaai/mlx_backend/vlm/main.py +365 -0
- nexaai/mlx_backend/vlm/modeling/__init__.py +0 -0
- nexaai/mlx_backend/vlm/modeling/convert.py +68 -0
- nexaai/mlx_backend/vlm/modeling/models/__init__.py +0 -0
- nexaai/mlx_backend/vlm/modeling/models/aya_vision/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/aya_vision/aya_vision.py +193 -0
- nexaai/mlx_backend/vlm/modeling/models/aya_vision/interpolate.py +186 -0
- nexaai/mlx_backend/vlm/modeling/models/aya_vision/language.py +233 -0
- nexaai/mlx_backend/vlm/modeling/models/aya_vision/vision.py +503 -0
- nexaai/mlx_backend/vlm/modeling/models/base.py +202 -0
- nexaai/mlx_backend/vlm/modeling/models/cache.py +230 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/__init__.py +10 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/conversation.py +264 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +472 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/language.py +591 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +526 -0
- nexaai/mlx_backend/vlm/modeling/models/deepseek_vl_v2/vision.py +356 -0
- nexaai/mlx_backend/vlm/modeling/models/florence2/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/florence2/florence2.py +366 -0
- nexaai/mlx_backend/vlm/modeling/models/florence2/language.py +488 -0
- nexaai/mlx_backend/vlm/modeling/models/florence2/vision.py +591 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3/gemma3.py +213 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3/language.py +315 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3/vision.py +238 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/__init__.py +2 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/audio.py +1038 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/config.py +139 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/gemma3n.py +322 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/language.py +629 -0
- nexaai/mlx_backend/vlm/modeling/models/gemma3n/vision.py +1022 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics2/__init__.py +9 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics2/idefics2.py +294 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics2/language.py +191 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics2/vision.py +267 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics3/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics3/idefics3.py +175 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics3/language.py +192 -0
- nexaai/mlx_backend/vlm/modeling/models/idefics3/vision.py +233 -0
- nexaai/mlx_backend/vlm/modeling/models/internvl_chat/__init__.py +9 -0
- nexaai/mlx_backend/vlm/modeling/models/internvl_chat/internvl_chat.py +140 -0
- nexaai/mlx_backend/vlm/modeling/models/internvl_chat/language.py +220 -0
- nexaai/mlx_backend/vlm/modeling/models/internvl_chat/processor.py +393 -0
- nexaai/mlx_backend/vlm/modeling/models/internvl_chat/vision.py +293 -0
- nexaai/mlx_backend/vlm/modeling/models/kernels.py +307 -0
- nexaai/mlx_backend/vlm/modeling/models/kimi_vl/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/kimi_vl/kimi_vl.py +143 -0
- nexaai/mlx_backend/vlm/modeling/models/kimi_vl/language.py +509 -0
- nexaai/mlx_backend/vlm/modeling/models/kimi_vl/vision.py +522 -0
- nexaai/mlx_backend/vlm/modeling/models/llama4/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/llama4/language.py +386 -0
- nexaai/mlx_backend/vlm/modeling/models/llama4/llama4.py +138 -0
- nexaai/mlx_backend/vlm/modeling/models/llama4/vision.py +560 -0
- nexaai/mlx_backend/vlm/modeling/models/llava/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/llava/language.py +240 -0
- nexaai/mlx_backend/vlm/modeling/models/llava/llava.py +153 -0
- nexaai/mlx_backend/vlm/modeling/models/llava/vision.py +259 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_bunny/__init__.py +9 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_bunny/language.py +236 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_bunny/llava_bunny.py +256 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_bunny/vision.py +303 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_next/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_next/language.py +230 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_next/llava_next.py +160 -0
- nexaai/mlx_backend/vlm/modeling/models/llava_next/vision.py +243 -0
- nexaai/mlx_backend/vlm/modeling/models/mistral3/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/mistral3/mistral3.py +283 -0
- nexaai/mlx_backend/vlm/modeling/models/mllama/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/mllama/language.py +416 -0
- nexaai/mlx_backend/vlm/modeling/models/mllama/mllama.py +172 -0
- nexaai/mlx_backend/vlm/modeling/models/mllama/vision.py +499 -0
- nexaai/mlx_backend/vlm/modeling/models/molmo/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/molmo/language.py +243 -0
- nexaai/mlx_backend/vlm/modeling/models/molmo/molmo.py +133 -0
- nexaai/mlx_backend/vlm/modeling/models/molmo/vision.py +465 -0
- nexaai/mlx_backend/vlm/modeling/models/multi_modality/__init__.py +10 -0
- nexaai/mlx_backend/vlm/modeling/models/multi_modality/language.py +230 -0
- nexaai/mlx_backend/vlm/modeling/models/multi_modality/multi_modality.py +385 -0
- nexaai/mlx_backend/vlm/modeling/models/multi_modality/sam.py +557 -0
- nexaai/mlx_backend/vlm/modeling/models/multi_modality/vision.py +526 -0
- nexaai/mlx_backend/vlm/modeling/models/paligemma/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/paligemma/language.py +282 -0
- nexaai/mlx_backend/vlm/modeling/models/paligemma/paligemma.py +160 -0
- nexaai/mlx_backend/vlm/modeling/models/paligemma/vision.py +242 -0
- nexaai/mlx_backend/vlm/modeling/models/phi3_v/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/phi3_v/language.py +21 -0
- nexaai/mlx_backend/vlm/modeling/models/phi3_v/phi3_v.py +243 -0
- nexaai/mlx_backend/vlm/modeling/models/phi3_v/su_rope.py +71 -0
- nexaai/mlx_backend/vlm/modeling/models/phi3_v/vision.py +324 -0
- nexaai/mlx_backend/vlm/modeling/models/pixtral/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/pixtral/language.py +229 -0
- nexaai/mlx_backend/vlm/modeling/models/pixtral/pixtral.py +161 -0
- nexaai/mlx_backend/vlm/modeling/models/pixtral/vision.py +320 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_5_vl/__init__.py +2 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_5_vl/config.py +108 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_5_vl/language.py +490 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +168 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_5_vl/vision.py +414 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_vl/__init__.py +2 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_vl/config.py +104 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_vl/language.py +490 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_vl/qwen2_vl.py +167 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen2_vl/vision.py +312 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/base.py +117 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/cache.py +531 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/generate.py +701 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +255 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +303 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +407 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/processor.py +476 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/qwen3vl.py +1262 -0
- 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 +1308 -0
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/switch_layers.py +210 -0
- nexaai/mlx_backend/vlm/modeling/models/smolvlm/__init__.py +8 -0
- nexaai/mlx_backend/vlm/modeling/models/smolvlm/smolvlm.py +62 -0
- nexaai/mlx_backend/vlm/modeling/processing_qwen2_5_vl.py +209 -0
- nexaai/mlx_backend/vlm/modeling/processing_qwen2_vl.py +215 -0
- nexaai/mlx_backend/vlm/modeling/prompt_utils.py +474 -0
- nexaai/mlx_backend/vlm/modeling/sample_utils.py +39 -0
- nexaai/mlx_backend/vlm/modeling/tokenizer_utils.py +344 -0
- nexaai/mlx_backend/vlm/modeling/trainer/__init__.py +9 -0
- nexaai/mlx_backend/vlm/modeling/trainer/lora.py +70 -0
- nexaai/mlx_backend/vlm/modeling/trainer/trainer.py +296 -0
- nexaai/mlx_backend/vlm/modeling/trainer/utils.py +160 -0
- nexaai/mlx_backend/vlm/modeling/utils.py +928 -0
- nexaai/rerank.py +57 -0
- nexaai/rerank_impl/__init__.py +0 -0
- nexaai/rerank_impl/mlx_rerank_impl.py +94 -0
- nexaai/rerank_impl/pybind_rerank_impl.py +136 -0
- nexaai/runtime.py +68 -0
- nexaai/runtime_error.py +24 -0
- nexaai/tts.py +75 -0
- nexaai/tts_impl/__init__.py +0 -0
- nexaai/tts_impl/mlx_tts_impl.py +94 -0
- nexaai/tts_impl/pybind_tts_impl.py +43 -0
- nexaai/utils/decode.py +18 -0
- nexaai/utils/manifest_utils.py +531 -0
- nexaai/utils/model_manager.py +1745 -0
- nexaai/utils/model_types.py +49 -0
- nexaai/utils/progress_tracker.py +389 -0
- nexaai/utils/quantization_utils.py +245 -0
- nexaai/vlm.py +130 -0
- nexaai/vlm_impl/__init__.py +0 -0
- nexaai/vlm_impl/mlx_vlm_impl.py +259 -0
- nexaai/vlm_impl/pybind_vlm_impl.py +275 -0
- nexaai-1.0.29.dist-info/METADATA +35 -0
- nexaai-1.0.29.dist-info/RECORD +580 -0
- nexaai-1.0.29.dist-info/WHEEL +5 -0
- nexaai-1.0.29.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
|
|
5
|
+
import mlx.core as mx
|
|
6
|
+
import mlx.nn as nn
|
|
7
|
+
|
|
8
|
+
from ..kernels import bicubic_interpolate
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class VisionConfig:
|
|
13
|
+
model_type: str = "moonvit"
|
|
14
|
+
depth: int = 27
|
|
15
|
+
embed_dim: int = 1152
|
|
16
|
+
hidden_size: int = 1152
|
|
17
|
+
num_heads: int = 16
|
|
18
|
+
image_size: int = 384
|
|
19
|
+
patch_size: int = 14
|
|
20
|
+
vocab_size: int = 32000
|
|
21
|
+
mlp_ratio: float = 4.0
|
|
22
|
+
num_channels: int = 3
|
|
23
|
+
layer_norm_eps: float = 1e-6
|
|
24
|
+
intermediate_size: int = 4304
|
|
25
|
+
init_pos_emb_height: int = 64
|
|
26
|
+
init_pos_emb_width: int = 64
|
|
27
|
+
spatial_patch_size: int = 14
|
|
28
|
+
spatial_merge_size: int = 2
|
|
29
|
+
temporal_patch_size: int = 2
|
|
30
|
+
merge_kernel_size: list[int, int] = None
|
|
31
|
+
|
|
32
|
+
def __post_init__(self):
|
|
33
|
+
if self.merge_kernel_size is None:
|
|
34
|
+
self.merge_kernel_size = (self.spatial_merge_size, self.spatial_merge_size)
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def from_dict(cls, params):
|
|
38
|
+
return cls(
|
|
39
|
+
**{
|
|
40
|
+
k: v
|
|
41
|
+
for k, v in params.items()
|
|
42
|
+
if k in inspect.signature(cls).parameters
|
|
43
|
+
}
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def check_array_shape(arr):
|
|
48
|
+
shape = arr.shape
|
|
49
|
+
|
|
50
|
+
# Check if the shape has 4 dimensions
|
|
51
|
+
if len(shape) != 4:
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
out_channels, kH, KW, _ = shape
|
|
55
|
+
|
|
56
|
+
# Check if out_channels is the largest, and kH and KW are the same
|
|
57
|
+
if (out_channels >= kH) and (out_channels >= KW) and (kH == KW):
|
|
58
|
+
return True
|
|
59
|
+
else:
|
|
60
|
+
return False
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def rotate_half(x):
|
|
64
|
+
"""Rotates half the hidden dims of the input."""
|
|
65
|
+
x1 = x[..., : x.shape[-1] // 2]
|
|
66
|
+
x2 = x[..., x.shape[-1] // 2 :]
|
|
67
|
+
return mx.concatenate([-x2, x1], axis=-1)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def apply_rotary_pos_emb_vision(tensor, freqs) -> mx.array:
|
|
71
|
+
orig_dtype = tensor.dtype
|
|
72
|
+
|
|
73
|
+
cos = mx.cos(freqs)
|
|
74
|
+
sin = mx.sin(freqs)
|
|
75
|
+
|
|
76
|
+
cos = mx.expand_dims(cos, axis=1) # Equivalent to unsqueeze(1)
|
|
77
|
+
cos = mx.tile(cos, (1, 1, 2)) # Equivalent to repeat(1, 1, 2)
|
|
78
|
+
cos = mx.expand_dims(cos, axis=0) # Equivalent to [None, ...]
|
|
79
|
+
|
|
80
|
+
sin = mx.expand_dims(sin, axis=1) # Equivalent to unsqueeze(1)
|
|
81
|
+
sin = mx.tile(sin, (1, 1, 2)) # Equivalent to repeat(1, 1, 2)
|
|
82
|
+
sin = mx.expand_dims(sin, axis=0) # Equivalent to [None, ...]
|
|
83
|
+
|
|
84
|
+
output = (tensor * cos) + (rotate_half(tensor) * sin)
|
|
85
|
+
return output.astype(orig_dtype)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class VisionRotaryEmbedding(nn.Module):
|
|
89
|
+
def __init__(self, dim: int, theta: float = 10000.0) -> None:
|
|
90
|
+
super().__init__()
|
|
91
|
+
self.dim = dim
|
|
92
|
+
self.theta = theta
|
|
93
|
+
|
|
94
|
+
def __call__(self, seqlen: int) -> mx.array:
|
|
95
|
+
inv_freq = 1.0 / (
|
|
96
|
+
self.theta ** (mx.arange(0, self.dim, 2, dtype=mx.float32) / self.dim)
|
|
97
|
+
)
|
|
98
|
+
seq = mx.arange(seqlen.tolist(), dtype=inv_freq.dtype)
|
|
99
|
+
freqs = mx.outer(seq, inv_freq)
|
|
100
|
+
return freqs
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class Learnable2DInterpPosEmb(nn.Module):
|
|
104
|
+
def __init__(
|
|
105
|
+
self, height: int, width: int, dim: int, interpolation_mode: str = "bicubic"
|
|
106
|
+
) -> None:
|
|
107
|
+
super().__init__()
|
|
108
|
+
self.height = height
|
|
109
|
+
self.width = width
|
|
110
|
+
self.interpolation_mode = interpolation_mode
|
|
111
|
+
self.weight = mx.ones((height, width, dim))
|
|
112
|
+
|
|
113
|
+
def __call__(self, x: mx.array, grid_hws: mx.array) -> mx.array:
|
|
114
|
+
pos_embs = []
|
|
115
|
+
for shape in grid_hws.tolist():
|
|
116
|
+
if shape == self.weight.shape[:-1]:
|
|
117
|
+
pos_embs.append(self.weight.flatten(end_axis=1))
|
|
118
|
+
else:
|
|
119
|
+
result = (
|
|
120
|
+
bicubic_interpolate(
|
|
121
|
+
mx.expand_dims(self.weight.transpose(2, 0, 1), axis=0),
|
|
122
|
+
size=shape,
|
|
123
|
+
)
|
|
124
|
+
.squeeze(0)
|
|
125
|
+
.transpose(1, 2, 0)
|
|
126
|
+
.flatten(end_axis=1)
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
pos_embs.append(result)
|
|
130
|
+
|
|
131
|
+
out = x + mx.concatenate(pos_embs).astype(x.dtype)
|
|
132
|
+
return out
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class PatchEmbed(nn.Module):
|
|
136
|
+
def __init__(
|
|
137
|
+
self,
|
|
138
|
+
patch_size: int = 14,
|
|
139
|
+
num_channels: int = 3,
|
|
140
|
+
embed_dim: int = 1152,
|
|
141
|
+
init_pos_emb_height: int = 64,
|
|
142
|
+
) -> None:
|
|
143
|
+
super().__init__()
|
|
144
|
+
self.patch_size = patch_size
|
|
145
|
+
self.num_channels = num_channels
|
|
146
|
+
self.embed_dim = embed_dim
|
|
147
|
+
self.init_pos_emb_height = init_pos_emb_height
|
|
148
|
+
|
|
149
|
+
self.proj = nn.Conv2d(
|
|
150
|
+
num_channels,
|
|
151
|
+
embed_dim,
|
|
152
|
+
kernel_size=patch_size,
|
|
153
|
+
stride=patch_size,
|
|
154
|
+
bias=True,
|
|
155
|
+
)
|
|
156
|
+
self.pos_emb = Learnable2DInterpPosEmb(
|
|
157
|
+
height=init_pos_emb_height, width=init_pos_emb_height, dim=embed_dim
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
def __call__(self, hidden_states: mx.array, grid_thw: mx.array) -> mx.array:
|
|
161
|
+
hidden_states = self.proj(hidden_states).swapaxes(1, 3)
|
|
162
|
+
hidden_states = hidden_states.reshape(hidden_states.shape[0], -1)
|
|
163
|
+
hidden_states = self.pos_emb(hidden_states, grid_thw)
|
|
164
|
+
return hidden_states
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _apply_rope_input_validation(x, freqs_cis):
|
|
168
|
+
assert x.ndim == freqs_cis.ndim + 1, (x.shape, freqs_cis.shape)
|
|
169
|
+
assert x.shape[:-2] == freqs_cis.shape[:-1], (x.shape, freqs_cis.shape)
|
|
170
|
+
assert x.shape[-1] == 2 * freqs_cis.shape[-1], (x.shape, freqs_cis.shape)
|
|
171
|
+
assert freqs_cis.dtype == mx.complex64, freqs_cis.dtype
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def view_as_complex(x):
|
|
175
|
+
"""
|
|
176
|
+
Convert a tensor with shape (..., 2) to a complex tensor with shape (...).
|
|
177
|
+
"""
|
|
178
|
+
# Get real and imaginary parts
|
|
179
|
+
real, imag = x[..., 0], x[..., 1]
|
|
180
|
+
# Create complex tensor
|
|
181
|
+
return real + 1j * imag
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def view_as_real(x):
|
|
185
|
+
"""
|
|
186
|
+
Convert a complex tensor with shape (...) to a real tensor with shape (..., 2).
|
|
187
|
+
"""
|
|
188
|
+
# Get real and imaginary parts
|
|
189
|
+
real = mx.real(x)
|
|
190
|
+
imag = mx.imag(x)
|
|
191
|
+
# Combine into a tensor with last dimension 2
|
|
192
|
+
return mx.stack([real, imag], axis=-1)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def apply_rope(
|
|
196
|
+
q: mx.array, k: mx.array, freqs_cis: mx.array
|
|
197
|
+
) -> tuple[mx.array, mx.array]:
|
|
198
|
+
"""
|
|
199
|
+
Args: (The leading dimensions of all inputs should be the same)
|
|
200
|
+
q: query, array of shape (..., num_heads, head_dim)
|
|
201
|
+
k: key, array of shape (..., num_heads, head_dim)
|
|
202
|
+
freqs_cis: array of shape (..., head_dim/2), dtype=mx.complex64. It contains the precomputed cis(freqs) for each position in the 2D grid.
|
|
203
|
+
Returns:
|
|
204
|
+
xq_out, xk_out: arrays of shape (..., num_heads, head_dim)
|
|
205
|
+
"""
|
|
206
|
+
_apply_rope_input_validation(q, freqs_cis)
|
|
207
|
+
_apply_rope_input_validation(k, freqs_cis)
|
|
208
|
+
|
|
209
|
+
freqs_cis = mx.expand_dims(freqs_cis, axis=-2) # ..., 1, head_dim/2
|
|
210
|
+
# ..., num_heads, head_dim/2
|
|
211
|
+
q_ = view_as_complex(q.astype(mx.float32).reshape(*q.shape[:-1], -1, 2))
|
|
212
|
+
k_ = view_as_complex(k.astype(mx.float32).reshape(*k.shape[:-1], -1, 2))
|
|
213
|
+
q_out = view_as_real(q_ * freqs_cis).flatten(-2) # ..., num_heads, head_dim
|
|
214
|
+
k_out = view_as_real(k_ * freqs_cis).flatten(-2) # ..., num_heads, head_dim
|
|
215
|
+
return q_out.astype(q.dtype), k_out.astype(k.dtype)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
class Attention(nn.Module):
|
|
219
|
+
def __init__(self, dim: int, num_heads: int = 16) -> None:
|
|
220
|
+
super().__init__()
|
|
221
|
+
self.num_heads = num_heads
|
|
222
|
+
self.head_dim = head_dim = dim // num_heads
|
|
223
|
+
self.scale = head_dim**-0.5
|
|
224
|
+
self.wqkv = nn.Linear(dim, dim * 3, bias=True)
|
|
225
|
+
self.wo = nn.Linear(dim, dim, bias=True)
|
|
226
|
+
|
|
227
|
+
def __call__(
|
|
228
|
+
self, x: mx.array, cu_seqlens: mx.array, rotary_pos_emb: mx.array = None
|
|
229
|
+
) -> mx.array:
|
|
230
|
+
seq_length = x.shape[0]
|
|
231
|
+
qkv = self.wqkv(x)
|
|
232
|
+
|
|
233
|
+
qkv_shape = qkv.shape[:-1] + (
|
|
234
|
+
3,
|
|
235
|
+
self.num_heads,
|
|
236
|
+
self.head_dim,
|
|
237
|
+
)
|
|
238
|
+
# xqkv: (batch_size, seqlen, 3, nheads, headdim)
|
|
239
|
+
qkv = qkv.reshape(*qkv_shape)
|
|
240
|
+
|
|
241
|
+
q, k, v = mx.split(qkv, 3, axis=1)
|
|
242
|
+
q = q.squeeze(1)
|
|
243
|
+
k = k.squeeze(1)
|
|
244
|
+
v = v.squeeze(1)
|
|
245
|
+
|
|
246
|
+
q, k = apply_rope(q, k, rotary_pos_emb)
|
|
247
|
+
|
|
248
|
+
attention_mask = mx.zeros((1, seq_length, seq_length), dtype=x.dtype)
|
|
249
|
+
|
|
250
|
+
# Create attention mask for each sequence in the batch
|
|
251
|
+
for i in range(1, len(cu_seqlens)):
|
|
252
|
+
start = int(cu_seqlens[i - 1])
|
|
253
|
+
end = int(cu_seqlens[i])
|
|
254
|
+
attention_mask[..., start:end, start:end] = 1
|
|
255
|
+
|
|
256
|
+
q = q.transpose(1, 0, 2)
|
|
257
|
+
k = k.transpose(1, 0, 2)
|
|
258
|
+
v = v.transpose(1, 0, 2)
|
|
259
|
+
|
|
260
|
+
attn_weight = q @ k.swapaxes(-2, -1) / mx.sqrt(q.shape[-1])
|
|
261
|
+
attn_weight += attention_mask
|
|
262
|
+
attn_weight = mx.softmax(attn_weight, axis=-1).astype(q.dtype)
|
|
263
|
+
|
|
264
|
+
attn_output = attn_weight @ v
|
|
265
|
+
attn_output = attn_output.transpose(1, 0, 2)
|
|
266
|
+
attn_output = attn_output.reshape(seq_length, -1)
|
|
267
|
+
return self.wo(attn_output)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class MLP(nn.Module):
|
|
271
|
+
def __init__(self, dim, hidden_dim):
|
|
272
|
+
super().__init__()
|
|
273
|
+
self.activation_fn = nn.GELU()
|
|
274
|
+
self.fc0 = nn.Linear(dim, hidden_dim)
|
|
275
|
+
self.fc1 = nn.Linear(hidden_dim, dim)
|
|
276
|
+
|
|
277
|
+
def __call__(self, x: mx.array) -> mx.array:
|
|
278
|
+
x = self.activation_fn(self.fc0(x))
|
|
279
|
+
x = self.fc1(x)
|
|
280
|
+
return x
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
class Qwen2VLVisionBlock(nn.Module):
|
|
284
|
+
def __init__(self, config: VisionConfig) -> None:
|
|
285
|
+
super().__init__()
|
|
286
|
+
self.norm0 = nn.LayerNorm(config.embed_dim, eps=1e-6)
|
|
287
|
+
self.norm1 = nn.LayerNorm(config.embed_dim, eps=1e-6)
|
|
288
|
+
|
|
289
|
+
self.attn = Attention(dim=config.embed_dim, num_heads=config.num_heads)
|
|
290
|
+
self.mlp = MLP(dim=config.embed_dim, hidden_dim=config.intermediate_size)
|
|
291
|
+
|
|
292
|
+
def __call__(self, hidden_states, cu_seqlens, rotary_pos_emb) -> mx.array:
|
|
293
|
+
hidden_states = hidden_states + self.attn(
|
|
294
|
+
self.norm0(hidden_states),
|
|
295
|
+
cu_seqlens=cu_seqlens,
|
|
296
|
+
rotary_pos_emb=rotary_pos_emb,
|
|
297
|
+
)
|
|
298
|
+
hidden_states = hidden_states + self.mlp(self.norm1(hidden_states))
|
|
299
|
+
return hidden_states
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class Rope2DPosEmb(nn.Module):
|
|
303
|
+
"""2D rotary position embedding with multi-resolution support.
|
|
304
|
+
|
|
305
|
+
This class is intended to be used in the following way:
|
|
306
|
+
1. Before training, create an instance of Rope2DPosEmb. This instance will hold the precomputed cis.
|
|
307
|
+
2. Before each forward pass, call `get_freqs_cis_by_*` to get the `freqs_cis` tensor for this iteration.
|
|
308
|
+
3. During the forward pass, pass the `freqs_cis` tensor to each attention layer, and call `apply` just before each attention operation.
|
|
309
|
+
The rope is shared across all attention layers and all heads.
|
|
310
|
+
|
|
311
|
+
Refs:
|
|
312
|
+
- RoFormer: https://arxiv.org/abs/2104.09864
|
|
313
|
+
- VisionLLaMA: https://arxiv.org/abs/2403.00522
|
|
314
|
+
- https://github.com/Meituan-AutoML/VisionLLaMA/blob/main/dit/models.py
|
|
315
|
+
|
|
316
|
+
Args:
|
|
317
|
+
dim (int): usually the multi-head attention dimension, should be divisible by 4 (TODO: relax this constraint if needed)
|
|
318
|
+
max_height (int): the maximum height of the 2D grid
|
|
319
|
+
max_width (int): the maximum width of the 2D grid
|
|
320
|
+
theta_base (float): the base of the theta
|
|
321
|
+
"""
|
|
322
|
+
|
|
323
|
+
def __init__(self, dim: int, max_height: int, max_width: int, theta_base=10000):
|
|
324
|
+
super().__init__()
|
|
325
|
+
self.dim = dim
|
|
326
|
+
assert self.dim % 4 == 0, "dim must be divisible by 4"
|
|
327
|
+
self.max_height = max_height
|
|
328
|
+
self.max_width = max_width
|
|
329
|
+
self.theta_base = theta_base
|
|
330
|
+
|
|
331
|
+
self._freqs_cis = None
|
|
332
|
+
|
|
333
|
+
def extra_repr(self):
|
|
334
|
+
return f"dim={self.dim}, max_height={self.max_height}, max_width={self.max_width}, theta_base={self.theta_base}"
|
|
335
|
+
|
|
336
|
+
def _precompute_freqs_cis(self) -> mx.array:
|
|
337
|
+
"""Calculate the cis(freqs) for each position in the 2D grid.
|
|
338
|
+
|
|
339
|
+
Return: complex array of shape (max_height, max_width, dim//2) and value:
|
|
340
|
+
height axis: ret[h, w, 2*i] = cis(h * theta_base**(-4*i/dim))
|
|
341
|
+
weight axis: ret[h, w, 2*i+1] = cis(w * theta_base**(-4*i/dim)) with (i in [0, dim//4))
|
|
342
|
+
note: `cis` is a mathematical notation defined by cis x = cos x + i sin x,
|
|
343
|
+
"""
|
|
344
|
+
N = self.max_height * self.max_width
|
|
345
|
+
flat_pos = mx.arange(0, N, dtype=mx.float32)
|
|
346
|
+
x_pos = flat_pos % self.max_width
|
|
347
|
+
y_pos = flat_pos // self.max_width
|
|
348
|
+
dim_range = mx.arange(0, self.dim, 4)[: (self.dim // 4)].astype(
|
|
349
|
+
mx.float32
|
|
350
|
+
) # C/4
|
|
351
|
+
freqs = 1.0 / (self.theta_base ** (dim_range / self.dim))
|
|
352
|
+
x_freqs = mx.outer(x_pos, freqs) # N, C/4
|
|
353
|
+
y_freqs = mx.outer(y_pos, freqs) # N, C/4
|
|
354
|
+
|
|
355
|
+
# Create complex numbers using cos and sin
|
|
356
|
+
x_cos = mx.cos(x_freqs)
|
|
357
|
+
x_sin = mx.sin(x_freqs)
|
|
358
|
+
y_cos = mx.cos(y_freqs)
|
|
359
|
+
y_sin = mx.sin(y_freqs)
|
|
360
|
+
|
|
361
|
+
# Create complex numbers
|
|
362
|
+
x_cis = x_cos + 1j * x_sin # N, C/4
|
|
363
|
+
y_cis = y_cos + 1j * y_sin # N, C/4
|
|
364
|
+
|
|
365
|
+
# N, C/4, 2
|
|
366
|
+
freqs_cis = mx.stack([x_cis, y_cis], axis=-1)
|
|
367
|
+
|
|
368
|
+
# max_height, max_width, C/2
|
|
369
|
+
freqs_cis = freqs_cis.reshape(self.max_height, self.max_width, -1)
|
|
370
|
+
return freqs_cis
|
|
371
|
+
|
|
372
|
+
def get_freqs_cis(self, grid_hws: mx.array) -> mx.array:
|
|
373
|
+
"""
|
|
374
|
+
Args:
|
|
375
|
+
grid_hws (mx.array): grid height and width
|
|
376
|
+
|
|
377
|
+
Returns:
|
|
378
|
+
freqs_cis: array of shape (sum(t * height * width), dim//2)
|
|
379
|
+
"""
|
|
380
|
+
if self._freqs_cis is None:
|
|
381
|
+
self._freqs_cis = self._precompute_freqs_cis()
|
|
382
|
+
|
|
383
|
+
shapes = grid_hws.tolist()
|
|
384
|
+
assert all(
|
|
385
|
+
1 <= h <= self.max_height and 1 <= w <= self.max_width for h, w in shapes
|
|
386
|
+
), (
|
|
387
|
+
shapes,
|
|
388
|
+
self.max_height,
|
|
389
|
+
self.max_width,
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
freqs_cis_list = []
|
|
393
|
+
for h, w in shapes:
|
|
394
|
+
# Get the slice of precomputed frequencies for this shape
|
|
395
|
+
shape_freqs = self._freqs_cis[:h, :w]
|
|
396
|
+
# Reshape to flatten the spatial dimensions
|
|
397
|
+
shape_freqs = shape_freqs.reshape(-1, self.dim // 2)
|
|
398
|
+
freqs_cis_list.append(shape_freqs)
|
|
399
|
+
|
|
400
|
+
freqs_cis = mx.concatenate(freqs_cis_list, axis=0)
|
|
401
|
+
return freqs_cis
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def patch_merger(
|
|
405
|
+
x: mx.array,
|
|
406
|
+
grid_hws: mx.array,
|
|
407
|
+
merge_kernel_size: list[int, int] = (2, 2),
|
|
408
|
+
) -> List[mx.array]:
|
|
409
|
+
d_model = x.shape[-1]
|
|
410
|
+
|
|
411
|
+
outputs = []
|
|
412
|
+
pre_sum = 0
|
|
413
|
+
for x_shape in grid_hws.tolist():
|
|
414
|
+
height, width = x_shape[0], x_shape[1]
|
|
415
|
+
# Get the current sequence
|
|
416
|
+
seq = x[pre_sum : pre_sum + height * width]
|
|
417
|
+
# Reshape along self.merge_kernel_size and concat to the last dimension
|
|
418
|
+
kernel_height, kernel_width = merge_kernel_size
|
|
419
|
+
new_height, new_width = height // kernel_height, width // kernel_width
|
|
420
|
+
reshaped_seq = seq.reshape(
|
|
421
|
+
new_height, kernel_height, new_width, kernel_width, d_model
|
|
422
|
+
)
|
|
423
|
+
reshaped_seq = mx.transpose(reshaped_seq, (0, 2, 1, 3, 4))
|
|
424
|
+
padded_seq = reshaped_seq.reshape(
|
|
425
|
+
new_height * new_width, kernel_height * kernel_width, -1
|
|
426
|
+
)
|
|
427
|
+
outputs.append(padded_seq)
|
|
428
|
+
pre_sum += height * width
|
|
429
|
+
|
|
430
|
+
return outputs
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
class VisionModel(nn.Module):
|
|
434
|
+
|
|
435
|
+
def __init__(self, config: VisionConfig) -> None:
|
|
436
|
+
super().__init__()
|
|
437
|
+
self.config = config
|
|
438
|
+
self.model_type = config.model_type
|
|
439
|
+
if self.model_type not in ["qwen2_vl", "moonvit"]:
|
|
440
|
+
raise ValueError(f"Unsupported model type: {self.model_type}")
|
|
441
|
+
self.spatial_merge_size = config.spatial_merge_size
|
|
442
|
+
self.merge_kernel_size = config.merge_kernel_size
|
|
443
|
+
|
|
444
|
+
self.patch_embed = PatchEmbed(
|
|
445
|
+
patch_size=config.patch_size,
|
|
446
|
+
num_channels=config.num_channels,
|
|
447
|
+
embed_dim=config.embed_dim,
|
|
448
|
+
init_pos_emb_height=config.init_pos_emb_height,
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
head_dim = config.embed_dim // config.num_heads
|
|
452
|
+
self.rotary_pos_emb = VisionRotaryEmbedding(head_dim // 2)
|
|
453
|
+
|
|
454
|
+
self.blocks = [Qwen2VLVisionBlock(config) for _ in range(config.depth)]
|
|
455
|
+
self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=1e-6)
|
|
456
|
+
self.rope_pos_emb = Rope2DPosEmb(head_dim, 512, 512)
|
|
457
|
+
|
|
458
|
+
def __call__(
|
|
459
|
+
self,
|
|
460
|
+
hidden_states: mx.array,
|
|
461
|
+
grid_thw: mx.array,
|
|
462
|
+
output_hidden_states: Optional[bool] = None,
|
|
463
|
+
) -> mx.array:
|
|
464
|
+
|
|
465
|
+
hidden_states = self.patch_embed(hidden_states, grid_thw)
|
|
466
|
+
rotary_pos_emb = self.rope_pos_emb.get_freqs_cis(grid_thw)
|
|
467
|
+
|
|
468
|
+
# Assuming grid_thw has shape (batch_size, 3)
|
|
469
|
+
batch_size = grid_thw.shape[0]
|
|
470
|
+
|
|
471
|
+
# Calculate cu_seqlens for each item in the batch
|
|
472
|
+
lengths = mx.concatenate(
|
|
473
|
+
(
|
|
474
|
+
mx.zeros((1,), dtype=grid_thw.dtype),
|
|
475
|
+
grid_thw[:, 0] * grid_thw[:, 1],
|
|
476
|
+
)
|
|
477
|
+
)
|
|
478
|
+
cu_seqlens = mx.cumsum(lengths.astype(mx.int32), axis=0)
|
|
479
|
+
|
|
480
|
+
encoder_states = (hidden_states,) if output_hidden_states else None
|
|
481
|
+
|
|
482
|
+
for blk in self.blocks:
|
|
483
|
+
hidden_states = blk(
|
|
484
|
+
hidden_states, cu_seqlens=cu_seqlens, rotary_pos_emb=rotary_pos_emb
|
|
485
|
+
)
|
|
486
|
+
if output_hidden_states:
|
|
487
|
+
encoder_states = encoder_states + (hidden_states,)
|
|
488
|
+
|
|
489
|
+
hidden_states = self.final_layernorm(hidden_states)
|
|
490
|
+
|
|
491
|
+
hidden_states = patch_merger(
|
|
492
|
+
hidden_states, grid_thw, merge_kernel_size=self.merge_kernel_size
|
|
493
|
+
)
|
|
494
|
+
|
|
495
|
+
return hidden_states
|
|
496
|
+
|
|
497
|
+
def sanitize(self, weights):
|
|
498
|
+
sanitized_weights = {}
|
|
499
|
+
for k, v in weights.items():
|
|
500
|
+
if "position_ids" in k:
|
|
501
|
+
# Remove unused position_ids
|
|
502
|
+
continue
|
|
503
|
+
elif "patch_embed.proj.weight" in k:
|
|
504
|
+
# PyTorch conv2d weight tensors have shape:
|
|
505
|
+
# [out_channels, in_channels, kH, KW]
|
|
506
|
+
# MLX conv2d expects the weight be of shape:
|
|
507
|
+
# [out_channels, kH, KW, in_channels]
|
|
508
|
+
if check_array_shape(v):
|
|
509
|
+
sanitized_weights[k] = v
|
|
510
|
+
else:
|
|
511
|
+
sanitized_weights[k] = v.transpose(0, 2, 3, 1)
|
|
512
|
+
|
|
513
|
+
elif "vision_tower.blocks" in k:
|
|
514
|
+
if "attn" not in k and ("wqkv" in k or "wo" in k):
|
|
515
|
+
new_key = k.replace("wqkv", "attn.wqkv").replace("wo", "attn.wo")
|
|
516
|
+
sanitized_weights[new_key] = v
|
|
517
|
+
else:
|
|
518
|
+
sanitized_weights[k] = v
|
|
519
|
+
else:
|
|
520
|
+
sanitized_weights[k] = v
|
|
521
|
+
|
|
522
|
+
return sanitized_weights
|