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.
- nexaai/_stub.cpython-310-darwin.so +0 -0
- nexaai/_version.py +1 -1
- nexaai/binds/libnexa_bridge.dylib +0 -0
- nexaai/mlx_backend/vlm/generate_qwen3_vl.py +14 -31
- nexaai/mlx_backend/vlm/generate_qwen3_vl_moe.py +15 -32
- nexaai/mlx_backend/vlm/modeling/models/qwen3_vl/qwen3vl.py +7 -23
- nexaai/mlx_backend/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +8 -24
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/METADATA +1 -1
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/RECORD +11 -200
- nexaai/binds/nexa_mlx/py-lib/asr/__init__.py +0 -12
- nexaai/binds/nexa_mlx/py-lib/asr/interface.py +0 -122
- nexaai/binds/nexa_mlx/py-lib/common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/common/utils.py +0 -25
- nexaai/binds/nexa_mlx/py-lib/cv/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/cv/generate.py +0 -195
- nexaai/binds/nexa_mlx/py-lib/cv/interface.py +0 -151
- nexaai/binds/nexa_mlx/py-lib/cv/main.py +0 -81
- nexaai/binds/nexa_mlx/py-lib/cv/modeling/pp_ocr_v4.py +0 -1736
- nexaai/binds/nexa_mlx/py-lib/embedding/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/generate.py +0 -333
- nexaai/binds/nexa_mlx/py-lib/embedding/interface.py +0 -617
- nexaai/binds/nexa_mlx/py-lib/embedding/main.py +0 -173
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/embedding/modeling/nexa_jina_v2.py +0 -399
- nexaai/binds/nexa_mlx/py-lib/image_gen/__init__.py +0 -1
- nexaai/binds/nexa_mlx/py-lib/image_gen/generate_sd.py +0 -244
- nexaai/binds/nexa_mlx/py-lib/image_gen/interface.py +0 -82
- nexaai/binds/nexa_mlx/py-lib/image_gen/main.py +0 -281
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/__init__.py +0 -306
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/clip.py +0 -116
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/config.py +0 -65
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/model_io.py +0 -386
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/sampler.py +0 -105
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/tokenizer.py +0 -100
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/unet.py +0 -460
- nexaai/binds/nexa_mlx/py-lib/image_gen/stable_diffusion/vae.py +0 -274
- nexaai/binds/nexa_mlx/py-lib/llm/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/llm/generate.py +0 -149
- nexaai/binds/nexa_mlx/py-lib/llm/interface.py +0 -764
- nexaai/binds/nexa_mlx/py-lib/llm/main.py +0 -68
- nexaai/binds/nexa_mlx/py-lib/rerank/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/generate.py +0 -174
- nexaai/binds/nexa_mlx/py-lib/rerank/interface.py +0 -287
- nexaai/binds/nexa_mlx/py-lib/rerank/main.py +0 -127
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/rerank/modeling/nexa_jina_rerank.py +0 -330
- nexaai/binds/nexa_mlx/py-lib/sd/__init__.py +0 -1
- nexaai/binds/nexa_mlx/py-lib/sd/interface.py +0 -362
- nexaai/binds/nexa_mlx/py-lib/sd/main.py +0 -286
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/__init__.py +0 -306
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/clip.py +0 -116
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/config.py +0 -65
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/model_io.py +0 -385
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/sampler.py +0 -105
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/tokenizer.py +0 -100
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/unet.py +0 -460
- nexaai/binds/nexa_mlx/py-lib/sd/modeling/vae.py +0 -274
- nexaai/binds/nexa_mlx/py-lib/tts/__init__.py +0 -12
- nexaai/binds/nexa_mlx/py-lib/tts/interface.py +0 -276
- nexaai/binds/nexa_mlx/py-lib/vlm/__init__.py +0 -3
- nexaai/binds/nexa_mlx/py-lib/vlm/generate.py +0 -572
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl.py +0 -294
- nexaai/binds/nexa_mlx/py-lib/vlm/generate_qwen3_vl_moe.py +0 -276
- nexaai/binds/nexa_mlx/py-lib/vlm/interface.py +0 -504
- nexaai/binds/nexa_mlx/py-lib/vlm/main.py +0 -320
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/convert.py +0 -68
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/aya_vision.py +0 -193
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/interpolate.py +0 -186
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/language.py +0 -233
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/aya_vision/vision.py +0 -503
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/base.py +0 -202
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/cache.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/__init__.py +0 -10
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/conversation.py +0 -264
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/deepseek_vl_v2.py +0 -472
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/language.py +0 -591
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/processing_deepsek_vl_v2.py +0 -526
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/deepseek_vl_v2/vision.py +0 -356
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/florence2.py +0 -366
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/language.py +0 -488
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/florence2/vision.py +0 -591
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/gemma3.py +0 -213
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/language.py +0 -315
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3/vision.py +0 -238
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/audio.py +0 -1038
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/config.py +0 -139
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/gemma3n.py +0 -322
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/language.py +0 -629
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/gemma3n/vision.py +0 -1022
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/idefics2.py +0 -294
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/language.py +0 -191
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics2/vision.py +0 -267
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/idefics3.py +0 -175
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/language.py +0 -192
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/idefics3/vision.py +0 -233
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/internvl_chat.py +0 -140
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/language.py +0 -220
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/processor.py +0 -393
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/internvl_chat/vision.py +0 -293
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kernels.py +0 -307
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/kimi_vl.py +0 -143
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/language.py +0 -509
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/kimi_vl/vision.py +0 -522
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/language.py +0 -386
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/llama4.py +0 -138
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llama4/vision.py +0 -560
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/language.py +0 -240
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/llava.py +0 -153
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava/vision.py +0 -259
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/language.py +0 -236
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/llava_bunny.py +0 -256
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_bunny/vision.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/language.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/llava_next.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/llava_next/vision.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mistral3/mistral3.py +0 -283
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/language.py +0 -416
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/mllama.py +0 -172
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/mllama/vision.py +0 -499
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/language.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/molmo.py +0 -133
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/molmo/vision.py +0 -465
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/__init__.py +0 -10
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/language.py +0 -230
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/multi_modality.py +0 -385
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/sam.py +0 -557
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/multi_modality/vision.py +0 -526
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/language.py +0 -282
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/paligemma.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/paligemma/vision.py +0 -242
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/language.py +0 -21
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/phi3_v.py +0 -243
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/su_rope.py +0 -71
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/phi3_v/vision.py +0 -324
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/language.py +0 -229
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/pixtral.py +0 -161
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/pixtral/vision.py +0 -320
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/config.py +0 -108
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/language.py +0 -490
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/qwen2_5_vl.py +0 -168
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_5_vl/vision.py +0 -414
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/__init__.py +0 -2
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/config.py +0 -104
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/language.py +0 -490
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/qwen2_vl.py +0 -167
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen2_vl/vision.py +0 -312
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/base.py +0 -117
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/cache.py +0 -531
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/generate.py +0 -701
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/rope_utils.py +0 -255
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/sample_utils.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/llm_common/tokenizer_utils.py +0 -407
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/processor.py +0 -476
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3_vl/qwen3vl.py +0 -1223
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/__init__.py +0 -0
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/base.py +0 -117
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/cache.py +0 -531
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/generate.py +0 -701
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/rope_utils.py +0 -255
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/sample_utils.py +0 -303
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/llm_common/tokenizer_utils.py +0 -407
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/processor.py +0 -476
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/qwen3vl_moe.py +0 -1309
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/qwen3vl_moe/switch_layers.py +0 -210
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/__init__.py +0 -8
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/models/smolvlm/smolvlm.py +0 -62
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_5_vl.py +0 -209
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/processing_qwen2_vl.py +0 -215
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/prompt_utils.py +0 -474
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/sample_utils.py +0 -39
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/tokenizer_utils.py +0 -344
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/__init__.py +0 -9
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/lora.py +0 -70
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/trainer.py +0 -296
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/trainer/utils.py +0 -160
- nexaai/binds/nexa_mlx/py-lib/vlm/modeling/utils.py +0 -928
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.dist-info}/WHEEL +0 -0
- {nexaai-1.0.19rc7.dist-info → nexaai-1.0.19rc9.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,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
|