Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pixtral large #2481

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/Instruction/支持的模型和数据集.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
|minicpm-v-v2_5-chat|[OpenBMB/MiniCPM-Llama3-V-2_5](https://modelscope.cn/models/OpenBMB/MiniCPM-Llama3-V-2_5/summary)|^(llm\|resampler)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|minicpm-v-v2_5|✔|✔|✘|✘|timm, transformers>=4.36|vision|[openbmb/MiniCPM-Llama3-V-2_5](https://huggingface.co/openbmb/MiniCPM-Llama3-V-2_5)|
|minicpm-v-v2_6-chat|[OpenBMB/MiniCPM-V-2_6](https://modelscope.cn/models/OpenBMB/MiniCPM-V-2_6/summary)|^(llm\|resampler)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|minicpm-v-v2_6|✔|✔|✘|✘|timm, transformers>=4.36|vision, video|[openbmb/MiniCPM-V-2_6](https://huggingface.co/openbmb/MiniCPM-V-2_6)|
|pixtral-12b|[AI-ModelScope/pixtral-12b](https://modelscope.cn/models/AI-ModelScope/pixtral-12b/summary)|^(language_model\|multi_modal_projector)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|pixtral|✘|✘|✘|✘|transformers>=4.45|vision|[mistral-community/pixtral-12b](https://huggingface.co/mistral-community/pixtral-12b)|
|pixtral-large-instruct|[AI-ModelScope/Pixtral-Large-Instruct-2411](https://modelscope.cn/models/AI-ModelScope/Pixtral-Large-Instruct-2411/summary)|^(language_model\|multi_modal_projector)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|pixtral-large|✘|✘|✘|✘|transformers>=4.45|vision|[mistralai/Pixtral-Large-Instruct-2411](https://huggingface.co/mistralai/Pixtral-Large-Instruct-2411)|
|mplug-owl2-chat|[iic/mPLUG-Owl2](https://modelscope.cn/models/iic/mPLUG-Owl2/summary)|q_proj, k_proj.multiway.0, k_proj.multiway.1, v_proj.multiway.0, v_proj.multiway.1|mplug-owl2|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers<4.35, icecream|vision|[MAGAer13/mplug-owl2-llama2-7b](https://huggingface.co/MAGAer13/mplug-owl2-llama2-7b)|
|mplug-owl2_1-chat|[iic/mPLUG-Owl2.1](https://modelscope.cn/models/iic/mPLUG-Owl2.1/summary)|c_attn.multiway.0, c_attn.multiway.1|mplug-owl2|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers<4.35, icecream|vision|[Mizukiluke/mplug_owl_2_1](https://huggingface.co/Mizukiluke/mplug_owl_2_1)|
|mplug-owl3-1b-chat|[iic/mPLUG-Owl3-1B-241014](https://modelscope.cn/models/iic/mPLUG-Owl3-1B-241014/summary)|^(language_model\|vision2text_model)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|mplug_owl3|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers>=4.36, icecream|vision, video|[mPLUG/mPLUG-Owl3-1B-241014](https://huggingface.co/mPLUG/mPLUG-Owl3-1B-241014)|
Expand Down
1 change: 1 addition & 0 deletions docs/source_en/Instruction/Supported-models-datasets.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ The table below introcudes all models supported by SWIFT:
|minicpm-v-v2_5-chat|[OpenBMB/MiniCPM-Llama3-V-2_5](https://modelscope.cn/models/OpenBMB/MiniCPM-Llama3-V-2_5/summary)|^(llm\|resampler)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|minicpm-v-v2_5|&#x2714;|&#x2714;|&#x2718;|&#x2718;|timm, transformers>=4.36|vision|[openbmb/MiniCPM-Llama3-V-2_5](https://huggingface.co/openbmb/MiniCPM-Llama3-V-2_5)|
|minicpm-v-v2_6-chat|[OpenBMB/MiniCPM-V-2_6](https://modelscope.cn/models/OpenBMB/MiniCPM-V-2_6/summary)|^(llm\|resampler)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|minicpm-v-v2_6|&#x2714;|&#x2714;|&#x2718;|&#x2718;|timm, transformers>=4.36|vision, video|[openbmb/MiniCPM-V-2_6](https://huggingface.co/openbmb/MiniCPM-V-2_6)|
|pixtral-12b|[AI-ModelScope/pixtral-12b](https://modelscope.cn/models/AI-ModelScope/pixtral-12b/summary)|^(language_model\|multi_modal_projector)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|pixtral|&#x2718;|&#x2718;|&#x2718;|&#x2718;|transformers>=4.45|vision|[mistral-community/pixtral-12b](https://huggingface.co/mistral-community/pixtral-12b)|
|pixtral-large-instruct|[AI-ModelScope/Pixtral-Large-Instruct-2411](https://modelscope.cn/models/AI-ModelScope/Pixtral-Large-Instruct-2411/summary)|^(language_model\|multi_modal_projector)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|pixtral-large|&#x2718;|&#x2718;|&#x2718;|&#x2718;|transformers>=4.45|vision|[mistralai/Pixtral-Large-Instruct-2411](https://huggingface.co/mistralai/Pixtral-Large-Instruct-2411)|
|mplug-owl2-chat|[iic/mPLUG-Owl2](https://modelscope.cn/models/iic/mPLUG-Owl2/summary)|q_proj, k_proj.multiway.0, k_proj.multiway.1, v_proj.multiway.0, v_proj.multiway.1|mplug-owl2|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers<4.35, icecream|vision|[MAGAer13/mplug-owl2-llama2-7b](https://huggingface.co/MAGAer13/mplug-owl2-llama2-7b)|
|mplug-owl2_1-chat|[iic/mPLUG-Owl2.1](https://modelscope.cn/models/iic/mPLUG-Owl2.1/summary)|c_attn.multiway.0, c_attn.multiway.1|mplug-owl2|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers<4.35, icecream|vision|[Mizukiluke/mplug_owl_2_1](https://huggingface.co/Mizukiluke/mplug_owl_2_1)|
|mplug-owl3-1b-chat|[iic/mPLUG-Owl3-1B-241014](https://modelscope.cn/models/iic/mPLUG-Owl3-1B-241014/summary)|^(language_model\|vision2text_model)(?!.\*(lm_head\|output\|emb\|wte\|shared)).\*|mplug_owl3|&#x2714;|&#x2718;|&#x2718;|&#x2718;|transformers>=4.36, icecream|vision, video|[mPLUG/mPLUG-Owl3-1B-241014](https://huggingface.co/mPLUG/mPLUG-Owl3-1B-241014)|
Expand Down
10 changes: 10 additions & 0 deletions swift/llm/utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class ModelType:
ministral_8b_instruct_2410 = 'ministral-8b-instruct-2410'

pixtral_12b = 'pixtral-12b'
pixtral_large_instruct = 'pixtral-large-instruct'
# wizardlm
wizardlm2_7b_awq = 'wizardlm2-7b-awq'
wizardlm2_8x22b = 'wizardlm2-8x22b'
Expand Down Expand Up @@ -1124,6 +1125,15 @@ def _output_device_map_hook(module, input, output):
return output.to(input[0].device)


@register_model(
ModelType.pixtral_large_instruct,
'AI-ModelScope/Pixtral-Large-Instruct-2411',
LoRATM.llava,
TemplateType.pixtral_large,
requires=['transformers>=4.45'],
placeholder_tokens=['[IMG]'],
tags=['multi-modal', 'vision'],
hf_model_id='mistralai/Pixtral-Large-Instruct-2411')
@register_model(
ModelType.pixtral_12b,
'AI-ModelScope/pixtral-12b',
Expand Down
37 changes: 31 additions & 6 deletions swift/llm/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from contextlib import contextmanager
from copy import deepcopy
from datetime import datetime
from datetime import datetime, timedelta
from functools import partial, wraps
from types import MethodType
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, TypeVar, Union
Expand Down Expand Up @@ -89,6 +89,7 @@ class TemplateType:
idefics3 = 'idefics3'
mistral_nemo = 'mistral-nemo'
pixtral = 'pixtral'
pixtral_large = 'pixtral-large'
openbuddy = 'openbuddy'
openbuddy2 = 'openbuddy2'
internlm = 'internlm'
Expand Down Expand Up @@ -1712,9 +1713,6 @@ def _gather_list(batch: List[Dict[str, Any]], attr_name: str) -> Optional[List[A

class PixtralTemplate(Template):

def __init__(self):
super().__init__(['<s>{{SYSTEM}}'], ['[INST]{{QUERY}}[/INST]'], ['</s>'], ['</s>'], None)

def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int,
example: Dict[str, Any]) -> List[Context]:
return ['[IMG]']
Expand Down Expand Up @@ -1761,7 +1759,34 @@ def data_collator(self, batch: List[Dict[str, Any]], padding_to: Optional[int] =
return res


register_template(TemplateType.pixtral, PixtralTemplate(), lazy_tokenize=True)
register_template(
TemplateType.pixtral,
PixtralTemplate(['<s>[INST]'], ['{{SYSTEM}}\n\n', '{{QUERY}}[/INST]'], ['</s>[INST]'], ['</s>'], None),
lazy_tokenize=True)


class PixtralLargeTemplate(PixtralTemplate):

@staticmethod
def _load_system_prompt(model_dir: str) -> str:
file_path = os.path.join(model_dir, 'SYSTEM_PROMPT.txt')
with open(file_path, 'r') as file:
system_prompt = file.read()
today = datetime.today().strftime('%Y-%m-%d')
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')
model_name = model_dir.split('/')[-1]
return system_prompt.format(name=model_name, today=today, yesterday=yesterday)

def _init_template(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs) -> None:
self.default_system = self._load_system_prompt(tokenizer.model_dir)
return super()._init_template(tokenizer, *args, **kwargs)


register_template(
TemplateType.pixtral_large,
PixtralLargeTemplate(['<s>[SYSTEM_PROMPT]{{SYSTEM}}[/SYSTEM_PROMPT]'], ['[INST]{{QUERY}}[/INST]'], ['</s>'],
['</s>'], None),
lazy_tokenize=True)


class YiCoderTemplate(ChatmlTemplate):
Expand Down Expand Up @@ -1948,7 +1973,7 @@ def data_collator(self, batch: List[Dict[str, Any]], padding_to: Optional[int] =
['<<SYS>>\n{{SYSTEM}}\n<</SYS>>\n\n']))

register_template(TemplateType.mistral_nemo,
Template(['<s>[INST] '], ['{{SYSTEM}}\n\n', '{{QUERY}}[/INST]'], ['</s>[INST] '], ['</s>']))
Template(['<s>[INST]'], ['{{SYSTEM}}\n\n', '{{QUERY}}[/INST]'], ['</s>[INST]'], ['</s>']))


class Llama3TemplateMixin:
Expand Down
Loading