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,509 +0,0 @@
1
- import inspect
2
- import math
3
- import re
4
- from dataclasses import dataclass
5
- from functools import partial
6
- from typing import Any, Dict, Optional, Tuple
7
-
8
- import mlx.core as mx
9
- import mlx.nn as nn
10
- from mlx_lm.models.switch_layers import SwitchGLU
11
-
12
- from ..base import (
13
- LanguageModelOutput,
14
- create_attention_mask,
15
- scaled_dot_product_attention,
16
- )
17
- from ..cache import KVCache
18
-
19
-
20
- @dataclass
21
- class TextConfig:
22
- model_type: str = "deepseek_v3"
23
- vocab_size: int = 102400
24
- hidden_size: int = 4096
25
- intermediate_size: int = 11008
26
- moe_intermediate_size: int = 1407
27
- num_hidden_layers: int = 30
28
- num_attention_heads: int = 32
29
- num_key_value_heads: int = 32
30
- n_shared_experts: Optional[int] = None
31
- n_routed_experts: Optional[int] = None
32
- routed_scaling_factor: float = 1.0
33
- kv_lora_rank: int = 512
34
- q_lora_rank: int = 1536
35
- qk_rope_head_dim: int = 64
36
- v_head_dim: int = 128
37
- qk_nope_head_dim: int = 128
38
- topk_method: str = "noaux_tc"
39
- scoring_func: str = "sigmoid"
40
- norm_topk_prob: bool = True
41
- n_group: Optional[int] = None
42
- topk_group: Optional[int] = None
43
- num_experts_per_tok: Optional[int] = None
44
- moe_layer_freq: int = 1
45
- first_k_dense_replace: int = 0
46
- max_position_embeddings: int = 2048
47
- rms_norm_eps: float = 1e-6
48
- rope_theta: float = 10000.0
49
- rope_scaling: Dict = None
50
- attention_bias: bool = False
51
-
52
- @classmethod
53
- def from_dict(cls, params):
54
- return cls(
55
- **{
56
- k: v
57
- for k, v in params.items()
58
- if k in inspect.signature(cls).parameters
59
- }
60
- )
61
-
62
- def __post_init__(self):
63
- if self.num_key_value_heads is None:
64
- self.num_key_value_heads = self.num_attention_heads
65
-
66
-
67
- def yarn_find_correction_dim(
68
- num_rotations, dim, base=10000, max_position_embeddings=2048
69
- ):
70
- return (dim * math.log(max_position_embeddings / (num_rotations * 2 * math.pi))) / (
71
- 2 * math.log(base)
72
- )
73
-
74
-
75
- def yarn_find_correction_range(
76
- low_rot, high_rot, dim, base=10000, max_position_embeddings=2048
77
- ):
78
- low = math.floor(
79
- yarn_find_correction_dim(low_rot, dim, base, max_position_embeddings)
80
- )
81
- high = math.ceil(
82
- yarn_find_correction_dim(high_rot, dim, base, max_position_embeddings)
83
- )
84
- return max(low, 0), min(high, dim - 1)
85
-
86
-
87
- def yarn_get_mscale(scale=1, mscale=1):
88
- if scale <= 1:
89
- return 1.0
90
- return 0.1 * mscale * math.log(scale) + 1.0
91
-
92
-
93
- def yarn_linear_ramp_mask(min_val, max_val, dim):
94
- if min_val == max_val:
95
- max_val += 0.001 # Prevent singularity
96
-
97
- linear_func = (mx.arange(dim, dtype=mx.float32) - min_val) / (max_val - min_val)
98
- return mx.clip(linear_func, 0, 1)
99
-
100
-
101
- class DeepseekV3YarnRotaryEmbedding(nn.Module):
102
- def __init__(
103
- self,
104
- dim,
105
- max_position_embeddings=2048,
106
- base=10000,
107
- scaling_factor=1.0,
108
- original_max_position_embeddings=4096,
109
- beta_fast=32,
110
- beta_slow=1,
111
- mscale=1,
112
- mscale_all_dim=0,
113
- ):
114
- super().__init__()
115
- self.mscale = yarn_get_mscale(scaling_factor, mscale) / yarn_get_mscale(
116
- scaling_factor, mscale_all_dim
117
- )
118
- freq_extra = base ** (mx.arange(0, dim, 2, dtype=mx.float32) / dim)
119
- freq_inter = scaling_factor * freq_extra
120
- low, high = yarn_find_correction_range(
121
- beta_fast,
122
- beta_slow,
123
- dim,
124
- base,
125
- original_max_position_embeddings,
126
- )
127
- freq_mask = 1.0 - yarn_linear_ramp_mask(low, high, dim // 2)
128
- self._freqs = (freq_inter * freq_extra) / (
129
- freq_inter * freq_mask + freq_extra * (1 - freq_mask)
130
- )
131
-
132
- def __call__(self, x, offset=0):
133
- if self.mscale != 1.0:
134
- x = self.mscale * x
135
- return mx.fast.rope(
136
- x,
137
- x.shape[-1],
138
- traditional=True,
139
- base=None,
140
- scale=1.0,
141
- offset=offset,
142
- freqs=self._freqs,
143
- )
144
-
145
-
146
- # A clipped silu to prevent fp16 from overflowing
147
- @partial(mx.compile, shapeless=True)
148
- def clipped_silu(x):
149
- return mx.clip(x * mx.sigmoid(x), a_min=-100, a_max=100)
150
-
151
-
152
- class DeepseekV3Attention(nn.Module):
153
- def __init__(self, config: TextConfig):
154
- super().__init__()
155
- self.config = config
156
- self.hidden_size = config.hidden_size
157
- self.num_heads = config.num_attention_heads
158
- self.max_position_embeddings = config.max_position_embeddings
159
- self.rope_theta = config.rope_theta
160
- self.q_lora_rank = config.q_lora_rank
161
- self.qk_rope_head_dim = config.qk_rope_head_dim
162
- self.kv_lora_rank = config.kv_lora_rank
163
- self.v_head_dim = config.v_head_dim
164
- self.qk_nope_head_dim = config.qk_nope_head_dim
165
- self.q_head_dim = config.qk_nope_head_dim + config.qk_rope_head_dim
166
-
167
- self.scale = self.q_head_dim**-0.5
168
-
169
- if self.q_lora_rank is None:
170
- self.q_proj = nn.Linear(
171
- self.hidden_size, self.num_heads * self.q_head_dim, bias=False
172
- )
173
- else:
174
- self.q_a_proj = nn.Linear(
175
- self.hidden_size, self.q_lora_rank, bias=config.attention_bias
176
- )
177
- self.q_a_layernorm = nn.RMSNorm(self.q_lora_rank, eps=1e-6)
178
- self.q_b_proj = nn.Linear(
179
- self.q_lora_rank, self.num_heads * self.q_head_dim, bias=False
180
- )
181
-
182
- self.kv_a_proj_with_mqa = nn.Linear(
183
- self.hidden_size,
184
- self.kv_lora_rank + self.qk_rope_head_dim,
185
- bias=config.attention_bias,
186
- )
187
- self.kv_a_layernorm = nn.RMSNorm(self.kv_lora_rank, eps=1e-6)
188
- self.kv_b_proj = nn.Linear(
189
- self.kv_lora_rank,
190
- self.num_heads
191
- * (self.q_head_dim - self.qk_rope_head_dim + self.v_head_dim),
192
- bias=False,
193
- )
194
-
195
- self.o_proj = nn.Linear(
196
- self.num_heads * self.v_head_dim,
197
- self.hidden_size,
198
- bias=config.attention_bias,
199
- )
200
-
201
- if self.config.rope_scaling is not None:
202
- mscale_all_dim = self.config.rope_scaling.get("mscale_all_dim", 0)
203
- scaling_factor = self.config.rope_scaling["factor"]
204
- if mscale_all_dim:
205
- mscale = yarn_get_mscale(scaling_factor, mscale_all_dim)
206
- self.scale = self.scale * mscale * mscale
207
-
208
- rope_kwargs = {
209
- key: self.config.rope_scaling[key]
210
- for key in [
211
- "original_max_position_embeddings",
212
- "beta_fast",
213
- "beta_slow",
214
- "mscale",
215
- "mscale_all_dim",
216
- ]
217
- if key in self.config.rope_scaling
218
- }
219
- self.rope = DeepseekV3YarnRotaryEmbedding(
220
- dim=self.qk_rope_head_dim,
221
- max_position_embeddings=self.max_position_embeddings,
222
- scaling_factor=scaling_factor,
223
- base=self.rope_theta,
224
- **rope_kwargs,
225
- )
226
- else:
227
- self.rope = nn.RoPE(
228
- dims=self.qk_rope_head_dim,
229
- base=self.rope_theta,
230
- traditional=True,
231
- )
232
-
233
- def __call__(
234
- self,
235
- x: mx.array,
236
- mask: Optional[mx.array] = None,
237
- cache: Optional[Any] = None,
238
- ) -> mx.array:
239
- B, L, D = x.shape
240
-
241
- if self.q_lora_rank is None:
242
- q = self.q_proj(x)
243
- else:
244
- q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(x)))
245
-
246
- q = q.reshape(B, L, self.num_heads, self.q_head_dim).transpose(0, 2, 1, 3)
247
- q_nope, q_pe = mx.split(q, [self.qk_nope_head_dim], axis=-1)
248
- compressed_kv = self.kv_a_proj_with_mqa(x)
249
- compressed_kv, k_pe = mx.split(compressed_kv, [self.kv_lora_rank], axis=-1)
250
- k_pe = k_pe.reshape(B, L, 1, self.qk_rope_head_dim).transpose(0, 2, 1, 3)
251
- kv = self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
252
- kv = kv.reshape(B, L, self.num_heads, -1).transpose(0, 2, 1, 3)
253
-
254
- k_nope, values = mx.split(kv, [self.qk_nope_head_dim], axis=-1)
255
-
256
- if cache is not None:
257
- q_pe = self.rope(q_pe, cache.offset)
258
- k_pe = self.rope(k_pe, cache.offset)
259
- k_pe = mx.repeat(k_pe, self.num_heads, axis=1)
260
- keys, values = cache.update_and_fetch(
261
- mx.concatenate([k_nope, k_pe], axis=-1), values
262
- )
263
- else:
264
- q_pe = self.rope(q_pe)
265
- k_pe = self.rope(k_pe)
266
- k_pe = mx.repeat(k_pe, self.num_heads, axis=1)
267
- keys = mx.concatenate([k_nope, k_pe], axis=-1)
268
-
269
- queries = mx.concatenate([q_nope, q_pe], axis=-1)
270
-
271
- output = scaled_dot_product_attention(
272
- queries, keys, values, cache, scale=self.scale, mask=mask
273
- )
274
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
275
- return self.o_proj(output)
276
-
277
-
278
- class DeepseekV3MLP(nn.Module):
279
- def __init__(
280
- self, config: TextConfig, hidden_size: int = None, intermediate_size: int = None
281
- ):
282
- super().__init__()
283
- self.config = config
284
- self.hidden_size = config.hidden_size if hidden_size is None else hidden_size
285
- self.intermediate_size = (
286
- config.intermediate_size if intermediate_size is None else intermediate_size
287
- )
288
-
289
- self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
290
- self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
291
- self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
292
-
293
- def __call__(self, x):
294
- down_proj = self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x))
295
- return down_proj
296
-
297
-
298
- @mx.compile
299
- def group_expert_select(
300
- gates,
301
- e_score_correction_bias,
302
- top_k,
303
- n_group,
304
- topk_group,
305
- routed_scaling_factor,
306
- norm_topk_prob,
307
- ):
308
-
309
- k = top_k
310
- scores = mx.sigmoid(gates.astype(mx.float32))
311
- orig_scores = scores
312
- scores = scores + e_score_correction_bias
313
- scores = mx.unflatten(scores, axis=-1, shape=(n_group, -1))
314
- group_scores = mx.topk(scores, 2, axis=-1).sum(axis=-1, keepdims=True)
315
- k = n_group - topk_group
316
- group_idx = mx.argpartition(group_scores, kth=k - 1, axis=-2)[..., :k, :]
317
- scores = mx.put_along_axis(scores, group_idx, mx.array(0.0), axis=-2)
318
- scores = mx.flatten(scores, -2, -1)
319
-
320
- k = top_k
321
- inds = mx.argpartition(-scores, kth=k - 1, axis=-1)[..., :k]
322
- scores = mx.take_along_axis(orig_scores, inds, axis=-1)
323
- if top_k > 1 and norm_topk_prob:
324
- denominator = scores.sum(axis=-1, keepdims=True)
325
- scores = scores / denominator
326
- scores = scores * routed_scaling_factor
327
-
328
- return inds, scores
329
-
330
-
331
- class MoEGate(nn.Module):
332
- def __init__(self, config: TextConfig):
333
- super().__init__()
334
- self.config = config
335
- self.top_k = config.num_experts_per_tok
336
- self.norm_topk_prob = config.norm_topk_prob
337
- self.n_routed_experts = config.n_routed_experts
338
- self.routed_scaling_factor = config.routed_scaling_factor
339
- self.n_group = config.n_group
340
- self.topk_group = config.topk_group
341
- self.weight = mx.zeros((self.n_routed_experts, config.hidden_size))
342
- self.e_score_correction_bias = mx.zeros((self.n_routed_experts,))
343
- assert config.topk_method == "noaux_tc", "Unsupported topk method."
344
-
345
- def __call__(self, x):
346
- return group_expert_select(
347
- x @ self.weight.T,
348
- self.e_score_correction_bias,
349
- self.top_k,
350
- self.n_group,
351
- self.topk_group,
352
- self.routed_scaling_factor,
353
- self.norm_topk_prob,
354
- )
355
-
356
-
357
- class DeepseekV3MoE(nn.Module):
358
- def __init__(self, config: TextConfig):
359
- super().__init__()
360
- self.config = config
361
- self.num_experts_per_tok = config.num_experts_per_tok
362
- self.switch_mlp = SwitchGLU(
363
- config.hidden_size,
364
- config.moe_intermediate_size,
365
- config.n_routed_experts,
366
- activation=clipped_silu,
367
- )
368
-
369
- self.gate = MoEGate(config)
370
- if config.n_shared_experts is not None:
371
- intermediate_size = config.moe_intermediate_size * config.n_shared_experts
372
- self.shared_experts = DeepseekV3MLP(
373
- config=config, intermediate_size=intermediate_size
374
- )
375
-
376
- def __call__(self, x):
377
- inds, scores = self.gate(x)
378
- y = self.switch_mlp(x, inds)
379
- y = (y * scores[..., None]).sum(axis=-2).astype(y.dtype)
380
- if self.config.n_shared_experts is not None:
381
- y = y + self.shared_experts(x)
382
-
383
- return y
384
-
385
-
386
- class DeepseekV3DecoderLayer(nn.Module):
387
- def __init__(self, config: TextConfig, layer_idx: int):
388
- super().__init__()
389
- self.self_attn = DeepseekV3Attention(config)
390
- self.mlp = (
391
- DeepseekV3MoE(config)
392
- if (
393
- config.n_routed_experts is not None
394
- and layer_idx >= config.first_k_dense_replace
395
- and layer_idx % config.moe_layer_freq == 0
396
- )
397
- else DeepseekV3MLP(config)
398
- )
399
- self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
400
- self.post_attention_layernorm = nn.RMSNorm(
401
- config.hidden_size, eps=config.rms_norm_eps
402
- )
403
-
404
- def __call__(
405
- self,
406
- x: mx.array,
407
- mask: Optional[mx.array] = None,
408
- cache: Optional[Any] = None,
409
- ) -> mx.array:
410
- r = self.self_attn(self.input_layernorm(x), mask, cache)
411
- h = x + r
412
- r = self.mlp(self.post_attention_layernorm(h))
413
- return h + r
414
-
415
-
416
- class DeepseekV3Model(nn.Module):
417
- def __init__(self, config: TextConfig):
418
- super().__init__()
419
- self.vocab_size = config.vocab_size
420
- self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
421
- self.layers = [
422
- DeepseekV3DecoderLayer(config, idx)
423
- for idx in range(config.num_hidden_layers)
424
- ]
425
- self.start_idx = 0
426
- self.end_idx = len(self.layers)
427
- self.num_layers = self.end_idx
428
-
429
- self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
430
-
431
- def __call__(
432
- self,
433
- x: mx.array,
434
- inputs_embeds: Optional[mx.array] = None,
435
- cache: Optional[Any] = None,
436
- mask: Optional[mx.array] = None,
437
- ) -> mx.array:
438
-
439
- if inputs_embeds is None:
440
- h = self.embed_tokens(x)
441
- else:
442
- h = inputs_embeds
443
-
444
- if mask is None:
445
- mask = create_attention_mask(h, cache)
446
-
447
- if cache is None:
448
- cache = [None] * self.num_layers
449
-
450
- for layer, c in zip(self.layers, cache):
451
- h = layer(h, mask, c)
452
-
453
- return self.norm(h)
454
-
455
-
456
- class LanguageModel(nn.Module):
457
- def __init__(self, config: TextConfig):
458
- super().__init__()
459
- self.config = config
460
- self.model_type = config.model_type
461
- self.model = DeepseekV3Model(config)
462
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
463
-
464
- def __call__(
465
- self,
466
- inputs: mx.array,
467
- inputs_embeds: Optional[mx.array] = None,
468
- cache: Optional[Any] = None,
469
- mask: Optional[mx.array] = None,
470
- ):
471
- out = self.model(inputs, inputs_embeds=inputs_embeds, cache=cache, mask=mask)
472
- out = self.lm_head(out)
473
- return LanguageModelOutput(logits=out)
474
-
475
- def sanitize(self, weights):
476
- def keep(key):
477
- return "rotary_emb" not in key
478
-
479
- weights = {k: v for k, v in weights.items() if keep(k)}
480
- # Stack experts
481
- for l in range(self.config.num_hidden_layers):
482
- prefix = f"language_model.model.layers.{l}"
483
- for m in [("gate_proj"), ("down_proj"), ("up_proj")]:
484
- for k in ["weight", "scales", "biases"]:
485
- if f"{prefix}.mlp.experts.0.{m}.{k}" in weights:
486
- to_join = [
487
- weights.pop(f"{prefix}.mlp.experts.{e}.{m}.{k}")
488
- for e in range(self.config.n_routed_experts)
489
- ]
490
- weights[f"{prefix}.mlp.switch_mlp.{m}.{k}"] = mx.stack(to_join)
491
-
492
- return weights
493
-
494
- def embed_tokens(self, x):
495
- return self.model.embed_tokens(x)
496
-
497
- @property
498
- def layers(self):
499
- return self.model.layers[self.model.start_idx : self.model.end_idx]
500
-
501
- @property
502
- def n_kv_heads(self):
503
- return self.config.num_key_value_heads
504
-
505
- def cast_predicate(self):
506
- def predicate(k):
507
- return "e_score_correction_bias" not in k
508
-
509
- return predicate