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,213 +0,0 @@
1
- import glob
2
- import inspect
3
- import json
4
- from dataclasses import dataclass
5
- from pathlib import Path
6
- from typing import List, Optional
7
-
8
- import mlx.core as mx
9
- import mlx.nn as nn
10
- import numpy as np
11
- from huggingface_hub import snapshot_download
12
-
13
- from .language import LanguageModel, RMSNorm, TextConfig
14
- from .vision import VisionConfig, VisionModel
15
-
16
-
17
- @dataclass
18
- class ModelConfig:
19
- text_config: TextConfig
20
- vision_config: VisionConfig
21
- model_type: str
22
- vocab_size: int = 257152
23
- ignore_index: int = -100
24
- image_token_index: int = 262144
25
- hidden_size: int = 2048
26
- pad_token_id: int = 0
27
- eos_token_id: Optional[List[int]] = None
28
-
29
- @classmethod
30
- def from_dict(cls, params):
31
- return cls(
32
- **{
33
- k: v
34
- for k, v in params.items()
35
- if k in inspect.signature(cls).parameters
36
- }
37
- )
38
-
39
-
40
- class Gemma3MultiModalProjector(nn.Module):
41
- def __init__(self, config: ModelConfig):
42
- super().__init__()
43
- self.mm_input_projection_weight = mx.ones(
44
- (config.vision_config.hidden_size, config.text_config.hidden_size)
45
- )
46
-
47
- self.mm_soft_emb_norm = RMSNorm(
48
- config.vision_config.hidden_size, eps=config.vision_config.layer_norm_eps
49
- )
50
- self.patches_per_image = int(
51
- config.vision_config.image_size // config.vision_config.patch_size
52
- )
53
- self.tokens_per_side = int(config.text_config.mm_tokens_per_image**0.5)
54
- self.kernel_size = self.patches_per_image // self.tokens_per_side
55
- self.avg_pool = nn.AvgPool2d(
56
- kernel_size=self.kernel_size, stride=self.kernel_size
57
- )
58
-
59
- def __call__(self, x: mx.array) -> mx.array:
60
- b, _, l = x.shape
61
-
62
- reshaped_vision_outputs = x.transpose(0, 2, 1)
63
- reshaped_vision_outputs = reshaped_vision_outputs.reshape(
64
- b, l, self.patches_per_image, self.patches_per_image
65
- )
66
-
67
- # Transpose to place h, w in indices 1, 2
68
- reshaped_vision_outputs = reshaped_vision_outputs.transpose(0, 2, 3, 1)
69
- pooled_vision_outputs = self.avg_pool(reshaped_vision_outputs)
70
- pooled_vision_outputs = pooled_vision_outputs.transpose(0, 3, 1, 2).flatten(2)
71
- pooled_vision_outputs = pooled_vision_outputs.transpose(0, 2, 1)
72
-
73
- normed_vision_outputs = self.mm_soft_emb_norm(pooled_vision_outputs)
74
-
75
- projected_vision_outputs = mx.einsum(
76
- "btm,md->btd", normed_vision_outputs, self.mm_input_projection_weight
77
- )
78
- return projected_vision_outputs.astype(x.dtype)
79
-
80
-
81
- def masked_scatter(
82
- final_embedding: mx.array,
83
- image_mask_expanded: mx.array,
84
- scaled_image_features: mx.array,
85
- ):
86
- # Reshape the tensors to 1D
87
- final_embedding_shape = final_embedding.shape
88
- scaled_image_features_flattened = mx.flatten(scaled_image_features)
89
- final_embedding_flattened = mx.flatten(final_embedding)
90
- image_mask_expanded_flattened = mx.flatten(image_mask_expanded)
91
-
92
- # Scatter the scaled image features into the special image token positions
93
- image_positions = mx.array(np.where(image_mask_expanded_flattened)[0], mx.uint32)
94
- final_embedding_flattened[image_positions] = scaled_image_features_flattened
95
-
96
- # Reshape back to the original shape
97
- final_embedding = mx.reshape(final_embedding_flattened, final_embedding_shape)
98
-
99
- return final_embedding
100
-
101
-
102
- class Model(nn.Module):
103
- def __init__(self, config: ModelConfig):
104
- super().__init__()
105
- self.model_type = config.model_type
106
- self.config = config
107
-
108
- self.vision_tower = VisionModel(config.vision_config)
109
- self.language_model = LanguageModel(config.text_config)
110
- self.multi_modal_projector = Gemma3MultiModalProjector(config)
111
-
112
- def get_input_embeddings(
113
- self,
114
- input_ids: Optional[mx.array] = None,
115
- pixel_values: Optional[mx.array] = None,
116
- mask: Optional[mx.array] = None,
117
- ):
118
- if pixel_values is None:
119
- return self.language_model.model.embed_tokens(input_ids), None
120
-
121
- inputs_embeds = self.language_model.model.embed_tokens(input_ids)
122
-
123
- hidden_state, _, _ = self.vision_tower(
124
- pixel_values.transpose(0, 2, 3, 1).astype(inputs_embeds.dtype),
125
- output_hidden_states=True,
126
- )
127
-
128
- image_features = self.multi_modal_projector(hidden_state)
129
-
130
- final_inputs_embeds, final_attention_mask_4d = (
131
- self.prepare_inputs_for_multimodal(
132
- self.config.hidden_size,
133
- self.config.pad_token_id,
134
- self.config.image_token_index,
135
- image_features,
136
- inputs_embeds,
137
- input_ids,
138
- mask,
139
- )
140
- )
141
- return final_inputs_embeds, final_attention_mask_4d
142
-
143
- @staticmethod
144
- def prepare_inputs_for_multimodal(
145
- hidden_size,
146
- pad_token_id,
147
- image_token_index,
148
- image_features,
149
- inputs_embeds,
150
- input_ids,
151
- attention_mask,
152
- ):
153
- _, _, embed_dim = image_features.shape
154
-
155
- batch_size, sequence_length = input_ids.shape
156
- scaled_image_features = image_features / (hidden_size**0.5)
157
- final_embedding = mx.zeros((batch_size, sequence_length, embed_dim))
158
-
159
- pad_token_id = pad_token_id
160
- pad_token_id = pad_token_id if pad_token_id is not None else 0
161
- text_mask = (input_ids != image_token_index) & (input_ids != pad_token_id)
162
- image_mask = input_ids == image_token_index
163
- pad_mask = input_ids == pad_token_id
164
-
165
- # expand masks to match embedding dimension
166
- text_mask_expanded = mx.expand_dims(text_mask, -1)
167
- text_mask_expanded = mx.repeat(text_mask_expanded, embed_dim, axis=-1)
168
- pad_mask_expanded = mx.expand_dims(pad_mask, -1)
169
- pad_mask_expanded = mx.repeat(pad_mask_expanded, embed_dim, axis=-1)
170
- image_mask_expanded = mx.expand_dims(image_mask, -1)
171
- image_mask_expanded = mx.repeat(image_mask_expanded, embed_dim, axis=-1)
172
-
173
- # insert padding and text token embeddings
174
- final_embedding = mx.where(text_mask_expanded, inputs_embeds, final_embedding)
175
- final_embedding = mx.where(
176
- pad_mask_expanded, mx.zeros_like(final_embedding), final_embedding
177
- )
178
-
179
- # insert image token embeddings
180
- final_embedding = masked_scatter(
181
- final_embedding, image_mask_expanded, scaled_image_features
182
- )
183
-
184
- attention_mask_expanded_1 = mx.expand_dims(attention_mask, 1)
185
- attention_mask_expanded_2 = mx.expand_dims(attention_mask, 2)
186
- final_attention_mask_4d = attention_mask_expanded_1 * attention_mask_expanded_2
187
- final_attention_mask_4d = final_attention_mask_4d
188
- final_attention_mask_4d = mx.expand_dims(final_attention_mask_4d, 1)
189
- final_embedding = mx.array(final_embedding)
190
- return final_embedding.astype(inputs_embeds.dtype), final_attention_mask_4d
191
-
192
- @property
193
- def layers(self):
194
- return self.language_model.model.layers
195
-
196
- def __call__(
197
- self,
198
- input_ids: mx.array,
199
- pixel_values: mx.array,
200
- mask: Optional[mx.array] = None,
201
- cache: Optional[mx.array] = None,
202
- **kwargs,
203
- ):
204
- input_embeddings, final_attention_mask_4d = self.get_input_embeddings(
205
- input_ids, pixel_values, mask
206
- )
207
-
208
- logits = self.language_model(
209
- inputs=input_ids,
210
- cache=cache,
211
- inputs_embeds=input_embeddings,
212
- )
213
- return logits
@@ -1,315 +0,0 @@
1
- import inspect
2
- from dataclasses import dataclass
3
- from functools import partial
4
- from typing import Any, Dict, List, Optional, Union
5
-
6
- import mlx.core as mx
7
- import mlx.nn as nn
8
-
9
- from ..base import (
10
- LanguageModelOutput,
11
- create_attention_mask,
12
- scaled_dot_product_attention,
13
- )
14
- from ..cache import KVCache, RotatingKVCache
15
-
16
-
17
- @dataclass
18
- class TextConfig:
19
- model_type: str
20
- hidden_size: int
21
- num_hidden_layers: int
22
- intermediate_size: int
23
- num_attention_heads: int = 8
24
- head_dim: int = 256
25
- rms_norm_eps: float = 1.0e-6
26
- vocab_size: int = 262208
27
- num_key_value_heads: int = 4
28
- rope_global_base_freq: float = 1_000_000.0
29
- rope_local_base_freq: float = 10_000.0
30
- rope_traditional: bool = False
31
- query_pre_attn_scalar: float = 256
32
- sliding_window: int = 1024
33
- rope_scaling: Optional[Dict[str, Union[float, List[float]]]] = None
34
- mm_tokens_per_image: int = 256
35
- sliding_window_pattern: int = 6
36
- max_position_embeddings: int = 4096
37
-
38
- @classmethod
39
- def from_dict(cls, params):
40
- return cls(
41
- **{
42
- k: v
43
- for k, v in params.items()
44
- if k in inspect.signature(cls).parameters
45
- }
46
- )
47
-
48
-
49
- class RMSNorm(nn.Module):
50
- def __init__(self, dims: int, eps: float = 1e-5):
51
- super().__init__()
52
- self.weight = mx.ones((dims,))
53
- self.eps = eps
54
-
55
- def __call__(self, x):
56
- return mx.fast.rms_norm(x, 1.0 + self.weight, self.eps)
57
-
58
-
59
- class Attention(nn.Module):
60
- def __init__(self, config: TextConfig, layer_idx: int):
61
- super().__init__()
62
-
63
- dim = config.hidden_size
64
- self.n_heads = n_heads = config.num_attention_heads
65
- self.n_kv_heads = n_kv_heads = config.num_key_value_heads
66
- self.repeats = n_heads // n_kv_heads
67
- self.head_dim = head_dim = config.head_dim
68
- self.layer_idx = layer_idx
69
-
70
- self.scale = config.query_pre_attn_scalar**-0.5
71
-
72
- self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=False)
73
- self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False)
74
- self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=False)
75
- self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=False)
76
-
77
- self.q_norm = RMSNorm(dims=head_dim, eps=config.rms_norm_eps)
78
- self.k_norm = RMSNorm(dims=head_dim, eps=config.rms_norm_eps)
79
- self.is_sliding = (layer_idx + 1) % config.sliding_window_pattern != 0
80
-
81
- self.rope = nn.RoPE(
82
- head_dim,
83
- traditional=config.rope_traditional,
84
- base=(
85
- config.rope_local_base_freq
86
- if self.is_sliding
87
- else config.rope_global_base_freq
88
- ),
89
- )
90
-
91
- def __call__(
92
- self,
93
- x: mx.array,
94
- mask: Optional[mx.array] = None,
95
- cache: Optional[Any] = None,
96
- ) -> mx.array:
97
- B, L, _ = x.shape
98
- queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
99
- queries = queries.reshape(B, L, self.n_heads, -1).transpose(0, 2, 1, 3)
100
-
101
- keys = keys.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
102
- values = values.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
103
-
104
- queries = self.q_norm(queries)
105
- keys = self.k_norm(keys)
106
-
107
- if cache is not None:
108
- queries = self.rope(queries, offset=cache.offset)
109
- keys = self.rope(keys, offset=cache.offset)
110
- keys, values = cache.update_and_fetch(keys, values)
111
- else:
112
- queries = self.rope(queries)
113
- keys = self.rope(keys)
114
-
115
- # Sliding window
116
- if mask is not None and isinstance(mask, mx.array):
117
- if mask.shape[-1] != keys.shape[-2]:
118
- mask = mask[..., -keys.shape[-2] :]
119
-
120
- output = scaled_dot_product_attention(
121
- queries, keys, values, cache, scale=self.scale, mask=mask
122
- )
123
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
124
- return self.o_proj(output)
125
-
126
-
127
- class MLP(nn.Module):
128
- def __init__(self, dim, hidden_dim):
129
- super().__init__()
130
- self.gate_proj = nn.Linear(dim, hidden_dim, bias=False)
131
- self.down_proj = nn.Linear(hidden_dim, dim, bias=False)
132
- self.up_proj = nn.Linear(dim, hidden_dim, bias=False)
133
-
134
- def __call__(self, x) -> mx.array:
135
- # This should not be GELU approx, jax.nn.gelu
136
- return self.down_proj(nn.gelu_approx(self.gate_proj(x)) * self.up_proj(x))
137
-
138
-
139
- @partial(mx.compile, shapeless=True)
140
- def clip_residual(x, y=None):
141
- bound = mx.finfo(mx.float16).max
142
- if y is None:
143
- if x.dtype == mx.float16:
144
- return mx.clip(x.astype(mx.float32), -bound, bound).astype(mx.float16)
145
- else:
146
- return x
147
-
148
- if x.dtype != mx.float16:
149
- return x + y
150
-
151
- return mx.clip(x.astype(mx.float32) + y.astype(mx.float32), -bound, bound).astype(
152
- mx.float16
153
- )
154
-
155
-
156
- class TransformerBlock(nn.Module):
157
- def __init__(self, config: TextConfig, layer_idx: int):
158
- super().__init__()
159
- self.num_attention_heads = config.num_attention_heads
160
- self.hidden_size = config.hidden_size
161
- self.self_attn = Attention(config, layer_idx)
162
- self.mlp = MLP(config.hidden_size, config.intermediate_size)
163
- self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
164
- self.post_attention_layernorm = RMSNorm(
165
- config.hidden_size, eps=config.rms_norm_eps
166
- )
167
- self.pre_feedforward_layernorm = RMSNorm(
168
- config.hidden_size, eps=config.rms_norm_eps
169
- )
170
- self.post_feedforward_layernorm = RMSNorm(
171
- config.hidden_size, eps=config.rms_norm_eps
172
- )
173
-
174
- def __call__(
175
- self,
176
- x: mx.array,
177
- mask: Optional[mx.array] = None,
178
- cache: Optional[Any] = None,
179
- ) -> mx.array:
180
-
181
- # Clip the input to avoid overflow in float16
182
- # Float16 has a max value of 65504. When values exceed this limit, they become inf.
183
- # Example: If x contains 70000.0 in float16, it becomes inf, causing gradient issues.
184
- # We upcast to float32 for operations that might exceed the limit, then clip and
185
- # convert back to float16 to maintain numerical stability.
186
-
187
- # Clip input to avoid overflow in float16
188
- x = clip_residual(x)
189
-
190
- # Self-attention block
191
- r = self.self_attn(self.input_layernorm(x), mask, cache)
192
- h = self.post_attention_layernorm(r)
193
-
194
- # Add residual connection with overflow protection for float16
195
- h = clip_residual(x + h)
196
-
197
- # MLP block
198
- r = self.mlp(self.pre_feedforward_layernorm(h))
199
- out = self.post_feedforward_layernorm(r)
200
-
201
- # Add residual connection with overflow protection for float16
202
- out = clip_residual(h + out)
203
-
204
- return out
205
-
206
-
207
- class Gemma3Model(nn.Module):
208
- def __init__(self, config: TextConfig):
209
- super().__init__()
210
- self.config = config
211
- self.vocab_size = config.vocab_size
212
- self.num_hidden_layers = config.num_hidden_layers
213
- assert self.vocab_size > 0
214
- self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
215
- self.layers = [
216
- TransformerBlock(config=config, layer_idx=layer_idx)
217
- for layer_idx in range(config.num_hidden_layers)
218
- ]
219
- self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
220
-
221
- def __call__(
222
- self,
223
- inputs: mx.array,
224
- inputs_embeds: mx.array = None,
225
- mask: mx.array = None,
226
- cache=None,
227
- ):
228
- if inputs_embeds is None:
229
- h = self.embed_tokens(inputs)
230
- else:
231
- h = inputs_embeds
232
-
233
- h *= mx.array(self.config.hidden_size**0.5, mx.bfloat16).astype(h.dtype)
234
-
235
- if cache is None:
236
- cache = [None] * len(self.layers)
237
-
238
- if mask is None:
239
- j = self.config.sliding_window_pattern
240
- full_mask = create_attention_mask(h, cache[j - 1 : j])
241
- sliding_window_mask = create_attention_mask(h, cache)
242
-
243
- for i, (layer, c) in enumerate(zip(self.layers, cache)):
244
- is_global = (
245
- i % self.config.sliding_window_pattern
246
- == self.config.sliding_window_pattern - 1
247
- )
248
-
249
- local_mask = mask
250
- if mask is None and is_global:
251
- local_mask = full_mask
252
- elif mask is None:
253
- local_mask = sliding_window_mask
254
-
255
- h = layer(h, local_mask, c)
256
-
257
- return self.norm(h)
258
-
259
-
260
- class LanguageModel(nn.Module):
261
- def __init__(self, config: TextConfig):
262
- super().__init__()
263
- self.config = config
264
- self.model_type = config.model_type
265
- self.model = Gemma3Model(config)
266
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
267
-
268
- def __call__(
269
- self,
270
- inputs: mx.array,
271
- inputs_embeds: Optional[mx.array] = None,
272
- mask: Optional[mx.array] = None,
273
- cache=None,
274
- ):
275
- out = self.model(inputs, inputs_embeds=inputs_embeds, mask=mask, cache=cache)
276
- out = self.lm_head(out)
277
- return LanguageModelOutput(logits=out)
278
-
279
- def sanitize(self, weights):
280
- if "lm_head.weight" not in weights:
281
- weights["language_model.lm_head.weight"] = weights[
282
- "language_model.model.embed_tokens.weight"
283
- ]
284
- return {
285
- k: v for k, v in weights.items() if "self_attn.rotary_emb.inv_freq" not in k
286
- }
287
-
288
- @property
289
- def layers(self):
290
- return self.model.layers
291
-
292
- @property
293
- def head_dim(self):
294
- return self.config.head_dim
295
-
296
- @property
297
- def n_kv_heads(self):
298
- return self.config.num_key_value_heads
299
-
300
- def make_cache(self):
301
- caches = []
302
- for i in range(self.config.num_hidden_layers):
303
- if (
304
- i % self.config.sliding_window_pattern
305
- == self.config.sliding_window_pattern - 1
306
- ):
307
- caches.append(KVCache())
308
- else:
309
- caches.append(
310
- RotatingKVCache(
311
- max_size=self.config.sliding_window,
312
- keep=0,
313
- )
314
- )
315
- return caches