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 internvl2 grounding #1473

Merged
merged 23 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
lint
  • Loading branch information
tastelikefeet committed Jul 24, 2024
commit fdb68a6909027cf3057914b542a155c1bd55380d
27 changes: 23 additions & 4 deletions swift/llm/export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Alibaba, Inc. and its affiliates.
import os
from typing import Optional
from typing import List, Optional

import json
import torch
Expand Down Expand Up @@ -93,6 +93,25 @@ def gptq_model_quantize(model, tokenizer):
return gptq_quantizer


def replace_and_concat(template: 'Template', template_list: List, placeholder: str, keyword: str):
final_str = ''
for t in template_list:
if isinstance(t, str):
final_str += t.replace(placeholder, keyword)
elif isinstance(t, (tuple, list)):
if isinstance(t[0], int):
final_str += template.tokenizer.decode(t)
else:
for attr in t:
if attr == 'bos_token_id':
final_str += template.tokenizer.bos_token
elif attr == 'eos_token_id':
final_str += template.tokenizer.eos_token
else:
raise ValueError(f'Unknown token: {attr}')
return final_str


def llm_export(args: ExportArguments) -> None:
global _args, template
logger.info(f'args: {args}')
Expand Down Expand Up @@ -131,14 +150,14 @@ def llm_export(args: ExportArguments) -> None:
with open(os.path.join(args.ollama_output_dir, 'Modelfile'), 'w') as f:
f.write(f'FROM {model_dir}\n')
f.write(f'TEMPLATE """{{{{ if .System }}}}'
f'{template.system_prefix[0].replace("{{SYSTEM}}", "{{ .System }}")}'
f'{replace_and_concat(template, template.system_prefix, "{{SYSTEM}}", "{{ .System }}")}'
f'{{{{ end }}}}')
f.write(f'{{{{ if .Prompt }}}}'
f'{template.prompt[0].replace("{{QUERY}}", "{{ .Prompt }}")}'
f'{replace_and_concat(template, template.prompt, "{{QUERY}}", "{{ .Prompt }}")}'
f'{{{{ end }}}}')
f.write('{{ .Response }}')
f.write(template.suffix[0] + '"""\n')
f.write(f'PARAMETER stop "{template.suffix[0]}"\n')
f.write(f'PARAMETER stop "{replace_and_concat(template, template.suffix, "", "")}"\n')
if args.stop_words:
for stop_word in args.stop_words:
f.write(f'PARAMETER stop "{stop_word}"\n')
Expand Down
4 changes: 2 additions & 2 deletions swift/llm/utils/vision_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def draw_plot(img_dir: str, bbox: List[int], bbox_type: str, output_file: str):
Template.normalize_bbox(objects, [image], 'real')
bbox = objects[0]['bbox']
draw = ImageDraw.Draw(image)
draw.rectangle(bbox, outline="red", width=2)
draw.rectangle(bbox, outline='red', width=2)
image.save(output_file)


if __name__ == '__main__':
# A test main to draw bbox
draw_plot('/mnt/workspace/man.jpg', [354, 462, 580, 738], 'norm_1000', '/mnt/workspace/man_bbox.jpg')
draw_plot('/mnt/workspace/man.jpg', [354, 462, 580, 738], 'norm_1000', '/mnt/workspace/man_bbox.jpg')
1 change: 0 additions & 1 deletion tests/llm/test_ollama_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def test_llama3(self):
self.assertTrue(template in content)
self.assertTrue(stop in content)

@unittest.skip('TODO FIX')
def test_glm4(self):
args = ExportArguments(model_type='glm4-9b-chat', to_ollama=True, ollama_output_dir=self.tmp_dir)
export_main(args)
Expand Down
Loading