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,296 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
import warnings
|
|
3
|
-
from dataclasses import dataclass, field
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
from typing import Union
|
|
6
|
-
|
|
7
|
-
import mlx.core as mx
|
|
8
|
-
import mlx.nn as nn
|
|
9
|
-
import numpy as np
|
|
10
|
-
from mlx.utils import tree_flatten, tree_map
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def get_prompt(model_type, processor, conversation):
|
|
14
|
-
if model_type == "paligemma":
|
|
15
|
-
return conversation
|
|
16
|
-
|
|
17
|
-
if "chat_template" in processor.__dict__.keys():
|
|
18
|
-
prompt = processor.apply_chat_template(
|
|
19
|
-
conversation,
|
|
20
|
-
tokenize=False,
|
|
21
|
-
add_generation_prompt=False,
|
|
22
|
-
)
|
|
23
|
-
elif "tokenizer" in processor.__dict__.keys():
|
|
24
|
-
prompt = processor.tokenizer.apply_chat_template(
|
|
25
|
-
conversation,
|
|
26
|
-
tokenize=False,
|
|
27
|
-
add_generation_prompt=False,
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
return prompt
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class Dataset:
|
|
34
|
-
def __init__(
|
|
35
|
-
self,
|
|
36
|
-
hf_dataset,
|
|
37
|
-
config,
|
|
38
|
-
processor,
|
|
39
|
-
image_processor=None,
|
|
40
|
-
take=None,
|
|
41
|
-
split=None,
|
|
42
|
-
image_resize_shape=None,
|
|
43
|
-
):
|
|
44
|
-
if split is not None:
|
|
45
|
-
self.dataset = hf_dataset[split]
|
|
46
|
-
else:
|
|
47
|
-
self.dataset = hf_dataset
|
|
48
|
-
if take is not None:
|
|
49
|
-
self.dataset = self.dataset.take(take)
|
|
50
|
-
self.processor = processor
|
|
51
|
-
self.config = config
|
|
52
|
-
self.image_processor = image_processor
|
|
53
|
-
self.image_resize_shape = image_resize_shape
|
|
54
|
-
|
|
55
|
-
def __len__(self):
|
|
56
|
-
return len(self.dataset)
|
|
57
|
-
|
|
58
|
-
def __getitem__(self, idx):
|
|
59
|
-
from mlx_vlm.utils import prepare_inputs
|
|
60
|
-
|
|
61
|
-
item = self.dataset[idx]
|
|
62
|
-
|
|
63
|
-
images = item["images"]
|
|
64
|
-
conversations = item["messages"]
|
|
65
|
-
prompts = []
|
|
66
|
-
|
|
67
|
-
if isinstance(conversations, list) and isinstance(conversations[0], list):
|
|
68
|
-
for conversation in conversations:
|
|
69
|
-
if self.config["model_type"] == "pixtral":
|
|
70
|
-
conversation = [json.loads(i) for i in conversation]
|
|
71
|
-
if len(conversations) > 1:
|
|
72
|
-
warnings.warn(
|
|
73
|
-
"Pixtral batch processing is not supported yet. Set batch size to 1."
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
prompt = get_prompt(
|
|
77
|
-
self.config["model_type"], self.processor, conversation
|
|
78
|
-
)
|
|
79
|
-
prompts.append(prompt)
|
|
80
|
-
|
|
81
|
-
else:
|
|
82
|
-
if self.config["model_type"] == "pixtral":
|
|
83
|
-
conversations = [json.loads(i) for i in conversations]
|
|
84
|
-
prompt = get_prompt(
|
|
85
|
-
self.config["model_type"], self.processor, conversations
|
|
86
|
-
)
|
|
87
|
-
prompts.append(prompt)
|
|
88
|
-
|
|
89
|
-
image_token_index = self.config["image_token_index"]
|
|
90
|
-
|
|
91
|
-
inputs = prepare_inputs(
|
|
92
|
-
self.processor,
|
|
93
|
-
images,
|
|
94
|
-
prompts,
|
|
95
|
-
image_token_index,
|
|
96
|
-
self.image_resize_shape,
|
|
97
|
-
)
|
|
98
|
-
input_ids = inputs["input_ids"]
|
|
99
|
-
pixel_values = inputs["pixel_values"]
|
|
100
|
-
mask = inputs["attention_mask"]
|
|
101
|
-
kwargs = {
|
|
102
|
-
k: v
|
|
103
|
-
for k, v in inputs.items()
|
|
104
|
-
if k not in ["input_ids", "pixel_values", "attention_mask"]
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if mask is None:
|
|
108
|
-
mask = mx.ones_like(input_ids)
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
"pixel_values": pixel_values,
|
|
112
|
-
"input_ids": input_ids,
|
|
113
|
-
"attention_mask": mask,
|
|
114
|
-
**kwargs,
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
def grad_checkpoint(layer):
|
|
119
|
-
"""
|
|
120
|
-
Update all instances of type(layer) to use gradient checkpointing.
|
|
121
|
-
"""
|
|
122
|
-
fn = type(layer).__call__
|
|
123
|
-
|
|
124
|
-
def checkpointed_fn(model, *args, **kwargs):
|
|
125
|
-
def inner_fn(params, *args, **kwargs):
|
|
126
|
-
model.update(params)
|
|
127
|
-
return fn(model, *args, **kwargs)
|
|
128
|
-
|
|
129
|
-
return mx.checkpoint(inner_fn)(model.trainable_parameters(), *args, **kwargs)
|
|
130
|
-
|
|
131
|
-
type(layer).__call__ = checkpointed_fn
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
@dataclass
|
|
135
|
-
class TrainingArgs:
|
|
136
|
-
batch_size: int = field(default=4, metadata={"help": "Minibatch size."})
|
|
137
|
-
iters: int = field(default=100, metadata={"help": "Iterations to train for."})
|
|
138
|
-
val_batches: int = field(
|
|
139
|
-
default=25,
|
|
140
|
-
metadata={
|
|
141
|
-
"help": "Number of validation batches, -1 uses the entire validation set."
|
|
142
|
-
},
|
|
143
|
-
)
|
|
144
|
-
steps_per_report: int = field(
|
|
145
|
-
default=10,
|
|
146
|
-
metadata={"help": "Number of training steps between loss reporting."},
|
|
147
|
-
)
|
|
148
|
-
steps_per_eval: int = field(
|
|
149
|
-
default=200, metadata={"help": "Number of training steps between validations."}
|
|
150
|
-
)
|
|
151
|
-
steps_per_save: int = field(
|
|
152
|
-
default=100, metadata={"help": "Save the model every number steps"}
|
|
153
|
-
)
|
|
154
|
-
max_seq_length: int = field(
|
|
155
|
-
default=2048, metadata={"help": "Maximum sequence length."}
|
|
156
|
-
)
|
|
157
|
-
adapter_file: str = field(
|
|
158
|
-
default="adapters.safetensors",
|
|
159
|
-
metadata={"help": "Save/load path for the trained adapter weights."},
|
|
160
|
-
)
|
|
161
|
-
grad_checkpoint: bool = field(
|
|
162
|
-
default=False,
|
|
163
|
-
metadata={"help": "Use gradient checkpointing to reduce memory use."},
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
def default_loss(model, inputs, targets, lengths):
|
|
168
|
-
logits = model(inputs)
|
|
169
|
-
logits = logits.astype(mx.float32)
|
|
170
|
-
|
|
171
|
-
length_mask = mx.arange(inputs.shape[1])[None, :] < lengths[:, None]
|
|
172
|
-
|
|
173
|
-
ce = nn.losses.cross_entropy(logits, targets) * length_mask
|
|
174
|
-
ntoks = length_mask.sum()
|
|
175
|
-
ce = ce.sum() / ntoks
|
|
176
|
-
|
|
177
|
-
return ce, ntoks
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
class Trainer:
|
|
181
|
-
def __init__(
|
|
182
|
-
self,
|
|
183
|
-
model,
|
|
184
|
-
optimizer,
|
|
185
|
-
train_on_completions=False,
|
|
186
|
-
assistant_id=77091,
|
|
187
|
-
clip_gradients=None,
|
|
188
|
-
):
|
|
189
|
-
self.model = model
|
|
190
|
-
self.optimizer = optimizer
|
|
191
|
-
self.train_on_completions = train_on_completions
|
|
192
|
-
self.assistant_id = assistant_id
|
|
193
|
-
self.clip_gradients = clip_gradients
|
|
194
|
-
|
|
195
|
-
def loss_fn(self, model, batch):
|
|
196
|
-
pixel_values = batch["pixel_values"]
|
|
197
|
-
input_ids = batch["input_ids"]
|
|
198
|
-
attention_mask = batch["attention_mask"]
|
|
199
|
-
lengths = mx.sum(attention_mask, axis=1)
|
|
200
|
-
labels = input_ids[:, 1:]
|
|
201
|
-
|
|
202
|
-
batch_size, seq_length = input_ids.shape
|
|
203
|
-
|
|
204
|
-
if self.train_on_completions:
|
|
205
|
-
weight_mask = mx.ones_like(attention_mask)
|
|
206
|
-
|
|
207
|
-
assistant_response_index = np.where(input_ids == self.assistant_id)[1]
|
|
208
|
-
range_matrix = mx.repeat(
|
|
209
|
-
mx.expand_dims(mx.arange(seq_length), 0), batch_size, axis=0
|
|
210
|
-
)
|
|
211
|
-
assistant_mask = range_matrix <= mx.array(assistant_response_index).reshape(
|
|
212
|
-
-1, 1
|
|
213
|
-
)
|
|
214
|
-
# Apply the mask to weight_mask
|
|
215
|
-
weight_mask = mx.where(
|
|
216
|
-
assistant_mask, mx.zeros_like(weight_mask), weight_mask
|
|
217
|
-
)[:, 1:]
|
|
218
|
-
else:
|
|
219
|
-
weight_mask = None
|
|
220
|
-
|
|
221
|
-
input_ids = input_ids[:, :-1]
|
|
222
|
-
|
|
223
|
-
kwargs = {
|
|
224
|
-
k: v
|
|
225
|
-
for k, v in batch.items()
|
|
226
|
-
if k not in ["input_ids", "pixel_values", "attention_mask"]
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
# Forward pass
|
|
230
|
-
outputs = model(input_ids, pixel_values, attention_mask, **kwargs)
|
|
231
|
-
|
|
232
|
-
# Cast to float32
|
|
233
|
-
logits = outputs.logits.astype(mx.float32)
|
|
234
|
-
|
|
235
|
-
# Ensure logits and labels have the same sequence length
|
|
236
|
-
def align_logits_with_labels(logits, labels):
|
|
237
|
-
if logits.shape[1] < labels.shape[1]:
|
|
238
|
-
pad_length = labels.shape[1] - logits.shape[1]
|
|
239
|
-
pad_width = ((0, 0), (0, pad_length), (0, 0))
|
|
240
|
-
return mx.pad(logits, pad_width, mode="constant", constant_values=-100)
|
|
241
|
-
elif logits.shape[1] > labels.shape[1]:
|
|
242
|
-
return logits[:, -labels.shape[1] :, :]
|
|
243
|
-
return logits
|
|
244
|
-
|
|
245
|
-
logits = align_logits_with_labels(logits, labels)
|
|
246
|
-
|
|
247
|
-
length_mask = mx.arange(input_ids.shape[1])[None, :] < lengths[:, None]
|
|
248
|
-
|
|
249
|
-
# Compute loss only on non-padded tokens
|
|
250
|
-
ce = (
|
|
251
|
-
nn.losses.cross_entropy(
|
|
252
|
-
logits,
|
|
253
|
-
labels,
|
|
254
|
-
weights=weight_mask,
|
|
255
|
-
)
|
|
256
|
-
* length_mask
|
|
257
|
-
)
|
|
258
|
-
ntoks = length_mask.sum()
|
|
259
|
-
ce = ce.sum() / ntoks
|
|
260
|
-
|
|
261
|
-
return ce
|
|
262
|
-
|
|
263
|
-
def train_step(self, batch):
|
|
264
|
-
loss_and_grad_fn = nn.value_and_grad(self.model, self.loss_fn)
|
|
265
|
-
loss, grads = loss_and_grad_fn(self.model, batch)
|
|
266
|
-
|
|
267
|
-
# Add gradient clipping
|
|
268
|
-
if self.clip_gradients is not None:
|
|
269
|
-
grads = tree_map(
|
|
270
|
-
lambda g: mx.clip(g, -self.clip_gradients, self.clip_gradients), grads
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
self.optimizer.update(self.model, grads)
|
|
274
|
-
|
|
275
|
-
return loss
|
|
276
|
-
|
|
277
|
-
@mx.compile
|
|
278
|
-
def train_epoch(self, dataloader):
|
|
279
|
-
total_loss = 0
|
|
280
|
-
for batch in dataloader:
|
|
281
|
-
loss = self.train_step(batch)
|
|
282
|
-
mx.eval(self.model, self.optimizer.state)
|
|
283
|
-
total_loss += loss
|
|
284
|
-
return total_loss / len(dataloader)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
def save_adapter(
|
|
288
|
-
model: nn.Module,
|
|
289
|
-
adapter_file: Union[str, Path],
|
|
290
|
-
):
|
|
291
|
-
path = Path(adapter_file)
|
|
292
|
-
if hasattr(model.config, "lora"):
|
|
293
|
-
with open(path.parent / "adapter_config.json", "w") as f:
|
|
294
|
-
json.dump(model.config.lora, f)
|
|
295
|
-
flattened_tree = tree_flatten(model.trainable_parameters())
|
|
296
|
-
mx.save_safetensors(str(adapter_file), dict(flattened_tree))
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
from pathlib import Path
|
|
3
|
-
|
|
4
|
-
import mlx.nn as nn
|
|
5
|
-
from mlx.utils import tree_flatten
|
|
6
|
-
|
|
7
|
-
from .lora import LoRaLayer
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def get_module_by_name(model, name):
|
|
11
|
-
parts = name.split(".")
|
|
12
|
-
module = model
|
|
13
|
-
for part in parts:
|
|
14
|
-
if part.isdigit():
|
|
15
|
-
module = module[int(part)]
|
|
16
|
-
else:
|
|
17
|
-
module = getattr(module, part)
|
|
18
|
-
return module
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def set_module_by_name(model, name, new_module):
|
|
22
|
-
parts = name.split(".")
|
|
23
|
-
module = model
|
|
24
|
-
for part in parts[:-1]:
|
|
25
|
-
if part.isdigit():
|
|
26
|
-
module = module[int(part)]
|
|
27
|
-
else:
|
|
28
|
-
module = getattr(module, part)
|
|
29
|
-
if parts[-1].isdigit():
|
|
30
|
-
module[int(parts[-1])] = new_module
|
|
31
|
-
else:
|
|
32
|
-
setattr(module, parts[-1], new_module)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def get_peft_model(
|
|
36
|
-
model, linear_layers, rank=10, alpha=0.1, dropout=0.1, freeze=True, verbose=True
|
|
37
|
-
):
|
|
38
|
-
if freeze:
|
|
39
|
-
freeze_model(model)
|
|
40
|
-
|
|
41
|
-
for name, module in model.language_model.named_modules():
|
|
42
|
-
if isinstance(module, nn.Linear) or isinstance(module, nn.QuantizedLinear):
|
|
43
|
-
if name.split(".")[-1] in linear_layers:
|
|
44
|
-
lora_layer = LoRaLayer(module, rank, alpha, dropout)
|
|
45
|
-
set_module_by_name(model.language_model, name, lora_layer)
|
|
46
|
-
|
|
47
|
-
model.config.lora = {}
|
|
48
|
-
model.config.lora["rank"] = rank
|
|
49
|
-
model.config.lora["alpha"] = alpha
|
|
50
|
-
model.config.lora["dropout"] = dropout
|
|
51
|
-
|
|
52
|
-
if verbose:
|
|
53
|
-
print_trainable_parameters(model.language_model)
|
|
54
|
-
|
|
55
|
-
return model
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def freeze_model(model):
|
|
59
|
-
for name, module in model.named_modules():
|
|
60
|
-
name = name.split(".")[0]
|
|
61
|
-
if name in [
|
|
62
|
-
"language_model",
|
|
63
|
-
"vision_model",
|
|
64
|
-
"vision_tower",
|
|
65
|
-
"aligner",
|
|
66
|
-
"connector",
|
|
67
|
-
"multi_modal_projector",
|
|
68
|
-
"mm_projector",
|
|
69
|
-
]:
|
|
70
|
-
model[f"{name}"].freeze()
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def find_all_linear_names(model):
|
|
74
|
-
cls = nn.Linear
|
|
75
|
-
quantized_cls = nn.QuantizedLinear
|
|
76
|
-
lora_module_names = set()
|
|
77
|
-
multimodal_keywords = [
|
|
78
|
-
"mm_projector",
|
|
79
|
-
"vision_tower",
|
|
80
|
-
"vision_resampler",
|
|
81
|
-
"aligner",
|
|
82
|
-
]
|
|
83
|
-
for name, module in model.named_modules():
|
|
84
|
-
if any(mm_keyword in name for mm_keyword in multimodal_keywords):
|
|
85
|
-
continue
|
|
86
|
-
if isinstance(module, cls) or isinstance(module, quantized_cls):
|
|
87
|
-
names = name.split(".")
|
|
88
|
-
lora_module_names.add(names[0] if len(names) == 1 else names[-1])
|
|
89
|
-
|
|
90
|
-
if "lm_head" in lora_module_names: # needed for 16-bit
|
|
91
|
-
lora_module_names.remove("lm_head")
|
|
92
|
-
return list(lora_module_names)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def count_parameters(model):
|
|
96
|
-
def nparams(m):
|
|
97
|
-
if isinstance(m, (nn.QuantizedLinear, nn.QuantizedEmbedding)):
|
|
98
|
-
return m.weight.size * (32 // m.bits)
|
|
99
|
-
return sum(v.size for _, v in tree_flatten(m.parameters()))
|
|
100
|
-
|
|
101
|
-
leaf_modules = tree_flatten(
|
|
102
|
-
model.leaf_modules(), is_leaf=lambda m: isinstance(m, nn.Module)
|
|
103
|
-
)
|
|
104
|
-
total_p = sum(nparams(m) for _, m in leaf_modules) / 10**6
|
|
105
|
-
|
|
106
|
-
return total_p
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
def print_trainable_parameters(model):
|
|
110
|
-
def nparams(m):
|
|
111
|
-
if isinstance(m, (nn.QuantizedLinear, nn.QuantizedEmbedding)):
|
|
112
|
-
return m.weight.size * (32 // m.bits)
|
|
113
|
-
return sum(v.size for _, v in tree_flatten(m.parameters()))
|
|
114
|
-
|
|
115
|
-
leaf_modules = tree_flatten(
|
|
116
|
-
model.leaf_modules(), is_leaf=lambda m: isinstance(m, nn.Module)
|
|
117
|
-
)
|
|
118
|
-
total_p = sum(nparams(m) for _, m in leaf_modules) / 10**6
|
|
119
|
-
trainable_p = (
|
|
120
|
-
sum(v.size for _, v in tree_flatten(model.trainable_parameters())) / 10**6
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
print(
|
|
124
|
-
f"#trainable params: {trainable_p} M || all params: {total_p} M || trainable%: {(trainable_p * 100 / total_p):.3f}%"
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
def apply_lora_layers(model: nn.Module, adapter_path: str) -> nn.Module:
|
|
129
|
-
"""
|
|
130
|
-
Apply LoRA layers to the model.
|
|
131
|
-
|
|
132
|
-
Args:
|
|
133
|
-
model (nn.Module): The neural network model.
|
|
134
|
-
adapter_path (str): Path to the adapter configuration file.
|
|
135
|
-
|
|
136
|
-
Returns:
|
|
137
|
-
nn.Module: The updated model with LoRA layers applied.
|
|
138
|
-
"""
|
|
139
|
-
adapter_path = Path(adapter_path)
|
|
140
|
-
|
|
141
|
-
if not adapter_path.exists():
|
|
142
|
-
raise FileNotFoundError(f"The adapter path does not exist: {adapter_path}")
|
|
143
|
-
|
|
144
|
-
# Check if the adapter has lora params in the config (adapter_config.json)
|
|
145
|
-
with open(adapter_path / "adapter_config.json", "r") as f:
|
|
146
|
-
config = json.load(f)
|
|
147
|
-
if "rank" not in config:
|
|
148
|
-
raise ValueError("The adapter does not have lora params in the config")
|
|
149
|
-
|
|
150
|
-
# TODO: add lora params to the config and load them here
|
|
151
|
-
list_of_modules = find_all_linear_names(model.language_model.model)
|
|
152
|
-
if config is not None:
|
|
153
|
-
model = get_peft_model(model, list_of_modules, **config)
|
|
154
|
-
else:
|
|
155
|
-
model = get_peft_model(model, list_of_modules)
|
|
156
|
-
|
|
157
|
-
# TODO: Use custom adapter name
|
|
158
|
-
model.load_weights(str(adapter_path / "adapters.safetensors"), strict=False)
|
|
159
|
-
|
|
160
|
-
return model
|