nexaai 1.0.19rc6__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nexaai might be problematic. Click here for more details.

Files changed (224) hide show
  1. nexaai/_stub.cpython-310-darwin.so +0 -0
  2. nexaai/_version.py +1 -1
  3. nexaai/binds/libnexa_bridge.dylib +0 -0
  4. nexaai/binds/nexa_llama_cpp/libggml-base.dylib +0 -0
  5. nexaai/binds/nexa_llama_cpp/libggml-cpu.so +0 -0
  6. nexaai/binds/nexa_llama_cpp/libggml-metal.so +0 -0
  7. nexaai/binds/nexa_llama_cpp/libggml.dylib +0 -0
  8. nexaai/binds/nexa_llama_cpp/libllama.dylib +0 -0
  9. nexaai/binds/nexa_llama_cpp/libmtmd.dylib +0 -0
  10. nexaai/binds/nexa_llama_cpp/libnexa_plugin.dylib +0 -0
  11. nexaai/binds/nexa_mlx/libnexa_plugin.dylib +0 -0
  12. nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +12 -0
  13. nexaai/binds/nexa_mlx/py-lib/asr/interface.py +122 -0
  14. nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
  15. nexaai/binds/nexa_mlx/py-lib/common/utils.py +25 -0
  16. nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
  17. nexaai/binds/nexa_mlx/py-lib/cv/generate.py +195 -0
  18. nexaai/binds/nexa_mlx/py-lib/cv/interface.py +151 -0
  19. nexaai/binds/nexa_mlx/py-lib/cv/main.py +81 -0
  20. nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +1736 -0
  21. nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
  22. nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +333 -0
  23. nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +617 -0
  24. nexaai/binds/nexa_mlx/py-lib/embedding/main.py +173 -0
  25. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
  26. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +399 -0
  27. nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +1 -0
  28. nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +244 -0
  29. nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +82 -0
  30. nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +281 -0
  31. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +306 -0
  32. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +116 -0
  33. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +65 -0
  34. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +386 -0
  35. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +105 -0
  36. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +100 -0
  37. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +460 -0
  38. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +274 -0
  39. nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
  40. nexaai/binds/nexa_mlx/py-lib/llm/generate.py +149 -0
  41. nexaai/binds/nexa_mlx/py-lib/llm/interface.py +764 -0
  42. nexaai/binds/nexa_mlx/py-lib/llm/main.py +68 -0
  43. nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
  44. nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +174 -0
  45. nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +287 -0
  46. nexaai/binds/nexa_mlx/py-lib/rerank/main.py +127 -0
  47. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
  48. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +330 -0
  49. nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +1 -0
  50. nexaai/binds/nexa_mlx/py-lib/sd/interface.py +362 -0
  51. nexaai/binds/nexa_mlx/py-lib/sd/main.py +286 -0
  52. nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +306 -0
  53. nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +116 -0
  54. nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +65 -0
  55. nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +385 -0
  56. nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +105 -0
  57. nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +100 -0
  58. nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +460 -0
  59. nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +274 -0
  60. nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +12 -0
  61. nexaai/binds/nexa_mlx/py-lib/tts/interface.py +276 -0
  62. nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +3 -0
  63. nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +572 -0
  64. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +294 -0
  65. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +276 -0
  66. nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +504 -0
  67. nexaai/binds/nexa_mlx/py-lib/vlm/main.py +320 -0
  68. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
  69. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +68 -0
  70. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
  71. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +8 -0
  72. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +193 -0
  73. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +186 -0
  74. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +233 -0
  75. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +503 -0
  76. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +202 -0
  77. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +230 -0
  78. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +10 -0
  79. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +264 -0
  80. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +472 -0
  81. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +591 -0
  82. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +526 -0
  83. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +356 -0
  84. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +8 -0
  85. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +366 -0
  86. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +488 -0
  87. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +591 -0
  88. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +8 -0
  89. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +213 -0
  90. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +315 -0
  91. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +238 -0
  92. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +2 -0
  93. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +1038 -0
  94. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +139 -0
  95. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +322 -0
  96. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +629 -0
  97. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +1022 -0
  98. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +9 -0
  99. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +294 -0
  100. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +191 -0
  101. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +267 -0
  102. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +8 -0
  103. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +175 -0
  104. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +192 -0
  105. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +233 -0
  106. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +9 -0
  107. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +140 -0
  108. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +220 -0
  109. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +393 -0
  110. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +293 -0
  111. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +307 -0
  112. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +8 -0
  113. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +143 -0
  114. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +509 -0
  115. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +522 -0
  116. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +8 -0
  117. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +386 -0
  118. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +138 -0
  119. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +560 -0
  120. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +8 -0
  121. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +240 -0
  122. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +153 -0
  123. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +259 -0
  124. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +9 -0
  125. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +236 -0
  126. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +256 -0
  127. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +303 -0
  128. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +8 -0
  129. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +230 -0
  130. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +160 -0
  131. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +243 -0
  132. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +8 -0
  133. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +283 -0
  134. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +8 -0
  135. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +416 -0
  136. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +172 -0
  137. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +499 -0
  138. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +8 -0
  139. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +243 -0
  140. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +133 -0
  141. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +465 -0
  142. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +10 -0
  143. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +230 -0
  144. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +385 -0
  145. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +557 -0
  146. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +526 -0
  147. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +8 -0
  148. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +282 -0
  149. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +160 -0
  150. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +242 -0
  151. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +8 -0
  152. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +21 -0
  153. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +243 -0
  154. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +71 -0
  155. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +324 -0
  156. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +8 -0
  157. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +229 -0
  158. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +161 -0
  159. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +320 -0
  160. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +2 -0
  161. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +108 -0
  162. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +490 -0
  163. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +168 -0
  164. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +414 -0
  165. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +2 -0
  166. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +104 -0
  167. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +490 -0
  168. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +167 -0
  169. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +312 -0
  170. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
  171. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +117 -0
  172. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +531 -0
  173. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +701 -0
  174. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +255 -0
  175. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +303 -0
  176. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +407 -0
  177. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +476 -0
  178. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +1223 -0
  179. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  180. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +117 -0
  181. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +531 -0
  182. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +701 -0
  183. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +255 -0
  184. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +303 -0
  185. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +407 -0
  186. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +476 -0
  187. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +1309 -0
  188. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +210 -0
  189. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +8 -0
  190. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +62 -0
  191. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +209 -0
  192. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +215 -0
  193. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +474 -0
  194. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +39 -0
  195. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +344 -0
  196. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +9 -0
  197. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +70 -0
  198. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +296 -0
  199. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +160 -0
  200. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +928 -0
  201. nexaai/binds/nexa_nexaml/libggml-base.dylib +0 -0
  202. nexaai/binds/nexa_nexaml/libggml-cpu.so +0 -0
  203. nexaai/binds/nexa_nexaml/libggml-metal.so +0 -0
  204. nexaai/binds/nexa_nexaml/libggml.dylib +0 -0
  205. nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +276 -0
  206. nexaai/mlx_backend/vlm/interface.py +21 -4
  207. nexaai/mlx_backend/vlm/main.py +6 -2
  208. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  209. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +117 -0
  210. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +531 -0
  211. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +701 -0
  212. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +255 -0
  213. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +303 -0
  214. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +407 -0
  215. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/processor.py +476 -0
  216. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +1309 -0
  217. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/switch_layers.py +210 -0
  218. nexaai/utils/manifest_utils.py +222 -15
  219. nexaai/utils/model_manager.py +83 -7
  220. nexaai/utils/model_types.py +2 -0
  221. {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/METADATA +1 -1
  222. {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/RECORD +224 -24
  223. {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/WHEEL +0 -0
  224. {nexaai-1.0.19rc6.dist-info → nexaai-1.0.19rc7.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,296 @@
1
+ import json
2
+ import warnings
3
+ from dataclasses import dataclass, field
4
+ from pathlib import Path
5
+ from typing import Union
6
+
7
+ import mlx.core as mx
8
+ import mlx.nn as nn
9
+ import numpy as np
10
+ from mlx.utils import tree_flatten, tree_map
11
+
12
+
13
+ def get_prompt(model_type, processor, conversation):
14
+ if model_type == "paligemma":
15
+ return conversation
16
+
17
+ if "chat_template" in processor.__dict__.keys():
18
+ prompt = processor.apply_chat_template(
19
+ conversation,
20
+ tokenize=False,
21
+ add_generation_prompt=False,
22
+ )
23
+ elif "tokenizer" in processor.__dict__.keys():
24
+ prompt = processor.tokenizer.apply_chat_template(
25
+ conversation,
26
+ tokenize=False,
27
+ add_generation_prompt=False,
28
+ )
29
+
30
+ return prompt
31
+
32
+
33
+ class Dataset:
34
+ def __init__(
35
+ self,
36
+ hf_dataset,
37
+ config,
38
+ processor,
39
+ image_processor=None,
40
+ take=None,
41
+ split=None,
42
+ image_resize_shape=None,
43
+ ):
44
+ if split is not None:
45
+ self.dataset = hf_dataset[split]
46
+ else:
47
+ self.dataset = hf_dataset
48
+ if take is not None:
49
+ self.dataset = self.dataset.take(take)
50
+ self.processor = processor
51
+ self.config = config
52
+ self.image_processor = image_processor
53
+ self.image_resize_shape = image_resize_shape
54
+
55
+ def __len__(self):
56
+ return len(self.dataset)
57
+
58
+ def __getitem__(self, idx):
59
+ from mlx_vlm.utils import prepare_inputs
60
+
61
+ item = self.dataset[idx]
62
+
63
+ images = item["images"]
64
+ conversations = item["messages"]
65
+ prompts = []
66
+
67
+ if isinstance(conversations, list) and isinstance(conversations[0], list):
68
+ for conversation in conversations:
69
+ if self.config["model_type"] == "pixtral":
70
+ conversation = [json.loads(i) for i in conversation]
71
+ if len(conversations) > 1:
72
+ warnings.warn(
73
+ "Pixtral batch processing is not supported yet. Set batch size to 1."
74
+ )
75
+
76
+ prompt = get_prompt(
77
+ self.config["model_type"], self.processor, conversation
78
+ )
79
+ prompts.append(prompt)
80
+
81
+ else:
82
+ if self.config["model_type"] == "pixtral":
83
+ conversations = [json.loads(i) for i in conversations]
84
+ prompt = get_prompt(
85
+ self.config["model_type"], self.processor, conversations
86
+ )
87
+ prompts.append(prompt)
88
+
89
+ image_token_index = self.config["image_token_index"]
90
+
91
+ inputs = prepare_inputs(
92
+ self.processor,
93
+ images,
94
+ prompts,
95
+ image_token_index,
96
+ self.image_resize_shape,
97
+ )
98
+ input_ids = inputs["input_ids"]
99
+ pixel_values = inputs["pixel_values"]
100
+ mask = inputs["attention_mask"]
101
+ kwargs = {
102
+ k: v
103
+ for k, v in inputs.items()
104
+ if k not in ["input_ids", "pixel_values", "attention_mask"]
105
+ }
106
+
107
+ if mask is None:
108
+ mask = mx.ones_like(input_ids)
109
+
110
+ return {
111
+ "pixel_values": pixel_values,
112
+ "input_ids": input_ids,
113
+ "attention_mask": mask,
114
+ **kwargs,
115
+ }
116
+
117
+
118
+ def grad_checkpoint(layer):
119
+ """
120
+ Update all instances of type(layer) to use gradient checkpointing.
121
+ """
122
+ fn = type(layer).__call__
123
+
124
+ def checkpointed_fn(model, *args, **kwargs):
125
+ def inner_fn(params, *args, **kwargs):
126
+ model.update(params)
127
+ return fn(model, *args, **kwargs)
128
+
129
+ return mx.checkpoint(inner_fn)(model.trainable_parameters(), *args, **kwargs)
130
+
131
+ type(layer).__call__ = checkpointed_fn
132
+
133
+
134
+ @dataclass
135
+ class TrainingArgs:
136
+ batch_size: int = field(default=4, metadata={"help": "Minibatch size."})
137
+ iters: int = field(default=100, metadata={"help": "Iterations to train for."})
138
+ val_batches: int = field(
139
+ default=25,
140
+ metadata={
141
+ "help": "Number of validation batches, -1 uses the entire validation set."
142
+ },
143
+ )
144
+ steps_per_report: int = field(
145
+ default=10,
146
+ metadata={"help": "Number of training steps between loss reporting."},
147
+ )
148
+ steps_per_eval: int = field(
149
+ default=200, metadata={"help": "Number of training steps between validations."}
150
+ )
151
+ steps_per_save: int = field(
152
+ default=100, metadata={"help": "Save the model every number steps"}
153
+ )
154
+ max_seq_length: int = field(
155
+ default=2048, metadata={"help": "Maximum sequence length."}
156
+ )
157
+ adapter_file: str = field(
158
+ default="adapters.safetensors",
159
+ metadata={"help": "Save/load path for the trained adapter weights."},
160
+ )
161
+ grad_checkpoint: bool = field(
162
+ default=False,
163
+ metadata={"help": "Use gradient checkpointing to reduce memory use."},
164
+ )
165
+
166
+
167
+ def default_loss(model, inputs, targets, lengths):
168
+ logits = model(inputs)
169
+ logits = logits.astype(mx.float32)
170
+
171
+ length_mask = mx.arange(inputs.shape[1])[None, :] < lengths[:, None]
172
+
173
+ ce = nn.losses.cross_entropy(logits, targets) * length_mask
174
+ ntoks = length_mask.sum()
175
+ ce = ce.sum() / ntoks
176
+
177
+ return ce, ntoks
178
+
179
+
180
+ class Trainer:
181
+ def __init__(
182
+ self,
183
+ model,
184
+ optimizer,
185
+ train_on_completions=False,
186
+ assistant_id=77091,
187
+ clip_gradients=None,
188
+ ):
189
+ self.model = model
190
+ self.optimizer = optimizer
191
+ self.train_on_completions = train_on_completions
192
+ self.assistant_id = assistant_id
193
+ self.clip_gradients = clip_gradients
194
+
195
+ def loss_fn(self, model, batch):
196
+ pixel_values = batch["pixel_values"]
197
+ input_ids = batch["input_ids"]
198
+ attention_mask = batch["attention_mask"]
199
+ lengths = mx.sum(attention_mask, axis=1)
200
+ labels = input_ids[:, 1:]
201
+
202
+ batch_size, seq_length = input_ids.shape
203
+
204
+ if self.train_on_completions:
205
+ weight_mask = mx.ones_like(attention_mask)
206
+
207
+ assistant_response_index = np.where(input_ids == self.assistant_id)[1]
208
+ range_matrix = mx.repeat(
209
+ mx.expand_dims(mx.arange(seq_length), 0), batch_size, axis=0
210
+ )
211
+ assistant_mask = range_matrix <= mx.array(assistant_response_index).reshape(
212
+ -1, 1
213
+ )
214
+ # Apply the mask to weight_mask
215
+ weight_mask = mx.where(
216
+ assistant_mask, mx.zeros_like(weight_mask), weight_mask
217
+ )[:, 1:]
218
+ else:
219
+ weight_mask = None
220
+
221
+ input_ids = input_ids[:, :-1]
222
+
223
+ kwargs = {
224
+ k: v
225
+ for k, v in batch.items()
226
+ if k not in ["input_ids", "pixel_values", "attention_mask"]
227
+ }
228
+
229
+ # Forward pass
230
+ outputs = model(input_ids, pixel_values, attention_mask, **kwargs)
231
+
232
+ # Cast to float32
233
+ logits = outputs.logits.astype(mx.float32)
234
+
235
+ # Ensure logits and labels have the same sequence length
236
+ def align_logits_with_labels(logits, labels):
237
+ if logits.shape[1] < labels.shape[1]:
238
+ pad_length = labels.shape[1] - logits.shape[1]
239
+ pad_width = ((0, 0), (0, pad_length), (0, 0))
240
+ return mx.pad(logits, pad_width, mode="constant", constant_values=-100)
241
+ elif logits.shape[1] > labels.shape[1]:
242
+ return logits[:, -labels.shape[1] :, :]
243
+ return logits
244
+
245
+ logits = align_logits_with_labels(logits, labels)
246
+
247
+ length_mask = mx.arange(input_ids.shape[1])[None, :] < lengths[:, None]
248
+
249
+ # Compute loss only on non-padded tokens
250
+ ce = (
251
+ nn.losses.cross_entropy(
252
+ logits,
253
+ labels,
254
+ weights=weight_mask,
255
+ )
256
+ * length_mask
257
+ )
258
+ ntoks = length_mask.sum()
259
+ ce = ce.sum() / ntoks
260
+
261
+ return ce
262
+
263
+ def train_step(self, batch):
264
+ loss_and_grad_fn = nn.value_and_grad(self.model, self.loss_fn)
265
+ loss, grads = loss_and_grad_fn(self.model, batch)
266
+
267
+ # Add gradient clipping
268
+ if self.clip_gradients is not None:
269
+ grads = tree_map(
270
+ lambda g: mx.clip(g, -self.clip_gradients, self.clip_gradients), grads
271
+ )
272
+
273
+ self.optimizer.update(self.model, grads)
274
+
275
+ return loss
276
+
277
+ @mx.compile
278
+ def train_epoch(self, dataloader):
279
+ total_loss = 0
280
+ for batch in dataloader:
281
+ loss = self.train_step(batch)
282
+ mx.eval(self.model, self.optimizer.state)
283
+ total_loss += loss
284
+ return total_loss / len(dataloader)
285
+
286
+
287
+ def save_adapter(
288
+ model: nn.Module,
289
+ adapter_file: Union[str, Path],
290
+ ):
291
+ path = Path(adapter_file)
292
+ if hasattr(model.config, "lora"):
293
+ with open(path.parent / "adapter_config.json", "w") as f:
294
+ json.dump(model.config.lora, f)
295
+ flattened_tree = tree_flatten(model.trainable_parameters())
296
+ mx.save_safetensors(str(adapter_file), dict(flattened_tree))
@@ -0,0 +1,160 @@
1
+ import json
2
+ from pathlib import Path
3
+
4
+ import mlx.nn as nn
5
+ from mlx.utils import tree_flatten
6
+
7
+ from .lora import LoRaLayer
8
+
9
+
10
+ def get_module_by_name(model, name):
11
+ parts = name.split(".")
12
+ module = model
13
+ for part in parts:
14
+ if part.isdigit():
15
+ module = module[int(part)]
16
+ else:
17
+ module = getattr(module, part)
18
+ return module
19
+
20
+
21
+ def set_module_by_name(model, name, new_module):
22
+ parts = name.split(".")
23
+ module = model
24
+ for part in parts[:-1]:
25
+ if part.isdigit():
26
+ module = module[int(part)]
27
+ else:
28
+ module = getattr(module, part)
29
+ if parts[-1].isdigit():
30
+ module[int(parts[-1])] = new_module
31
+ else:
32
+ setattr(module, parts[-1], new_module)
33
+
34
+
35
+ def get_peft_model(
36
+ model, linear_layers, rank=10, alpha=0.1, dropout=0.1, freeze=True, verbose=True
37
+ ):
38
+ if freeze:
39
+ freeze_model(model)
40
+
41
+ for name, module in model.language_model.named_modules():
42
+ if isinstance(module, nn.Linear) or isinstance(module, nn.QuantizedLinear):
43
+ if name.split(".")[-1] in linear_layers:
44
+ lora_layer = LoRaLayer(module, rank, alpha, dropout)
45
+ set_module_by_name(model.language_model, name, lora_layer)
46
+
47
+ model.config.lora = {}
48
+ model.config.lora["rank"] = rank
49
+ model.config.lora["alpha"] = alpha
50
+ model.config.lora["dropout"] = dropout
51
+
52
+ if verbose:
53
+ print_trainable_parameters(model.language_model)
54
+
55
+ return model
56
+
57
+
58
+ def freeze_model(model):
59
+ for name, module in model.named_modules():
60
+ name = name.split(".")[0]
61
+ if name in [
62
+ "language_model",
63
+ "vision_model",
64
+ "vision_tower",
65
+ "aligner",
66
+ "connector",
67
+ "multi_modal_projector",
68
+ "mm_projector",
69
+ ]:
70
+ model[f"{name}"].freeze()
71
+
72
+
73
+ def find_all_linear_names(model):
74
+ cls = nn.Linear
75
+ quantized_cls = nn.QuantizedLinear
76
+ lora_module_names = set()
77
+ multimodal_keywords = [
78
+ "mm_projector",
79
+ "vision_tower",
80
+ "vision_resampler",
81
+ "aligner",
82
+ ]
83
+ for name, module in model.named_modules():
84
+ if any(mm_keyword in name for mm_keyword in multimodal_keywords):
85
+ continue
86
+ if isinstance(module, cls) or isinstance(module, quantized_cls):
87
+ names = name.split(".")
88
+ lora_module_names.add(names[0] if len(names) == 1 else names[-1])
89
+
90
+ if "lm_head" in lora_module_names: # needed for 16-bit
91
+ lora_module_names.remove("lm_head")
92
+ return list(lora_module_names)
93
+
94
+
95
+ def count_parameters(model):
96
+ def nparams(m):
97
+ if isinstance(m, (nn.QuantizedLinear, nn.QuantizedEmbedding)):
98
+ return m.weight.size * (32 // m.bits)
99
+ return sum(v.size for _, v in tree_flatten(m.parameters()))
100
+
101
+ leaf_modules = tree_flatten(
102
+ model.leaf_modules(), is_leaf=lambda m: isinstance(m, nn.Module)
103
+ )
104
+ total_p = sum(nparams(m) for _, m in leaf_modules) / 10**6
105
+
106
+ return total_p
107
+
108
+
109
+ def print_trainable_parameters(model):
110
+ def nparams(m):
111
+ if isinstance(m, (nn.QuantizedLinear, nn.QuantizedEmbedding)):
112
+ return m.weight.size * (32 // m.bits)
113
+ return sum(v.size for _, v in tree_flatten(m.parameters()))
114
+
115
+ leaf_modules = tree_flatten(
116
+ model.leaf_modules(), is_leaf=lambda m: isinstance(m, nn.Module)
117
+ )
118
+ total_p = sum(nparams(m) for _, m in leaf_modules) / 10**6
119
+ trainable_p = (
120
+ sum(v.size for _, v in tree_flatten(model.trainable_parameters())) / 10**6
121
+ )
122
+
123
+ print(
124
+ f"#trainable params: {trainable_p} M || all params: {total_p} M || trainable%: {(trainable_p * 100 / total_p):.3f}%"
125
+ )
126
+
127
+
128
+ def apply_lora_layers(model: nn.Module, adapter_path: str) -> nn.Module:
129
+ """
130
+ Apply LoRA layers to the model.
131
+
132
+ Args:
133
+ model (nn.Module): The neural network model.
134
+ adapter_path (str): Path to the adapter configuration file.
135
+
136
+ Returns:
137
+ nn.Module: The updated model with LoRA layers applied.
138
+ """
139
+ adapter_path = Path(adapter_path)
140
+
141
+ if not adapter_path.exists():
142
+ raise FileNotFoundError(f"The adapter path does not exist: {adapter_path}")
143
+
144
+ # Check if the adapter has lora params in the config (adapter_config.json)
145
+ with open(adapter_path / "adapter_config.json", "r") as f:
146
+ config = json.load(f)
147
+ if "rank" not in config:
148
+ raise ValueError("The adapter does not have lora params in the config")
149
+
150
+ # TODO: add lora params to the config and load them here
151
+ list_of_modules = find_all_linear_names(model.language_model.model)
152
+ if config is not None:
153
+ model = get_peft_model(model, list_of_modules, **config)
154
+ else:
155
+ model = get_peft_model(model, list_of_modules)
156
+
157
+ # TODO: Use custom adapter name
158
+ model.load_weights(str(adapter_path / "adapters.safetensors"), strict=False)
159
+
160
+ return model