|
| 1 | + |
| 2 | +# Phi3-Vision 最佳实践 |
| 3 | + |
| 4 | +## 目录 |
| 5 | +- [环境准备](#环境准备) |
| 6 | +- [推理](#推理) |
| 7 | +- [微调](#微调) |
| 8 | +- [微调后推理](#微调后推理) |
| 9 | + |
| 10 | + |
| 11 | +## 环境准备 |
| 12 | +```shell |
| 13 | +git clone https://github.com/modelscope/swift.git |
| 14 | +cd swift |
| 15 | +pip install -e '.[llm]' |
| 16 | +``` |
| 17 | +模型链接: |
| 18 | +- phi3-vision-128k-instruct: [https://modelscope.cn/models/LLM-Research/Phi-3-vision-128k-instruct/summary](https://modelscope.cn/models/LLM-Research/Phi-3-vision-128k-instruct/summary) |
| 19 | + |
| 20 | + |
| 21 | +## 推理 |
| 22 | + |
| 23 | +推理 phi3-vision-128k-instruct: |
| 24 | +```shell |
| 25 | +# Experimental environment: A10, 3090, V100, ... |
| 26 | +# 16GB GPU memory |
| 27 | +CUDA_VISIBLE_DEVICES=0 swift infer --model_type phi3-vision-128k-instruct |
| 28 | +``` |
| 29 | + |
| 30 | +输出: (支持传入本地路径或URL) |
| 31 | +```python |
| 32 | +""" |
| 33 | +<<< Who are you? |
| 34 | +I am Phi, an AI developed by Microsoft to assist with providing information, answering questions, and helping users find solutions to their queries. How can I assist you today? |
| 35 | +-------------------------------------------------- |
| 36 | +<<< clear |
| 37 | +<<< <img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png</img><img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png</img>What is the difference between these two pictures? |
| 38 | +The first picture shows a group of four cartoon sheep standing in a field, while the second picture is a close-up of a kitten with a blurred background. The main difference between these two pictures is the subject matter and the setting. The first picture features animals that are typically associated with farm life and agriculture, while the second picture focuses on a domestic animal, a kitten, which is more commonly found in households. Additionally, the first picture has a more peaceful and serene atmosphere, while the second picture has a more intimate and detailed view of the kitten. |
| 39 | +-------------------------------------------------- |
| 40 | +<<< clear |
| 41 | +<<< <img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png</img>How many sheep are there in the picture? |
| 42 | +There are four sheep in the picture. |
| 43 | +-------------------------------------------------- |
| 44 | +<<< clear |
| 45 | +<<< <img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/math.png</img>What is the result of the calculation? |
| 46 | +The result of the calculation 1452 + 45304 is 46756. |
| 47 | +-------------------------------------------------- |
| 48 | +<<< clear |
| 49 | +<<< <img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/poem.png</img>Write a poem based on the content of the picture. |
| 50 | +In the tranquil night, a boat sails, |
| 51 | +Through the darkened river, it sets sail. |
| 52 | +A single candle flickers, casting light, |
| 53 | +Guiding the way through the endless night. |
| 54 | +
|
| 55 | +The stars above, like diamonds bright, |
| 56 | +Gleam down upon the boat's gentle flight. |
| 57 | +The moon, a silent guardian in the sky, |
| 58 | +Watches over the boat as it sails by. |
| 59 | +
|
| 60 | +The river, a mirror to the night, |
| 61 | +Reflects the boat's journey, a beautiful sight. |
| 62 | +The trees on either side, standing tall, |
| 63 | +Whisper secrets to the boat, one and all. |
| 64 | +
|
| 65 | +In the stillness of the night, a sense of peace, |
| 66 | +The boat, the river, the trees, all in their place. |
| 67 | +A moment frozen in time, a scene so serene, |
| 68 | +A journey through the night, a dream so unseen. |
| 69 | +""" |
| 70 | +``` |
| 71 | + |
| 72 | +示例图片如下: |
| 73 | + |
| 74 | +cat: |
| 75 | + |
| 76 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png" width="250" style="display: inline-block;"> |
| 77 | + |
| 78 | +animal: |
| 79 | + |
| 80 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png" width="250" style="display: inline-block;"> |
| 81 | + |
| 82 | +math: |
| 83 | + |
| 84 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/math.png" width="250" style="display: inline-block;"> |
| 85 | + |
| 86 | +poem: |
| 87 | + |
| 88 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/poem.png" width="250" style="display: inline-block;"> |
| 89 | + |
| 90 | + |
| 91 | +**单样本推理** |
| 92 | + |
| 93 | +```python |
| 94 | +import os |
| 95 | +os.environ['CUDA_VISIBLE_DEVICES'] = '0' |
| 96 | + |
| 97 | +from swift.llm import ( |
| 98 | + get_model_tokenizer, get_template, inference, ModelType, |
| 99 | + get_default_template_type, inference_stream |
| 100 | +) |
| 101 | +from swift.utils import seed_everything |
| 102 | +import torch |
| 103 | + |
| 104 | +model_type = ModelType.phi3_vision_128k_instruct |
| 105 | +template_type = get_default_template_type(model_type) |
| 106 | +print(f'template_type: {template_type}') |
| 107 | + |
| 108 | +model, tokenizer = get_model_tokenizer(model_type, torch.float16, |
| 109 | + model_kwargs={'device_map': 'auto'}) |
| 110 | +model.generation_config.max_new_tokens = 256 |
| 111 | +template = get_template(template_type, tokenizer) |
| 112 | +seed_everything(42) |
| 113 | + |
| 114 | +query = """<img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/road.png</img>How far is it from each city?""" |
| 115 | +response, history = inference(model, template, query) |
| 116 | +print(f'query: {query}') |
| 117 | +print(f'response: {response}') |
| 118 | + |
| 119 | +# 流式 |
| 120 | +query = 'Which city is the farthest?' |
| 121 | +gen = inference_stream(model, template, query, history) |
| 122 | +print_idx = 0 |
| 123 | +print(f'query: {query}\nresponse: ', end='') |
| 124 | +for response, history in gen: |
| 125 | + delta = response[print_idx:] |
| 126 | + print(delta, end='', flush=True) |
| 127 | + print_idx = len(response) |
| 128 | +print() |
| 129 | +print(f'history: {history}') |
| 130 | +""" |
| 131 | +query: Which city is the farthest? |
| 132 | +response: Guangzhou is the farthest city, located 293km away. |
| 133 | +history: [['<img>http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/road.png</img>How far is it from each city?', 'The distances are as follows: Mata is 14km away, Yangjiang is 62km away, and Guangzhou is 293km away.'], ['Which city is the farthest?', 'Guangzhou is the farthest city, located 293km away.']] |
| 134 | +""" |
| 135 | +``` |
| 136 | + |
| 137 | +示例图片如下: |
| 138 | + |
| 139 | +road: |
| 140 | + |
| 141 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/road.png" width="250" style="display: inline-block;"> |
| 142 | + |
| 143 | + |
| 144 | +## 微调 |
| 145 | +多模态大模型微调通常使用**自定义数据集**进行微调. 这里展示可直接运行的demo: |
| 146 | + |
| 147 | +(默认只对LLM部分的qkv进行lora微调. 如果你想对所有linear含vision模型部分都进行微调, 可以指定`--lora_target_modules ALL`. 支持全参数微调.) |
| 148 | +```shell |
| 149 | +# Experimental environment: A10, 3090, V100, ... |
| 150 | +# 16GB GPU memory |
| 151 | +CUDA_VISIBLE_DEVICES=0 swift sft \ |
| 152 | + --model_type phi3-vision-128k-instruct \ |
| 153 | + --dataset coco-en-mini \ |
| 154 | +``` |
| 155 | + |
| 156 | +[自定义数据集](../LLM/自定义与拓展.md#-推荐命令行参数的形式)支持json, jsonl样式, 以下是自定义数据集的例子: |
| 157 | + |
| 158 | +(支持多轮对话, 支持每轮对话含多张图片或不含图片, 支持传入本地路径或URL) |
| 159 | + |
| 160 | +```json |
| 161 | +[ |
| 162 | + {"conversations": [ |
| 163 | + {"from": "user", "value": "<img>img_path</img>11111"}, |
| 164 | + {"from": "assistant", "value": "22222"} |
| 165 | + ]}, |
| 166 | + {"conversations": [ |
| 167 | + {"from": "user", "value": "<img>img_path</img><img>img_path2</img><img>img_path3</img>aaaaa"}, |
| 168 | + {"from": "assistant", "value": "bbbbb"}, |
| 169 | + {"from": "user", "value": "<img>img_path</img>ccccc"}, |
| 170 | + {"from": "assistant", "value": "ddddd"} |
| 171 | + ]}, |
| 172 | + {"conversations": [ |
| 173 | + {"from": "user", "value": "AAAAA"}, |
| 174 | + {"from": "assistant", "value": "BBBBB"}, |
| 175 | + {"from": "user", "value": "CCCCC"}, |
| 176 | + {"from": "assistant", "value": "DDDDD"} |
| 177 | + ]} |
| 178 | +] |
| 179 | +``` |
| 180 | + |
| 181 | + |
| 182 | +## 微调后推理 |
| 183 | +直接推理: |
| 184 | +```shell |
| 185 | +CUDA_VISIBLE_DEVICES=0 swift infer \ |
| 186 | + --ckpt_dir output/phi3-vision-128k-instruct/vx-xxx/checkpoint-xxx \ |
| 187 | + --load_dataset_config true \ |
| 188 | +``` |
| 189 | + |
| 190 | +**merge-lora**并推理: |
| 191 | +```shell |
| 192 | +CUDA_VISIBLE_DEVICES=0 swift export \ |
| 193 | + --ckpt_dir output/phi3-vision-128k-instruct/vx-xxx/checkpoint-xxx \ |
| 194 | + --merge_lora true --safe_serialization false |
| 195 | + |
| 196 | +CUDA_VISIBLE_DEVICES=0 swift infer \ |
| 197 | + --ckpt_dir output/phi3-vision-128k-instruct/vx-xxx/checkpoint-xxx-merged \ |
| 198 | + --load_dataset_config true |
| 199 | +``` |
0 commit comments