-
Notifications
You must be signed in to change notification settings - Fork 460
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 mllm pt #2146
Merged
Merged
support mllm pt #2146
Changes from 1 commit
Commits
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 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 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 |
---|---|---|
|
@@ -404,62 +404,50 @@ def check_example(self, example: Dict[str, Any]) -> None: | |
def add_default_tags(self, example: Dict[str, Any]) -> None: | ||
history: History = deepcopy(example.get('history') or []) | ||
query: str = example.get('query') or '' | ||
response: str = example.get('response') or '' | ||
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. The response supports images and supports interleaved pre-training. |
||
history.append([query, response]) | ||
for media_key, media_tag in [('videos', '<video>'), ('images', '<image>'), ('audios', '<audio>')]: | ||
if example.get(media_key): | ||
infer_media_type = TEMPLATE_MAPPING[self.template_type].get('infer_media_type') | ||
if infer_media_type == 'round': | ||
n_round = len(example[media_key]) | ||
assert n_round == len(history) + 1 | ||
for i, h, m in zip(range(n_round), history + [[query, None]], example[media_key]): | ||
num_media_tags = len(re.findall(media_tag, h[0])) | ||
assert n_round == len(history) | ||
for i, h, m in zip(range(n_round), history, example[media_key]): | ||
content = f'{h[0]}\n{h[1]}' | ||
num_media_tags = len(re.findall(media_tag, content)) | ||
if m: | ||
assert num_media_tags <= 1, ( | ||
'The model includes at most one media per round. However, ' | ||
f'this round contains {num_media_tags} media_tags. query: {h[0]}') | ||
f'this round contains {num_media_tags} media_tags. query: {h[0]}, response: {h[1]}') | ||
if num_media_tags == 0: | ||
h[0] = media_tag + h[0] | ||
else: | ||
assert num_media_tags == 0, f'Missing media. query: {h[0]}' | ||
if i == n_round - 1: | ||
query = h[0] | ||
else: | ||
history[i][0] = h[0] | ||
history[i][0] = h[0] | ||
|
||
example[media_key] = [m for m in example[media_key] if m] | ||
|
||
else: | ||
num_media_tags = len(re.findall(media_tag, '\n'.join([h[0] for h in history]) + f'\n{query}')) | ||
num_media_tags = len(re.findall(media_tag, '\n'.join([f'{h[0]}\n{h[1]}' for h in history]))) | ||
example[media_key] = [m for m in example[media_key] if m] | ||
num_media = len(example[media_key]) | ||
num_new_tags = num_media - num_media_tags | ||
assert num_new_tags >= 0, f'Number of media: {num_media}, number of media_tags: {num_media_tags}' | ||
if history: | ||
history[0][0] = media_tag * num_new_tags + history[0][0] | ||
else: | ||
query = media_tag * num_new_tags + query | ||
history[0][0] = media_tag * num_new_tags + history[0][0] | ||
|
||
example['query'] = query | ||
example['history'] = history | ||
example['query'] = history[-1][0] | ||
example['response'] = history[-1][1] | ||
example['history'] = history[:-1] | ||
|
||
def replace_media_tags(self, example) -> None: | ||
# Parse <img></img> format images and merged into images key | ||
if self.is_multimodal in {True, None}: # If False, do not perform replace_img_tag | ||
example['query'], example['history'], images_path = replace_img_tag( | ||
example.get('query'), | ||
example.get('history') or [], '<image>') | ||
|
||
if example.get('images') and images_path: | ||
raise ValueError('Do not mix use the <img></img> tag and <image> tag.') | ||
example['images'] = example.get('images') or [] + images_path | ||
|
||
# audio, video | ||
if self.is_multimodal in {True, None}: | ||
for k, tag, pattern in zip(['audios', 'videos'], ['<audio>', '<video>'], | ||
[r'<audio>(.+?)</audio>', r'<video>(.+?)</video>']): | ||
example['query'], example['history'], medias_path = replace_img_tag( | ||
example.get('query'), | ||
for k, tag, pattern in zip(['images', 'audios', 'videos'], ['<image>', '<audio>', '<video>'], | ||
[r'<img>(.+?)</img>', r'<audio>(.+?)</audio>', r'<video>(.+?)</video>']): | ||
example['query'], example['response'], example['history'], medias_path = replace_img_tag( | ||
example.get('query'), example.get('response'), | ||
example.get('history') or [], tag, pattern) | ||
|
||
if example.get(k) and medias_path: | ||
raise ValueError(f'Do not mix use the {pattern} tag and {tag} tag.') | ||
example[k] = example.get(k) or [] + medias_path | ||
|
||
def _preprocess_media(self, example): | ||
|
@@ -2118,23 +2106,23 @@ class Internlm2Template(ChatmlTemplate): | |
|
||
|
||
def replace_img_tag(query: str, | ||
response: str, | ||
history: History, | ||
replace_token: str, | ||
pattern=r'<img>(.+?)</img>') -> Tuple[str, History, List[str]]: | ||
images_path = [] | ||
new_history = [] | ||
history.append([query, response]) | ||
for i, h in enumerate(history): | ||
if h[0] is None: | ||
new_history.append(h.copy()) | ||
else: | ||
images_path += re.findall(pattern, h[0]) | ||
new_history.append([re.sub(pattern, replace_token, h[0]), h[1]]) | ||
if query is None: | ||
new_query = query # pretrain dataset | ||
else: | ||
images_path += re.findall(pattern, query) | ||
new_query = re.sub(pattern, replace_token, query) | ||
return new_query, new_history, images_path | ||
new_h = [] | ||
for content in h: | ||
if content is None: | ||
new_h.append(content) | ||
else: | ||
images_path += re.findall(pattern, content) | ||
new_h.append(re.sub(pattern, replace_token, content)) | ||
new_history.append(new_h) | ||
return new_history[-1][0], new_history[-1][1], new_history[:-1], images_path | ||
|
||
|
||
class InternLMXComposer2Template(Template): | ||
|
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.
fix lora