-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[model] support Midashenglm #5325
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
98f90bd
support midashenglm
JimmyMa99 3f56bb7
support midashenglm
JimmyMa99 4b9b144
Update swift/llm/model/model/qwen.py
JimmyMa99 a457cf1
Merge branch 'modelscope:main' into main
JimmyMa99 5837b58
midasheng
JimmyMa99 dc2c43e
Fix flake8 E302 and E305 blank line issues
JimmyMa99 a346f0f
Apply yapf formatting changes
JimmyMa99 d218696
Merge branch 'modelscope:main' into main
JimmyMa99 aadc0f9
fp32
fresh-little-lemon 28a0952
Merge branch 'modelscope:main' into main
JimmyMa99 2bddbf6
midasheng-train-infer
JimmyMa99 fca0ece
Merge branch 'main' of github.com:JimmyMa99/ms-swift
JimmyMa99 93f689e
midasheng-train-infer-test
JimmyMa99 1823eaf
[fix]remove requires
JimmyMa99 8de5cce
midashenglm
JimmyMa99 e64df5f
update
Jintao-Huang d9d2594
update
Jintao-Huang cbc57b7
update
Jintao-Huang ca0b400
fix
Jintao-Huang b3eb211
fix
Jintao-Huang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| from . import (baidu, bert, deepseek, emu3, gemma, glm, idefics3, internlm, internvl, kwai, llama, llava, llm, megrez, | ||
| microsoft, minicpm, minimax, mistral, molmo, moonshot, mplug, openbuddy, pixtral, qwen, stepfun, valley, | ||
| yi) | ||
| microsoft, midashenglm, minicpm, minimax, mistral, molmo, moonshot, mplug, openbuddy, pixtral, qwen, | ||
| stepfun, valley, yi) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||||||||||||||
| # Copyright (c) Alibaba, Inc. and its affiliates. | ||||||||||||||||||||||||
| from dataclasses import dataclass, field | ||||||||||||||||||||||||
| from functools import partial | ||||||||||||||||||||||||
| from typing import Any, Dict, List, Literal, Optional | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||
| import torch.nn.functional as F | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from swift.utils import get_env_args | ||||||||||||||||||||||||
| from ..base import Template | ||||||||||||||||||||||||
| from ..constant import MLLMTemplateType | ||||||||||||||||||||||||
| from ..register import register_template | ||||||||||||||||||||||||
| from ..template_inputs import StdTemplateInputs | ||||||||||||||||||||||||
| from ..utils import Context, Word, findall | ||||||||||||||||||||||||
| from ..vision_utils import load_batch | ||||||||||||||||||||||||
| from .qwen import QwenTemplateMeta | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| class MiDashengLMTemplate(Template): | ||||||||||||||||||||||||
| placeholder_tokens = ['<|AUDIO|>'] | ||||||||||||||||||||||||
| skip_prompt = False | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int, | ||||||||||||||||||||||||
| inputs: StdTemplateInputs) -> List[Context]: | ||||||||||||||||||||||||
| assert media_type == 'audio' | ||||||||||||||||||||||||
| return ['<|AUDIO|>'] | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def _encode(self, inputs: StdTemplateInputs) -> Dict[str, Any]: | ||||||||||||||||||||||||
| from transformers.audio_utils import load_audio | ||||||||||||||||||||||||
| encoded = super()._encode(inputs) | ||||||||||||||||||||||||
| input_ids = encoded['input_ids'] | ||||||||||||||||||||||||
| sampling_rate = get_env_args('sampling_rate', int, 16000) | ||||||||||||||||||||||||
| inputs.audios = load_batch(inputs.audios, partial(load_audio, sampling_rate=sampling_rate)) | ||||||||||||||||||||||||
| audio_token = self._tokenize('<|AUDIO|>')[0] | ||||||||||||||||||||||||
| idx_list = findall(input_ids, audio_token) | ||||||||||||||||||||||||
| if idx_list: | ||||||||||||||||||||||||
| split_token = self._tokenize('\n')[0] | ||||||||||||||||||||||||
| audio_inputs = self.processor(text='\n'.join(['<|AUDIO|>'] * len(inputs.audios)), audio=inputs.audios) | ||||||||||||||||||||||||
| splited_tokens = self._split_list(audio_inputs['input_ids'][0].tolist(), split_token) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| encoded['input_ids'], encoded['labels'], encoded['loss_scale'] = self._extend_tokens( | ||||||||||||||||||||||||
| input_ids, encoded['labels'], encoded['loss_scale'], idx_list, lambda i: splited_tokens[i]) | ||||||||||||||||||||||||
| encoded['input_values'] = audio_inputs['input_values'] | ||||||||||||||||||||||||
| encoded['audio_length'] = audio_inputs['audio_length'] | ||||||||||||||||||||||||
| return encoded | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def _data_collator(self, batch: List[Dict[str, Any]], *, padding_to: Optional[int] = None) -> Dict[str, Any]: | ||||||||||||||||||||||||
| res = super()._data_collator(batch, padding_to=padding_to) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| input_values = [b['input_values'] for b in batch if b.get('input_values') is not None] | ||||||||||||||||||||||||
| audio_lengths = [b['audio_length'] for b in batch if b.get('audio_length') is not None] | ||||||||||||||||||||||||
|
Comment on lines
+50
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the code iterates over the
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if input_values: | ||||||||||||||||||||||||
| res['audio_length'] = torch.concat(audio_lengths) | ||||||||||||||||||||||||
| for i in range(len(input_values)): | ||||||||||||||||||||||||
| pad_len = (res['audio_length'].max() - input_values[i].shape[1]).item() | ||||||||||||||||||||||||
| input_values[i] = F.pad(input_values[i], (0, pad_len), 'constant', 0) | ||||||||||||||||||||||||
| res['input_values'] = torch.concat(input_values) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return res | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| register_template(QwenTemplateMeta(MLLMTemplateType.midashenglm, template_cls=MiDashengLMTemplate)) | ||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
_extend_tokensuses list slicing and concatenation inside a loop (list = list[:idx] + ...). This can be inefficient for large lists as it creates a new list in each iteration, leading to quadratic complexity in the worst case (O(M*N) where M is the number of replacements and N is the list length). A more performant approach would be to build the new lists by appending segments, which would be closer to linear time complexity.