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,39 +0,0 @@
1
- import mlx.core as mx
2
-
3
-
4
- def top_p_sampling(logits: mx.array, top_p: float, temperature: float) -> mx.array:
5
- """
6
- Apply top-p (nucleus) sampling to logits.
7
-
8
- Args:
9
- logits: The logits from the model's output.
10
- top_p: The cumulative probability threshold for top-p filtering.
11
- temperature: Temperature parameter for softmax distribution reshaping.
12
- Returns:
13
- token selected based on the top-p criterion.
14
- """
15
- if (
16
- logits.dtype == mx.bfloat16
17
- ): # workaround for unable to load kernel contiguous_scan_inclusive_sum_bfloat16_bfloat16
18
- logits = logits.astype(mx.float32)
19
-
20
- # referenced implementation from https://github.com/huggingface/transformers/blob/main/src/transformers/generation/logits_process.py#L449-L460
21
- probs = mx.softmax(logits / temperature, axis=-1)
22
-
23
- # sort probs in ascending order
24
- sorted_indices = mx.argsort(probs, axis=-1)
25
- sorted_probs = probs[..., sorted_indices.squeeze(0)]
26
-
27
- cumulative_probs = mx.cumsum(sorted_probs, axis=-1)
28
-
29
- # select tokens with cumulative probs below threshold
30
- top_probs = mx.where(
31
- cumulative_probs > 1 - top_p,
32
- sorted_probs,
33
- mx.zeros_like(sorted_probs),
34
- )
35
-
36
- sorted_token = mx.random.categorical(mx.log(top_probs))
37
- token = sorted_indices.squeeze(0)[sorted_token]
38
-
39
- return token
@@ -1,344 +0,0 @@
1
- import json
2
- from functools import partial
3
- from json import JSONDecodeError
4
- from typing import List
5
-
6
- from transformers import AutoTokenizer
7
-
8
- REPLACEMENT_CHAR = "\ufffd"
9
-
10
-
11
- def _remove_space(x):
12
- if x and x[0] == " ":
13
- return x[1:]
14
- return x
15
-
16
-
17
- class StreamingDetokenizer:
18
- """The streaming detokenizer interface so that we can detokenize one token at a time.
19
-
20
- Example usage is as follows:
21
-
22
- detokenizer = ...
23
-
24
- # Reset the tokenizer state
25
- detokenizer.reset()
26
-
27
- for token in generate(...):
28
- detokenizer.add_token(token.item())
29
-
30
- # Contains the whole text so far. Some tokens may not be included
31
- # since it contains whole words usually.
32
- detokenizer.text
33
-
34
- # Contains the printable segment (usually a word) since the last
35
- # time it was accessed
36
- detokenizer.last_segment
37
-
38
- # Contains all the tokens added so far
39
- detokenizer.tokens
40
-
41
- # Make sure that we detokenize any remaining tokens
42
- detokenizer.finalize()
43
-
44
- # Now detokenizer.text should match tokenizer.decode(detokenizer.tokens)
45
- """
46
-
47
- __slots__ = ("text", "tokens", "offset")
48
-
49
- def reset(self):
50
- raise NotImplementedError()
51
-
52
- def add_token(self, token, skip_special_token_ids: List[int] = []):
53
- raise NotImplementedError()
54
-
55
- def finalize(self):
56
- raise NotImplementedError()
57
-
58
- @property
59
- def last_segment(self):
60
- """Return the last segment of readable text since last time this property was accessed."""
61
- text = self.text
62
- if text and text[-1] != REPLACEMENT_CHAR:
63
- segment = text[self.offset :]
64
- self.offset = len(text)
65
- return segment
66
- return ""
67
-
68
-
69
- class NaiveStreamingDetokenizer(StreamingDetokenizer):
70
- """NaiveStreamingDetokenizer relies on the underlying tokenizer
71
- implementation and should work with every tokenizer.
72
-
73
- Its complexity is O(T^2) where T is the longest line since it will
74
- repeatedly detokenize the same tokens until a new line is generated.
75
- """
76
-
77
- def __init__(self, tokenizer):
78
- self._tokenizer = tokenizer
79
- self._tokenizer.decode([0])
80
- self.reset()
81
-
82
- def reset(self):
83
- self.offset = 0
84
- self._tokens = []
85
- self._text = ""
86
- self._current_tokens = []
87
- self._current_text = ""
88
-
89
- def add_token(self, token, skip_special_token_ids: List[int] = []):
90
- if token in skip_special_token_ids:
91
- return
92
- self._current_tokens.append(token)
93
-
94
- def finalize(self):
95
- self._tokens.extend(self._current_tokens)
96
- self._text += self._tokenizer.decode(self._current_tokens)
97
- self._current_tokens = []
98
- self._current_text = ""
99
-
100
- @property
101
- def text(self):
102
- if self._current_tokens:
103
- self._current_text = self._tokenizer.decode(self._current_tokens)
104
- if self._current_text and self._current_text[-1] == "\n":
105
- self._tokens.extend(self._current_tokens)
106
- self._text += self._current_text
107
- self._current_tokens.clear()
108
- self._current_text = ""
109
- return self._text + self._current_text
110
-
111
- @property
112
- def tokens(self):
113
- return self._tokens
114
-
115
-
116
- class SPMStreamingDetokenizer(StreamingDetokenizer):
117
- """A streaming detokenizer for SPM models.
118
-
119
- It adds tokens to the text if the next token starts with the special SPM
120
- underscore which results in linear complexity.
121
- """
122
-
123
- def __init__(self, tokenizer, trim_space=True):
124
- self.trim_space = trim_space
125
-
126
- # Extract the tokens in a list from id to text
127
- self.tokenmap = [None] * len(tokenizer.vocab)
128
- for value, tokenid in tokenizer.vocab.items():
129
- self.tokenmap[tokenid] = value
130
-
131
- # Replace bytes with their value
132
- for i in range(len(self.tokenmap)):
133
- if self.tokenmap[i].startswith("<0x"):
134
- self.tokenmap[i] = chr(int(self.tokenmap[i][3:5], 16))
135
-
136
- self.reset()
137
-
138
- def reset(self):
139
- self.offset = 0
140
- self._unflushed = ""
141
- self.text = ""
142
- self.tokens = []
143
-
144
- def add_token(self, token, skip_special_token_ids: List[int] = []):
145
- if token in skip_special_token_ids:
146
- return
147
- v = self.tokenmap[token]
148
- if v[0] == "\u2581":
149
- if self.text or not self.trim_space:
150
- self.text += self._unflushed.replace("\u2581", " ")
151
- else:
152
- self.text = _remove_space(self._unflushed.replace("\u2581", " "))
153
- self._unflushed = v
154
- else:
155
- self._unflushed += v
156
-
157
- def finalize(self):
158
- if self.text or not self.trim_space:
159
- self.text += self._unflushed.replace("\u2581", " ")
160
- else:
161
- self.text = _remove_space(self._unflushed.replace("\u2581", " "))
162
- self._unflushed = ""
163
-
164
-
165
- class BPEStreamingDetokenizer(StreamingDetokenizer):
166
- """A streaming detokenizer for OpenAI style BPE models.
167
-
168
- It adds tokens to the text if the next token starts with a space similar to
169
- the SPM detokenizer.
170
- """
171
-
172
- _byte_decoder = None
173
-
174
- def __init__(self, tokenizer, trim_space=False):
175
- self.trim_space = trim_space
176
-
177
- # Extract the tokens in a list from id to text
178
- self.tokenmap = [None] * len(tokenizer.vocab)
179
- for value, tokenid in tokenizer.vocab.items():
180
- self.tokenmap[tokenid] = value
181
-
182
- self.reset()
183
-
184
- # Make the BPE byte decoder from
185
- # https://github.com/openai/gpt-2/blob/master/src/encoder.py
186
- self.make_byte_decoder()
187
-
188
- def reset(self):
189
- self.offset = 0
190
- self._unflushed = ""
191
- self.text = ""
192
- self.tokens = []
193
-
194
- def add_token(self, token, skip_special_token_ids: List[int] = []):
195
- if token in skip_special_token_ids:
196
- return
197
- v = self.tokenmap[token]
198
- # if the token starts with space
199
- if self._byte_decoder[v[0]] == 32:
200
- current_text = bytearray(
201
- self._byte_decoder[c] for c in self._unflushed
202
- ).decode("utf-8")
203
- if self.text or not self.trim_space:
204
- self.text += current_text
205
- else:
206
- self.text += _remove_space(current_text)
207
- self._unflushed = v
208
- else:
209
- self._unflushed += v
210
-
211
- def finalize(self):
212
- current_text = bytearray(self._byte_decoder[c] for c in self._unflushed).decode(
213
- "utf-8"
214
- )
215
- if self.text or not self.trim_space:
216
- self.text += current_text
217
- else:
218
- self.text += _remove_space(current_text)
219
- self._unflushed = ""
220
-
221
- @classmethod
222
- def make_byte_decoder(cls):
223
- """See https://github.com/openai/gpt-2/blob/master/src/encoder.py for the rationale."""
224
- if cls._byte_decoder is not None:
225
- return
226
-
227
- char_to_bytes = {}
228
- limits = [
229
- 0,
230
- ord("!"),
231
- ord("~") + 1,
232
- ord("¡"),
233
- ord("¬") + 1,
234
- ord("®"),
235
- ord("ÿ") + 1,
236
- ]
237
- n = 0
238
- for i, (start, stop) in enumerate(zip(limits, limits[1:])):
239
- if i % 2 == 0:
240
- for b in range(start, stop):
241
- char_to_bytes[chr(2**8 + n)] = b
242
- n += 1
243
- else:
244
- for b in range(start, stop):
245
- char_to_bytes[chr(b)] = b
246
- cls._byte_decoder = char_to_bytes
247
-
248
-
249
- class TokenizerWrapper:
250
- """A wrapper that combines an HF tokenizer and a detokenizer.
251
-
252
- Accessing any attribute other than the ``detokenizer`` is forwarded to the
253
- huggingface tokenizer.
254
- """
255
-
256
- def __init__(self, tokenizer, detokenizer_class=NaiveStreamingDetokenizer):
257
- self._tokenizer = tokenizer
258
- self._detokenizer = detokenizer_class(tokenizer)
259
-
260
- def __getattr__(self, attr):
261
- if attr == "detokenizer":
262
- return self._detokenizer
263
- else:
264
- return getattr(self._tokenizer, attr)
265
-
266
-
267
- def _match(a, b):
268
- if type(a) != type(b):
269
- return False
270
- if isinstance(a, dict):
271
- return len(a) == len(b) and all(k in b and _match(a[k], b[k]) for k in a)
272
- if isinstance(a, list):
273
- return len(a) == len(b) and all(_match(ai, bi) for ai, bi in zip(a, b))
274
-
275
- return a == b
276
-
277
-
278
- def _is_spm_decoder(decoder):
279
- _target_description = {
280
- "type": "Sequence",
281
- "decoders": [
282
- {"type": "Replace", "pattern": {"String": "▁"}, "content": " "},
283
- {"type": "ByteFallback"},
284
- {"type": "Fuse"},
285
- {"type": "Strip", "content": " ", "start": 1, "stop": 0},
286
- ],
287
- }
288
- return _match(_target_description, decoder)
289
-
290
-
291
- def _is_spm_decoder_no_space(decoder):
292
- _target_description = {
293
- "type": "Sequence",
294
- "decoders": [
295
- {"type": "Replace", "pattern": {"String": "▁"}, "content": " "},
296
- {"type": "ByteFallback"},
297
- {"type": "Fuse"},
298
- ],
299
- }
300
- return _match(_target_description, decoder)
301
-
302
-
303
- def _is_bpe_decoder(decoder):
304
- _target_description = {
305
- "type": "ByteLevel",
306
- "add_prefix_space": False,
307
- "trim_offsets": False,
308
- "use_regex": False,
309
- }
310
-
311
- return _match(_target_description, decoder)
312
-
313
-
314
- def load_tokenizer(model_path, return_tokenizer=True, tokenizer_config_extra={}):
315
- """Load a huggingface tokenizer and try to infer the type of streaming
316
- detokenizer to use.
317
-
318
- Note, to use a fast streaming tokenizer, pass a local file path rather than
319
- a Hugging Face repo ID.
320
- """
321
- detokenizer_class = NaiveStreamingDetokenizer
322
-
323
- tokenizer_file = model_path / "tokenizer.json"
324
- if tokenizer_file.exists():
325
- with open(tokenizer_file, "r") as f:
326
- try:
327
- tokenizer_content = json.load(f)
328
- except JSONDecodeError as e:
329
- raise JSONDecodeError("Failed to parse tokenizer.json", e.doc, e.pos)
330
- if "decoder" in tokenizer_content:
331
- if _is_spm_decoder(tokenizer_content["decoder"]):
332
- detokenizer_class = SPMStreamingDetokenizer
333
- elif _is_spm_decoder_no_space(tokenizer_content["decoder"]):
334
- detokenizer_class = partial(SPMStreamingDetokenizer, trim_space=False)
335
- elif _is_bpe_decoder(tokenizer_content["decoder"]):
336
- detokenizer_class = BPEStreamingDetokenizer
337
-
338
- if return_tokenizer:
339
- return TokenizerWrapper(
340
- AutoTokenizer.from_pretrained(model_path, **tokenizer_config_extra),
341
- detokenizer_class,
342
- )
343
- else:
344
- return detokenizer_class
@@ -1,9 +0,0 @@
1
- from .lora import LoRaLayer, replace_lora_with_linear
2
- from .trainer import Dataset, Trainer, save_adapter
3
- from .utils import (
4
- apply_lora_layers,
5
- count_parameters,
6
- find_all_linear_names,
7
- get_peft_model,
8
- print_trainable_parameters,
9
- )
@@ -1,70 +0,0 @@
1
- import math
2
- from typing import Union
3
-
4
- import mlx.core as mx
5
- import mlx.nn as nn
6
-
7
-
8
- class LoRaLayer(nn.Module):
9
- def __init__(
10
- self,
11
- linear: Union[nn.Linear, nn.QuantizedLinear],
12
- rank: int,
13
- alpha: float = 0.1,
14
- dropout: float = 0.0,
15
- ):
16
- super().__init__()
17
-
18
- self.original_layer = linear
19
-
20
- self.dropout = nn.Dropout(p=dropout)
21
-
22
- output_dims, input_dims = linear.weight.shape
23
- if isinstance(linear, nn.QuantizedLinear):
24
- input_dims *= 32 // linear.bits
25
-
26
- std_dev = 1 / math.sqrt(rank)
27
-
28
- self.A = mx.random.uniform(
29
- low=-std_dev,
30
- high=std_dev,
31
- shape=(input_dims, rank),
32
- )
33
- self.B = mx.zeros((rank, output_dims))
34
- self.alpha = alpha
35
-
36
- def __call__(self, x):
37
- y = self.original_layer(x)
38
- lora_update = (self.dropout(x) @ self.A) @ self.B
39
- return y + (self.alpha * lora_update).astype(x.dtype)
40
-
41
-
42
- def replace_lora_with_linear(model):
43
- for i, layer in enumerate(model.layers):
44
- if isinstance(layer, LoRaLayer):
45
- # Compute the final merged weight
46
- lora_update = layer.alpha * (layer.A @ layer.B)
47
- updated_weight = layer.original_layer.weight + lora_update
48
- use_bias = layer.original_layer.bias is not None
49
-
50
- updated_bias = layer.original_layer.bias
51
-
52
- # Create a new Linear layer with the updated parameters
53
- new_linear_layer = nn.Linear(
54
- updated_weight.size(1), updated_weight.size(0), bias=use_bias
55
- )
56
-
57
- new_linear_layer.weight = updated_weight
58
-
59
- if use_bias:
60
- new_linear_layer.bias = updated_bias
61
-
62
- if isinstance(layer.original_layer, nn.QuantizedLinear):
63
- new_linear_layer = nn.QuantizedLinear.from_linear(
64
- new_linear_layer,
65
- new_linear_layer.group_size,
66
- new_linear_layer.bits,
67
- )
68
-
69
- # Replace the LoRaLayer with the new Linear layer in the model
70
- model.layers[i] = new_linear_layer