nexaai 1.0.19rc7__cp310-cp310-macosx_14_0_universal2.whl → 1.0.19rc9__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 (200) 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/mlx_backend/vlm/generate_qwen3_vl.py +14 -31
  5. nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +15 -32
  6. nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/qwen3vl.py +7 -23
  7. nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +8 -24
  8. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/METADATA +1 -1
  9. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/RECORD +11 -200
  10. nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
  11. nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
  12. nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
  13. nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
  14. nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
  15. nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
  16. nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
  17. nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
  18. nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
  19. nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
  20. nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
  21. nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
  22. nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
  23. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
  24. nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
  25. nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
  26. nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
  27. nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
  28. nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
  29. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
  30. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
  31. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
  32. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
  33. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
  34. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
  35. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
  36. nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
  37. nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
  38. nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
  39. nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
  40. nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
  41. nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
  42. nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
  43. nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
  44. nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
  45. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
  46. nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
  47. nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
  48. nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
  49. nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
  50. nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
  51. nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
  52. nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
  53. nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
  54. nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
  55. nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
  56. nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
  57. nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
  58. nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
  59. nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
  60. nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
  61. nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
  62. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
  63. nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
  64. nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
  65. nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
  66. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
  67. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
  68. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
  69. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
  70. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
  71. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
  72. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
  73. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
  74. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
  75. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
  76. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
  77. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
  78. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
  79. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
  80. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
  81. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
  82. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
  83. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
  84. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
  85. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
  86. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
  87. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
  88. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
  89. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
  90. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
  91. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
  92. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
  93. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
  94. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
  95. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
  96. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
  97. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
  98. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
  99. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
  100. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
  101. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
  102. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
  103. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
  104. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
  105. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
  106. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
  107. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
  108. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
  109. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
  110. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
  111. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
  112. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
  113. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
  114. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
  115. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
  116. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
  117. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
  118. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
  119. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
  120. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
  121. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
  122. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
  123. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
  124. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
  125. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
  126. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
  127. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
  128. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
  129. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
  130. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
  131. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
  132. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
  133. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
  134. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
  135. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
  136. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
  137. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
  138. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
  139. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
  140. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
  141. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
  142. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
  143. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
  144. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
  145. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
  146. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
  147. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
  148. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
  149. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
  150. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
  151. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
  152. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
  153. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
  154. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
  155. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
  156. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
  157. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
  158. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
  159. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
  160. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
  161. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
  162. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
  163. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
  164. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
  165. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
  166. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
  167. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
  168. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
  169. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
  170. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
  171. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
  172. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
  173. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
  174. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
  175. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
  176. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
  177. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
  178. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
  179. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
  180. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
  181. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
  182. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
  183. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
  184. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
  185. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
  186. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
  187. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
  188. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
  189. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
  190. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
  191. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
  192. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
  193. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
  194. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
  195. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
  196. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
  197. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
  198. nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
  199. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/WHEEL +0 -0
  200. {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/top_level.txt +0 -0
@@ -1,407 +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, PreTrainedTokenizerFast
7
-
8
-
9
- class StreamingDetokenizer:
10
- """The streaming detokenizer interface so that we can detokenize one token at a time.
11
-
12
- Example usage is as follows:
13
-
14
- detokenizer = ...
15
-
16
- # Reset the tokenizer state
17
- detokenizer.reset()
18
-
19
- for token in generate(...):
20
- detokenizer.add_token(token.item())
21
-
22
- # Contains the whole text so far. Some tokens may not be included
23
- # since it contains whole words usually.
24
- detokenizer.text
25
-
26
- # Contains the printable segment (usually a word) since the last
27
- # time it was accessed
28
- detokenizer.last_segment
29
-
30
- # Contains all the tokens added so far
31
- detokenizer.tokens
32
-
33
- # Make sure that we detokenize any remaining tokens
34
- detokenizer.finalize()
35
-
36
- # Now detokenizer.text should match tokenizer.decode(detokenizer.tokens)
37
- """
38
-
39
- __slots__ = ("text", "tokens", "offset")
40
-
41
- def reset(self):
42
- raise NotImplementedError()
43
-
44
- def add_token(self, token):
45
- raise NotImplementedError()
46
-
47
- def finalize(self):
48
- raise NotImplementedError()
49
-
50
- @property
51
- def last_segment(self):
52
- """Return the last segment of readable text since last time this property was accessed."""
53
- text = self.text
54
- segment = text[self.offset :]
55
- self.offset = len(text)
56
- return segment
57
-
58
-
59
- class NaiveStreamingDetokenizer(StreamingDetokenizer):
60
- """NaiveStreamingDetokenizer relies on the underlying tokenizer
61
- implementation and should work with every tokenizer.
62
-
63
- Its complexity is O(T^2) where T is the longest line since it will
64
- repeatedly detokenize the same tokens until a new line is generated.
65
- """
66
-
67
- def __init__(self, tokenizer):
68
- self._tokenizer = tokenizer
69
- self._tokenizer.decode([0])
70
- self.reset()
71
-
72
- def reset(self):
73
- self.offset = 0
74
- self.tokens = []
75
- self._text = ""
76
- self._current_tokens = []
77
- self._current_text = ""
78
-
79
- def add_token(self, token):
80
- self._current_tokens.append(token)
81
- self.tokens.append(token)
82
-
83
- def finalize(self):
84
- self._text += self._tokenizer.decode(self._current_tokens)
85
- self._current_tokens = []
86
- self._current_text = ""
87
-
88
- @property
89
- def text(self):
90
- if self._current_tokens:
91
- self._current_text = self._tokenizer.decode(self._current_tokens)
92
- if self._tokenizer.clean_up_tokenization_spaces and self._current_text[-1] == " ":
93
- self._current_text = self._current_text[:-1]
94
- if self._current_text and self._current_text[-1] == "\n":
95
- self._text += self._current_text
96
- self._current_tokens.clear()
97
- self._current_text = ""
98
- return self._text + self._current_text
99
-
100
-
101
- class SPMStreamingDetokenizer(StreamingDetokenizer):
102
- """A streaming detokenizer for SPM models.
103
-
104
- It adds tokens to the text if the next token starts with the special SPM
105
- underscore which results in linear complexity.
106
- """
107
-
108
- def __init__(self, tokenizer, trim_space=True):
109
- self.trim_space = trim_space
110
- self._sep = "\u2581".encode()
111
-
112
- # Extract the tokens in a list from id to text
113
- self.tokenmap = [""] * (max(tokenizer.vocab.values()) + 1)
114
- for value, tokenid in tokenizer.vocab.items():
115
- if value.startswith("<0x"):
116
- # Replace bytes with their value
117
- self.tokenmap[tokenid] = bytes([int(value[3:5], 16)])
118
- else:
119
- self.tokenmap[tokenid] = value.encode()
120
-
121
- self.reset()
122
-
123
- def reset(self):
124
- self.offset = 0
125
- self._unflushed = b""
126
- self.text = ""
127
- self.tokens = []
128
-
129
- def _try_flush(self, force=False):
130
- text = self._unflushed.replace(self._sep, b" ").decode("utf-8", "replace")
131
- if not force and text.endswith("\ufffd"):
132
- return
133
- if not self.text and self.trim_space and text and text[0] == " ":
134
- text = text[1:]
135
- self.text += text
136
- self._unflushed = b""
137
-
138
- def add_token(self, token):
139
- self.tokens.append(token)
140
- v = self.tokenmap[token]
141
- self._unflushed += v
142
- self._try_flush()
143
-
144
- def finalize(self):
145
- self._try_flush(force=True)
146
- self._unflushed = b""
147
-
148
-
149
- class BPEStreamingDetokenizer(StreamingDetokenizer):
150
- """A streaming detokenizer for OpenAI style BPE models.
151
-
152
- It adds tokens to the text if the next token starts with a space similar to
153
- the SPM detokenizer.
154
- """
155
-
156
- _byte_decoder = None
157
- _space_matches = (".", "?", "!", ",", "n't", "'m", "'s", "'ve", "'re")
158
-
159
- def __init__(self, tokenizer):
160
- self.clean_spaces = tokenizer.clean_up_tokenization_spaces
161
-
162
- # Extract the tokens in a list from id to text
163
- self.tokenmap = [None] * len(tokenizer.vocab)
164
- for value, tokenid in tokenizer.vocab.items():
165
- self.tokenmap[tokenid] = value
166
-
167
- self.reset()
168
-
169
- # Make the BPE byte decoder from
170
- # https://github.com/openai/gpt-2/blob/master/src/encoder.py
171
- self.make_byte_decoder()
172
-
173
- def reset(self):
174
- self.offset = 0
175
- self._unflushed = ""
176
- self.text = ""
177
- self.tokens = []
178
-
179
- def _decode_bytes(self, seq):
180
- barr = bytearray()
181
- for c in seq:
182
- res = self._byte_decoder.get(c, False)
183
- if res:
184
- barr.append(res)
185
- else:
186
- barr.extend(bytes(c, "utf-8"))
187
- return barr.decode("utf-8", "replace")
188
-
189
- def _maybe_trim_space(self, current_text):
190
- if len(current_text) == 0:
191
- return current_text
192
- elif current_text[0] != " ":
193
- return current_text
194
- elif not self.text:
195
- return current_text[1:]
196
- elif self.clean_spaces and current_text[1:].startswith(self._space_matches):
197
- return current_text[1:]
198
- return current_text
199
-
200
- def add_token(self, token):
201
- self.tokens.append(token)
202
- v = self.tokenmap[token]
203
- self._unflushed += v
204
- text = self._decode_bytes(self._unflushed)
205
-
206
- # For multi-byte utf-8 wait until they are complete
207
- # For single spaces wait until the next token to clean it if needed
208
- if not text.endswith("\ufffd") and not (len(v) == 1 and self._byte_decoder[v[0]] == 32):
209
- self.text += self._maybe_trim_space(text)
210
- self._unflushed = ""
211
-
212
- def finalize(self):
213
- current_text = bytearray(self._byte_decoder[c] for c in self._unflushed).decode(
214
- "utf-8",
215
- "replace",
216
- )
217
- self.text += self._maybe_trim_space(current_text)
218
- self._unflushed = ""
219
-
220
- @classmethod
221
- def make_byte_decoder(cls):
222
- """See https://github.com/openai/gpt-2/blob/master/src/encoder.py for the rationale."""
223
- if cls._byte_decoder is not None:
224
- return
225
-
226
- char_to_bytes = {}
227
- limits = [
228
- 0,
229
- ord("!"),
230
- ord("~") + 1,
231
- ord("¡"),
232
- ord("¬") + 1,
233
- ord("®"),
234
- ord("ÿ") + 1,
235
- ]
236
- n = 0
237
- for i, (start, stop) in enumerate(zip(limits, limits[1:])):
238
- if i % 2 == 0:
239
- for b in range(start, stop):
240
- char_to_bytes[chr(2**8 + n)] = b
241
- n += 1
242
- else:
243
- for b in range(start, stop):
244
- char_to_bytes[chr(b)] = b
245
- cls._byte_decoder = char_to_bytes
246
-
247
-
248
- class TokenizerWrapper:
249
- """A wrapper that combines an HF tokenizer and a detokenizer.
250
-
251
- Accessing any attribute other than the ``detokenizer`` is forwarded to the
252
- huggingface tokenizer.
253
- """
254
-
255
- def __init__(self, tokenizer, detokenizer_class=NaiveStreamingDetokenizer, eos_token_ids=None):
256
- self._tokenizer = tokenizer
257
- self._detokenizer = detokenizer_class(tokenizer)
258
- self._eos_token_ids = (
259
- set(eos_token_ids) if eos_token_ids is not None else {tokenizer.eos_token_id}
260
- )
261
-
262
- def add_eos_token(self, token: str):
263
- token_id = None
264
- try:
265
- token_id = int(token)
266
- except ValueError:
267
- token_id = self._tokenizer.convert_tokens_to_ids(token)
268
-
269
- if token_id is None:
270
- raise ValueError(f"'{token}' is not a token for this tokenizer")
271
-
272
- self._eos_token_ids.add(token_id)
273
-
274
- def __getattr__(self, attr):
275
- if attr == "detokenizer":
276
- return self._detokenizer
277
- elif attr == "eos_token_ids":
278
- return self._eos_token_ids
279
- elif attr.startswith("_"):
280
- return self.__getattribute__(attr)
281
- else:
282
- return getattr(self._tokenizer, attr)
283
-
284
- def __setattr__(self, attr, value):
285
- if attr in {"detokenizer", "eos_token_ids"}:
286
- if attr == "detokenizer":
287
- raise AttributeError("Cannot set the detokenizer.")
288
- elif attr == "eos_token_ids":
289
- self._eos_token_ids = set(value) if value is not None else set()
290
- elif attr.startswith("_"):
291
- super().__setattr__(attr, value)
292
- else:
293
- setattr(self._tokenizer, attr, value)
294
-
295
-
296
- class NewlineTokenizer(PreTrainedTokenizerFast):
297
- """A tokenizer that replaces newlines with <n> and <n> with new line."""
298
-
299
- def __init__(self, *args, **kwargs):
300
- super().__init__(*args, **kwargs)
301
-
302
- def _preprocess_text(self, text):
303
- return text.replace("\n", "<n>")
304
-
305
- def _postprocess_text(self, text):
306
- return text.replace("<n>", "\n")
307
-
308
- def encode(self, text, **kwargs):
309
- return super().encode(self._preprocess_text(text), **kwargs)
310
-
311
- def encode_batch(self, texts, **kwargs):
312
- return super().encode_batch([self._preprocess_text(t) for t in texts], **kwargs)
313
-
314
- def decode(self, *args, **kwargs):
315
- return self._postprocess_text(super().decode(*args, **kwargs))
316
-
317
- def batch_decode(self, *args, **kwargs):
318
- decoded = super().batch_decode(*args, **kwargs)
319
- return [self._postprocess_text(d) for d in decoded]
320
-
321
-
322
- AutoTokenizer.register("NewlineTokenizer", fast_tokenizer_class=NewlineTokenizer)
323
-
324
-
325
- def _match(a, b):
326
- if type(a) != type(b):
327
- return False
328
- if isinstance(a, dict):
329
- return len(a) == len(b) and all(k in b and _match(a[k], b[k]) for k in a)
330
- if isinstance(a, list):
331
- return len(a) == len(b) and all(_match(ai, bi) for ai, bi in zip(a, b))
332
-
333
- return a == b
334
-
335
-
336
- def _is_spm_decoder(decoder):
337
- _target_description = {
338
- "type": "Sequence",
339
- "decoders": [
340
- {"type": "Replace", "pattern": {"String": "▁"}, "content": " "},
341
- {"type": "ByteFallback"},
342
- {"type": "Fuse"},
343
- {"type": "Strip", "content": " ", "start": 1, "stop": 0},
344
- ],
345
- }
346
- return _match(_target_description, decoder)
347
-
348
-
349
- def _is_spm_decoder_no_space(decoder):
350
- _target_description = {
351
- "type": "Sequence",
352
- "decoders": [
353
- {"type": "Replace", "pattern": {"String": "▁"}, "content": " "},
354
- {"type": "ByteFallback"},
355
- {"type": "Fuse"},
356
- ],
357
- }
358
- return _match(_target_description, decoder)
359
-
360
-
361
- def _is_bpe_decoder(decoder):
362
- return isinstance(decoder, dict) and decoder.get("type", None) == "ByteLevel"
363
-
364
-
365
- def load_tokenizer(
366
- model_path, tokenizer_config_extra={}, return_tokenizer=True, eos_token_ids=None
367
- ):
368
- """Load a huggingface tokenizer and try to infer the type of streaming
369
- detokenizer to use.
370
-
371
- Note, to use a fast streaming tokenizer, pass a local file path rather than
372
- a Hugging Face repo ID.
373
- """
374
- detokenizer_class = NaiveStreamingDetokenizer
375
-
376
- tokenizer_file = model_path / "tokenizer.json"
377
- if tokenizer_file.exists():
378
- with open(tokenizer_file, "r", encoding="utf-8") as fid:
379
- try:
380
- tokenizer_content = json.load(fid)
381
- except JSONDecodeError as e:
382
- raise JSONDecodeError("Failed to parse tokenizer.json", e.doc, e.pos)
383
-
384
- if "decoder" in tokenizer_content:
385
- if _is_spm_decoder(tokenizer_content["decoder"]):
386
- detokenizer_class = SPMStreamingDetokenizer
387
- elif _is_spm_decoder_no_space(tokenizer_content["decoder"]):
388
- detokenizer_class = partial(SPMStreamingDetokenizer, trim_space=False)
389
- elif _is_bpe_decoder(tokenizer_content["decoder"]):
390
- detokenizer_class = BPEStreamingDetokenizer
391
-
392
- if isinstance(eos_token_ids, int):
393
- eos_token_ids = [eos_token_ids]
394
-
395
- if return_tokenizer:
396
- return TokenizerWrapper(
397
- AutoTokenizer.from_pretrained(model_path, **tokenizer_config_extra),
398
- detokenizer_class,
399
- eos_token_ids=eos_token_ids,
400
- )
401
- else:
402
- return detokenizer_class
403
-
404
-
405
- def no_bos_or_eos(sequence: List, bos: int, eos: int) -> List:
406
- removed_bos = sequence if sequence[0] != bos else sequence[1:]
407
- return removed_bos[:-1] if removed_bos[-1] == eos else removed_bos