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,591 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import math
|
|
3
|
+
from collections import OrderedDict
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import List, Optional, Tuple, Union
|
|
6
|
+
|
|
7
|
+
import mlx.core as mx
|
|
8
|
+
import mlx.nn as nn
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class VisionConfig:
|
|
13
|
+
"""Configuration class for Florence2 Vision model (DaViT)."""
|
|
14
|
+
|
|
15
|
+
model_type: str = "davit"
|
|
16
|
+
in_chans: int = 3
|
|
17
|
+
num_classes: int = 1000
|
|
18
|
+
depths: List[int] = field(default_factory=lambda: [1, 1, 9, 1])
|
|
19
|
+
dim_embed: List[int] = field(default_factory=lambda: [128, 256, 512, 1024])
|
|
20
|
+
num_heads: List[int] = field(default_factory=lambda: [4, 8, 16, 32])
|
|
21
|
+
num_groups: List[int] = field(default_factory=lambda: [4, 8, 16, 32])
|
|
22
|
+
window_size: int = 12
|
|
23
|
+
mlp_ratio: float = 4.0
|
|
24
|
+
drop_path_rate: float = 0.1
|
|
25
|
+
patch_size: List[int] = field(default_factory=lambda: [7, 3, 3, 3])
|
|
26
|
+
patch_stride: List[int] = field(default_factory=lambda: [4, 2, 2, 2])
|
|
27
|
+
patch_padding: List[int] = field(default_factory=lambda: [3, 1, 1, 1])
|
|
28
|
+
patch_prenorm: List[bool] = field(
|
|
29
|
+
default_factory=lambda: [False, False, False, False]
|
|
30
|
+
)
|
|
31
|
+
qkv_bias: bool = True
|
|
32
|
+
conv_at_attn: bool = True
|
|
33
|
+
conv_at_ffn: bool = True
|
|
34
|
+
hidden_size: int = 768
|
|
35
|
+
image_size: Tuple[int, int] = (768, 768)
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def from_dict(cls, params):
|
|
39
|
+
return cls(
|
|
40
|
+
**{
|
|
41
|
+
k: v
|
|
42
|
+
for k, v in params.items()
|
|
43
|
+
if k in inspect.signature(cls).parameters
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def check_array_shape(arr):
|
|
49
|
+
shape = arr.shape
|
|
50
|
+
|
|
51
|
+
# Check if the shape has 4 dimensions
|
|
52
|
+
if len(shape) != 4:
|
|
53
|
+
return False
|
|
54
|
+
|
|
55
|
+
out_channels, kH, KW, _ = shape
|
|
56
|
+
|
|
57
|
+
# Check if out_channels is the largest, and kH and KW are the same
|
|
58
|
+
if (out_channels >= kH) and (out_channels >= KW) and (kH == KW):
|
|
59
|
+
return True
|
|
60
|
+
else:
|
|
61
|
+
return False
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class MlpFC(nn.Module):
|
|
65
|
+
"""MLP FC module"""
|
|
66
|
+
|
|
67
|
+
def __init__(
|
|
68
|
+
self,
|
|
69
|
+
in_features: int,
|
|
70
|
+
hidden_features: Optional[int] = None,
|
|
71
|
+
out_features: Optional[int] = None,
|
|
72
|
+
):
|
|
73
|
+
super().__init__()
|
|
74
|
+
self.fc1 = nn.Linear(in_features, hidden_features)
|
|
75
|
+
self.fc2 = nn.Linear(hidden_features, out_features)
|
|
76
|
+
self.gelu = nn.GELU()
|
|
77
|
+
|
|
78
|
+
def __call__(self, x):
|
|
79
|
+
return self.fc2(self.gelu(self.fc1(x)))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class Mlp(nn.Module):
|
|
83
|
+
"""MLP module"""
|
|
84
|
+
|
|
85
|
+
def __init__(
|
|
86
|
+
self,
|
|
87
|
+
in_features: int,
|
|
88
|
+
hidden_features: Optional[int] = None,
|
|
89
|
+
out_features: Optional[int] = None,
|
|
90
|
+
):
|
|
91
|
+
super().__init__()
|
|
92
|
+
out_features = out_features or in_features
|
|
93
|
+
hidden_features = hidden_features or in_features
|
|
94
|
+
|
|
95
|
+
self.net = MlpFC(in_features, hidden_features, out_features)
|
|
96
|
+
|
|
97
|
+
def __call__(self, x, size):
|
|
98
|
+
return self.net(x), size
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class DepthWiseConv2d(nn.Module):
|
|
102
|
+
"""Depthwise Convolution"""
|
|
103
|
+
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
dim_in: int,
|
|
107
|
+
kernel_size: int,
|
|
108
|
+
padding: int,
|
|
109
|
+
stride: int,
|
|
110
|
+
bias: bool = True,
|
|
111
|
+
):
|
|
112
|
+
super().__init__()
|
|
113
|
+
|
|
114
|
+
self.dw = nn.Conv2d(
|
|
115
|
+
dim_in,
|
|
116
|
+
dim_in,
|
|
117
|
+
kernel_size=kernel_size,
|
|
118
|
+
padding=padding,
|
|
119
|
+
stride=stride,
|
|
120
|
+
bias=bias,
|
|
121
|
+
groups=dim_in,
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
def __call__(self, x, size):
|
|
125
|
+
B, N, C = x.shape
|
|
126
|
+
H, W = size
|
|
127
|
+
assert N == H * W
|
|
128
|
+
|
|
129
|
+
x = self.dw(x.reshape(B, H, W, C))
|
|
130
|
+
|
|
131
|
+
x = x.transpose(0, 3, 1, 2)
|
|
132
|
+
|
|
133
|
+
size = (x.shape[-2], x.shape[-1])
|
|
134
|
+
x = x.flatten(2).transpose(0, 2, 1)
|
|
135
|
+
return x, size
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class ConvEmbed(nn.Module):
|
|
139
|
+
"""Image to Patch Embedding"""
|
|
140
|
+
|
|
141
|
+
def __init__(
|
|
142
|
+
self,
|
|
143
|
+
patch_size: int = 7,
|
|
144
|
+
in_chans: int = 3,
|
|
145
|
+
embed_dim: int = 64,
|
|
146
|
+
stride: int = 4,
|
|
147
|
+
padding: int = 2,
|
|
148
|
+
norm_layer: Optional[nn.Module] = None,
|
|
149
|
+
pre_norm: bool = True,
|
|
150
|
+
):
|
|
151
|
+
super().__init__()
|
|
152
|
+
self.proj = nn.Conv2d(
|
|
153
|
+
in_chans, embed_dim, kernel_size=patch_size, stride=stride, padding=padding
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
if norm_layer and pre_norm:
|
|
157
|
+
self.norm = norm_layer(in_chans)
|
|
158
|
+
elif norm_layer:
|
|
159
|
+
self.norm = norm_layer(embed_dim)
|
|
160
|
+
else:
|
|
161
|
+
self.norm = None
|
|
162
|
+
|
|
163
|
+
self.pre_norm = pre_norm
|
|
164
|
+
|
|
165
|
+
def __call__(self, x, size):
|
|
166
|
+
H, W = size
|
|
167
|
+
if len(x.shape) == 3:
|
|
168
|
+
|
|
169
|
+
if self.norm and self.pre_norm:
|
|
170
|
+
x = self.norm(x)
|
|
171
|
+
|
|
172
|
+
x = x.reshape(-1, H, W, x.shape[-1])
|
|
173
|
+
else:
|
|
174
|
+
x = x.transpose(0, 2, 3, 1)
|
|
175
|
+
|
|
176
|
+
x = self.proj(x)
|
|
177
|
+
|
|
178
|
+
B, H, W, C = x.shape
|
|
179
|
+
|
|
180
|
+
x = x.reshape(B, H * W, C)
|
|
181
|
+
|
|
182
|
+
if self.norm and not self.pre_norm:
|
|
183
|
+
x = self.norm(x)
|
|
184
|
+
|
|
185
|
+
return x, (H, W)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
class ChannelAttention(nn.Module):
|
|
189
|
+
"""Channel Attention module"""
|
|
190
|
+
|
|
191
|
+
def __init__(self, dim: int, groups: int = 8, qkv_bias: bool = True):
|
|
192
|
+
super().__init__()
|
|
193
|
+
self.groups = groups
|
|
194
|
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
|
195
|
+
self.proj = nn.Linear(dim, dim)
|
|
196
|
+
|
|
197
|
+
def __call__(self, x, size):
|
|
198
|
+
B, N, C = x.shape
|
|
199
|
+
|
|
200
|
+
qkv = self.qkv(x).reshape(B, N, 3, self.groups, C // self.groups)
|
|
201
|
+
qkv = qkv.transpose(2, 0, 3, 1, 4)
|
|
202
|
+
q, k, v = qkv[0], qkv[1], qkv[2] # Each has shape (B, groups, N, C//groups)
|
|
203
|
+
|
|
204
|
+
q = q * (float(N) ** -0.5)
|
|
205
|
+
|
|
206
|
+
# For multi-head attention, we need to keep the groups dimension
|
|
207
|
+
attention = mx.matmul(q.transpose(0, 1, 3, 2), k) # (B, groups, N, N)
|
|
208
|
+
attention = mx.softmax(attention, axis=-1)
|
|
209
|
+
|
|
210
|
+
x = mx.matmul(attention, v.transpose(0, 1, 3, 2)).transpose(
|
|
211
|
+
0, 1, 3, 2
|
|
212
|
+
) # (B, groups, N, C//groups)
|
|
213
|
+
x = x.transpose(0, 2, 1, 3).reshape(B, N, C)
|
|
214
|
+
x = self.proj(x)
|
|
215
|
+
|
|
216
|
+
return x, size
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def window_partition(x: mx.array, window_size: int):
|
|
220
|
+
"""Partition into non-overlapping windows"""
|
|
221
|
+
B, H, W, C = x.shape
|
|
222
|
+
x = mx.reshape(
|
|
223
|
+
x, (B, H // window_size, window_size, W // window_size, window_size, C)
|
|
224
|
+
)
|
|
225
|
+
windows = mx.reshape(
|
|
226
|
+
mx.transpose(x, (0, 1, 3, 2, 4, 5)), (-1, window_size, window_size, C)
|
|
227
|
+
)
|
|
228
|
+
return windows
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def window_reverse(
|
|
232
|
+
windows: mx.array, batch_size: int, window_size: int, H: int, W: int
|
|
233
|
+
):
|
|
234
|
+
"""Merge windows back to feature map"""
|
|
235
|
+
B = batch_size
|
|
236
|
+
x = mx.reshape(
|
|
237
|
+
windows, (B, H // window_size, W // window_size, window_size, window_size, -1)
|
|
238
|
+
)
|
|
239
|
+
x = mx.reshape(mx.transpose(x, (0, 1, 3, 2, 4, 5)), (B, H, W, -1))
|
|
240
|
+
return x
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class WindowAttention(nn.Module):
|
|
244
|
+
"""Window based multi-head self attention module"""
|
|
245
|
+
|
|
246
|
+
def __init__(
|
|
247
|
+
self, dim: int, num_heads: int, window_size: int, qkv_bias: bool = True
|
|
248
|
+
):
|
|
249
|
+
super().__init__()
|
|
250
|
+
self.dim = dim
|
|
251
|
+
self.window_size = window_size
|
|
252
|
+
self.num_heads = num_heads
|
|
253
|
+
head_dim = dim // num_heads
|
|
254
|
+
self.scale = float(head_dim) ** -0.5
|
|
255
|
+
|
|
256
|
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
|
257
|
+
self.proj = nn.Linear(dim, dim)
|
|
258
|
+
|
|
259
|
+
def __call__(self, x, size):
|
|
260
|
+
H, W = size
|
|
261
|
+
B, L, C = x.shape
|
|
262
|
+
|
|
263
|
+
assert L == H * W, f"input feature has wrong size {L} == {H * W}"
|
|
264
|
+
|
|
265
|
+
x = mx.reshape(x, (B, H, W, C))
|
|
266
|
+
|
|
267
|
+
# Calculate padding
|
|
268
|
+
pad_l = pad_t = 0
|
|
269
|
+
pad_r = (self.window_size - W % self.window_size) % self.window_size
|
|
270
|
+
pad_b = (self.window_size - H % self.window_size) % self.window_size
|
|
271
|
+
|
|
272
|
+
# MLX padding
|
|
273
|
+
x = mx.pad(x, [(0, 0), (pad_t, pad_b), (pad_l, pad_r), (0, 0)])
|
|
274
|
+
|
|
275
|
+
_, Hp, Wp, _ = x.shape
|
|
276
|
+
|
|
277
|
+
# Window partition
|
|
278
|
+
x = window_partition(x, self.window_size)
|
|
279
|
+
x = mx.reshape(x, (-1, self.window_size * self.window_size, C))
|
|
280
|
+
|
|
281
|
+
# Multi-head self attention
|
|
282
|
+
B_, N, C = x.shape
|
|
283
|
+
qkv = mx.reshape(self.qkv(x), (B_, N, 3, self.num_heads, C // self.num_heads))
|
|
284
|
+
qkv = mx.transpose(qkv, (2, 0, 3, 1, 4))
|
|
285
|
+
q, k, v = qkv[0], qkv[1], qkv[2]
|
|
286
|
+
|
|
287
|
+
# Scaled dot-product attention
|
|
288
|
+
q = q * self.scale
|
|
289
|
+
attn = mx.matmul(q, mx.transpose(k, (0, 1, 3, 2)))
|
|
290
|
+
attn = mx.softmax(attn, axis=-1)
|
|
291
|
+
|
|
292
|
+
x = mx.reshape(mx.transpose(mx.matmul(attn, v), (0, 2, 1, 3)), (B_, N, C))
|
|
293
|
+
x = self.proj(x)
|
|
294
|
+
|
|
295
|
+
# Merge windows
|
|
296
|
+
x = mx.reshape(x, (-1, self.window_size, self.window_size, C))
|
|
297
|
+
x = window_reverse(x, B, self.window_size, Hp, Wp)
|
|
298
|
+
|
|
299
|
+
if pad_r > 0 or pad_b > 0:
|
|
300
|
+
x = x[:, :H, :W, :]
|
|
301
|
+
|
|
302
|
+
x = mx.reshape(x, (B, H * W, C))
|
|
303
|
+
return x, size
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class PreNorm(nn.Module):
|
|
307
|
+
"""Pre-normalization module"""
|
|
308
|
+
|
|
309
|
+
def __init__(self, norm, fn, drop_path=None):
|
|
310
|
+
super().__init__()
|
|
311
|
+
self.norm = norm
|
|
312
|
+
self.fn = fn
|
|
313
|
+
self.drop_path = drop_path
|
|
314
|
+
|
|
315
|
+
def __call__(self, x, size):
|
|
316
|
+
shortcut = x
|
|
317
|
+
if self.norm is not None:
|
|
318
|
+
x = self.norm(x)
|
|
319
|
+
x, size = self.fn(x, size)
|
|
320
|
+
|
|
321
|
+
if self.drop_path is not None:
|
|
322
|
+
x = self.drop_path(x)
|
|
323
|
+
|
|
324
|
+
x = shortcut + x
|
|
325
|
+
return x, size
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
class DropPath(nn.Module):
|
|
329
|
+
"""Drop paths (Stochastic Depth) per sample."""
|
|
330
|
+
|
|
331
|
+
def __init__(self, drop_prob: float = 0.0):
|
|
332
|
+
super().__init__()
|
|
333
|
+
self.drop_prob = drop_prob
|
|
334
|
+
|
|
335
|
+
def __call__(self, x):
|
|
336
|
+
if self.drop_prob == 0.0 or not self.training:
|
|
337
|
+
return x
|
|
338
|
+
|
|
339
|
+
keep_prob = 1 - self.drop_prob
|
|
340
|
+
shape = (x.shape[0],) + (1,) * (x.ndim - 1)
|
|
341
|
+
random_tensor = keep_prob + mx.random.uniform(shape)
|
|
342
|
+
random_tensor = mx.floor(random_tensor)
|
|
343
|
+
output = x * random_tensor / keep_prob
|
|
344
|
+
return output
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
class SpatialBlock(nn.Module):
|
|
348
|
+
"""Spatial attention block"""
|
|
349
|
+
|
|
350
|
+
def __init__(
|
|
351
|
+
self,
|
|
352
|
+
dim: int,
|
|
353
|
+
num_heads: int,
|
|
354
|
+
window_size: int,
|
|
355
|
+
mlp_ratio: float = 4.0,
|
|
356
|
+
qkv_bias: bool = True,
|
|
357
|
+
drop_path_rate: float = 0.0,
|
|
358
|
+
conv_at_attn: bool = True,
|
|
359
|
+
conv_at_ffn: bool = True,
|
|
360
|
+
):
|
|
361
|
+
super().__init__()
|
|
362
|
+
|
|
363
|
+
drop_path = DropPath(drop_path_rate) if drop_path_rate > 0.0 else None
|
|
364
|
+
|
|
365
|
+
self.conv1 = (
|
|
366
|
+
PreNorm(None, DepthWiseConv2d(dim, 3, 1, 1), None) if conv_at_attn else None
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
self.window_attn = PreNorm(
|
|
370
|
+
nn.LayerNorm(dim),
|
|
371
|
+
WindowAttention(dim, num_heads, window_size, qkv_bias=qkv_bias),
|
|
372
|
+
drop_path,
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
self.conv2 = (
|
|
376
|
+
PreNorm(None, DepthWiseConv2d(dim, 3, 1, 1), None) if conv_at_ffn else None
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
self.ffn = PreNorm(
|
|
380
|
+
nn.LayerNorm(dim),
|
|
381
|
+
Mlp(
|
|
382
|
+
in_features=dim, hidden_features=int(dim * mlp_ratio), out_features=dim
|
|
383
|
+
),
|
|
384
|
+
drop_path,
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
def __call__(self, x, size):
|
|
388
|
+
if self.conv1 is not None:
|
|
389
|
+
x, size = self.conv1(x, size)
|
|
390
|
+
x, size = self.window_attn(x, size)
|
|
391
|
+
|
|
392
|
+
if self.conv2 is not None:
|
|
393
|
+
x, size = self.conv2(x, size)
|
|
394
|
+
x, size = self.ffn(x, size)
|
|
395
|
+
return x, size
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
class ChannelBlock(nn.Module):
|
|
399
|
+
"""Channel attention block"""
|
|
400
|
+
|
|
401
|
+
def __init__(
|
|
402
|
+
self,
|
|
403
|
+
dim: int,
|
|
404
|
+
groups: int,
|
|
405
|
+
mlp_ratio: float = 4.0,
|
|
406
|
+
qkv_bias: bool = True,
|
|
407
|
+
drop_path_rate: float = 0.0,
|
|
408
|
+
conv_at_attn: bool = True,
|
|
409
|
+
conv_at_ffn: bool = True,
|
|
410
|
+
):
|
|
411
|
+
super().__init__()
|
|
412
|
+
|
|
413
|
+
drop_path = DropPath(drop_path_rate) if drop_path_rate > 0.0 else None
|
|
414
|
+
|
|
415
|
+
self.conv1 = (
|
|
416
|
+
PreNorm(None, DepthWiseConv2d(dim, 3, 1, 1), None) if conv_at_attn else None
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
self.channel_attn = PreNorm(
|
|
420
|
+
nn.LayerNorm(dim),
|
|
421
|
+
ChannelAttention(dim, groups=groups, qkv_bias=qkv_bias),
|
|
422
|
+
drop_path,
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
self.conv2 = (
|
|
426
|
+
PreNorm(None, DepthWiseConv2d(dim, 3, 1, 1), None) if conv_at_ffn else None
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
self.ffn = PreNorm(
|
|
430
|
+
nn.LayerNorm(dim),
|
|
431
|
+
Mlp(
|
|
432
|
+
in_features=dim, hidden_features=int(dim * mlp_ratio), out_features=dim
|
|
433
|
+
),
|
|
434
|
+
drop_path,
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
def __call__(self, x, size):
|
|
438
|
+
if self.conv1 is not None:
|
|
439
|
+
x, size = self.conv1(x, size)
|
|
440
|
+
x, size = self.channel_attn(x, size)
|
|
441
|
+
|
|
442
|
+
if self.conv2 is not None:
|
|
443
|
+
x, size = self.conv2(x, size)
|
|
444
|
+
x, size = self.ffn(x, size)
|
|
445
|
+
|
|
446
|
+
return x, size
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
class Block(nn.Module):
|
|
450
|
+
def __init__(
|
|
451
|
+
self,
|
|
452
|
+
dim: int,
|
|
453
|
+
num_heads: int,
|
|
454
|
+
num_groups: int,
|
|
455
|
+
window_size: int,
|
|
456
|
+
mlp_ratio: float = 4.0,
|
|
457
|
+
qkv_bias: bool = True,
|
|
458
|
+
drop_path_rate: Tuple[float, float] = (0.0, 0.0),
|
|
459
|
+
conv_at_attn: bool = True,
|
|
460
|
+
conv_at_ffn: bool = True,
|
|
461
|
+
):
|
|
462
|
+
super().__init__()
|
|
463
|
+
self.spatial_block = SpatialBlock(
|
|
464
|
+
dim,
|
|
465
|
+
num_heads,
|
|
466
|
+
window_size,
|
|
467
|
+
drop_path_rate=drop_path_rate[0],
|
|
468
|
+
qkv_bias=qkv_bias,
|
|
469
|
+
mlp_ratio=mlp_ratio,
|
|
470
|
+
conv_at_attn=conv_at_attn,
|
|
471
|
+
conv_at_ffn=conv_at_ffn,
|
|
472
|
+
)
|
|
473
|
+
self.channel_block = ChannelBlock(
|
|
474
|
+
dim,
|
|
475
|
+
num_groups,
|
|
476
|
+
drop_path_rate=drop_path_rate[1],
|
|
477
|
+
qkv_bias=qkv_bias,
|
|
478
|
+
mlp_ratio=mlp_ratio,
|
|
479
|
+
conv_at_attn=conv_at_attn,
|
|
480
|
+
conv_at_ffn=conv_at_ffn,
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
def __call__(self, x, size):
|
|
484
|
+
x, size = self.spatial_block(x, size)
|
|
485
|
+
x, size = self.channel_block(x, size)
|
|
486
|
+
return x, size
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
class VisionModel(nn.Module):
|
|
490
|
+
"""DaViT: Dual Attention Vision Transformer"""
|
|
491
|
+
|
|
492
|
+
def __init__(self, config: VisionConfig):
|
|
493
|
+
super().__init__()
|
|
494
|
+
|
|
495
|
+
self.num_classes = config.num_classes
|
|
496
|
+
self.model_type = config.model_type
|
|
497
|
+
self.dim_embed = config.dim_embed
|
|
498
|
+
self.num_heads = config.num_heads
|
|
499
|
+
self.num_groups = config.num_groups
|
|
500
|
+
self.num_stages = len(self.dim_embed)
|
|
501
|
+
assert self.num_stages == len(self.num_heads) == len(self.num_groups)
|
|
502
|
+
|
|
503
|
+
if self.model_type not in ["davit", ""]:
|
|
504
|
+
raise ValueError(
|
|
505
|
+
f"Model type {self.model_type} not supported. Currently only 'davit' is supported"
|
|
506
|
+
)
|
|
507
|
+
|
|
508
|
+
# Convert PyTorch's linspace to MLX equivalent
|
|
509
|
+
total_blocks = sum(config.depths) * 2
|
|
510
|
+
dpr = [
|
|
511
|
+
i * config.drop_path_rate / (total_blocks - 1) for i in range(total_blocks)
|
|
512
|
+
]
|
|
513
|
+
|
|
514
|
+
depth_offset = 0
|
|
515
|
+
self.convs = []
|
|
516
|
+
self.blocks = []
|
|
517
|
+
|
|
518
|
+
for i in range(self.num_stages):
|
|
519
|
+
|
|
520
|
+
conv_embed = ConvEmbed(
|
|
521
|
+
patch_size=config.patch_size[i],
|
|
522
|
+
stride=config.patch_stride[i],
|
|
523
|
+
padding=config.patch_padding[i],
|
|
524
|
+
in_chans=config.in_chans if i == 0 else self.dim_embed[i - 1],
|
|
525
|
+
embed_dim=self.dim_embed[i],
|
|
526
|
+
norm_layer=nn.LayerNorm,
|
|
527
|
+
pre_norm=config.patch_prenorm[i],
|
|
528
|
+
)
|
|
529
|
+
self.convs.append(conv_embed)
|
|
530
|
+
|
|
531
|
+
block = []
|
|
532
|
+
for j in range(config.depths[i]):
|
|
533
|
+
block.append(
|
|
534
|
+
Block(
|
|
535
|
+
self.dim_embed[i],
|
|
536
|
+
config.num_heads[i],
|
|
537
|
+
config.num_groups[i],
|
|
538
|
+
config.window_size,
|
|
539
|
+
config.mlp_ratio,
|
|
540
|
+
config.qkv_bias,
|
|
541
|
+
(dpr[depth_offset + j * 2], dpr[depth_offset + j * 2 + 1]),
|
|
542
|
+
config.conv_at_attn,
|
|
543
|
+
config.conv_at_ffn,
|
|
544
|
+
)
|
|
545
|
+
)
|
|
546
|
+
|
|
547
|
+
self.blocks.append(block)
|
|
548
|
+
|
|
549
|
+
depth_offset += config.depths[i] * 2
|
|
550
|
+
|
|
551
|
+
def __call__(self, x):
|
|
552
|
+
input_size = x.shape[2:]
|
|
553
|
+
|
|
554
|
+
# Process through stages
|
|
555
|
+
for conv, blks in zip(self.convs, self.blocks):
|
|
556
|
+
x, input_size = conv(x, input_size)
|
|
557
|
+
for blk in blks:
|
|
558
|
+
x, input_size = blk(x, input_size)
|
|
559
|
+
|
|
560
|
+
return x
|
|
561
|
+
|
|
562
|
+
@staticmethod
|
|
563
|
+
def sanitize(weights):
|
|
564
|
+
sanitized_weights = {}
|
|
565
|
+
for k, v in weights.items():
|
|
566
|
+
if "position_ids" in k:
|
|
567
|
+
# Remove unused position_ids
|
|
568
|
+
continue
|
|
569
|
+
elif "convs" in k:
|
|
570
|
+
if "proj.weight" in k:
|
|
571
|
+
# PyTorch conv2d weight tensors have shape:
|
|
572
|
+
# [out_channels, in_channels, kH, KW]
|
|
573
|
+
# MLX conv2d expects the weight be of shape:
|
|
574
|
+
# [out_channels, kH, KW, in_channels]
|
|
575
|
+
if check_array_shape(v):
|
|
576
|
+
sanitized_weights[k] = v
|
|
577
|
+
else:
|
|
578
|
+
sanitized_weights[k] = v.transpose(0, 2, 3, 1)
|
|
579
|
+
else:
|
|
580
|
+
sanitized_weights[k] = v
|
|
581
|
+
elif "blocks" in k:
|
|
582
|
+
if "dw.weight" in k:
|
|
583
|
+
sanitized_weights[k] = (
|
|
584
|
+
v.transpose(0, 2, 3, 1) if v.shape[1] < v.shape[-1] else v
|
|
585
|
+
)
|
|
586
|
+
else:
|
|
587
|
+
sanitized_weights[k] = v
|
|
588
|
+
else:
|
|
589
|
+
sanitized_weights[k] = v
|
|
590
|
+
|
|
591
|
+
return sanitized_weights
|