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,488 +0,0 @@
1
- import inspect
2
- import math
3
- from dataclasses import dataclass
4
- from typing import Optional, Tuple
5
-
6
- import mlx.core as mx
7
- import mlx.nn as nn
8
-
9
- from ..base import (
10
- LanguageModelOutput,
11
- create_attention_mask,
12
- scaled_dot_product_attention,
13
- )
14
- from ..cache import SimpleKVCache
15
-
16
-
17
- @dataclass
18
- class TextConfig:
19
- d_model: int = 768
20
- model_type: str = "florence2"
21
- encoder_attention_heads: int = 8
22
- decoder_attention_heads: int = 8
23
- encoder_ffn_dim: int = 3072
24
- decoder_ffn_dim: int = 3072
25
- dropout: float = 0.1
26
- attention_dropout: float = 0.0
27
- activation_dropout: float = 0.0
28
- activation_function: str = "gelu"
29
- init_std: float = 0.02
30
- encoder_layerdrop: float = 0.0
31
- decoder_layerdrop: float = 0.0
32
- scale_embedding: bool = False
33
- use_cache: bool = True
34
- max_position_embeddings: int = 1024
35
- vocab_size: int = 51289
36
- pad_token_id: int = 1
37
- bos_token_id: int = 0
38
- eos_token_id: int = 2
39
- encoder_layers: int = 6
40
- decoder_layers: int = 6
41
-
42
- @classmethod
43
- def from_dict(cls, params):
44
- return cls(
45
- **{
46
- k: v
47
- for k, v in params.items()
48
- if k in inspect.signature(cls).parameters
49
- }
50
- )
51
-
52
-
53
- class Florence2Attention(nn.Module):
54
- def __init__(
55
- self, config: TextConfig, is_decoder: bool = False, is_causal: bool = False
56
- ):
57
- super().__init__()
58
- self.embed_dim = config.d_model
59
- self.num_heads = (
60
- config.decoder_attention_heads
61
- if is_decoder
62
- else config.encoder_attention_heads
63
- )
64
- self.is_decoder = is_decoder
65
- self.is_causal = is_causal
66
- self.head_dim = self.embed_dim // self.num_heads
67
- self.scaling = self.head_dim**-0.5
68
-
69
- self.k_proj = nn.Linear(self.embed_dim, self.embed_dim)
70
- self.v_proj = nn.Linear(self.embed_dim, self.embed_dim)
71
- self.q_proj = nn.Linear(self.embed_dim, self.embed_dim)
72
- self.out_proj = nn.Linear(self.embed_dim, self.embed_dim)
73
-
74
- def __call__(
75
- self,
76
- hidden_states,
77
- key_value_states=None,
78
- cache: Optional[SimpleKVCache] = None,
79
- attention_mask=None,
80
- layer_head_mask=None,
81
- ):
82
- batch_size, tgt_len, _ = hidden_states.shape
83
-
84
- q = (
85
- self.q_proj(hidden_states)
86
- .reshape(batch_size, tgt_len, self.num_heads, self.head_dim)
87
- .transpose(0, 2, 1, 3)
88
- )
89
-
90
- is_cross_attention = key_value_states is not None
91
-
92
- batch_size, tgt_len, _ = hidden_states.shape
93
- src_len = (
94
- key_value_states.shape[1]
95
- if key_value_states is not None
96
- else hidden_states.shape[1]
97
- )
98
-
99
- if (
100
- is_cross_attention
101
- and cache.cache_length > 0
102
- and cache.keys.shape[2] == key_value_states.shape[1]
103
- ):
104
- # k = cache[0]
105
- # v = cache[1]
106
- k = cache.keys
107
- v = cache.values
108
-
109
- elif is_cross_attention:
110
- # Cross attention
111
- k = (
112
- self.k_proj(key_value_states)
113
- .reshape(batch_size, src_len, self.num_heads, self.head_dim)
114
- .transpose(0, 2, 1, 3)
115
- )
116
- v = (
117
- self.v_proj(key_value_states)
118
- .reshape(batch_size, src_len, self.num_heads, self.head_dim)
119
- .transpose(0, 2, 1, 3)
120
- )
121
- elif cache is not None:
122
- # reuse k, v, self_attention
123
- k = (
124
- self.k_proj(hidden_states)
125
- .reshape(batch_size, src_len, self.num_heads, -1)
126
- .transpose(0, 2, 1, 3)
127
- )
128
- v = (
129
- self.v_proj(hidden_states)
130
- .reshape(batch_size, src_len, self.num_heads, -1)
131
- .transpose(0, 2, 1, 3)
132
- )
133
-
134
- k, v = cache.update_and_fetch(k, v)
135
- else:
136
- # Self attention
137
- k = (
138
- self.k_proj(hidden_states)
139
- .reshape(batch_size, src_len, self.num_heads, self.head_dim)
140
- .transpose(0, 2, 1, 3)
141
- )
142
- v = (
143
- self.v_proj(hidden_states)
144
- .reshape(batch_size, src_len, self.num_heads, self.head_dim)
145
- .transpose(0, 2, 1, 3)
146
- )
147
-
148
- if self.is_decoder:
149
- cache.update(k, v)
150
-
151
- if self.is_causal and self.is_decoder:
152
- causal_mask = create_attention_mask(hidden_states)
153
- attention_mask = causal_mask
154
-
155
- attn_output = (
156
- scaled_dot_product_attention(
157
- q, k, v, cache=cache, scale=self.scaling, mask=attention_mask
158
- )
159
- .transpose(0, 2, 1, 3)
160
- .reshape(batch_size, tgt_len, -1)
161
- )
162
-
163
- attn_output = self.out_proj(attn_output)
164
-
165
- return attn_output
166
-
167
-
168
- class Florence2EncoderLayer(nn.Module):
169
- def __init__(self, config: TextConfig):
170
- super().__init__()
171
- self.embed_dim = config.d_model
172
- self.self_attn = Florence2Attention(config, is_decoder=False, is_causal=False)
173
- self.self_attn_layer_norm = nn.LayerNorm(self.embed_dim)
174
- self.activation_fn = nn.GELU()
175
- self.fc1 = nn.Linear(self.embed_dim, config.encoder_ffn_dim)
176
- self.fc2 = nn.Linear(config.encoder_ffn_dim, self.embed_dim)
177
- self.final_layer_norm = nn.LayerNorm(self.embed_dim)
178
-
179
- def __call__(self, hidden_states, attention_mask=None):
180
- residual = hidden_states
181
- hidden_states = self.self_attn(hidden_states, attention_mask=attention_mask)
182
- hidden_states = residual + hidden_states
183
- hidden_states = self.self_attn_layer_norm(hidden_states)
184
-
185
- residual = hidden_states
186
- hidden_states = self.activation_fn(self.fc1(hidden_states))
187
- hidden_states = self.fc2(hidden_states)
188
- hidden_states = residual + hidden_states
189
- hidden_states = self.final_layer_norm(hidden_states)
190
-
191
- return hidden_states
192
-
193
-
194
- class Florence2DecoderLayer(nn.Module):
195
- def __init__(self, config: TextConfig):
196
- super().__init__()
197
- self.embed_dim = config.d_model
198
- self.self_attn = Florence2Attention(config, is_decoder=True, is_causal=True)
199
- self.dropout = config.dropout
200
- self.activation_fn = nn.GELU()
201
- self.activation_dropout = config.activation_dropout
202
-
203
- self.self_attn_layer_norm = nn.LayerNorm(self.embed_dim)
204
- self.encoder_attn = Florence2Attention(config, is_decoder=True)
205
- self.encoder_attn_layer_norm = nn.LayerNorm(self.embed_dim)
206
- self.fc1 = nn.Linear(self.embed_dim, config.decoder_ffn_dim)
207
- self.fc2 = nn.Linear(config.decoder_ffn_dim, self.embed_dim)
208
- self.final_layer_norm = nn.LayerNorm(self.embed_dim)
209
-
210
- def __call__(
211
- self,
212
- hidden_states,
213
- encoder_hidden_states,
214
- attention_mask=None,
215
- encoder_attention_mask=None,
216
- cache: Optional[Tuple[SimpleKVCache, SimpleKVCache]] = None,
217
- ):
218
- residual = hidden_states
219
-
220
- # decoder uni-directional self-attention cached key/values tuple is at positions 1,2
221
- self_attn_cache = cache[0] if cache[0] is not None else None
222
-
223
- hidden_states = self.self_attn(
224
- hidden_states, attention_mask=attention_mask, cache=self_attn_cache
225
- )
226
-
227
- hidden_states = residual + hidden_states
228
- hidden_states = self.self_attn_layer_norm(hidden_states)
229
-
230
- if encoder_hidden_states is not None:
231
- residual = hidden_states
232
-
233
- mask = create_attention_mask(hidden_states)
234
-
235
- # cross_attn cached key/values tuple is at positions 3,4 of cache tuple
236
- cross_attn_cache = cache[-1] if cache[-1] is not None else None
237
-
238
- hidden_states = self.encoder_attn(
239
- hidden_states,
240
- key_value_states=encoder_hidden_states,
241
- attention_mask=mask,
242
- cache=cross_attn_cache,
243
- )
244
- hidden_states = residual + hidden_states
245
- hidden_states = self.encoder_attn_layer_norm(hidden_states)
246
-
247
- # Fully Connected
248
- residual = hidden_states
249
- hidden_states = self.activation_fn(self.fc1(hidden_states))
250
- hidden_states = self.fc2(hidden_states)
251
- hidden_states = residual + hidden_states
252
- hidden_states = self.final_layer_norm(hidden_states)
253
-
254
- return hidden_states
255
-
256
-
257
- class Florence2Encoder(nn.Module):
258
- def __init__(self, config: TextConfig):
259
- super().__init__()
260
- self.config = config
261
- self.dropout = config.dropout
262
- self.layerdrop = config.encoder_layerdrop
263
-
264
- embed_dim = config.d_model
265
- self.embed_scale = math.sqrt(embed_dim) if config.scale_embedding else 1.0
266
- self.offset = 2
267
- self.embed_positions = nn.Embedding(
268
- config.max_position_embeddings + self.offset, embed_dim
269
- )
270
- self.layers = [
271
- Florence2EncoderLayer(config) for _ in range(config.encoder_layers)
272
- ]
273
- self.layernorm_embedding = nn.LayerNorm(embed_dim)
274
-
275
- def __call__(self, input_ids=None, inputs_embeds=None, attention_mask=None):
276
-
277
- if inputs_embeds is None:
278
- inputs_embeds = self.embed_tokens(input_ids)
279
- input_shape = inputs_embeds.shape
280
- else:
281
- input_shape = inputs_embeds.shape
282
-
283
- positions = mx.arange(input_shape[1])
284
-
285
- if positions.ndim == 1:
286
- positions = mx.expand_dims(positions, axis=0)
287
-
288
- embed_pos = self.embed_positions(positions + self.offset)
289
-
290
- hidden_states = inputs_embeds + embed_pos
291
- hidden_states = self.layernorm_embedding(hidden_states)
292
-
293
- for encoder_layer in self.layers:
294
- # Add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
295
- dropout_probability = mx.random.uniform()
296
- if self.training and (dropout_probability < self.layerdrop):
297
- continue
298
- hidden_states = encoder_layer(hidden_states, attention_mask)
299
-
300
- return hidden_states
301
-
302
-
303
- class Florence2Decoder(nn.Module):
304
- def __init__(self, config: TextConfig):
305
- super().__init__()
306
- self.config = config
307
- self.dropout = config.dropout
308
- self.layerdrop = config.decoder_layerdrop
309
- self.padding_idx = config.pad_token_id
310
- self.max_target_positions = config.max_position_embeddings
311
- self.embed_scale = math.sqrt(config.d_model) if config.scale_embedding else 1.0
312
- self.offset = 2
313
- self.embed_positions = nn.Embedding(
314
- config.max_position_embeddings + self.offset, config.d_model
315
- )
316
- self.layers = [
317
- Florence2DecoderLayer(config) for _ in range(config.decoder_layers)
318
- ]
319
- self.layernorm_embedding = nn.LayerNorm(config.d_model)
320
-
321
- def __call__(
322
- self,
323
- input_ids=None,
324
- attention_mask=None,
325
- encoder_hidden_states=None,
326
- encoder_attention_mask=None,
327
- head_mask=None,
328
- cross_attn_head_mask=None,
329
- inputs_embeds=None,
330
- cache=None,
331
- ):
332
- if input_ids is not None and inputs_embeds is not None:
333
- raise ValueError(
334
- "You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time"
335
- )
336
- elif input_ids is not None:
337
- inputs_embeds = self.embed_tokens(input_ids)
338
- input_shape = inputs_embeds.shape # for 2d masks
339
- positions = input_ids
340
- elif inputs_embeds is not None:
341
- input_shape = inputs_embeds.shape[:-1] # for 4d masks
342
- positions = inputs_embeds[:, :, -1]
343
- else:
344
- raise ValueError(
345
- "You have to specify either decoder_input_ids or decoder_inputs_embeds"
346
- )
347
-
348
- if positions.ndim == 1:
349
- positions = mx.expand_dims(positions, axis=0)
350
-
351
- cache_length = cache[0][0].keys.shape[2] if cache[0][0].cache_length > 0 else 0
352
-
353
- bsz, seq_len = inputs_embeds.shape[:2]
354
- positions = mx.arange(
355
- cache_length,
356
- cache_length + seq_len,
357
- dtype=mx.int64,
358
- )
359
- positions = mx.expand_dims(positions, axis=0)
360
-
361
- embed_pos = self.embed_positions(positions + self.offset)
362
-
363
- hidden_states = inputs_embeds + embed_pos
364
- hidden_states = self.layernorm_embedding(hidden_states)
365
-
366
- for decoder_layer, c in zip(self.layers, cache):
367
- # add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
368
- dropout_probability = mx.random.uniform()
369
- if self.training and (dropout_probability < self.layerdrop):
370
- continue
371
- hidden_states = decoder_layer(
372
- hidden_states=hidden_states,
373
- encoder_hidden_states=encoder_hidden_states,
374
- attention_mask=attention_mask,
375
- encoder_attention_mask=encoder_attention_mask,
376
- cache=c,
377
- )
378
-
379
- return hidden_states
380
-
381
-
382
- class Florence2LanguageModel(nn.Module):
383
- def __init__(self, config: TextConfig):
384
- super().__init__()
385
- self.config = config
386
- self.shared = nn.Embedding(config.vocab_size, config.d_model)
387
- self.encoder = Florence2Encoder(config)
388
- self.decoder = Florence2Decoder(config)
389
- if config.scale_embedding:
390
- self.embed_scale = math.sqrt(config.d_model)
391
- else:
392
- self.embed_scale = 1.0
393
-
394
- def __call__(
395
- self,
396
- input_ids=None,
397
- inputs_embeds=None,
398
- decoder_input_ids=None,
399
- decoder_inputs_embeds=None,
400
- attention_mask=None,
401
- decoder_attention_mask=None,
402
- encoder_outputs=None,
403
- cache=None,
404
- ):
405
- self.encoder.embed_tokens = self.shared
406
- self.decoder.embed_tokens = self.shared
407
-
408
- if decoder_input_ids is None and decoder_inputs_embeds is None:
409
- if input_ids is None:
410
- raise ValueError(
411
- "If no `decoder_input_ids` or `decoder_inputs_embeds` are "
412
- "passed, `input_ids` cannot be `None`. Please pass either "
413
- "`input_ids` or `decoder_input_ids` or `decoder_inputs_embeds`."
414
- )
415
-
416
- decoder_input_ids = mx.zeros_like(input_ids)
417
- decoder_input_ids[:, 1:] = input_ids[:, :-1]
418
- decoder_input_ids[:, 0] = self.config.bos_token_id
419
-
420
- if inputs_embeds is not None:
421
- inputs_embeds = inputs_embeds * self.embed_scale
422
-
423
- if cache is None:
424
- cache = [(SimpleKVCache(), SimpleKVCache())] * len(self.decoder.layers)
425
-
426
- if encoder_outputs is None:
427
- encoder_outputs = self.encoder(
428
- input_ids=input_ids,
429
- inputs_embeds=inputs_embeds,
430
- attention_mask=attention_mask,
431
- )
432
-
433
- decoder_outputs = self.decoder(
434
- input_ids=decoder_input_ids,
435
- attention_mask=decoder_attention_mask,
436
- encoder_hidden_states=encoder_outputs,
437
- encoder_attention_mask=attention_mask,
438
- inputs_embeds=decoder_inputs_embeds,
439
- cache=cache,
440
- )
441
- return decoder_outputs, encoder_outputs
442
-
443
-
444
- class LanguageModel(nn.Module):
445
- def __init__(self, config: TextConfig):
446
- super().__init__()
447
- self.config = config
448
- self.model = Florence2LanguageModel(config)
449
- self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
450
-
451
- def __call__(
452
- self,
453
- input_ids=None,
454
- inputs_embeds=None,
455
- decoder_input_ids=None,
456
- decoder_inputs_embeds=None,
457
- attention_mask=None,
458
- decoder_attention_mask=None,
459
- encoder_outputs=None,
460
- cache=None,
461
- ):
462
- decoder_outputs, encoder_outputs = self.model(
463
- input_ids,
464
- inputs_embeds,
465
- decoder_input_ids,
466
- decoder_inputs_embeds,
467
- attention_mask,
468
- decoder_attention_mask,
469
- encoder_outputs,
470
- cache,
471
- )
472
- out = self.lm_head(decoder_outputs)
473
- return LanguageModelOutput(logits=out, encoder_outputs=encoder_outputs)
474
-
475
- @property
476
- def layers(self):
477
- return range(self.model.config.decoder_layers)
478
-
479
- @property
480
- def head_dim(self):
481
- return self.config.d_model // self.config.decoder_attention_heads
482
-
483
- @property
484
- def n_kv_heads(self):
485
- return self.config.decoder_attention_heads
486
-
487
- def make_cache(self):
488
- return [(SimpleKVCache(), SimpleKVCache()) for n in self.layers]