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,472 +0,0 @@
1
- import glob
2
- import inspect
3
- import json
4
- import math
5
- from dataclasses import dataclass
6
- from pathlib import Path
7
- from typing import List, Optional, Tuple, Union
8
-
9
- import mlx.core as mx
10
- import mlx.nn as nn
11
- import numpy as np
12
- from huggingface_hub import snapshot_download
13
- from PIL import Image
14
- from transformers import AutoProcessor
15
- from transformers.image_processing_utils import BaseImageProcessor, BatchFeature
16
- from transformers.image_utils import to_numpy_array
17
-
18
- from ..base import expand2square
19
- from .language import LanguageModel, TextConfig
20
- from .processing_deepsek_vl_v2 import DeepseekVLV2Processor
21
- from .vision import VisionConfig, VisionModel
22
-
23
- AutoProcessor.register("deepseek_vl_v2", DeepseekVLV2Processor)
24
-
25
-
26
- @dataclass
27
- class ProjectorConfig:
28
- projector_type: str = "downsample_mlp_gelu"
29
- input_dim: int = 1152
30
- n_embed: int = 2048
31
- depth: int = 2
32
- mlp_ratio: int = 1
33
- downsample_ratio: int = 2
34
- token_pooling: bool = False
35
-
36
- @classmethod
37
- def from_dict(cls, params):
38
- return cls(
39
- **{
40
- k: v
41
- for k, v in params.items()
42
- if k in inspect.signature(cls).parameters
43
- }
44
- )
45
-
46
-
47
- @dataclass
48
- class ModelConfig:
49
- text_config: TextConfig
50
- vision_config: VisionConfig
51
- projector_config: ProjectorConfig
52
- model_type: str
53
- ignore_index: int = -100
54
- image_token_index: int = 100015
55
- vision_feature_select_strategy: str = "default"
56
- select_layer: int = -1
57
- pad_id: int = 100001
58
- num_image_tokens: int = 576
59
- vocab_size: int = 32000
60
- tile_tag: str = "2D"
61
- global_view_pos: str = "head"
62
- eos_token_id: Optional[List[int]] = None
63
-
64
- @classmethod
65
- def from_dict(cls, params):
66
- if "language_config" in params:
67
- params["text_config"] = params["language_config"]
68
- del params["language_config"]
69
-
70
- return cls(
71
- **{
72
- k: v
73
- for k, v in params.items()
74
- if k in inspect.signature(cls).parameters
75
- }
76
- )
77
-
78
-
79
- class MlpProjector(nn.Module):
80
- def __init__(self, config: ProjectorConfig):
81
- super().__init__()
82
- self.config = config
83
- if config.projector_config.projector_type == "identity":
84
- modules = nn.Identity()
85
- elif config.projector_config.projector_type == "linear":
86
- modules = nn.Linear(
87
- config.projector_config.input_dim, config.projector_config.n_embed
88
- )
89
- elif config.projector_config.projector_type == "mlp_gelu":
90
- mlp_depth = config.projector_config.depth
91
- modules = [
92
- nn.Linear(
93
- config.projector_config.input_dim, config.projector_config.n_embed
94
- )
95
- ]
96
- for _ in range(1, mlp_depth):
97
- modules.append(nn.GELU())
98
- modules.append(
99
- nn.Linear(
100
- config.projector_config.n_embed, config.projector_config.n_embed
101
- )
102
- )
103
- elif config.projector_config.projector_type == "downsample_mlp_gelu":
104
- mlp_depth = config.projector_config.depth
105
- mlp_ratio = config.projector_config.mlp_ratio
106
- modules = [
107
- nn.Linear(
108
- config.projector_config.input_dim
109
- * config.projector_config.downsample_ratio
110
- * config.projector_config.downsample_ratio,
111
- config.projector_config.n_embed * mlp_ratio,
112
- )
113
- ]
114
- for _ in range(1, mlp_depth - 1):
115
- modules.append(nn.GELU())
116
- modules.append(
117
- nn.Linear(
118
- config.projector_config.n_embed * mlp_ratio,
119
- config.projector_config.n_embed * mlp_ratio,
120
- )
121
- )
122
- modules.append(nn.GELU())
123
- modules.append(
124
- nn.Linear(
125
- config.projector_config.n_embed * mlp_ratio,
126
- config.projector_config.n_embed,
127
- )
128
- )
129
- else:
130
- raise ValueError(
131
- f"Unknown projector type: {config.projector_config.projector_type}"
132
- )
133
-
134
- if config.projector_config.token_pooling:
135
- self.token_pooling_layer = nn.Linear(
136
- config.projector_config.input_dim * 4, config.projector_config.input_dim
137
- )
138
- self.layers = modules
139
-
140
- def __call__(self, x):
141
- if self.config.projector_config.token_pooling:
142
- batch_size, wxh, channels = x.shape
143
- w = h = int(math.sqrt(wxh))
144
- x = mx.reshape(x, (batch_size, w, h, channels))
145
- x = mx.transpose(x, (0, 3, 1, 2)) # B, C, H, W
146
-
147
- # Implement unfold operation manually since MLX doesn't have unfold
148
- patches = []
149
- for i in range(0, h - 1, 2):
150
- for j in range(0, w - 1, 2):
151
- patch = x[:, :, i : i + 2, j : j + 2]
152
- patches.append(patch)
153
-
154
- patches = mx.stack(patches, axis=2) # B, C, N_patches, 2, 2
155
- batch_size, channels, n_patches, _, _ = patches.shape
156
-
157
- # Reshape and concatenate
158
- patches = mx.reshape(patches, (batch_size, channels, n_patches, -1))
159
- patches = mx.transpose(patches, (0, 2, 1, 3))
160
- patches = mx.reshape(patches, (batch_size, n_patches, channels * 4))
161
- x = self.token_pooling_layer(patches)
162
-
163
- elif self.config.projector_config.projector_type == "downsample_mlp_gelu":
164
- bs, hw, input_dim = x.shape
165
- h = w = int(math.sqrt(hw))
166
-
167
- # Compute padding
168
- pad = (
169
- 0
170
- if h % self.config.projector_config.downsample_ratio == 0
171
- else self.config.projector_config.downsample_ratio
172
- - h % self.config.projector_config.downsample_ratio
173
- )
174
-
175
- x = mx.reshape(x, (bs, h, w, input_dim))
176
- if pad > 0:
177
- x = mx.pad(x, [(0, 0), (0, pad), (0, pad), (0, 0)], constant_values=0)
178
-
179
- x = mx.transpose(x, (0, 3, 1, 2)) # B, C, H, W
180
-
181
- # Manual implementation of unfold for downsampling
182
- h_pad, w_pad = x.shape[2], x.shape[3]
183
- ds = self.config.projector_config.downsample_ratio
184
- patches = []
185
-
186
- for i in range(0, h_pad - ds + 1, ds):
187
- for j in range(0, w_pad - ds + 1, ds):
188
- patch = x[:, :, i : i + ds, j : j + ds]
189
- patches.append(mx.reshape(patch, (bs, -1)))
190
-
191
- x = mx.stack(patches, axis=1) # B, N_patches, C*ds*ds
192
-
193
- for layer in self.layers:
194
- x = layer(x)
195
- return x
196
-
197
-
198
- class Model(nn.Module):
199
- def __init__(self, config: ModelConfig):
200
- super().__init__()
201
- self.config = config
202
- self.vision = VisionModel(config.vision_config)
203
- self.language_model = LanguageModel(config.text_config)
204
- self.projector = MlpProjector(config)
205
- self.vision_feature_layer = config.select_layer
206
- self.vision_feature_select_strategy = config.vision_feature_select_strategy
207
-
208
- self.tile_tag = config.tile_tag
209
- self.global_view_pos = config.global_view_pos
210
-
211
- # 用于format image token sequence的特殊token
212
- embed_std = 1 / mx.sqrt(
213
- mx.array(config.projector_config.n_embed, dtype=mx.float32)
214
- )
215
- if self.tile_tag == "2D":
216
- # <|view_separator|>, <|\n|>
217
- self.image_newline = mx.array(
218
- mx.random.normal((config.projector_config.n_embed,)) * embed_std
219
- )
220
- # fix the typo: view_seperater
221
- self.view_separator = mx.array(
222
- mx.random.normal((config.projector_config.n_embed,)) * embed_std
223
- )
224
- elif self.tile_tag == "1D":
225
- # <|tile_x|>, <|tile_global|>
226
- candidate_resolutions = config.candidate_resolutions
227
- if len(candidate_resolutions) == 0:
228
- raise ValueError(
229
- f"len(candidate_resolutions) should be larger than 0, but got {len(candidate_resolutions)}"
230
- )
231
- tile_variants_num = len(candidate_resolutions)
232
- self.tile_indicators = mx.array(
233
- mx.random.normal(
234
- (tile_variants_num + 1, config.projector_config.n_embed)
235
- )
236
- * embed_std
237
- )
238
- else:
239
- raise ValueError(
240
- f"tile tag should be either 1D or 2D, but got {self.tile_tag}"
241
- )
242
-
243
- def process_image_features(
244
- self,
245
- input_embeds,
246
- images_embeds,
247
- images_spatial_crop,
248
- images_seq_mask,
249
- h,
250
- w,
251
- n_dim,
252
- ):
253
- tile_index = 0
254
- all_batch_features = []
255
-
256
- for idx in range(images_spatial_crop.shape[0]):
257
- images_in_this_batch = []
258
- for jdx in range(images_spatial_crop.shape[1]):
259
- # Extract global & local features
260
- num_width_tiles, num_height_tiles = images_spatial_crop[idx, jdx]
261
- if num_width_tiles == 0 or num_height_tiles == 0:
262
- break
263
-
264
- num_tiles_in_image = (num_width_tiles * num_height_tiles).tolist()
265
-
266
- # Get global features [hw, D]
267
- global_features = images_embeds[tile_index]
268
-
269
- # Get local features [num_height_tiles * num_width_tiles, hw, D]
270
- local_features = images_embeds[
271
- tile_index + 1 : tile_index + 1 + num_tiles_in_image
272
- ]
273
-
274
- tile_index += num_tiles_in_image + 1
275
-
276
- # Format global and local features
277
- if self.tile_tag == "2D":
278
- # ----------------- global view add newline -----------------
279
- # [hw, D] -> [h, w, D]
280
- global_features = mx.reshape(global_features, (h, w, n_dim))
281
-
282
- # [D] -> [h, 1, D]
283
- new_lines_in_global = mx.expand_dims(self.image_newline, axis=0)
284
- new_lines_in_global = mx.repeat(
285
- new_lines_in_global, repeats=h, axis=0
286
- )
287
- new_lines_in_global = mx.expand_dims(new_lines_in_global, axis=1)
288
-
289
- # cat([h, w, D], [h, 1, D], dim=1) -> [h, w + 1, D]
290
- global_features = mx.concatenate(
291
- [global_features, new_lines_in_global], axis=1
292
- )
293
-
294
- # [h, w + 1, D] -> [h * (w + 1), D]
295
- global_features = mx.reshape(global_features, (-1, n_dim))
296
-
297
- # ----------------- local view add newline -----------------
298
- # Rearrange local features
299
- # [num_height_tiles * num_width_tiles, h * w, D] -> [num_height_tiles * h, num_width_tiles * w, D]
300
- local_features = mx.reshape(
301
- local_features, (num_height_tiles, num_width_tiles, h, w, n_dim)
302
- )
303
- local_features = mx.transpose(local_features, (0, 2, 1, 3, 4))
304
- local_features = mx.reshape(
305
- local_features,
306
- (num_height_tiles * h, num_width_tiles * w, n_dim),
307
- )
308
-
309
- # Create newlines for local features
310
- # [D] -> [num_height_tiles * h, 1, D]
311
- new_lines_in_local = mx.repeat(
312
- mx.expand_dims(self.image_newline, axis=0),
313
- repeats=num_height_tiles * h,
314
- axis=0,
315
- )
316
- new_lines_in_local = mx.expand_dims(new_lines_in_local, axis=1)
317
-
318
- # [num_height_tiles * h, num_width_tiles * w + 1, D]
319
- local_features = mx.concatenate(
320
- [local_features, new_lines_in_local], axis=1
321
- )
322
-
323
- # [(num_height_tiles * h) * (num_width_tiles * w + 1), D]
324
- local_features = mx.reshape(local_features, (-1, n_dim))
325
-
326
- # ----------------- merge global and local tiles -----------------
327
- view_separator = mx.expand_dims(self.view_separator, axis=0)
328
-
329
- if self.global_view_pos == "head":
330
- global_local_features = mx.concatenate(
331
- [global_features, view_separator, local_features], axis=0
332
- )
333
- else:
334
- global_local_features = mx.concatenate(
335
- [local_features, view_separator, global_features], axis=0
336
- )
337
-
338
- else:
339
- # 1D processing (legacy path)
340
- global_features = mx.concatenate(
341
- [
342
- mx.expand_dims(self.tile_indicators[0], axis=0),
343
- global_features,
344
- ],
345
- axis=0,
346
- )
347
-
348
- local_indicators = mx.expand_dims(
349
- self.tile_indicators[1 : num_tiles_in_image + 1], axis=1
350
- )
351
- local_features = mx.concatenate(
352
- [local_indicators, local_features], axis=1
353
- )
354
- local_features = mx.reshape(local_features, (-1, n_dim))
355
-
356
- if self.global_view_pos == "head":
357
- global_local_features = mx.concatenate(
358
- [global_features, local_features], axis=0
359
- )
360
- else:
361
- global_local_features = mx.concatenate(
362
- [local_features, global_features], axis=0
363
- )
364
-
365
- images_in_this_batch.append(global_local_features)
366
-
367
- if images_in_this_batch:
368
- images_in_this_batch = mx.concatenate(images_in_this_batch, axis=0)
369
- # Find positions where images should be placed
370
- image_indices = np.where(images_seq_mask[idx])[0].tolist()
371
- # Directly assign the image features to those positions
372
- input_embeds[idx, image_indices] = images_in_this_batch
373
-
374
- return input_embeds
375
-
376
- def get_input_embeddings(
377
- self,
378
- input_ids: Optional[mx.array] = None,
379
- pixel_values: Optional[mx.array] = None,
380
- images_spatial_crop: Optional[mx.array] = None,
381
- image_seq_mask: Optional[mx.array] = None,
382
- ):
383
- if pixel_values is None:
384
- return self.language_model.model.embed_tokens(input_ids)
385
-
386
- bs = pixel_values.shape[0]
387
- max_n_images = pixel_values.shape[1]
388
-
389
- batch_num_tiles = [0 for _ in range(bs)]
390
- total_tiles = []
391
-
392
- # Total number of tiles in each batch
393
- for idx in range(bs):
394
- for jdx in range(max_n_images):
395
- num_width_tiles, num_height_tiles = images_spatial_crop[idx][jdx]
396
- if num_width_tiles == 0 or num_height_tiles == 0:
397
- break
398
- batch_num_tiles[idx] += (
399
- 1 + num_width_tiles * num_height_tiles
400
- ).tolist()
401
-
402
- total_tiles.append(pixel_values[idx, : batch_num_tiles[idx]])
403
-
404
- total_tiles = mx.concatenate(total_tiles, axis=0)
405
-
406
- if total_tiles.shape[0] == 0:
407
- return self.language_model.model.embed_tokens(input_ids)
408
-
409
- # Get the input embeddings from the language model
410
- input_embeds = self.language_model.model.embed_tokens(input_ids)
411
-
412
- # Get the ouptut hidden states from the vision model
413
- hidden_states, *_ = self.vision(
414
- total_tiles.transpose(0, 2, 3, 1), output_hidden_states=True
415
- )
416
-
417
- # Pass image features through the multi-modal projector
418
- image_features = self.projector(hidden_states)
419
-
420
- _, hw, n_dim = image_features.shape
421
- h = w = int(hw**0.5)
422
-
423
- image_features = self.process_image_features(
424
- input_embeds,
425
- image_features,
426
- images_spatial_crop,
427
- image_seq_mask,
428
- h,
429
- w,
430
- n_dim,
431
- )
432
-
433
- return image_features
434
-
435
- @property
436
- def layers(self):
437
- return self.language_model.model.layers
438
-
439
- def __call__(
440
- self,
441
- input_ids: mx.array,
442
- pixel_values: Optional[mx.array] = None,
443
- mask: Optional[mx.array] = None,
444
- cache=None,
445
- **kwargs,
446
- ):
447
-
448
- images_spatial_crop = kwargs.get("images_spatial_crop", None)
449
- images_seq_mask = kwargs.get("images_seq_mask", None)
450
- input_embeddings = self.get_input_embeddings(
451
- input_ids, pixel_values, images_spatial_crop, images_seq_mask
452
- )
453
- logits = self.language_model(
454
- input_ids, cache=cache, inputs_embeds=input_embeddings
455
- )
456
- return logits
457
-
458
- @staticmethod
459
- def sanitize(weights):
460
- def transform_key(key):
461
- if "language" in key and "language_model" not in key:
462
- if ".model" in key:
463
- key = key.replace("language.model", "language_model.model")
464
- if ".lm_head" in key:
465
- key = key.replace("language", "language_model")
466
- if "vision" in key and "vision_tower" not in key:
467
- key = key.replace("vision", "vision.vision_tower")
468
- if "view_seperator" in key:
469
- key = key.replace("view_seperator", "view_separator")
470
- return key
471
-
472
- return {transform_key(k): v for k, v in weights.items()}