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,356 +0,0 @@
1
- import copy
2
- import inspect
3
- from dataclasses import dataclass
4
- from functools import partial
5
- from math import sqrt
6
- from typing import Dict, Optional, Union
7
-
8
- import mlx.core as mx
9
- import mlx.nn as nn
10
- import numpy as np
11
-
12
-
13
- @dataclass
14
- class VisionConfig:
15
- model_type: str
16
- layers: int = 27
17
- width: int = 1152
18
- intermediate_size: int = 4304
19
- num_attention_heads: int = 16
20
- image_size: int = 384
21
- patch_size: int = 16
22
- num_channels: int = 3
23
- layer_norm_eps: float = 1e-6
24
- mlp_ratio: float = 3.7362
25
- cls: str = None
26
- params: dict = None
27
-
28
- @classmethod
29
- def from_dict(cls, params):
30
- return cls(
31
- **{
32
- k: v
33
- for k, v in params.items()
34
- if k in inspect.signature(cls).parameters
35
- }
36
- )
37
-
38
-
39
- @dataclass
40
- class MLPConfig:
41
- width: int
42
- intermediate_size: int
43
-
44
-
45
- def check_array_shape(arr):
46
- shape = arr.shape
47
-
48
- # Check if the shape has 4 dimensions
49
- if len(shape) != 4:
50
- return False
51
-
52
- out_channels, kH, KW, _ = shape
53
-
54
- # Check if out_channels is the largest, and kH and KW are the same
55
- if (out_channels >= kH) and (out_channels >= KW) and (kH == KW):
56
- return True
57
- else:
58
- return False
59
-
60
-
61
- class AttentionPoolLatent(nn.Module):
62
- """Attention pooling w/ latent query"""
63
-
64
- def __init__(
65
- self,
66
- in_features: int,
67
- out_features: int = None,
68
- embed_dim: int = None,
69
- num_heads: int = 8,
70
- mlp_ratio: float = 4.0,
71
- qkv_bias: bool = True,
72
- qk_norm: bool = False,
73
- latent_len: int = 1,
74
- latent_dim: int = None,
75
- pos_embed: str = "",
76
- pool_type: str = "token",
77
- norm_layer: Optional[nn.Module] = None,
78
- drop: float = 0.0,
79
- ):
80
- super().__init__()
81
-
82
- embed_dim = embed_dim or in_features
83
- out_features = out_features or in_features
84
- assert embed_dim % num_heads == 0
85
- self.num_heads = num_heads
86
- self.head_dim = embed_dim // num_heads
87
- self.scale = self.head_dim**-0.5
88
- self.pool = pool_type
89
-
90
- self.latent_dim = latent_dim or embed_dim
91
- self.latent_len = latent_len
92
- self.latent = mx.zeros((self.latent_len, embed_dim))[None, :]
93
-
94
- self.q = nn.Linear(embed_dim, embed_dim, bias=qkv_bias)
95
- self.kv = nn.Linear(embed_dim, embed_dim * 2, bias=qkv_bias)
96
- self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
97
- self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
98
- self.proj = nn.Linear(embed_dim, embed_dim)
99
- self.proj_drop = nn.Dropout(drop)
100
-
101
- if pos_embed == "abs":
102
- spatial_len = self.feat_size
103
- self.pos_embed = mx.zeros((spatial_len, in_features))
104
- else:
105
- self.pos_embed = None
106
-
107
- self.norm = nn.LayerNorm(out_features)
108
- config = MLPConfig(
109
- width=embed_dim, intermediate_size=int(embed_dim * mlp_ratio)
110
- )
111
- self.mlp = MLP(config)
112
-
113
- def __call__(self, x: mx.array):
114
- B, N, C = x.shape
115
-
116
- if self.pos_embed is not None:
117
- x = x + self.pos_embed.unsqueeze(0).to(x.dtype)
118
-
119
- q_latent = mx.array(self.latent)
120
-
121
- q = (
122
- self.q(q_latent)
123
- .reshape(B, self.latent_len, self.num_heads, self.head_dim)
124
- .transpose(0, 2, 1, 3)
125
- )
126
-
127
- kv = (
128
- self.kv(x)
129
- .reshape(B, N, 2, self.num_heads, self.head_dim)
130
- .transpose(2, 0, 3, 1, 4)
131
- )
132
- k, v = mx.split(kv, 2, axis=0)
133
-
134
- q, k = self.q_norm(q), self.k_norm(k)
135
-
136
- x = mx.fast.scaled_dot_product_attention(
137
- q, k[0], v[0], scale=(1.0 / sqrt(q.shape[-1])), mask=None
138
- )
139
-
140
- x = x.transpose(0, 2, 1, 3).reshape(B, self.latent_len, C)
141
- x = self.proj(x)
142
- x = self.proj_drop(x)
143
-
144
- x = x + self.mlp(self.norm(x))
145
-
146
- # optional pool if latent seq_len > 1 and pooled output is desired
147
- if self.pool == "token":
148
- x = x[:, 0]
149
- elif self.pool == "avg":
150
- x = x.mean(1)
151
- return x
152
-
153
-
154
- class Attention(nn.Module):
155
- def __init__(
156
- self,
157
- dims: int,
158
- num_heads: int,
159
- qkv_bias: bool = True,
160
- ):
161
- super().__init__()
162
-
163
- if (dims % num_heads) != 0:
164
- raise ValueError(
165
- "The input feature dimensions should be divisible by the "
166
- f"number of heads ({dims} % {num_heads}) != 0"
167
- )
168
-
169
- self.num_heads = num_heads = num_heads
170
- head_dim = dims // num_heads
171
- self.scale = head_dim**-0.5
172
-
173
- self.qkv = nn.Linear(dims, dims * 3, bias=qkv_bias)
174
- self.proj = nn.Linear(dims, dims, bias=True)
175
-
176
- def __call__(self, x, mask=None):
177
- qkv = self.qkv(x)
178
- queries, keys, values = mx.split(qkv, 3, axis=-1)
179
-
180
- num_heads = self.num_heads
181
- B, L, D = queries.shape
182
- _, S, _ = keys.shape
183
- queries = queries.reshape(B, L, num_heads, -1).transpose(0, 2, 1, 3)
184
- keys = keys.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
185
- values = values.reshape(B, S, num_heads, -1).transpose(0, 2, 1, 3)
186
-
187
- output = mx.fast.scaled_dot_product_attention(
188
- queries, keys, values, scale=self.scale, mask=mask
189
- )
190
- output = output.transpose(0, 2, 1, 3).reshape(B, L, -1)
191
-
192
- return self.proj(output)
193
-
194
-
195
- class MLP(nn.Module):
196
- def __init__(self, config: Union[VisionConfig, Dict], bias: bool = True):
197
- super().__init__()
198
- self.activation_fn = nn.GELU(approx="precise")
199
- self.fc1 = nn.Linear(config.width, config.intermediate_size, bias=bias)
200
- self.fc2 = nn.Linear(config.intermediate_size, config.width, bias=bias)
201
-
202
- def __call__(self, x: mx.array) -> mx.array:
203
- x = self.activation_fn(self.fc1(x))
204
- x = self.fc2(x)
205
- return x
206
-
207
-
208
- class EncoderLayer(nn.Module):
209
- def __init__(self, config: VisionConfig):
210
- super().__init__()
211
- self.embed_dim = config.width
212
- self.attn = Attention(config.width, config.num_attention_heads, qkv_bias=True)
213
- self.norm1 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
214
- self.mlp = MLP(config)
215
- self.norm2 = nn.LayerNorm(self.embed_dim, eps=config.layer_norm_eps)
216
-
217
- def __call__(self, x: mx.array, mask: Optional[mx.array] = None) -> mx.array:
218
- y = self.norm1(x)
219
- y = self.attn(y, mask)
220
- x = x + y
221
- y = self.norm2(x)
222
- y = self.mlp(y)
223
- return x + y
224
-
225
-
226
- class VisionEmbeddings(nn.Module):
227
- def __init__(self, config: VisionConfig, norm_layer: bool = False):
228
- super().__init__()
229
- self.config = config
230
- self.embed_dim = config.width
231
- self.image_size = config.image_size
232
- self.patch_size = config.patch_size
233
-
234
- self.proj = nn.Conv2d(
235
- in_channels=config.num_channels,
236
- out_channels=self.embed_dim,
237
- kernel_size=self.patch_size,
238
- stride=self.patch_size,
239
- )
240
-
241
- self.num_patches = (self.image_size // self.patch_size) ** 2
242
- self.num_positions = self.num_patches
243
-
244
- self.norm = (
245
- nn.LayerNorm(config.width, eps=config.layer_norm_eps)
246
- if norm_layer
247
- else nn.Identity()
248
- )
249
-
250
- def __call__(self, x: mx.array) -> mx.array:
251
- patch_embeddings = self.proj(x)
252
- patch_embeddings = mx.flatten(patch_embeddings, start_axis=1, end_axis=2)
253
- return self.norm(patch_embeddings)
254
-
255
-
256
- class SigLipVisionModel(nn.Module):
257
- def __init__(
258
- self,
259
- config: VisionConfig,
260
- ignore_head: bool,
261
- pre_norm: bool = False,
262
- no_embed_class: bool = True,
263
- ):
264
- super().__init__()
265
- self.num_prefix_tokens = 1
266
- self.no_embed_class = False
267
- self.dynamic_img_size = False
268
- self.ignore_head = ignore_head
269
- self.cls_token = None
270
- self.reg_token = None
271
- self.patch_embed = VisionEmbeddings(config)
272
- self.norm_pre = nn.LayerNorm(config.width) if pre_norm else nn.Identity()
273
- self.blocks = [EncoderLayer(config) for _ in range(config.layers)]
274
- self.norm = nn.LayerNorm(config.width)
275
- num_patches = self.patch_embed.num_patches
276
- embed_len = (
277
- num_patches if no_embed_class else num_patches + self.num_prefix_tokens
278
- )
279
- self.pos_embed = mx.random.normal((embed_len, config.width))[None, :]
280
-
281
- norm_layer = partial(nn.LayerNorm, eps=1e-5)
282
- self.attn_pool = AttentionPoolLatent(
283
- config.width,
284
- num_heads=config.num_attention_heads,
285
- norm_layer=norm_layer,
286
- mlp_ratio=config.mlp_ratio,
287
- )
288
-
289
- def __call__(
290
- self,
291
- x: mx.array,
292
- output_hidden_states: Optional[bool] = None,
293
- ) -> mx.array:
294
- x = self.patch_embed(x)
295
- x += self.pos_embed
296
- x = self.norm_pre(x)
297
-
298
- encoder_states = (x,) if output_hidden_states else None
299
- for l in self.blocks:
300
- x = l(x, mask=None)
301
- if output_hidden_states:
302
- encoder_states = encoder_states + (x,)
303
-
304
- pooler_output = self.norm(x)
305
-
306
- if not self.ignore_head:
307
- pooler_output = self.attn_pool(pooler_output)
308
- return pooler_output, x, encoder_states
309
-
310
-
311
- class VisionModel(nn.Module):
312
- def __init__(self, config: VisionConfig, ignore_head: bool = True):
313
- super().__init__()
314
-
315
- self.model_type = config.model_type
316
- self.config = config
317
- if self.model_type != "vision":
318
- raise ValueError(f"Unsupported model type: {self.model_type}")
319
-
320
- self.vision_tower = SigLipVisionModel(config, ignore_head)
321
-
322
- def __call__(
323
- self, x: mx.array, output_hidden_states: Optional[bool] = None
324
- ) -> mx.array:
325
- return self.vision_tower(x, output_hidden_states)
326
-
327
- def sanitize(self, weights):
328
- sanitized_weights = {}
329
- weight_keys = {
330
- "neck.0.weight",
331
- "neck.2.weight",
332
- "neck_hd.0.weight",
333
- "neck_hd.2.weight",
334
- "downsamples.0.weight",
335
- "downsamples.1.weight",
336
- "patch_embed.proj.weight",
337
- }
338
- for k, v in weights.items():
339
- if "position_ids" in k:
340
- # Remove unused position_ids
341
- continue
342
-
343
- elif ".".join(k.split(".")[-3:]) in weight_keys:
344
- # PyTorch conv2d weight tensors have shape:
345
- # [out_channels, in_channels, kH, KW]
346
- # MLX conv2d expects the weight be of shape:
347
- # [out_channels, kH, KW, in_channels]
348
- if check_array_shape(v):
349
- sanitized_weights[k] = v
350
- else:
351
- sanitized_weights[k] = v.transpose(0, 2, 3, 1)
352
-
353
- else:
354
- sanitized_weights[k] = v
355
-
356
- return sanitized_weights
@@ -1,8 +0,0 @@
1
- from .florence2 import (
2
- LanguageModel,
3
- Model,
4
- ModelConfig,
5
- TextConfig,
6
- VisionConfig,
7
- VisionModel,
8
- )