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,526 +0,0 @@
1
- """
2
- From https://github.com/deepseek-ai/DeepSeek-VL2
3
- """
4
-
5
- import math
6
- from dataclasses import dataclass
7
- from typing import Dict, List, Literal, Optional, Tuple
8
-
9
- import mlx.core as mx
10
- import mlx.nn as nn
11
- import numpy as np
12
- from PIL import Image, ImageOps
13
- from transformers import LlamaTokenizerFast
14
- from transformers.processing_utils import ProcessorMixin
15
-
16
- from .conversation import get_conv_template
17
-
18
-
19
- def select_best_resolution(image_size, candidate_resolutions):
20
- # used for cropping
21
- original_width, original_height = image_size
22
- best_fit = None
23
- max_effective_resolution = 0
24
- min_wasted_resolution = float("inf")
25
-
26
- for width, height in candidate_resolutions:
27
- scale = min(width / original_width, height / original_height)
28
- downscaled_width, downscaled_height = int(original_width * scale), int(
29
- original_height * scale
30
- )
31
- effective_resolution = min(
32
- downscaled_width * downscaled_height, original_width * original_height
33
- )
34
- wasted_resolution = (width * height) - effective_resolution
35
-
36
- if effective_resolution > max_effective_resolution or (
37
- effective_resolution == max_effective_resolution
38
- and wasted_resolution < min_wasted_resolution
39
- ):
40
- max_effective_resolution = effective_resolution
41
- min_wasted_resolution = wasted_resolution
42
- best_fit = (width, height)
43
-
44
- return best_fit
45
-
46
-
47
- class DictOutput(object):
48
- def keys(self):
49
- return self.__dict__.keys()
50
-
51
- def __getitem__(self, item):
52
- if isinstance(item, int):
53
- return list(self.__dict__.values())[item]
54
- if item not in self.__dict__:
55
- raise KeyError(item)
56
- return self.__dict__[item]
57
-
58
- def __setitem__(self, key, value):
59
- self.__dict__[key] = value
60
-
61
-
62
- @dataclass
63
- class VLChatProcessorOutput(DictOutput):
64
- sft_format: str
65
- input_ids: mx.array
66
- target_ids: mx.array
67
- images: mx.array
68
- images_seq_mask: mx.array
69
- images_spatial_crop: mx.array
70
- num_image_tokens: List[int]
71
-
72
- def __len__(self):
73
- return len(self.input_ids)
74
-
75
-
76
- @dataclass
77
- class BatchCollateOutput(DictOutput):
78
- sft_format: List[str]
79
- input_ids: mx.array
80
- labels: mx.array
81
- images: mx.array
82
- attention_mask: mx.array
83
- images_seq_mask: mx.array
84
- images_spatial_crop: mx.array
85
- seq_lens: List[int]
86
-
87
-
88
- class ImageTransform:
89
- def __init__(
90
- self,
91
- mean: Optional[Tuple[float, float, float]] = (0.5, 0.5, 0.5),
92
- std: Optional[Tuple[float, float, float]] = (0.5, 0.5, 0.5),
93
- normalize: bool = True,
94
- ):
95
- self.mean = mean
96
- self.std = std
97
- self.normalize = normalize
98
-
99
- def __call__(self, pil_img: Image.Image):
100
- # Convert PIL image to numpy array and normalize
101
-
102
- img = mx.array(np.array(pil_img)) / 255.0
103
-
104
- # Transpose from HWC to CHW format
105
- img = mx.transpose(img, [2, 0, 1])
106
-
107
- if self.normalize:
108
- mean = mx.array(self.mean).reshape(-1, 1, 1)
109
- std = mx.array(self.std).reshape(-1, 1, 1)
110
- img = (img - mean) / std
111
-
112
- return img
113
-
114
-
115
- class DeepseekVLV2Processor(ProcessorMixin):
116
- tokenizer_class = ("LlamaTokenizer", "LlamaTokenizerFast")
117
- attributes = ["tokenizer"]
118
-
119
- def __init__(
120
- self,
121
- tokenizer: LlamaTokenizerFast,
122
- candidate_resolutions: Tuple[Tuple[int, int]],
123
- patch_size: int,
124
- downsample_ratio: int,
125
- image_mean: Tuple[float, float, float] = (0.5, 0.5, 0.5),
126
- image_std: Tuple[float, float, float] = (0.5, 0.5, 0.5),
127
- normalize: bool = True,
128
- image_token: str = "<image>",
129
- pad_token: str = "<|▁pad▁|>",
130
- add_special_token: bool = False,
131
- sft_format: str = "deepseek",
132
- mask_prompt: bool = True,
133
- ignore_id: int = -100,
134
- **kwargs,
135
- ):
136
- self.candidate_resolutions = candidate_resolutions
137
- self.image_size = candidate_resolutions[0][0]
138
- self.patch_size = patch_size
139
- self.image_mean = image_mean
140
- self.image_std = image_std
141
- self.normalize = normalize
142
- self.downsample_ratio = downsample_ratio
143
-
144
- self.image_transform = ImageTransform(
145
- mean=image_mean, std=image_std, normalize=normalize
146
- )
147
- self.tokenizer = tokenizer
148
- self.tokenizer.padding_side = "left"
149
-
150
- # Add special tokens
151
- if tokenizer.pad_token is None:
152
- self.tokenizer.add_special_tokens({"pad_token": pad_token})
153
- print(
154
- f"Add pad token = ['{pad_token}'] to the tokenizer\n"
155
- f"{pad_token}:{tokenizer.encode(pad_token, add_special_tokens=False)[0]}"
156
- )
157
-
158
- image_token_id = self.tokenizer.vocab.get(image_token)
159
- if image_token_id is None:
160
- special_tokens = [image_token]
161
- special_tokens_dict = {"additional_special_tokens": special_tokens}
162
- self.tokenizer.add_special_tokens(special_tokens_dict)
163
- self.image_token_id = self.tokenizer.vocab.get(image_token)
164
- print(
165
- f"Add image token = ['{image_token}'] to the tokenizer\n"
166
- f"{image_token}:{tokenizer.encode(image_token, add_special_tokens=False)[0]}"
167
- )
168
-
169
- # Add grounding-related tokens
170
- special_tokens = ["<|ref|>", "<|/ref|>", "<|det|>", "<|/det|>", "<|grounding|>"]
171
- special_tokens_dict = {"additional_special_tokens": special_tokens}
172
- self.tokenizer.add_special_tokens(special_tokens_dict)
173
- print("Added grounding-related tokens")
174
-
175
- # Add chat tokens
176
- special_tokens = ["<|User|>", "<|Assistant|>"]
177
- special_tokens_dict = {"additional_special_tokens": special_tokens}
178
- self.tokenizer.add_special_tokens(special_tokens_dict)
179
- print("Added chat tokens")
180
-
181
- self.image_token = image_token
182
- self.pad_token = pad_token
183
- self.add_special_token = add_special_token
184
- self.sft_format = sft_format
185
- self.mask_prompt = mask_prompt
186
- self.ignore_id = ignore_id
187
-
188
- super().__init__(tokenizer, **kwargs)
189
-
190
- @property
191
- def bos_id(self):
192
- return self.tokenizer.bos_token_id
193
-
194
- @property
195
- def eos_id(self):
196
- return self.tokenizer.eos_token_id
197
-
198
- @property
199
- def pad_id(self):
200
- return self.tokenizer.pad_token_id
201
-
202
- def encode(self, text: str, bos: bool = True, eos: bool = False):
203
- t = self.tokenizer.encode(text, add_special_tokens=False)
204
-
205
- if bos:
206
- t = [self.bos_id] + t
207
- if eos:
208
- t = t + [self.eos_id]
209
-
210
- return t
211
-
212
- def decode(self, t: List[int], **kwargs) -> str:
213
- return self.tokenizer.decode(t, **kwargs)
214
-
215
- def process_one(
216
- self,
217
- prompt: str = None,
218
- conversations: List[Dict[str, str]] = None,
219
- images: List[Image.Image] = None,
220
- apply_sft_format: bool = False,
221
- inference_mode: bool = True,
222
- system_prompt: str = "",
223
- **kwargs,
224
- ):
225
- assert (
226
- prompt is None or conversations is None
227
- ), "prompt and conversations cannot be used at the same time."
228
-
229
- if apply_sft_format:
230
- sft_format = self.format_prompts(
231
- prompts=prompt, sft_format=self.sft_format, system_prompt=system_prompt
232
- )
233
- else:
234
- sft_format = prompt
235
- (
236
- tokenized_str,
237
- images_list,
238
- images_seq_mask,
239
- images_spatial_crop,
240
- num_image_tokens,
241
- ) = self.tokenize_with_images(
242
- sft_format, images, bos=True, eos=True, cropping=len(images) <= 2
243
- )
244
- masked_tokenized_str = []
245
- for token_index in tokenized_str:
246
- if token_index != self.image_token_id:
247
- masked_tokenized_str.append(token_index)
248
- else:
249
- masked_tokenized_str.append(self.ignore_id)
250
-
251
- input_ids = mx.array(tokenized_str)
252
- target_ids = mx.array(masked_tokenized_str)
253
- images_seq_mask = mx.array(images_seq_mask)
254
-
255
- # Set ignored indices
256
- target_ids = mx.where(
257
- (input_ids < 0) | (input_ids == self.image_token_id),
258
- self.ignore_id,
259
- target_ids,
260
- )
261
- input_ids = mx.where(input_ids < 0, self.pad_id, input_ids)
262
-
263
- if inference_mode:
264
- input_ids = input_ids[:-1]
265
- target_ids = target_ids[:-1]
266
- images_seq_mask = images_seq_mask[:-1]
267
-
268
- if len(images_list) == 0:
269
- images = mx.zeros((1, 3, self.image_size, self.image_size))
270
- images_spatial_crop = mx.zeros((1, 2))
271
- else:
272
- images = mx.stack(images_list)
273
- images_spatial_crop = mx.array(images_spatial_crop)
274
-
275
- return VLChatProcessorOutput(
276
- sft_format=sft_format,
277
- input_ids=input_ids,
278
- target_ids=target_ids,
279
- images=images,
280
- images_seq_mask=images_seq_mask,
281
- images_spatial_crop=images_spatial_crop,
282
- num_image_tokens=num_image_tokens,
283
- )
284
-
285
- def pad_sequence(self, sequences, padding_value):
286
- # Get max length of sequences
287
- max_len = max(len(seq) for seq in sequences)
288
-
289
- # Pad each sequence to max length
290
- padded_seqs = []
291
- for seq in sequences:
292
- pad_length = max_len - len(seq)
293
- if pad_length > 0:
294
- padding = mx.full((pad_length,), padding_value)
295
- padded_seq = mx.concatenate([seq, padding])
296
- else:
297
- padded_seq = seq
298
- padded_seqs.append(padded_seq)
299
-
300
- return mx.stack(padded_seqs)
301
-
302
- def tokenize_with_images(
303
- self,
304
- conversation: str,
305
- images: List[Image.Image],
306
- bos: bool = True,
307
- eos: bool = True,
308
- cropping: bool = True,
309
- ):
310
- """Tokenize text with <image> tags."""
311
- assert conversation.count(self.image_token) == len(images)
312
- text_splits = conversation.split(self.image_token)
313
- images_list, images_seq_mask, images_spatial_crop = [], [], []
314
- num_image_tokens = []
315
- tokenized_str = []
316
- for text_sep, image in zip(text_splits, images):
317
- """encode text_sep"""
318
- tokenized_sep = self.encode(text_sep, bos=False, eos=False)
319
- tokenized_str += tokenized_sep
320
- images_seq_mask += [False] * len(tokenized_sep)
321
-
322
- """select best resolution for anyres"""
323
- if cropping:
324
- best_width, best_height = select_best_resolution(
325
- image.size, self.candidate_resolutions
326
- )
327
- else:
328
- best_width, best_height = self.image_size, self.image_size
329
-
330
- """process the global view"""
331
- global_view = ImageOps.pad(
332
- image,
333
- (self.image_size, self.image_size),
334
- color=tuple(int(x * 255) for x in self.image_transform.mean),
335
- )
336
- images_list.append(self.image_transform(global_view))
337
-
338
- """process the local views"""
339
- local_view = ImageOps.pad(
340
- image,
341
- (best_width, best_height),
342
- color=tuple(int(x * 255) for x in self.image_transform.mean),
343
- )
344
- for i in range(0, best_height, self.image_size):
345
- for j in range(0, best_width, self.image_size):
346
- images_list.append(
347
- self.image_transform(
348
- local_view.crop(
349
- (j, i, j + self.image_size, i + self.image_size)
350
- )
351
- )
352
- )
353
-
354
- """record height / width crop num"""
355
- num_width_tiles, num_height_tiles = (
356
- best_width // self.image_size,
357
- best_height // self.image_size,
358
- )
359
- images_spatial_crop.append([num_width_tiles, num_height_tiles])
360
-
361
- """add image tokens"""
362
- h = w = math.ceil(
363
- (self.image_size // self.patch_size) / self.downsample_ratio
364
- )
365
- # global views tokens h * (w + 1), 1 is for line seperator
366
- tokenized_image = [self.image_token_id] * h * (w + 1)
367
- # add a seperator between global and local views
368
- tokenized_image += [self.image_token_id]
369
- # local views tokens, (num_height_tiles * h) * (num_width_tiles * w + 1)
370
- tokenized_image += (
371
- [self.image_token_id]
372
- * (num_height_tiles * h)
373
- * (num_width_tiles * w + 1)
374
- )
375
-
376
- tokenized_str += tokenized_image
377
- images_seq_mask += [True] * len(tokenized_image)
378
- num_image_tokens.append(len(tokenized_image))
379
-
380
- """process the last text split"""
381
- tokenized_sep = self.encode(text_splits[-1], bos=False, eos=False)
382
- tokenized_str += tokenized_sep
383
- images_seq_mask += [False] * len(tokenized_sep)
384
-
385
- """add the bos and eos tokens"""
386
- if bos:
387
- tokenized_str = [self.bos_id] + tokenized_str
388
- images_seq_mask = [False] + images_seq_mask
389
- if eos:
390
- tokenized_str = tokenized_str + [self.eos_id]
391
- images_seq_mask = images_seq_mask + [False]
392
-
393
- assert len(tokenized_str) == len(
394
- images_seq_mask
395
- ), f"tokenize_with_images func: tokenized_str's length {len(tokenized_str)} is not equal to imags_seq_mask's length {len(images_seq_mask)}"
396
-
397
- return (
398
- tokenized_str,
399
- images_list,
400
- images_seq_mask,
401
- images_spatial_crop,
402
- num_image_tokens,
403
- )
404
-
405
- def batchify(
406
- self,
407
- sample_list: List[VLChatProcessorOutput],
408
- padding: Literal["left", "right"] = "left",
409
- ) -> BatchCollateOutput:
410
- batched_sft_format = [sample.sft_format for sample in sample_list]
411
- batched_input_ids = [sample.input_ids for sample in sample_list]
412
- batched_labels = [sample.target_ids for sample in sample_list]
413
- batched_images_seq_mask = [sample.images_seq_mask for sample in sample_list]
414
- seq_lens = [len(sample) for sample in sample_list]
415
-
416
- # Padding
417
- if padding == "left":
418
- # MLX implementation of padding
419
- max_len = max(len(ids) for ids in batched_input_ids)
420
-
421
- def pad_left(sequence, pad_val):
422
- pad_len = max_len - len(sequence)
423
- if pad_len > 0:
424
- padding = mx.full((pad_len,), pad_val)
425
- return mx.concatenate([padding, sequence])
426
- return sequence
427
-
428
- batched_input_ids = mx.stack(
429
- [pad_left(ids, self.pad_id) for ids in batched_input_ids]
430
- )
431
- batched_labels = mx.stack(
432
- [pad_left(ids, self.ignore_id) for ids in batched_labels]
433
- )
434
- batched_images_seq_mask = mx.stack(
435
- [pad_left(mask, False) for mask in batched_images_seq_mask]
436
- )
437
- batched_attention_mask = batched_input_ids != self.pad_id
438
-
439
- else:
440
- batched_input_ids = self.pad_sequence(batched_input_ids, self.pad_id)
441
- batched_labels = self.pad_sequence(batched_labels, self.ignore_id)
442
- batched_images_seq_mask = self.pad_sequence(batched_images_seq_mask, False)
443
- batched_attention_mask = batched_input_ids != self.pad_id
444
-
445
- # Padding images
446
- max_n_patches = max(sample.images.shape[0] for sample in sample_list)
447
- batched_images = []
448
- for sample in sample_list:
449
- images = sample.images
450
- n_pads = max_n_patches - images.shape[0]
451
- if n_pads > 0:
452
- pad_shape = (n_pads,) + images.shape[1:]
453
- pad_images = mx.zeros(pad_shape)
454
- images = mx.concatenate([images, pad_images])
455
- batched_images.append(images)
456
- batched_images = mx.stack(batched_images)
457
-
458
- # Padding spatial crop info
459
- max_n_images = max(
460
- sample.images_spatial_crop.shape[0] for sample in sample_list
461
- )
462
- batched_images_spatial_crop = []
463
- for sample in sample_list:
464
- spatial_crop = sample.images_spatial_crop
465
- n_pads = max_n_images - spatial_crop.shape[0]
466
- if n_pads > 0:
467
- pad_spatial = mx.zeros((n_pads, 2))
468
- spatial_crop = mx.concatenate([spatial_crop, pad_spatial])
469
- batched_images_spatial_crop.append(spatial_crop)
470
- batched_images_spatial_crop = mx.stack(batched_images_spatial_crop)
471
-
472
- return {
473
- "input_ids": batched_input_ids,
474
- "attention_mask": batched_attention_mask,
475
- "labels": batched_labels,
476
- "images": batched_images,
477
- "images_seq_mask": batched_images_seq_mask,
478
- "images_spatial_crop": batched_images_spatial_crop,
479
- "sft_format": batched_sft_format,
480
- "seq_lens": seq_lens,
481
- }
482
-
483
- def __call__(
484
- self,
485
- *,
486
- text: str = None,
487
- images: List[Image.Image] = None,
488
- apply_sft_format: bool = False,
489
- force_batchify: bool = True,
490
- inference_mode: bool = True,
491
- system_prompt: str = "",
492
- **kwargs,
493
- ):
494
- """
495
-
496
- Args:
497
- prompt (str): the formatted prompt;
498
- conversations (List[Dict]): conversations with a list of messages;
499
- images (List[ImageType]): the list of images;
500
- apply_sft_format (bool): if prompt is not None, then apply the SFT format to prompt;
501
- if conversations is not None, then it will always apply the SFT format to conversations;
502
- force_batchify (bool): force batchify the inputs;
503
- inference_mode (bool): if True, then remove the last eos token;
504
- system_prompt (str): the system prompt;
505
- **kwargs:
506
-
507
- Returns:
508
- outputs (BaseProcessorOutput): the output of the processor,
509
- - input_ids (torch.LongTensor): [N + image tokens]
510
- - images (torch.FloatTensor): [n_images, 3, H, W]
511
- - image_id (int): the id of the image token
512
- - num_image_tokens (List[int]): the number of image tokens
513
- """
514
-
515
- prepare = self.process_one(
516
- prompt=text,
517
- images=images,
518
- apply_sft_format=apply_sft_format,
519
- inference_mode=inference_mode,
520
- system_prompt=system_prompt,
521
- )
522
-
523
- if force_batchify:
524
- prepare = self.batchify([prepare])
525
-
526
- return prepare