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,557 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import List, Optional, Tuple, Type, Union
|
|
4
|
+
|
|
5
|
+
import mlx.core as mx
|
|
6
|
+
import mlx.nn as nn
|
|
7
|
+
import numpy as np
|
|
8
|
+
from PIL import Image
|
|
9
|
+
from PIL.Image import Resampling
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class SAMViTCfg:
|
|
14
|
+
image_size: Union[Tuple[int, int], int] = 1024
|
|
15
|
+
width: int = 768
|
|
16
|
+
layers: int = 12
|
|
17
|
+
heads: int = 12
|
|
18
|
+
patch_size: int = 16
|
|
19
|
+
window_size: int = 14
|
|
20
|
+
prompt_embed_dim: int = 256
|
|
21
|
+
global_attn_indexes: Union[List[int], Tuple[int]] = (2, 5, 8, 11)
|
|
22
|
+
downsample_channels: Union[List[int], Tuple[int]] = (512, 1024)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MLPBlock(nn.Module):
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
embedding_dim: int,
|
|
29
|
+
mlp_dim: int,
|
|
30
|
+
act: Type[nn.Module] = nn.GELU,
|
|
31
|
+
) -> None:
|
|
32
|
+
super().__init__()
|
|
33
|
+
self.lin1 = nn.Linear(embedding_dim, mlp_dim)
|
|
34
|
+
self.lin2 = nn.Linear(mlp_dim, embedding_dim)
|
|
35
|
+
self.act = act()
|
|
36
|
+
|
|
37
|
+
def __call__(self, x: mx.array):
|
|
38
|
+
return self.lin2(self.act(self.lin1(x)))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def resize_image(image_np, new_size=(96, 96), order=1):
|
|
42
|
+
"""
|
|
43
|
+
Resize an image with multiple channels using PIL.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
image_np (numpy.ndarray): The input image array of shape (batch, channels, height, width).
|
|
47
|
+
new_size (tuple): The target size as (height, width).
|
|
48
|
+
order (int): The order of interpolation (used to determine resampling method).
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
numpy.ndarray: The resized image array in the same format as input.
|
|
52
|
+
"""
|
|
53
|
+
# Remove batch dimension
|
|
54
|
+
image_np = np.array(image_np[0])
|
|
55
|
+
|
|
56
|
+
# Get dimensions
|
|
57
|
+
channels, height, width = image_np.shape
|
|
58
|
+
|
|
59
|
+
# Choose interpolation method based on order parameter
|
|
60
|
+
resample_method = Resampling.BILINEAR # Default to bilinear
|
|
61
|
+
if order == 0:
|
|
62
|
+
resample_method = Resampling.NEAREST
|
|
63
|
+
elif order == 2 or order == 3:
|
|
64
|
+
resample_method = Resampling.BICUBIC
|
|
65
|
+
|
|
66
|
+
# Handle different channel configurations
|
|
67
|
+
if channels == 1:
|
|
68
|
+
# For single-channel images (grayscale)
|
|
69
|
+
# Reshape to 2D array (height, width)
|
|
70
|
+
image_2d = image_np.reshape(height, width)
|
|
71
|
+
|
|
72
|
+
# Create PIL image - ensure proper mode and data type conversion
|
|
73
|
+
pil_image = Image.fromarray(image_2d.astype(np.float32))
|
|
74
|
+
|
|
75
|
+
# Resize using PIL (note: PIL takes width, height order)
|
|
76
|
+
resized_pil = pil_image.resize(
|
|
77
|
+
(new_size[1], new_size[0]), resample=resample_method
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Convert back to numpy array, reshape to add channel dimension
|
|
81
|
+
resized_np = np.array(resized_pil).reshape((1, new_size[0], new_size[1]))
|
|
82
|
+
else:
|
|
83
|
+
# For multi-channel images, process each channel individually
|
|
84
|
+
resized_channels = []
|
|
85
|
+
|
|
86
|
+
for c in range(channels):
|
|
87
|
+
channel_data = image_np[c]
|
|
88
|
+
pil_channel = Image.fromarray(channel_data.astype(np.float32))
|
|
89
|
+
resized_channel = pil_channel.resize(
|
|
90
|
+
(new_size[1], new_size[0]), resample=resample_method
|
|
91
|
+
)
|
|
92
|
+
resized_channels.append(np.array(resized_channel))
|
|
93
|
+
|
|
94
|
+
# Stack channels back together
|
|
95
|
+
resized_np = np.stack(resized_channels, axis=0)
|
|
96
|
+
|
|
97
|
+
# Add batch dimension back and convert to mx.array
|
|
98
|
+
return mx.array(resized_np)[None, :]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class SAMEncoder(nn.Module):
|
|
102
|
+
def __init__(
|
|
103
|
+
self,
|
|
104
|
+
img_size: int = 1024,
|
|
105
|
+
patch_size: int = 16,
|
|
106
|
+
in_chans: int = 3,
|
|
107
|
+
embed_dim: int = 768,
|
|
108
|
+
depth: int = 12,
|
|
109
|
+
num_heads: int = 12,
|
|
110
|
+
mlp_ratio: float = 4.0,
|
|
111
|
+
out_chans: int = 256,
|
|
112
|
+
qkv_bias: bool = True,
|
|
113
|
+
norm_layer: Type[nn.Module] = nn.LayerNorm,
|
|
114
|
+
act_layer: Type[nn.Module] = nn.GELU,
|
|
115
|
+
use_abs_pos: bool = True,
|
|
116
|
+
use_rel_pos: bool = True,
|
|
117
|
+
rel_pos_zero_init: bool = True,
|
|
118
|
+
window_size: int = 14,
|
|
119
|
+
global_attn_indexes: Tuple[int, ...] = (2, 5, 8, 11),
|
|
120
|
+
downsample_channels: Tuple[int, ...] = (512, 1024),
|
|
121
|
+
) -> None:
|
|
122
|
+
"""
|
|
123
|
+
Args:
|
|
124
|
+
img_size (int): Input image size.
|
|
125
|
+
patch_size (int): Patch size.
|
|
126
|
+
in_chans (int): Number of input image channels.
|
|
127
|
+
embed_dim (int): Patch embedding dimension.
|
|
128
|
+
depth (int): Depth of ViT.
|
|
129
|
+
num_heads (int): Number of attention heads in each ViT block.
|
|
130
|
+
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
|
|
131
|
+
qkv_bias (bool): If True, add a learnable bias to query, key, value.
|
|
132
|
+
norm_layer (nn.Module): Normalization layer.
|
|
133
|
+
act_layer (nn.Module): Activation layer.
|
|
134
|
+
use_abs_pos (bool): If True, use absolute positional embeddings.
|
|
135
|
+
use_rel_pos (bool): If True, add relative positional embeddings to the attention map.
|
|
136
|
+
rel_pos_zero_init (bool): If True, zero initialize relative positional parameters.
|
|
137
|
+
window_size (int): Window size for window attention blocks.
|
|
138
|
+
global_attn_indexes (list): Indexes for blocks using global attention.
|
|
139
|
+
downsample_channels (list): Channels for downsampling layers.
|
|
140
|
+
"""
|
|
141
|
+
super().__init__()
|
|
142
|
+
self.img_size = img_size
|
|
143
|
+
|
|
144
|
+
self.patch_embed = PatchEmbed(
|
|
145
|
+
kernel_size=(patch_size, patch_size),
|
|
146
|
+
stride=(patch_size, patch_size),
|
|
147
|
+
in_chans=in_chans,
|
|
148
|
+
embed_dim=embed_dim,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
if use_abs_pos:
|
|
152
|
+
# Initialize absolute positional embedding with pretrain image size.
|
|
153
|
+
self.pos_embed = mx.zeros(
|
|
154
|
+
(img_size // patch_size, img_size // patch_size, embed_dim)
|
|
155
|
+
)[None, :]
|
|
156
|
+
|
|
157
|
+
self.blocks = []
|
|
158
|
+
for i in range(depth):
|
|
159
|
+
block = Block(
|
|
160
|
+
dim=embed_dim,
|
|
161
|
+
num_heads=num_heads,
|
|
162
|
+
mlp_ratio=mlp_ratio,
|
|
163
|
+
qkv_bias=qkv_bias,
|
|
164
|
+
norm_layer=norm_layer,
|
|
165
|
+
act_layer=act_layer,
|
|
166
|
+
use_rel_pos=use_rel_pos,
|
|
167
|
+
rel_pos_zero_init=rel_pos_zero_init,
|
|
168
|
+
window_size=window_size if i not in global_attn_indexes else 0,
|
|
169
|
+
input_size=(img_size // patch_size, img_size // patch_size),
|
|
170
|
+
)
|
|
171
|
+
self.blocks.append(block)
|
|
172
|
+
|
|
173
|
+
self.neck = [
|
|
174
|
+
nn.Conv2d(
|
|
175
|
+
embed_dim,
|
|
176
|
+
out_chans,
|
|
177
|
+
kernel_size=1,
|
|
178
|
+
bias=False,
|
|
179
|
+
),
|
|
180
|
+
nn.LayerNorm(out_chans),
|
|
181
|
+
nn.Conv2d(
|
|
182
|
+
out_chans,
|
|
183
|
+
out_chans,
|
|
184
|
+
kernel_size=3,
|
|
185
|
+
padding=1,
|
|
186
|
+
bias=False,
|
|
187
|
+
),
|
|
188
|
+
nn.LayerNorm(out_chans),
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
in_channels = out_chans
|
|
192
|
+
self.downsamples = []
|
|
193
|
+
for i in range(len(downsample_channels)):
|
|
194
|
+
out_channels = downsample_channels[i]
|
|
195
|
+
self.downsamples.append(
|
|
196
|
+
nn.Conv2d(
|
|
197
|
+
in_channels,
|
|
198
|
+
out_channels,
|
|
199
|
+
kernel_size=3,
|
|
200
|
+
stride=2,
|
|
201
|
+
padding=1,
|
|
202
|
+
bias=False,
|
|
203
|
+
)
|
|
204
|
+
)
|
|
205
|
+
in_channels = out_channels
|
|
206
|
+
|
|
207
|
+
self.sam_hd = True
|
|
208
|
+
if self.sam_hd:
|
|
209
|
+
self.hd_alpha_downsamples = mx.zeros((1))
|
|
210
|
+
self.neck_hd = copy.deepcopy(self.neck)
|
|
211
|
+
|
|
212
|
+
def __call__(self, x: mx.array):
|
|
213
|
+
x = self.patch_embed(x)
|
|
214
|
+
if self.pos_embed is not None:
|
|
215
|
+
x += self.pos_embed
|
|
216
|
+
|
|
217
|
+
global_features = []
|
|
218
|
+
for _, blk in enumerate(self.blocks):
|
|
219
|
+
x = blk(x)
|
|
220
|
+
if self.sam_hd and blk.window_size == 0:
|
|
221
|
+
global_features.append(x)
|
|
222
|
+
|
|
223
|
+
for _, n in enumerate(self.neck):
|
|
224
|
+
x = n(x)
|
|
225
|
+
|
|
226
|
+
x = x.transpose(0, 3, 1, 2)
|
|
227
|
+
x = resize_image(x)
|
|
228
|
+
|
|
229
|
+
x = x.transpose(0, 2, 3, 1)
|
|
230
|
+
|
|
231
|
+
for _, downsample in enumerate(self.downsamples):
|
|
232
|
+
x = downsample(x)
|
|
233
|
+
|
|
234
|
+
if self.sam_hd:
|
|
235
|
+
first_global_feature = global_features[0]
|
|
236
|
+
for _, n_hd in enumerate(self.neck_hd):
|
|
237
|
+
first_global_feature = n_hd(first_global_feature)
|
|
238
|
+
|
|
239
|
+
first_global_feature = first_global_feature.transpose(0, 3, 1, 2)
|
|
240
|
+
|
|
241
|
+
first_global_feature = resize_image(first_global_feature)
|
|
242
|
+
|
|
243
|
+
first_global_feature = first_global_feature.transpose(0, 2, 3, 1)
|
|
244
|
+
for _, downsample in enumerate(self.downsamples):
|
|
245
|
+
first_global_feature = downsample(first_global_feature)
|
|
246
|
+
|
|
247
|
+
x = x + first_global_feature * self.hd_alpha_downsamples
|
|
248
|
+
|
|
249
|
+
return x
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
class Block(nn.Module):
|
|
253
|
+
"""Transformer blocks with support of window attention and residual propagation blocks"""
|
|
254
|
+
|
|
255
|
+
def __init__(
|
|
256
|
+
self,
|
|
257
|
+
dim: int,
|
|
258
|
+
num_heads: int,
|
|
259
|
+
mlp_ratio: float = 4.0,
|
|
260
|
+
qkv_bias: bool = True,
|
|
261
|
+
norm_layer: Type[nn.Module] = nn.LayerNorm,
|
|
262
|
+
act_layer: Type[nn.Module] = nn.GELU,
|
|
263
|
+
use_rel_pos: bool = False,
|
|
264
|
+
rel_pos_zero_init: bool = True,
|
|
265
|
+
window_size: int = 0,
|
|
266
|
+
input_size: Optional[Tuple[int, int]] = None,
|
|
267
|
+
) -> None:
|
|
268
|
+
"""
|
|
269
|
+
Args:
|
|
270
|
+
dim (int): Number of input channels.
|
|
271
|
+
num_heads (int): Number of attention heads in each ViT block.
|
|
272
|
+
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
|
|
273
|
+
qkv_bias (bool): If True, add a learnable bias to query, key, value.
|
|
274
|
+
norm_layer (nn.Module): Normalization layer.
|
|
275
|
+
act_layer (nn.Module): Activation layer.
|
|
276
|
+
use_rel_pos (bool): If True, add relative positional embeddings to the attention map.
|
|
277
|
+
rel_pos_zero_init (bool): If True, zero initialize relative positional parameters.
|
|
278
|
+
window_size (int): Window size for window attention blocks. If it equals 0, then
|
|
279
|
+
use global attention.
|
|
280
|
+
input_size (tuple(int, int) or None): Input resolution for calculating the relative
|
|
281
|
+
positional parameter size.
|
|
282
|
+
"""
|
|
283
|
+
super().__init__()
|
|
284
|
+
self.norm1 = norm_layer(dim)
|
|
285
|
+
self.attn = Attention(
|
|
286
|
+
dim,
|
|
287
|
+
num_heads=num_heads,
|
|
288
|
+
qkv_bias=qkv_bias,
|
|
289
|
+
use_rel_pos=use_rel_pos,
|
|
290
|
+
rel_pos_zero_init=rel_pos_zero_init,
|
|
291
|
+
input_size=input_size if window_size == 0 else (window_size, window_size),
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
self.norm2 = norm_layer(dim)
|
|
295
|
+
self.mlp = MLPBlock(
|
|
296
|
+
embedding_dim=dim, mlp_dim=int(dim * mlp_ratio), act=act_layer
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
self.window_size = window_size
|
|
300
|
+
|
|
301
|
+
def __call__(self, x: mx.array):
|
|
302
|
+
shortcut = x
|
|
303
|
+
x = self.norm1(x)
|
|
304
|
+
# Window partition
|
|
305
|
+
if self.window_size > 0:
|
|
306
|
+
H, W = x.shape[1], x.shape[2]
|
|
307
|
+
x, pad_hw = window_partition(x, self.window_size)
|
|
308
|
+
|
|
309
|
+
x = self.attn(x)
|
|
310
|
+
# Reverse window partition
|
|
311
|
+
if self.window_size > 0:
|
|
312
|
+
x = window_unpartition(x, self.window_size, pad_hw, (H, W))
|
|
313
|
+
|
|
314
|
+
x = shortcut + x
|
|
315
|
+
x = x + self.mlp(self.norm2(x))
|
|
316
|
+
|
|
317
|
+
return x
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
class Attention(nn.Module):
|
|
321
|
+
"""Multi-head Attention block with relative position embeddings."""
|
|
322
|
+
|
|
323
|
+
def __init__(
|
|
324
|
+
self,
|
|
325
|
+
dim: int,
|
|
326
|
+
num_heads: int = 8,
|
|
327
|
+
qkv_bias: bool = True,
|
|
328
|
+
use_rel_pos: bool = False,
|
|
329
|
+
rel_pos_zero_init: bool = True,
|
|
330
|
+
input_size: Optional[Tuple[int, int]] = None,
|
|
331
|
+
) -> None:
|
|
332
|
+
"""
|
|
333
|
+
Args:
|
|
334
|
+
dim (int): Number of input channels.
|
|
335
|
+
num_heads (int): Number of attention heads.
|
|
336
|
+
qkv_bias (bool): If True, add a learnable bias to query, key, value.
|
|
337
|
+
rel_pos (bool): If True, add relative positional embeddings to the attention map.
|
|
338
|
+
rel_pos_zero_init (bool): If True, zero initialize relative positional parameters.
|
|
339
|
+
input_size (tuple(int, int) or None): Input resolution for calculating the relative
|
|
340
|
+
positional parameter size.
|
|
341
|
+
"""
|
|
342
|
+
super().__init__()
|
|
343
|
+
self.num_heads = num_heads
|
|
344
|
+
head_dim = dim // num_heads
|
|
345
|
+
self.scale = head_dim**-0.5
|
|
346
|
+
|
|
347
|
+
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
|
348
|
+
self.proj = nn.Linear(dim, dim)
|
|
349
|
+
|
|
350
|
+
self.use_rel_pos = use_rel_pos
|
|
351
|
+
if self.use_rel_pos:
|
|
352
|
+
assert (
|
|
353
|
+
input_size is not None
|
|
354
|
+
), "Input size must be provided if using relative positional encoding."
|
|
355
|
+
# initialize relative positional embeddings
|
|
356
|
+
|
|
357
|
+
self.rel_pos_h = mx.zeros((2 * input_size[0] - 1, head_dim))
|
|
358
|
+
self.rel_pos_w = mx.zeros((2 * input_size[1] - 1, head_dim))
|
|
359
|
+
|
|
360
|
+
def __call__(self, x: mx.array):
|
|
361
|
+
B, H, W, _ = x.shape
|
|
362
|
+
x = mx.array(x)
|
|
363
|
+
# qkv with shape (3, B, nHead, H * W, C)
|
|
364
|
+
qkv = (
|
|
365
|
+
self.qkv(x)
|
|
366
|
+
.reshape(B, H * W, 3, self.num_heads, -1)
|
|
367
|
+
.transpose(2, 0, 3, 1, 4)
|
|
368
|
+
)
|
|
369
|
+
# q, k, v with shape (B * nHead, H * W, C)
|
|
370
|
+
q, k, v = qkv.reshape(3, B * self.num_heads, H * W, -1)
|
|
371
|
+
|
|
372
|
+
def do_attention(q, k, v):
|
|
373
|
+
attn = (q * self.scale) @ k.transpose(0, -1, -2)
|
|
374
|
+
if self.use_rel_pos:
|
|
375
|
+
attn = add_decomposed_rel_pos(
|
|
376
|
+
attn, q, self.rel_pos_h, self.rel_pos_w, (H, W), (H, W)
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
attn = mx.softmax(attn, axis=-1)
|
|
380
|
+
x = (
|
|
381
|
+
(attn @ v)
|
|
382
|
+
.reshape(B, self.num_heads, H, W, -1)
|
|
383
|
+
.transpose(0, 2, 3, 1, 4)
|
|
384
|
+
.reshape(B, H, W, -1)
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
return x
|
|
388
|
+
|
|
389
|
+
x = do_attention(q, k, v)
|
|
390
|
+
x = self.proj(x)
|
|
391
|
+
|
|
392
|
+
return x
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def window_partition(
|
|
396
|
+
x: np.ndarray, window_size: int
|
|
397
|
+
) -> Tuple[np.ndarray, Tuple[int, int]]:
|
|
398
|
+
"""
|
|
399
|
+
Partition into non-overlapping windows with padding if needed.
|
|
400
|
+
Args:
|
|
401
|
+
x (ndarray): input tokens with [B, H, W, C].
|
|
402
|
+
window_size (int): window size.
|
|
403
|
+
|
|
404
|
+
Returns:
|
|
405
|
+
windows: windows after partition with [B * num_windows, window_size, window_size, C].
|
|
406
|
+
(Hp, Wp): padded height and width before partition
|
|
407
|
+
"""
|
|
408
|
+
B, H, W, C = x.shape
|
|
409
|
+
|
|
410
|
+
pad_h = (window_size - H % window_size) % window_size
|
|
411
|
+
pad_w = (window_size - W % window_size) % window_size
|
|
412
|
+
if pad_h > 0 or pad_w > 0:
|
|
413
|
+
x = np.pad(x, ((0, 0), (0, pad_h), (0, pad_w), (0, 0)))
|
|
414
|
+
Hp, Wp = H + pad_h, W + pad_w
|
|
415
|
+
|
|
416
|
+
x = x.reshape(B, Hp // window_size, window_size, Wp // window_size, window_size, C)
|
|
417
|
+
windows = x.transpose(0, 1, 3, 2, 4, 5).reshape(-1, window_size, window_size, C)
|
|
418
|
+
return windows, (Hp, Wp)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def window_unpartition(
|
|
422
|
+
windows: np.ndarray,
|
|
423
|
+
window_size: int,
|
|
424
|
+
pad_hw: Tuple[int, int],
|
|
425
|
+
hw: Tuple[int, int],
|
|
426
|
+
):
|
|
427
|
+
"""
|
|
428
|
+
Window unpartition into original sequences and removing padding.
|
|
429
|
+
Args:
|
|
430
|
+
windows (ndarray): input tokens with [B * num_windows, window_size, window_size, C].
|
|
431
|
+
window_size (int): window size.
|
|
432
|
+
pad_hw (Tuple): padded height and width (Hp, Wp).
|
|
433
|
+
hw (Tuple): original height and width (H, W) before padding.
|
|
434
|
+
|
|
435
|
+
Returns:
|
|
436
|
+
x: unpartitioned sequences with [B, H, W, C].
|
|
437
|
+
"""
|
|
438
|
+
Hp, Wp = pad_hw
|
|
439
|
+
H, W = hw
|
|
440
|
+
B = windows.shape[0] // (Hp * Wp // window_size // window_size)
|
|
441
|
+
x = windows.reshape(
|
|
442
|
+
B, Hp // window_size, Wp // window_size, window_size, window_size, -1
|
|
443
|
+
)
|
|
444
|
+
x = x.transpose(0, 1, 3, 2, 4, 5).reshape(B, Hp, Wp, -1)
|
|
445
|
+
|
|
446
|
+
if Hp > H or Wp > W:
|
|
447
|
+
x = x[:, :H, :W, :]
|
|
448
|
+
return x
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def get_rel_pos(q_size: int, k_size: int, rel_pos: np.ndarray) -> np.ndarray:
|
|
452
|
+
"""
|
|
453
|
+
Get relative positional embeddings according to the relative positions of
|
|
454
|
+
query and key sizes.
|
|
455
|
+
Args:
|
|
456
|
+
q_size (int): size of query q.
|
|
457
|
+
k_size (int): size of key k.
|
|
458
|
+
rel_pos (ndarray): relative position embeddings (L, C).
|
|
459
|
+
|
|
460
|
+
Returns:
|
|
461
|
+
Extracted positional embeddings according to relative positions.
|
|
462
|
+
"""
|
|
463
|
+
rel_pos = np.array(rel_pos)
|
|
464
|
+
max_rel_dist = int(2 * max(q_size, k_size) - 1)
|
|
465
|
+
# Interpolate rel pos if needed.
|
|
466
|
+
if rel_pos.shape[0] != max_rel_dist:
|
|
467
|
+
# Interpolate rel pos.
|
|
468
|
+
rel_pos_resized = np.expand_dims(rel_pos, axis=0)
|
|
469
|
+
rel_pos_resized = np.transpose(rel_pos_resized, (0, 2, 1))
|
|
470
|
+
rel_pos_resized = np.interp(
|
|
471
|
+
np.linspace(0, max_rel_dist - 1, num=max_rel_dist),
|
|
472
|
+
np.arange(rel_pos.shape[0]),
|
|
473
|
+
rel_pos_resized[0],
|
|
474
|
+
)
|
|
475
|
+
rel_pos_resized = np.transpose(rel_pos_resized, (1, 0))
|
|
476
|
+
else:
|
|
477
|
+
rel_pos_resized = rel_pos
|
|
478
|
+
|
|
479
|
+
# Scale the coords with short length if shapes for q and k are different.
|
|
480
|
+
q_coords = np.arange(q_size)[:, np.newaxis] * max(k_size / q_size, 1.0)
|
|
481
|
+
k_coords = np.arange(k_size)[np.newaxis, :] * max(q_size / k_size, 1.0)
|
|
482
|
+
relative_coords = (q_coords - k_coords) + (k_size - 1) * max(q_size / k_size, 1.0)
|
|
483
|
+
relative_coords = relative_coords.astype(np.int64)
|
|
484
|
+
return rel_pos_resized[relative_coords]
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def add_decomposed_rel_pos(
|
|
488
|
+
attn: np.ndarray,
|
|
489
|
+
q: np.ndarray,
|
|
490
|
+
rel_pos_h: np.ndarray,
|
|
491
|
+
rel_pos_w: np.ndarray,
|
|
492
|
+
q_size: Tuple[int, int],
|
|
493
|
+
k_size: Tuple[int, int],
|
|
494
|
+
) -> np.ndarray:
|
|
495
|
+
"""
|
|
496
|
+
Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`.
|
|
497
|
+
https://github.com/facebookresearch/mvit/blob/19786631e330df9f3622e5402b4a419a263a2c80/mvit/models/attention.py # noqa B950
|
|
498
|
+
Args:
|
|
499
|
+
attn (ndarray): attention map.
|
|
500
|
+
q (ndarray): query q in the attention layer with shape (B, q_h * q_w, C).
|
|
501
|
+
rel_pos_h (ndarray): relative position embeddings (Lh, C) for height axis.
|
|
502
|
+
rel_pos_w (ndarray): relative position embeddings (Lw, C) for width axis.
|
|
503
|
+
q_size (Tuple): spatial sequence size of query q with (q_h, q_w).
|
|
504
|
+
k_size (Tuple): spatial sequence size of key k with (k_h, k_w).
|
|
505
|
+
|
|
506
|
+
Returns:
|
|
507
|
+
attn (ndarray): attention map with added relative positional embeddings.
|
|
508
|
+
"""
|
|
509
|
+
q_h, q_w = q_size
|
|
510
|
+
k_h, k_w = k_size
|
|
511
|
+
Rh = get_rel_pos(q_h, k_h, rel_pos_h)
|
|
512
|
+
Rw = get_rel_pos(q_w, k_w, rel_pos_w)
|
|
513
|
+
|
|
514
|
+
B, _, dim = q.shape
|
|
515
|
+
r_q = q.reshape(B, q_h, q_w, dim)
|
|
516
|
+
|
|
517
|
+
rel_h = np.einsum("bhwc,hkc->bhwk", r_q, Rh)
|
|
518
|
+
rel_w = np.einsum("bhwc,wkc->bhwk", r_q, Rw)
|
|
519
|
+
|
|
520
|
+
attn = (
|
|
521
|
+
attn.reshape(B, q_h, q_w, k_h, k_w)
|
|
522
|
+
+ rel_h[:, :, :, :, np.newaxis]
|
|
523
|
+
+ rel_w[:, :, :, np.newaxis, :]
|
|
524
|
+
).reshape(B, q_h * q_w, k_h * k_w)
|
|
525
|
+
|
|
526
|
+
return attn
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
class PatchEmbed(nn.Module):
|
|
530
|
+
"""
|
|
531
|
+
Image to Patch Embedding.
|
|
532
|
+
"""
|
|
533
|
+
|
|
534
|
+
def __init__(
|
|
535
|
+
self,
|
|
536
|
+
kernel_size: Tuple[int, int] = (16, 16),
|
|
537
|
+
stride: Tuple[int, int] = (16, 16),
|
|
538
|
+
in_chans: int = 3,
|
|
539
|
+
embed_dim: int = 768,
|
|
540
|
+
) -> None:
|
|
541
|
+
"""
|
|
542
|
+
Args:
|
|
543
|
+
kernel_size (Tuple): kernel size of the projection layer.
|
|
544
|
+
stride (Tuple): stride of the projection layer.
|
|
545
|
+
padding (Tuple): padding size of the projection layer.
|
|
546
|
+
in_chans (int): Number of input image channels.
|
|
547
|
+
embed_dim (int): Patch embedding dimension.
|
|
548
|
+
"""
|
|
549
|
+
super().__init__()
|
|
550
|
+
|
|
551
|
+
self.proj = nn.Conv2d(
|
|
552
|
+
in_chans, embed_dim, kernel_size=kernel_size, stride=stride
|
|
553
|
+
)
|
|
554
|
+
|
|
555
|
+
def __call__(self, x: mx.array):
|
|
556
|
+
x = self.proj(x)
|
|
557
|
+
return x
|