nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc9__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 (200) 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/mlx_backend/vlm/generate_qwen3_vl.py +14 -31
  5. nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +15 -32
  6. nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/qwen3vl.py +7 -23
  7. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +8 -24
  8. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/METADATA +1 -1
  9. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/RECORD +11 -200
  10. nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
  11. nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
  12. nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
  13. nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
  14. nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
  15. nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
  16. nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
  17. nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
  18. nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
  19. nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
  20. nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
  21. nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
  22. nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
  23. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
  24. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
  25. nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
  26. nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
  27. nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
  28. nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
  29. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
  30. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
  31. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
  32. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
  33. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
  34. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
  35. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
  36. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
  37. nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
  38. nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
  39. nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
  40. nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
  41. nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
  42. nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
  43. nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
  44. nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
  45. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
  46. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
  47. nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
  48. nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
  49. nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
  50. nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
  51. nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
  52. nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
  53. nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
  54. nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
  55. nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
  56. nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
  57. nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
  58. nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
  59. nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
  60. nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
  61. nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
  62. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
  63. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
  64. nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
  65. nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
  66. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
  67. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
  68. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
  69. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
  70. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
  71. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
  72. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
  73. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
  74. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
  75. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
  76. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
  77. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
  78. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
  79. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
  80. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
  81. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
  82. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
  83. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
  84. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
  85. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
  86. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
  87. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
  88. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
  89. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
  90. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
  91. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
  92. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
  93. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
  94. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
  95. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
  96. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
  97. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
  98. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
  99. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
  100. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
  101. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
  102. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
  103. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
  104. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
  105. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
  106. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
  107. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
  108. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
  109. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
  110. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
  111. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
  112. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
  113. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
  114. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
  115. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
  116. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
  117. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
  118. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
  119. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
  120. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
  121. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
  122. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
  123. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
  124. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
  125. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
  126. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
  127. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
  128. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
  129. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
  130. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
  131. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
  132. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
  133. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
  134. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
  135. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
  136. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
  137. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
  138. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
  139. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
  140. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
  141. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
  142. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
  143. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
  144. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
  145. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
  146. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
  147. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
  148. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
  149. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
  150. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
  151. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
  152. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
  153. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
  154. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
  155. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
  156. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
  157. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
  158. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
  159. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
  160. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
  161. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
  162. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
  163. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
  164. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
  165. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
  166. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
  167. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
  168. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
  169. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
  170. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
  171. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
  172. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
  173. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
  174. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
  175. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
  176. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
  177. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  178. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
  179. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
  180. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
  181. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
  182. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
  183. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
  184. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
  185. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
  186. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
  187. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
  188. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
  189. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
  190. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
  191. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
  192. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
  193. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
  194. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
  195. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
  196. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
  197. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
  198. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
  199. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/WHEEL +0 -0
  200. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/top_level.txt +0 -0
@@ -1,236 +0,0 @@
1
- import inspect
2
- from dataclasses import dataclass
3
- from typing import Dict, Optional, Tuple, Union
4
-
5
- import mlx.core as mx
6
- import mlx.nn as nn
7
-
8
- from ..base import (
9
- LanguageModelOutput,
10
- create_attention_mask,
11
- scaled_dot_product_attention,
12
- )
13
- from ..cache import KVCache
14
-
15
-
16
- @dataclass
17
- class TextConfig:
18
- model_type: str
19
- hidden_size: int
20
- num_hidden_layers: int
21
- intermediate_size: int
22
- num_attention_heads: int
23
- rms_norm_eps: float
24
- vocab_size: int
25
- attention_bias: bool = True
26
- num_key_value_heads: int = None
27
- rope_theta: float = 1000000
28
- rope_traditional: bool = False
29
- rope_scaling: Optional[Dict[str, Union[float, str]]] = None
30
- max_position_embeddings: int = 4096
31
- tie_word_embeddings: bool = True
32
-
33
- @classmethod
34
- def from_dict(cls, params):
35
- return cls(
36
- **{
37
- k: v
38
- for k, v in params.items()
39
- if k in inspect.signature(cls).parameters
40
- }
41
- )
42
-
43
- def __post_init__(self):
44
- if self.num_key_value_heads is None:
45
- self.num_key_value_heads = self.num_attention_heads
46
-
47
- if self.rope_scaling:
48
- required_keys = {"factor", "type"}
49
- if not all(key in self.rope_scaling for key in required_keys):
50
- raise ValueError(f"rope_scaling must contain keys {required_keys}")
51
-
52
- if self.rope_scaling["type"] != "linear":
53
- raise ValueError("rope_scaling 'type' currently only supports 'linear'")
54
-
55
-
56
- class Attention(nn.Module):
57
- def __init__(self, config: TextConfig):
58
- super().__init__()
59
-
60
- dim = config.hidden_size
61
- self.n_heads = n_heads = config.num_attention_heads
62
- self.n_kv_heads = n_kv_heads = config.num_key_value_heads
63
-
64
- head_dim = config.hidden_size // n_heads
65
- self.scale = head_dim**-0.5
66
-
67
- if hasattr(config, "attention_bias"):
68
- attention_bias = config.attention_bias
69
- else:
70
- attention_bias = False
71
-
72
- self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=attention_bias)
73
- self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
74
- self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
75
- self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=False)
76
-
77
- rope_scale = (
78
- 1 / config.rope_scaling["factor"]
79
- if config.rope_scaling is not None
80
- and config.rope_scaling["type"] == "linear"
81
- else 1
82
- )
83
- self.rope = nn.RoPE(
84
- head_dim,
85
- traditional=config.rope_traditional,
86
- base=config.rope_theta,
87
- scale=rope_scale,
88
- )
89
-
90
- def __call__(
91
- self,
92
- x: mx.array,
93
- mask: Optional[mx.array] = None,
94
- cache: Optional[Tuple[mx.array, mx.array]] = None,
95
- ) -> mx.array:
96
- B, L, D = x.shape
97
-
98
- queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
99
-
100
- # Prepare the queries, keys and values for the attention computation
101
- queries = queries.reshape(B, L, self.n_heads, -1).transpose(0, 2, 1, 3)
102
- keys = keys.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
103
- values = values.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
104
-
105
- if cache is not None:
106
- queries = self.rope(queries, offset=cache.offset)
107
- keys = self.rope(keys, offset=cache.offset)
108
- keys, values = cache.update_and_fetch(keys, values)
109
- else:
110
- queries = self.rope(queries)
111
- keys = self.rope(keys)
112
-
113
- output = scaled_dot_product_attention(
114
- queries, keys, values, cache, scale=self.scale, mask=mask
115
- )
116
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
117
- return self.o_proj(output)
118
-
119
-
120
- class MLP(nn.Module):
121
- def __init__(self, dim, hidden_dim):
122
- super().__init__()
123
- self.gate_proj = nn.Linear(dim, hidden_dim, bias=False)
124
- self.down_proj = nn.Linear(hidden_dim, dim, bias=False)
125
- self.up_proj = nn.Linear(dim, hidden_dim, bias=False)
126
-
127
- def __call__(self, x) -> mx.array:
128
- return self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x))
129
-
130
-
131
- class TransformerBlock(nn.Module):
132
- def __init__(self, config: TextConfig):
133
- super().__init__()
134
- self.num_attention_heads = config.num_attention_heads
135
- self.hidden_size = config.hidden_size
136
- self.self_attn = Attention(config)
137
- self.mlp = MLP(config.hidden_size, config.intermediate_size)
138
- self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
139
- self.post_attention_layernorm = nn.RMSNorm(
140
- config.hidden_size, eps=config.rms_norm_eps
141
- )
142
- self.config = config
143
-
144
- def __call__(
145
- self,
146
- x: mx.array,
147
- mask: Optional[mx.array] = None,
148
- cache=None,
149
- ) -> mx.array:
150
- r = self.self_attn(self.input_layernorm(x), mask, cache)
151
- h = x + r
152
- r = self.mlp(self.post_attention_layernorm(h))
153
- out = h + r
154
- return out
155
-
156
-
157
- class Qwen2Model(nn.Module):
158
- def __init__(self, config: TextConfig):
159
- super().__init__()
160
- self.config = config
161
- self.vocab_size = config.vocab_size
162
- self.num_hidden_layers = config.num_hidden_layers
163
- assert self.vocab_size > 0
164
- self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
165
- self.layers = [
166
- TransformerBlock(config=config) for _ in range(config.num_hidden_layers)
167
- ]
168
- self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
169
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
170
-
171
- def __call__(
172
- self,
173
- inputs: mx.array,
174
- inputs_embeds: Optional[mx.array] = None,
175
- mask: Optional[mx.array] = None,
176
- cache=None,
177
- ):
178
- # for passing merged input embeddings
179
- if inputs_embeds is None:
180
- h = self.embed_tokens(inputs)
181
- else:
182
- h = inputs_embeds
183
-
184
- if cache is None:
185
- cache = [None] * len(self.layers)
186
-
187
- if mask is None:
188
- mask = create_attention_mask(h, cache)
189
-
190
- for layer, c in zip(self.layers, cache):
191
- h = layer(h, mask, c)
192
-
193
- return self.lm_head(self.norm(h))
194
-
195
-
196
- class LanguageModel(nn.Module):
197
- def __init__(self, config: TextConfig):
198
- super().__init__()
199
- self.config = config
200
- self.model_type = config.model_type
201
- self.model = Qwen2Model(config)
202
-
203
- def __call__(
204
- self,
205
- inputs: mx.array,
206
- inputs_embeds: Optional[mx.array] = None,
207
- mask: Optional[mx.array] = None,
208
- cache=None,
209
- ):
210
- out = self.model(inputs, mask=mask, cache=cache, inputs_embeds=inputs_embeds)
211
- return LanguageModelOutput(logits=out)
212
-
213
- def sanitize(self, weights):
214
- if (
215
- self.config.tie_word_embeddings
216
- and "language_model.model.lm_head.weight" not in weights
217
- ):
218
- weights["language_model.model.lm_head.weight"] = weights[
219
- "language_model.model.embed_tokens.weight"
220
- ]
221
-
222
- return {
223
- k: v for k, v in weights.items() if "self_attn.rotary_emb.inv_freq" not in k
224
- }
225
-
226
- @property
227
- def layers(self):
228
- return self.model.layers
229
-
230
- @property
231
- def head_dim(self):
232
- return self.config.hidden_size // self.config.num_attention_heads
233
-
234
- @property
235
- def n_kv_heads(self):
236
- return self.config.num_key_value_heads
@@ -1,256 +0,0 @@
1
- import glob
2
- import inspect
3
- import json
4
- import re
5
- from dataclasses import dataclass
6
- from functools import partial, reduce
7
- from pathlib import Path
8
- from typing import Dict, List, Optional, Tuple
9
-
10
- import mlx.core as mx
11
- import mlx.nn as nn
12
- import numpy as np
13
- from huggingface_hub import snapshot_download
14
- from PIL import Image
15
- from transformers import AutoConfig
16
- from transformers.image_transforms import (
17
- convert_to_rgb,
18
- normalize,
19
- rescale,
20
- resize,
21
- to_channel_dimension_format,
22
- )
23
- from transformers.image_utils import to_numpy_array
24
-
25
- from ..base import BaseImageProcessor
26
- from .language import LanguageModel, TextConfig
27
- from .vision import VisionConfig, VisionModel
28
-
29
-
30
- @dataclass
31
- class ModelConfig:
32
- text_config: TextConfig
33
- vision_config: VisionConfig
34
- model_type: str
35
- auto_map: dict
36
- hidden_size: int
37
- mm_hidden_size: int
38
- mm_projector_type: str = "mlp2x_gelu"
39
- ignore_index: int = -100
40
- image_token_index: int = -200
41
- vocab_size: int = 151936
42
- eos_token_id: Optional[List[int]] = None
43
-
44
- @classmethod
45
- def from_dict(cls, params):
46
- if not params.get("text_config", {}):
47
- # Copy text config parameters from root level
48
- excluded_keys = {"vision_config"}
49
- params["text_config"] = dict(
50
- filter(lambda x: x[0] not in excluded_keys, params.items())
51
- )
52
- if not params.get("vision_config", {}).get("model_type", {}):
53
- # Set default model type
54
- params["vision_config"]["model_type"] = "siglip_vision_model"
55
-
56
- return cls(
57
- **{
58
- k: v
59
- for k, v in params.items()
60
- if k in inspect.signature(cls).parameters
61
- }
62
- )
63
-
64
-
65
- class ImageProcessor(BaseImageProcessor):
66
- def preprocess(self, images):
67
- if isinstance(images, Image.Image):
68
- images = [images]
69
- else:
70
- assert isinstance(images, list)
71
-
72
- transforms = [
73
- convert_to_rgb,
74
- to_numpy_array,
75
- partial(
76
- resize,
77
- size=self.size,
78
- resample=self.resample,
79
- data_format=self.data_format,
80
- ),
81
- partial(rescale, scale=self.rescale_factor, data_format=self.data_format),
82
- partial(
83
- normalize,
84
- mean=self.image_mean,
85
- std=self.image_std,
86
- data_format=self.data_format,
87
- ),
88
- partial(
89
- to_channel_dimension_format,
90
- channel_dim=self.data_format,
91
- input_channel_dim=self.data_format,
92
- ),
93
- ]
94
-
95
- images = reduce(lambda x, f: [*map(f, x)], transforms, images)
96
-
97
- return images
98
-
99
-
100
- class LlavaMultiModalProjector(nn.Module):
101
- def __init__(self, config: ModelConfig):
102
- super().__init__()
103
- self.linear_1 = nn.Linear(
104
- config.vision_config.hidden_size, config.text_config.hidden_size, bias=True
105
- )
106
- self.gelu = nn.GELU()
107
- self.linear_2 = nn.Linear(
108
- config.text_config.hidden_size, config.text_config.hidden_size, bias=True
109
- )
110
-
111
- def __call__(self, x: mx.array) -> mx.array:
112
- x = self.linear_1(x)
113
- x = self.gelu(x)
114
- x = self.linear_2(x)
115
- return x
116
-
117
-
118
- class SigLipVisionTower(nn.Module):
119
- def __init__(self, config: VisionConfig):
120
- super().__init__()
121
- self.vision_tower = VisionModel(config)
122
-
123
- def __call__(
124
- self, x: mx.array, output_hidden_states: Optional[bool] = None
125
- ) -> mx.array:
126
- return self.vision_tower(x, output_hidden_states)
127
-
128
-
129
- class Model(nn.Module):
130
- def __init__(self, config: ModelConfig):
131
- super().__init__()
132
- self.model_type = config.model_type
133
- self.config = config
134
-
135
- self.vision_tower = SigLipVisionTower(config.vision_config)
136
- self.language_model = LanguageModel(config.text_config)
137
- self.mm_projector = LlavaMultiModalProjector(config)
138
-
139
- def get_input_embeddings(
140
- self,
141
- input_ids: Optional[mx.array] = None,
142
- pixel_values: Optional[mx.array] = None,
143
- ):
144
- if pixel_values is None:
145
- return self.language_model.model.embed_tokens(input_ids)
146
-
147
- inputs_embeds = self.language_model.model.embed_tokens(input_ids)
148
-
149
- *_, hidden_state = self.vision_tower(
150
- pixel_values.transpose(0, 2, 3, 1), output_hidden_states=True
151
- )
152
-
153
- image_features = hidden_state[-1].astype(pixel_values.dtype)
154
- assert image_features.shape[-2] == 729
155
-
156
- image_features = self.mm_projector(image_features)
157
-
158
- final_inputs_embeds = self._prepare_inputs_for_multimodal(
159
- image_features, inputs_embeds, input_ids
160
- )
161
- return final_inputs_embeds
162
-
163
- def _prepare_inputs_for_multimodal(self, image_features, inputs_embeds, input_ids):
164
- image_token_index = self.config.image_token_index
165
- num_images, num_image_patches, embed_dim = image_features.shape
166
-
167
- batch_size, seq_length, embed_dim = inputs_embeds.shape
168
- num_images, num_image_patches, _ = image_features.shape
169
-
170
- # Positions of <image> tokens in input_ids for each batch
171
- image_positions = mx.argmax(input_ids == image_token_index, axis=1)
172
-
173
- final_embeddings = []
174
- for b in range(batch_size):
175
- text_segments = []
176
- start_idx = 0
177
- position = int(image_positions[b].item())
178
-
179
- text_segments.append(inputs_embeds[b : b + 1, start_idx:position])
180
- text_segments.append(image_features[b : b + 1])
181
- text_segments.append(inputs_embeds[b : b + 1, position + 1 :])
182
-
183
- batch_embeddings = mx.concatenate(text_segments, axis=1)
184
- final_embeddings.append(batch_embeddings)
185
-
186
- # Create a final embedding of shape
187
- # (batch_size, num_image_patches + sequence_len, embed_dim)
188
- return mx.concatenate(final_embeddings, axis=0)
189
-
190
- @property
191
- def layers(self):
192
- return self.language_model.model.layers
193
-
194
- def __call__(
195
- self,
196
- input_ids: mx.array,
197
- pixel_values: mx.array,
198
- mask: Optional[mx.array] = None,
199
- cache: Optional[Tuple[mx.array, mx.array]] = None,
200
- **kwargs,
201
- ):
202
- input_embeddings = self.get_input_embeddings(input_ids, pixel_values)
203
- logits = self.language_model(
204
- inputs=input_ids,
205
- cache=cache,
206
- inputs_embeds=input_embeddings,
207
- mask=None, # TODO: add mask
208
- )
209
- return logits
210
-
211
- def sanitize(self, weights):
212
- weights = {
213
- (
214
- f"{k.split('.', 1)[1]}"
215
- if re.match(r"^model\.vision_tower", k)
216
- else (
217
- f"mm_projector.linear_1.{k.split('.')[-1]}"
218
- if re.match(r"^model\.mm_projector\.0", k)
219
- else (
220
- f"mm_projector.linear_2.{k.split('.')[-1]}"
221
- if re.match(r"^model\.mm_projector\.2", k)
222
- else (
223
- f"language_model.model.{k}"
224
- if re.match(r"^lm_head", k)
225
- else (
226
- f"language_model.{k}"
227
- if re.match(r"^model\.(embed_tokens|norm|layers)", k)
228
- else k
229
- )
230
- )
231
- )
232
- )
233
- ): v
234
- for k, v in weights.items()
235
- }
236
-
237
- weights = {
238
- (
239
- f"vision_tower.vision_tower.vision_model.head.attention.in_proj.bias"
240
- if re.match(
241
- r"^vision_tower\.vision_tower\.vision_model\.head\.attention\.in_proj_bias",
242
- k,
243
- )
244
- else (
245
- f"vision_tower.vision_tower.vision_model.head.attention.in_proj.weight"
246
- if re.match(
247
- r"^vision_tower\.vision_tower\.vision_model\.head\.attention\.in_proj_weight",
248
- k,
249
- )
250
- else k
251
- )
252
- ): v
253
- for k, v in weights.items()
254
- }
255
-
256
- return weights