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,386 +0,0 @@
1
- import inspect
2
- from dataclasses import dataclass
3
- from typing import Any, Optional
4
-
5
- import mlx.core as mx
6
- import mlx.nn as nn
7
- from mlx_lm.models.rope_utils import initialize_rope
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 ChunkedKVCache, KVCache
16
-
17
-
18
- @dataclass
19
- class TextConfig:
20
- model_type: str
21
- hidden_size: int
22
- intermediate_size: int
23
- num_attention_heads: int
24
- rms_norm_eps: float
25
- vocab_size: int
26
- num_key_value_heads: int
27
- rope_theta: float = 500000.0
28
- num_hidden_layers: int = 48
29
- rope_traditional: bool = False
30
- rope_scaling: Optional[dict] = None # Add missing rope_scaling attribute
31
- tie_word_embeddings: bool = False
32
- head_dim: int = 128
33
- hidden_act: str = "silu"
34
- intermediate_size_mlp: int = 16384
35
- max_position_embeddings: int = 10485760
36
- num_experts_per_tok: int = 1
37
- num_local_experts: int = 16
38
- attention_dropout: float = 0.0
39
- use_qk_norm: bool = True
40
- bos_token_id: int = 200000
41
- eos_token_id: list = None
42
- pad_token_id: int = 200018
43
- attention_chunk_size: int = 8192
44
- attention_bias: bool = False
45
- interleave_moe_layer_step: int = 1
46
- no_rope_layers: list = 4
47
- output_router_logits: bool = False
48
- router_aux_loss_coef: float = 0.001
49
- router_jitter_noise: float = 0.0
50
- attn_temperature_tuning: int = 4
51
- floor_scale: float = 8192
52
- attn_scale: float = 0.1
53
- moe_layers: list = None
54
-
55
- @classmethod
56
- def from_dict(cls, params):
57
- return cls(
58
- **{
59
- k: v
60
- for k, v in params.items()
61
- if k in inspect.signature(cls).parameters
62
- }
63
- )
64
-
65
- def __post_init__(self):
66
- if self.num_key_value_heads is None:
67
- self.num_key_value_heads = self.num_attention_heads
68
-
69
-
70
- class Attention(nn.Module):
71
- def __init__(self, config: TextConfig, layer_idx: int):
72
- super().__init__()
73
-
74
- dim = config.hidden_size
75
- self.n_heads = n_heads = config.num_attention_heads
76
- self.n_kv_heads = n_kv_heads = config.num_key_value_heads
77
-
78
- self.use_rope = int((layer_idx + 1) % 4 != 0) # rope unused for dense layers
79
- self.attn_temperature_tuning = config.attn_temperature_tuning
80
- self.floor_scale = config.floor_scale
81
- self.attn_scale = config.attn_scale
82
-
83
- self.head_dim = head_dim = config.head_dim or config.hidden_size // n_heads
84
-
85
- self.scale = head_dim**-0.5
86
- if hasattr(config, "attention_bias"):
87
- attention_bias = config.attention_bias
88
- else:
89
- attention_bias = False
90
-
91
- self.q_proj = nn.Linear(dim, n_heads * head_dim, bias=attention_bias)
92
- self.k_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
93
- self.v_proj = nn.Linear(dim, n_kv_heads * head_dim, bias=attention_bias)
94
- self.o_proj = nn.Linear(n_heads * head_dim, dim, bias=attention_bias)
95
-
96
- self.use_qk_norm = config.use_qk_norm and self.use_rope
97
-
98
- if self.use_rope:
99
- self.rope = initialize_rope(
100
- head_dim,
101
- config.rope_theta,
102
- traditional=True,
103
- scaling_config=config.rope_scaling,
104
- max_position_embeddings=config.max_position_embeddings,
105
- )
106
-
107
- def __call__(
108
- self,
109
- x: mx.array,
110
- mask: Optional[mx.array] = None,
111
- cache: Optional[Any] = None,
112
- ) -> mx.array:
113
- B, L, D = x.shape
114
-
115
- queries, keys, values = self.q_proj(x), self.k_proj(x), self.v_proj(x)
116
-
117
- queries = queries.reshape(B, L, self.n_heads, -1).transpose(0, 2, 1, 3)
118
- keys = keys.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
119
- values = values.reshape(B, L, self.n_kv_heads, -1).transpose(0, 2, 1, 3)
120
-
121
- if cache is not None:
122
- offset = cache.offset
123
- else:
124
- offset = 0
125
-
126
- if self.use_rope:
127
- queries = self.rope(queries, offset=offset)
128
- keys = self.rope(keys, offset=offset)
129
-
130
- if self.use_qk_norm:
131
- queries = mx.fast.rms_norm(queries, weight=None, eps=1e-6)
132
- keys = mx.fast.rms_norm(keys, weight=None, eps=1e-6)
133
-
134
- if self.attn_temperature_tuning and not self.use_rope:
135
- attn_scales = (
136
- mx.log(
137
- mx.floor(mx.arange(offset + 1, offset + L + 1) / self.floor_scale)
138
- + 1.0
139
- )
140
- * self.attn_scale
141
- + 1.0
142
- )
143
- attn_scales = attn_scales[:, None]
144
- queries = (queries * attn_scales).astype(queries.dtype)
145
-
146
- if cache is not None:
147
- keys, values = cache.update_and_fetch(keys, values)
148
-
149
- if self.use_rope and isinstance(mask, mx.array):
150
- key_len = keys.shape[-2]
151
- if mask.shape[-1] != key_len:
152
- mask = mask[..., -key_len:]
153
-
154
- output = scaled_dot_product_attention(
155
- queries, keys, values, cache, scale=self.scale, mask=mask
156
- )
157
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
158
- return self.o_proj(output)
159
-
160
-
161
- class MLP(nn.Module):
162
- def __init__(self, config: TextConfig, intermediate_size: int = None):
163
- super().__init__()
164
-
165
- dim = config.hidden_size
166
- hidden_dim = intermediate_size or config.intermediate_size
167
-
168
- self.gate_proj = nn.Linear(dim, hidden_dim, bias=False)
169
- self.down_proj = nn.Linear(hidden_dim, dim, bias=False)
170
- self.up_proj = nn.Linear(dim, hidden_dim, bias=False)
171
-
172
- def __call__(self, x) -> mx.array:
173
- return self.down_proj(nn.silu(self.gate_proj(x)) * self.up_proj(x))
174
-
175
-
176
- class MoE(nn.Module):
177
- def __init__(self, config):
178
- super().__init__()
179
- self.top_k = config.num_experts_per_tok
180
- self.num_experts = config.num_local_experts
181
- self.experts = SwitchGLU(
182
- config.hidden_size, config.intermediate_size, self.num_experts
183
- )
184
- self.router = nn.Linear(
185
- config.hidden_size, config.num_local_experts, bias=False
186
- )
187
- self.shared_expert = MLP(config)
188
-
189
- def __call__(self, x) -> mx.array:
190
- logits = self.router(x)
191
- k = self.top_k
192
- indices = mx.argpartition(-logits, kth=k - 1, axis=-1)[..., :k]
193
- scores = mx.take_along_axis(logits, indices, axis=-1)
194
- scores = mx.sigmoid(scores.astype(mx.float32)).astype(x.dtype)
195
-
196
- out = self.experts(x * scores, indices).squeeze(2)
197
- return out + self.shared_expert(x)
198
-
199
-
200
- class TransformerBlock(nn.Module):
201
- def __init__(self, config: TextConfig, layer_idx: int):
202
- super().__init__()
203
- self.num_attention_heads = config.num_attention_heads
204
- self.hidden_size = config.hidden_size
205
- self.self_attn = Attention(config, layer_idx)
206
- self.use_chunked_attention = int((layer_idx + 1) % 4 != 0)
207
- self.is_moe_layer = (layer_idx % config.interleave_moe_layer_step) == (
208
- config.interleave_moe_layer_step - 1
209
- )
210
- if self.is_moe_layer:
211
- self.feed_forward = MoE(config)
212
- else:
213
- self.feed_forward = MLP(config, config.intermediate_size_mlp)
214
-
215
- self.input_layernorm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
216
- self.post_attention_layernorm = nn.RMSNorm(
217
- config.hidden_size, eps=config.rms_norm_eps
218
- )
219
- self.config = config
220
-
221
- self.use_chunked_attention = int((layer_idx + 1) % 4 != 0) # <=> use rope
222
-
223
- def __call__(
224
- self,
225
- x: mx.array,
226
- mask: Optional[mx.array] = None,
227
- cache: Optional[Any] = None,
228
- ) -> mx.array:
229
-
230
- r = self.self_attn(self.input_layernorm(x), mask, cache)
231
- h = x + r
232
- r = self.feed_forward(self.post_attention_layernorm(h))
233
- out = h + r
234
- return out
235
-
236
-
237
- class LlamaModel(nn.Module):
238
- def __init__(self, config: TextConfig):
239
- super().__init__()
240
- self.config = config
241
- self.vocab_size = config.vocab_size
242
- self.num_hidden_layers = config.num_hidden_layers
243
- assert self.vocab_size > 0
244
- self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
245
- self.layers = [
246
- TransformerBlock(config, i) for i in range(config.num_hidden_layers)
247
- ]
248
- self.norm = nn.RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
249
-
250
- def create_chunked_attention_mask(
251
- self, seq_len: int, attention_chunk_size: int, start: int = 0, offset: int = 0
252
- ) -> mx.array:
253
- """
254
- Generate the following:
255
-
256
- 'What' : 0 ■ ⬚ ⬚ ⬚ ⬚ ⬚ |
257
- '▁is' : 1 ■ ■ ⬚ ⬚ ⬚ ⬚ |
258
- '▁ch' : 2 ■ ■ ■ ⬚ ⬚ ⬚ |
259
- 'unked' : 3 ⬚ ⬚ ⬚ ■ ⬚ ⬚ |
260
- '▁attention': 4 ⬚ ⬚ ⬚ ■ ■ ⬚ |
261
- '?' : 5 ⬚ ⬚ ⬚ ■ ■ ■ |
262
-
263
- If the chunk size is 3.
264
- This can just be appplied over the already created attention mask
265
- """
266
-
267
- end = offset + seq_len
268
- linds = mx.arange(start, end)
269
- rinds = mx.arange(offset, end)[:, None]
270
- block_pos = mx.abs(
271
- (linds // attention_chunk_size) - (rinds // attention_chunk_size)
272
- )
273
- token_pos = linds <= rinds
274
- mask = (block_pos == 0) & (token_pos)
275
- return mask
276
-
277
- def __call__(
278
- self,
279
- input_ids: mx.array = None,
280
- input_embeds: mx.array = None,
281
- mask: mx.array = None,
282
- cache=None,
283
- ):
284
- if input_embeds is None:
285
- h = self.embed_tokens(input_ids)
286
- else:
287
- h = input_embeds
288
-
289
- if mask is None:
290
- mask = create_attention_mask(h, cache)
291
-
292
- if cache is not None:
293
- for idx, c in enumerate(cache):
294
- if (idx + 1) % 4 != 0:
295
- c.maybe_trim_front()
296
- start = cache[0].start_position
297
- offset = cache[0].offset
298
- else:
299
- start = 0
300
- offset = 0
301
-
302
- # Create a mask for the chunked attention
303
- chunk_mask = self.create_chunked_attention_mask(
304
- h.shape[1], self.config.attention_chunk_size, start, offset
305
- )
306
-
307
- if cache is None:
308
- cache = [None] * len(self.layers)
309
-
310
- for idx, (layer, c) in enumerate(zip(self.layers, cache)):
311
- use_chunked_attention = (idx + 1) % 4 != 0
312
- if use_chunked_attention:
313
- local_mask = chunk_mask
314
- else:
315
- local_mask = mask
316
- h = layer(h, local_mask, cache=c)
317
-
318
- return self.norm(h)
319
-
320
-
321
- class LanguageModel(nn.Module):
322
- def __init__(self, config: TextConfig):
323
- super().__init__()
324
- self.config = config
325
- self.model_type = config.model_type
326
- self.model = LlamaModel(self.config)
327
- self.lm_head = nn.Linear(
328
- self.config.hidden_size, self.config.vocab_size, bias=False
329
- )
330
-
331
- def __call__(
332
- self,
333
- input_ids: mx.array = None,
334
- input_embeds: mx.array = None,
335
- mask: mx.array = None,
336
- cache=None,
337
- ):
338
- out = self.model(
339
- input_ids=input_ids,
340
- input_embeds=input_embeds,
341
- mask=mask,
342
- cache=cache,
343
- )
344
- out = self.lm_head(out)
345
- return LanguageModelOutput(logits=out)
346
-
347
- def sanitize(self, weights):
348
- # Rename expert weights for SwitchGLU
349
- for l in range(self.config.num_hidden_layers):
350
- prefix = f"language_model.model.layers.{l}.feed_forward.experts"
351
- if f"{prefix}.gate_up_proj" in weights:
352
- v = weights.pop(f"{prefix}.gate_up_proj")
353
- gate_k = f"{prefix}.gate_proj.weight"
354
- up_k = f"{prefix}.up_proj.weight"
355
- gate_proj, up_proj = mx.split(v, 2, axis=-1)
356
- weights[gate_k] = mx.swapaxes(gate_proj, 1, 2)
357
- weights[up_k] = mx.swapaxes(up_proj, 1, 2)
358
- if f"{prefix}.down_proj" in weights:
359
- down_proj = weights.pop(f"{prefix}.down_proj")
360
- weights[f"{prefix}.down_proj.weight"] = mx.swapaxes(down_proj, 1, 2)
361
- return weights
362
-
363
- @property
364
- def layers(self):
365
- return self.model.layers
366
-
367
- @property
368
- def n_kv_heads(self):
369
- return self.config.num_key_value_heads
370
-
371
- @property
372
- def head_dim(self):
373
- return (
374
- self.config.head_dim
375
- if self.config.head_dim
376
- else self.config.hidden_size // self.config.num_attention_heads
377
- )
378
-
379
- def make_cache(self):
380
- caches = []
381
- for i in range(self.config.num_hidden_layers):
382
- if (i + 1) % 4 != 0:
383
- caches.append(ChunkedKVCache(self.config.attention_chunk_size))
384
- else:
385
- caches.append(KVCache()) # no chunking for dense layers
386
- return caches
@@ -1,138 +0,0 @@
1
- import glob
2
- import inspect
3
- import json
4
- import re
5
- from dataclasses import dataclass
6
- from pathlib import Path
7
- from typing import Any, Callable, List, Optional, Tuple, Union
8
-
9
- import mlx.core as mx
10
- import mlx.nn as nn
11
- import numpy as np
12
-
13
- from .language import LanguageModel, TextConfig
14
- from .vision import Llama4MultiModalProjector, VisionConfig, VisionModel
15
-
16
-
17
- @dataclass
18
- class ModelConfig:
19
- text_config: TextConfig
20
- vision_config: VisionConfig
21
- model_type: str
22
- ignore_index: int = -100
23
- image_token_id: int = 200092
24
- image_token_index: Optional[int] = None
25
- eos_token_id: Optional[List[int]] = None
26
-
27
- def __post_init__(self):
28
- if self.image_token_index is None:
29
- self.image_token_index = self.image_token_id
30
-
31
- @classmethod
32
- def from_dict(cls, params):
33
- return cls(
34
- **{
35
- k: v
36
- for k, v in params.items()
37
- if k in inspect.signature(cls).parameters
38
- }
39
- )
40
-
41
-
42
- class Model(nn.Module):
43
- def __init__(self, config: ModelConfig):
44
- super().__init__()
45
- self.config = config
46
- self.vision_model = VisionModel(config.vision_config)
47
- self.multi_modal_projector = Llama4MultiModalProjector(config)
48
- self.language_model = LanguageModel(config.text_config)
49
- self.vocab_size = config.text_config.vocab_size
50
-
51
- def set_input_embeddings(self, value):
52
- self.language_model.set_input_embeddings(value)
53
-
54
- def get_output_embeddings(self):
55
- return self.language_model.get_output_embeddings()
56
-
57
- def set_output_embeddings(self, new_embeddings):
58
- self.language_model.set_output_embeddings(new_embeddings)
59
-
60
- def set_decoder(self, decoder):
61
- self.language_model.set_decoder(decoder)
62
-
63
- def get_decoder(self):
64
- return self.language_model.get_decoder()
65
-
66
- def get_image_features(
67
- self,
68
- pixel_values: mx.array,
69
- vision_feature_layer: Union[int, List[int]],
70
- vision_feature_select_strategy: str,
71
- **kwargs,
72
- ):
73
- if vision_feature_select_strategy not in ["default", "full"]:
74
- raise ValueError(
75
- f"Unexpected select feature strategy: {self.vision_feature_select_strategy}"
76
- )
77
- kwargs = {k: v for k, v in kwargs.items() if v is not None}
78
- hidden_state = self.vision_model(
79
- pixel_values, output_hidden_states=False, **kwargs
80
- )
81
- return hidden_state
82
-
83
- def get_input_embeddings(
84
- self,
85
- input_ids: Optional[mx.array] = None,
86
- pixel_values: Optional[mx.array] = None,
87
- **kwargs,
88
- ):
89
- if pixel_values is None:
90
- return self.language_model.model.embed_tokens(input_ids)
91
-
92
- # Get the input embeddings from the language model
93
- inputs_embeds = self.language_model.model.embed_tokens(input_ids)
94
-
95
- image_features = self.get_image_features(
96
- pixel_values=pixel_values,
97
- vision_feature_layer=kwargs.get("vision_feature_layer", -1),
98
- vision_feature_select_strategy=kwargs.get(
99
- "vision_feature_select_strategy", "default"
100
- ),
101
- )
102
-
103
- vision_flat = image_features.reshape(-1, image_features.shape[-1])
104
- projected_vision_flat = self.multi_modal_projector(vision_flat)
105
-
106
- # Insert special image tokens in the input_ids
107
- final_inputs_embeds = self._prepare_inputs_for_multimodal(
108
- projected_vision_flat, inputs_embeds, input_ids
109
- )
110
- return final_inputs_embeds
111
-
112
- def _prepare_inputs_for_multimodal(self, image_features, inputs_embeds, input_ids):
113
- image_token_index = self.config.image_token_index
114
-
115
- # Positions of <image> tokens in input_ids, assuming batch size is 1
116
- image_positions = np.where(input_ids == image_token_index)[1].tolist()
117
-
118
- inputs_embeds[:, image_positions, :] = image_features
119
-
120
- return inputs_embeds
121
-
122
- @property
123
- def layers(self):
124
- return self.language_model.model.layers
125
-
126
- def __call__(
127
- self,
128
- input_ids: mx.array,
129
- pixel_values: mx.array,
130
- cache=None,
131
- **kwargs,
132
- ):
133
-
134
- input_embeddings = self.get_input_embeddings(input_ids, pixel_values, **kwargs)
135
- logits = self.language_model(
136
- input_ids=input_ids, cache=cache, input_embeds=input_embeddings
137
- )
138
- return logits