nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc8__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 (196) 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-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/METADATA +1 -1
  5. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/RECORD +7 -196
  6. nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
  7. nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
  8. nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
  9. nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
  10. nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
  11. nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
  12. nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
  13. nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
  14. nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
  15. nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
  16. nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
  17. nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
  18. nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
  19. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
  20. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
  21. nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
  22. nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
  23. nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
  24. nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
  25. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
  26. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
  27. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
  28. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
  29. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
  30. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
  31. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
  32. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
  33. nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
  34. nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
  35. nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
  36. nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
  37. nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
  38. nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
  39. nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
  40. nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
  41. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
  42. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
  43. nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
  44. nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
  45. nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
  46. nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
  47. nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
  48. nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
  49. nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
  50. nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
  51. nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
  52. nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
  53. nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
  54. nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
  55. nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
  56. nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
  57. nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
  58. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
  59. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
  60. nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
  61. nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
  62. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
  63. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
  64. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
  65. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
  66. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
  67. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
  68. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
  69. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
  70. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
  71. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
  72. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
  73. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
  74. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
  75. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
  76. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
  77. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
  78. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
  79. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
  80. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
  81. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
  82. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
  83. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
  84. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
  85. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
  86. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
  87. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
  88. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
  89. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
  90. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
  91. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
  92. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
  93. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
  94. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
  95. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
  96. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
  97. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
  98. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
  99. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
  100. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
  101. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
  102. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
  103. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
  104. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
  105. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
  106. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
  107. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
  108. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
  109. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
  110. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
  111. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
  112. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
  113. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
  114. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
  115. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
  116. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
  117. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
  118. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
  119. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
  120. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
  121. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
  122. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
  123. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
  124. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
  125. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
  126. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
  127. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
  128. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
  129. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
  130. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
  131. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
  132. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
  133. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
  134. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
  135. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
  136. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
  137. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
  138. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
  139. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
  140. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
  141. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
  142. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
  143. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
  144. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
  145. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
  146. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
  147. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
  148. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
  149. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
  150. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
  151. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
  152. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
  153. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
  154. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
  155. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
  156. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
  157. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
  158. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
  159. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
  160. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
  161. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
  162. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
  163. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
  164. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
  165. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
  166. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
  167. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
  168. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
  169. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
  170. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
  171. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
  172. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
  173. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  174. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
  175. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
  176. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
  177. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
  178. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
  179. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
  180. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
  181. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
  182. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
  183. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
  184. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
  185. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
  186. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
  187. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
  188. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
  189. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
  190. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
  191. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
  192. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
  193. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
  194. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
  195. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/WHEEL +0 -0
  196. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc8.dist-info}/top_level.txt +0 -0
@@ -1,591 +0,0 @@
1
- import inspect
2
- import math
3
- from dataclasses import dataclass
4
- from typing import Any, Dict, Optional, Tuple, Union
5
-
6
- import mlx.core as mx
7
- import mlx.nn as nn
8
- from mlx_lm.models.switch_layers import SwitchGLU
9
-
10
- from ..base import (
11
- LanguageModelOutput,
12
- create_attention_mask,
13
- scaled_dot_product_attention,
14
- )
15
- from ..cache import KVCache, RotatingKVCache
16
-
17
-
18
- @dataclass
19
- class TextConfig:
20
- model_type: str = "deepseek_v2"
21
- vocab_size: int = 102400
22
- hidden_size: int = 1280
23
- intermediate_size: int = 6848
24
- moe_intermediate_size: int = 896
25
- num_hidden_layers: int = 30
26
- num_attention_heads: int = 32
27
- num_key_value_heads: int = 32
28
- n_shared_experts: Optional[int] = 2
29
- n_routed_experts: Optional[int] = 64
30
- routed_scaling_factor: float = 1.0
31
- kv_lora_rank: int = 512
32
- q_lora_rank: int = 1536
33
- qk_rope_head_dim: int = 64
34
- v_head_dim: int = 128
35
- qk_nope_head_dim: int = 128
36
- topk_method: str = "greedy"
37
- n_group: Optional[int] = 1
38
- topk_group: Optional[int] = 1
39
- num_experts_per_tok: Optional[int] = 6
40
- moe_layer_freq: int = 1
41
- first_k_dense_replace: int = 0
42
- max_position_embeddings: int = 2048
43
- rms_norm_eps: float = 1e-6
44
- rope_theta: float = 10000.0
45
- rope_traditional: bool = True
46
- rope_scaling: Dict = None
47
- attention_bias: bool = False
48
- scoring_func: str = "softmax"
49
- attn_type: str = "DeepseekV2Attention"
50
-
51
- @classmethod
52
- def from_dict(cls, params):
53
- return cls(
54
- **{
55
- k: v
56
- for k, v in params.items()
57
- if k in inspect.signature(cls).parameters
58
- }
59
- )
60
-
61
- def __post_init__(self):
62
- if self.qk_nope_head_dim == 0:
63
- self.attn_type = "LlamaAttention"
64
-
65
- if self.num_key_value_heads is None:
66
- self.num_key_value_heads = self.num_attention_heads
67
-
68
-
69
- def yarn_find_correction_dim(
70
- num_rotations, dim, base=10000, max_position_embeddings=2048
71
- ):
72
- return (dim * math.log(max_position_embeddings / (num_rotations * 2 * math.pi))) / (
73
- 2 * math.log(base)
74
- )
75
-
76
-
77
- def yarn_find_correction_range(
78
- low_rot, high_rot, dim, base=10000, max_position_embeddings=2048
79
- ):
80
- low = math.floor(
81
- yarn_find_correction_dim(low_rot, dim, base, max_position_embeddings)
82
- )
83
- high = math.ceil(
84
- yarn_find_correction_dim(high_rot, dim, base, max_position_embeddings)
85
- )
86
- return max(low, 0), min(high, dim - 1)
87
-
88
-
89
- def yarn_get_mscale(scale=1, mscale=1):
90
- if scale <= 1:
91
- return 1.0
92
- return 0.1 * mscale * math.log(scale) + 1.0
93
-
94
-
95
- def yarn_linear_ramp_mask(min_val, max_val, dim):
96
- if min_val == max_val:
97
- max_val += 0.001 # Prevent singularity
98
-
99
- linear_func = (mx.arange(dim, dtype=mx.float32) - min_val) / (max_val - min_val)
100
- return mx.clip(linear_func, 0, 1)
101
-
102
-
103
- class DeepseekV2YarnRotaryEmbedding(nn.Module):
104
- def __init__(
105
- self,
106
- dim,
107
- max_position_embeddings=2048,
108
- base=10000,
109
- scaling_factor=1.0,
110
- original_max_position_embeddings=4096,
111
- beta_fast=32,
112
- beta_slow=1,
113
- mscale=1,
114
- mscale_all_dim=0,
115
- ):
116
- super().__init__()
117
- self.mscale = yarn_get_mscale(scaling_factor, mscale) / yarn_get_mscale(
118
- scaling_factor, mscale_all_dim
119
- )
120
- freq_extra = base ** (mx.arange(0, dim, 2, dtype=mx.float32) / dim)
121
- freq_inter = scaling_factor * base ** (
122
- mx.arange(0, dim, 2, dtype=mx.float32) / dim
123
- )
124
- low, high = yarn_find_correction_range(
125
- beta_fast,
126
- beta_slow,
127
- dim,
128
- base,
129
- original_max_position_embeddings,
130
- )
131
- freq_mask = 1.0 - yarn_linear_ramp_mask(low, high, dim // 2)
132
- self._freqs = (freq_inter * freq_extra) / (
133
- freq_inter * freq_mask + freq_extra * (1 - freq_mask)
134
- )
135
-
136
- def __call__(self, x, offset=0):
137
- if self.mscale != 1.0:
138
- x = self.mscale * x
139
- return mx.fast.rope(
140
- x,
141
- x.shape[-1],
142
- traditional=True,
143
- base=None,
144
- scale=1.0,
145
- offset=offset,
146
- freqs=self._freqs,
147
- )
148
-
149
-
150
- class DeepseekV2Attention(nn.Module):
151
- def __init__(self, config: TextConfig):
152
- super().__init__()
153
- self.config = config
154
- self.hidden_size = config.hidden_size
155
- self.num_heads = config.num_attention_heads
156
- self.max_position_embeddings = config.max_position_embeddings
157
- self.rope_theta = config.rope_theta
158
- self.q_lora_rank = config.q_lora_rank
159
- self.qk_rope_head_dim = config.qk_rope_head_dim
160
- self.kv_lora_rank = config.kv_lora_rank
161
- self.v_head_dim = config.v_head_dim
162
- self.qk_nope_head_dim = config.qk_nope_head_dim
163
- self.q_head_dim = config.qk_nope_head_dim + config.qk_rope_head_dim
164
-
165
- self.scale = self.q_head_dim**-0.5
166
-
167
- if self.q_lora_rank is None:
168
- self.q_proj = nn.Linear(
169
- self.hidden_size, self.num_heads * self.q_head_dim, bias=False
170
- )
171
- else:
172
- self.q_a_proj = nn.Linear(
173
- self.hidden_size, self.q_lora_rank, bias=config.attention_bias
174
- )
175
- self.q_a_layernorm = nn.RMSNorm(self.q_lora_rank)
176
- self.q_b_proj = nn.Linear(
177
- self.q_lora_rank, self.num_heads * self.q_head_dim, bias=False
178
- )
179
-
180
- self.kv_a_proj_with_mqa = nn.Linear(
181
- self.hidden_size,
182
- self.kv_lora_rank + self.qk_rope_head_dim,
183
- bias=config.attention_bias,
184
- )
185
- self.kv_a_layernorm = nn.RMSNorm(self.kv_lora_rank)
186
- self.kv_b_proj = nn.Linear(
187
- self.kv_lora_rank,
188
- self.num_heads
189
- * (self.q_head_dim - self.qk_rope_head_dim + self.v_head_dim),
190
- bias=False,
191
- )
192
-
193
- self.o_proj = nn.Linear(
194
- self.num_heads * self.v_head_dim,
195
- self.hidden_size,
196
- bias=config.attention_bias,
197
- )
198
-
199
- if self.config.rope_scaling is None:
200
- self.rope = nn.RoPE(
201
- self.qk_rope_head_dim,
202
- traditional=self.config.rope_traditional,
203
- base=self.rope_theta,
204
- )
205
- else:
206
- mscale_all_dim = self.config.rope_scaling.get("mscale_all_dim", 0)
207
- scaling_factor = self.config.rope_scaling.get("factor", 1)
208
- if mscale_all_dim:
209
- mscale = yarn_get_mscale(scaling_factor, mscale_all_dim)
210
- self.scale = self.scale * mscale * mscale
211
-
212
- rope_kwargs = {
213
- key: self.config.rope_scaling[key]
214
- for key in [
215
- "original_max_position_embeddings",
216
- "beta_fast",
217
- "beta_slow",
218
- "mscale",
219
- "mscale_all_dim",
220
- ]
221
- if key in self.config.rope_scaling
222
- }
223
- self.rope = DeepseekV2YarnRotaryEmbedding(
224
- dim=self.qk_rope_head_dim,
225
- max_position_embeddings=self.max_position_embeddings,
226
- scaling_factor=scaling_factor,
227
- base=self.rope_theta,
228
- **rope_kwargs,
229
- )
230
-
231
- def __call__(
232
- self,
233
- x: mx.array,
234
- mask: Optional[mx.array] = None,
235
- cache: Optional[Any] = None,
236
- ) -> mx.array:
237
- B, L, D = x.shape
238
-
239
- if self.q_lora_rank is None:
240
- q = self.q_proj(x)
241
- else:
242
- q = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(x)))
243
-
244
- q = q.reshape(B, L, self.num_heads, self.q_head_dim).transpose(0, 2, 1, 3)
245
- q_nope, q_pe = mx.split(q, [self.qk_nope_head_dim], axis=-1)
246
- compressed_kv = self.kv_a_proj_with_mqa(x)
247
- compressed_kv, k_pe = mx.split(compressed_kv, [self.kv_lora_rank], axis=-1)
248
- k_pe = k_pe.reshape(B, L, 1, self.qk_rope_head_dim).transpose(0, 2, 1, 3)
249
- kv = self.kv_b_proj(self.kv_a_layernorm(compressed_kv))
250
- kv = kv.reshape(B, L, self.num_heads, -1).transpose(0, 2, 1, 3)
251
-
252
- k_nope, values = mx.split(kv, [self.qk_nope_head_dim], axis=-1)
253
-
254
- if cache is not None:
255
- q_pe = self.rope(q_pe, cache.offset)
256
- k_pe = self.rope(k_pe, cache.offset)
257
- k_pe = mx.repeat(k_pe, self.num_heads, axis=1)
258
- keys, values = cache.update_and_fetch(
259
- mx.concatenate([k_nope, k_pe], axis=-1), values
260
- )
261
- else:
262
- q_pe = self.rope(q_pe)
263
- k_pe = self.rope(k_pe)
264
- k_pe = mx.repeat(k_pe, self.num_heads, axis=1)
265
- keys = mx.concatenate([k_nope, k_pe], axis=-1)
266
-
267
- queries = mx.concatenate([q_nope, q_pe], axis=-1)
268
-
269
- output = scaled_dot_product_attention(
270
- queries, keys, values, cache, scale=self.scale, mask=mask
271
- )
272
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
273
- return self.o_proj(output)
274
-
275
-
276
- class LlamaAttention(nn.Module):
277
- def __init__(self, config: TextConfig):
278
- super().__init__()
279
-
280
- dim = config.hidden_size
281
- self.n_heads = n_heads = config.num_attention_heads
282
- self.n_kv_heads = n_kv_heads = config.num_key_value_heads
283
-
284
- self.head_dim = head_dim = config.hidden_size // n_heads
285
-
286
- self.scale = head_dim**-0.5
287
- if config.attention_bias:
288
- attention_bias = config.attention_bias
289
- else:
290
- attention_bias = False
291
-
292
- self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=attention_bias)
293
- self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
294
- self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
295
- self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=attention_bias)
296
-
297
- rope_scale = (
298
- 1 / config.rope_scaling["factor"]
299
- if config.rope_scaling is not None
300
- and config.rope_scaling["type"] == "linear"
301
- else 1
302
- )
303
- self.rope = nn.RoPE(
304
- head_dim,
305
- traditional=config.rope_traditional,
306
- base=config.rope_theta,
307
- scale=rope_scale,
308
- )
309
-
310
- def __call__(
311
- self,
312
- x: mx.array,
313
- mask: Optional[mx.array] = None,
314
- cache: Optional[Any] = None,
315
- ) -> mx.array:
316
- B, L, D = x.shape
317
-
318
- queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
319
-
320
- # Prepare the queries, keys and values for the attention computation
321
- queries = queries.reshape(B, L, self.n_heads, -1).transpose(0, 2, 1, 3)
322
- keys = keys.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
323
- values = values.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
324
-
325
- if cache is not None:
326
- queries = self.rope(queries, offset=cache.offset)
327
- keys = self.rope(keys, offset=cache.offset)
328
- keys, values = cache.update_and_fetch(keys, values)
329
- else:
330
- queries = self.rope(queries)
331
- keys = self.rope(keys)
332
-
333
- output = scaled_dot_product_attention(
334
- queries, keys, values, cache, scale=self.scale, mask=mask
335
- )
336
-
337
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
338
- return self.o_proj(output)
339
-
340
-
341
- class DeepseekV2MLP(nn.Module):
342
- def __init__(
343
- self, config: TextConfig, hidden_size: int = None, intermediate_size: int = None
344
- ):
345
- super().__init__()
346
- self.config = config
347
- self.hidden_size = config.hidden_size if hidden_size is None else hidden_size
348
- self.intermediate_size = (
349
- config.intermediate_size if intermediate_size is None else intermediate_size
350
- )
351
-
352
- self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
353
- self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
354
- self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
355
-
356
- def __call__(self, x):
357
- down_proj = self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x))
358
- return down_proj
359
-
360
-
361
- class MoEGate(nn.Module):
362
- def __init__(self, config: TextConfig):
363
- super().__init__()
364
- self.config = config
365
- self.scoring_func = config.scoring_func
366
- self.top_k = config.num_experts_per_tok
367
- self.n_routed_experts = config.n_routed_experts
368
- self.routed_scaling_factor = config.routed_scaling_factor
369
- self.topk_method = config.topk_method
370
- self.n_group = config.n_group
371
- self.topk_group = config.topk_group
372
- if self.topk_method == "noaux_tc":
373
- self.e_score_correction_bias = mx.zeros((self.n_routed_experts))
374
- self.weight = mx.zeros((self.n_routed_experts, config.hidden_size))
375
-
376
- def __call__(self, x):
377
- gates = x @ self.weight.T
378
-
379
- if self.scoring_func == "softmax":
380
- scores = mx.softmax(gates, axis=-1, precise=True)
381
- elif self.scoring_func == "sigmoid":
382
- scores = mx.sigmoid(gates)
383
- else:
384
- raise ValueError(f"Unknown scoring function: {self.scoring_func}")
385
-
386
- if self.topk_method == "greedy":
387
- bsz, seq_len = x.shape[:2]
388
- scores = scores.reshape(bsz, seq_len, self.n_group, -1)
389
- group_scores = scores.max(axis=-1)
390
-
391
- # Get top-k groups
392
- k = self.n_group - self.topk_group
393
- group_idx = mx.argpartition(group_scores, kth=k - 1, axis=-1)[..., :k]
394
- batch_idx = mx.expand_dims(mx.arange(bsz), (1, 2))
395
- seq_idx = mx.expand_dims(mx.arange(seq_len), (0, 2))
396
-
397
- # Mask out top-k groups
398
- scores[batch_idx, seq_idx, group_idx] = 0.0
399
- scores = scores.reshape(bsz, seq_len, -1)
400
-
401
- # Get top-k indices and weights
402
- k = self.top_k
403
- inds = mx.argpartition(-scores, kth=k - 1, axis=-1)[..., :k]
404
- scores = mx.take_along_axis(scores, inds, axis=-1)
405
-
406
- elif self.topk_method == "noaux_tc":
407
- bsz, seq_len = x.shape[:2]
408
-
409
- # Add bias correction
410
- scores_for_choice = scores.reshape(bsz * seq_len, -1) + mx.expand_dims(
411
- self.e_score_correction_bias, 0
412
- )
413
-
414
- # Calculate group scores using top-2 sum per group
415
- scores_reshaped = scores_for_choice.reshape(bsz * seq_len, self.n_group, -1)
416
-
417
- # Get top 2 scores per group
418
- group_scores = mx.topk(scores_reshaped, 2, axis=-1).sum(axis=-1)
419
-
420
- # Get top groups
421
- k = self.n_group - self.topk_group
422
-
423
- # Create mask for selected groups
424
- group_idx = mx.argpartition(group_scores, kth=k - 1, axis=-1)[..., :k]
425
- batch_idx = mx.expand_dims(mx.arange(bsz), (1, 2))
426
-
427
- seq_idx = mx.expand_dims(mx.arange(seq_len), (0, 2))
428
- scores[batch_idx, seq_idx, group_idx] = 0.0
429
-
430
- # Get top-k indices and weights
431
- k = self.top_k
432
- inds = mx.argpartition(scores, kth=-k, axis=-1)[..., -k:]
433
-
434
- # Gather original scores for the selected indices
435
- scores_flat = scores.reshape(bsz * seq_len, -1)
436
- batch_idx = mx.expand_dims(mx.arange(bsz * seq_len), 1)
437
- scores = mx.take(scores_flat, inds + batch_idx * scores_flat.shape[1])
438
- else:
439
- raise ValueError(f"Unknown topk method: {self.topk_method}")
440
-
441
- scores = scores * self.routed_scaling_factor
442
- return inds, scores
443
-
444
-
445
- class DeepseekV2MoE(nn.Module):
446
- def __init__(self, config: TextConfig):
447
- super().__init__()
448
- self.config = config
449
- self.num_experts_per_tok = config.num_experts_per_tok
450
- self.switch_mlp = SwitchGLU(
451
- config.hidden_size, config.moe_intermediate_size, config.n_routed_experts
452
- )
453
-
454
- self.gate = MoEGate(config)
455
- if config.n_shared_experts is not None:
456
- intermediate_size = config.moe_intermediate_size * config.n_shared_experts
457
- self.shared_experts = DeepseekV2MLP(
458
- config=config, intermediate_size=intermediate_size
459
- )
460
-
461
- def __call__(self, x):
462
- inds, scores = self.gate(x)
463
- y = self.switch_mlp(x, inds)
464
- y = (y * scores[..., None]).sum(axis=-2)
465
- if self.config.n_shared_experts is not None:
466
- y = y + self.shared_experts(x)
467
-
468
- return y
469
-
470
-
471
- class DeepseekV2DecoderLayer(nn.Module):
472
- def __init__(self, config: TextConfig, layer_idx: int):
473
- super().__init__()
474
- self.attn_type = config.attn_type
475
- self.self_attn = (
476
- DeepseekV2Attention(config)
477
- if self.attn_type == "DeepseekV2Attention"
478
- else LlamaAttention(config)
479
- )
480
- self.mlp = (
481
- DeepseekV2MoE(config)
482
- if (
483
- config.n_routed_experts is not None
484
- and layer_idx >= config.first_k_dense_replace
485
- and layer_idx % config.moe_layer_freq == 0
486
- )
487
- else DeepseekV2MLP(config)
488
- )
489
- self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
490
- self.post_attention_layernorm = nn.RMSNorm(
491
- config.hidden_size, eps=config.rms_norm_eps
492
- )
493
-
494
- def __call__(
495
- self,
496
- x: mx.array,
497
- mask: Optional[mx.array] = None,
498
- cache: Optional[Any] = None,
499
- ) -> mx.array:
500
- r = self.self_attn(self.input_layernorm(x), mask, cache)
501
- h = x + r
502
- r = self.mlp(self.post_attention_layernorm(h))
503
- out = h + r
504
- return out
505
-
506
-
507
- class DeepseekV2Model(nn.Module):
508
- def __init__(self, config: TextConfig):
509
- super().__init__()
510
- self.vocab_size = config.vocab_size
511
- self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
512
- self.layers = [
513
- DeepseekV2DecoderLayer(config, idx)
514
- for idx in range(config.num_hidden_layers)
515
- ]
516
- self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
517
-
518
- def __call__(
519
- self,
520
- x: mx.array,
521
- mask: Optional[mx.array] = None,
522
- inputs_embeds: Optional[mx.array] = None,
523
- cache: Optional[Any] = None,
524
- ) -> mx.array:
525
-
526
- if inputs_embeds is None:
527
- h = self.embed_tokens(x)
528
- else:
529
- h = inputs_embeds
530
-
531
- if cache is None:
532
- cache = [None] * len(self.layers)
533
-
534
- if mask is None:
535
- mask = create_attention_mask(h, cache)
536
-
537
- for layer, c in zip(self.layers, cache):
538
- h = layer(h, mask, c)
539
-
540
- return self.norm(h)
541
-
542
-
543
- class LanguageModel(nn.Module):
544
- def __init__(self, config: TextConfig):
545
- super().__init__()
546
- self.config = config
547
- self.model_type = config.model_type
548
- self.model = DeepseekV2Model(config)
549
- self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
550
-
551
- def __call__(
552
- self,
553
- inputs: mx.array,
554
- inputs_embeds: Optional[mx.array] = None,
555
- mask: Optional[mx.array] = None,
556
- cache: Optional[Any] = None,
557
- ):
558
- out = self.model(inputs, mask=mask, inputs_embeds=inputs_embeds, cache=cache)
559
- out = self.lm_head(out)
560
- return LanguageModelOutput(logits=out)
561
-
562
- def sanitize(self, weights):
563
- for l in range(self.config.num_hidden_layers):
564
- prefix = f"language_model.model.layers.{l}"
565
- for n, m in [("w1", "gate_proj"), ("w2", "down_proj"), ("w3", "up_proj")]:
566
- for k in ["weight", "scales", "biases"]:
567
- if f"{prefix}.mlp.experts.0.{m}.{k}" in weights:
568
- to_join = [
569
- weights.pop(f"{prefix}.mlp.experts.{e}.{m}.{k}")
570
- for e in range(self.config.n_routed_experts)
571
- ]
572
- weights[f"{prefix}.mlp.switch_mlp.{m}.{k}"] = mx.stack(to_join)
573
- return weights
574
-
575
- @property
576
- def layers(self):
577
- return self.model.layers
578
-
579
- @property
580
- def head_dim(self):
581
- if self.config.attn_type == "DeepseekV2Attention":
582
- return (
583
- self.config.qk_nope_head_dim + self.config.qk_rope_head_dim,
584
- self.config.v_head_dim,
585
- )
586
- else:
587
- return self.config.hidden_size // self.config.num_key_value_heads
588
-
589
- @property
590
- def n_kv_heads(self):
591
- return self.config.num_key_value_heads