|
| 1 | + |
| 2 | +# Llava 最佳实践 |
| 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 | +## 推理 |
| 19 | + |
| 20 | +推理[llava1d6-mistral-7b-chat](https://modelscope.cn/models/AI-ModelScope/llava-v1.6-mistral-7b/summary): |
| 21 | +```shell |
| 22 | +# Experimental environment: A10, 3090, V100... |
| 23 | +# 20GB GPU memory |
| 24 | +CUDA_VISIBLE_DEVICES=0 swift infer --model_type llava1d6-mistral-7b-chat |
| 25 | +``` |
| 26 | + |
| 27 | +输出: (支持传入本地路径或URL) |
| 28 | +```python |
| 29 | +""" |
| 30 | +<<< Describe this image. |
| 31 | +Input a media path or URL <<< http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png |
| 32 | +The image shows a close-up of a kitten with a soft, blurred background that suggests a natural, outdoor setting. The kitten has a mix of white and gray fur with darker stripes, typical of a tabby pattern. Its eyes are wide open, with a striking blue color that contrasts with the kitten's fur. The kitten's nose is small and pink, and its whiskers are long and white, adding to the kitten's cute and innocent appearance. The lighting in the image is soft and diffused, creating a gentle and warm atmosphere. The focus is sharp on the kitten's face, while the rest of the image is slightly out of focus, which draws attention to the kitten's features. |
| 33 | +-------------------------------------------------- |
| 34 | +<<< How many sheep are in the picture? |
| 35 | +Input a media path or URL <<< http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png |
| 36 | +There are four sheep in the picture. |
| 37 | +-------------------------------------------------- |
| 38 | +<<< What is the calculation result? |
| 39 | +Input a media path or URL <<< http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/math.png |
| 40 | +The calculation result is 14352 + 45304 = 145304. |
| 41 | +-------------------------------------------------- |
| 42 | +<<< Write a poem based on the content of the picture. |
| 43 | +Input a media path or URL <<< http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/poem.png |
| 44 | +In the quiet of the night, |
| 45 | +A solitary boat takes flight, |
| 46 | +Across the water's gentle swell, |
| 47 | +Underneath the stars that softly fell. |
| 48 | +
|
| 49 | +The boat, a vessel of the night, |
| 50 | +Carries but one, a lone delight, |
| 51 | +A solitary figure, lost in thought, |
| 52 | +In the tranquil calm, they find a wraith. |
| 53 | +
|
| 54 | +The stars above, like diamonds bright, |
| 55 | +Reflect upon the water's surface light, |
| 56 | +Creating a path for the boat's journey, |
| 57 | +Guiding through the night with a gentle purity. |
| 58 | +
|
| 59 | +The boat, a silent sentinel, |
| 60 | +In the stillness, it gently swells, |
| 61 | +A vessel of peace and calm, |
| 62 | +In the quiet of the night, it carries on. |
| 63 | +
|
| 64 | +The figure on board, a soul at ease, |
| 65 | +In the serene embrace of nature's peace, |
| 66 | +They sail through the night, |
| 67 | +Under the watchful eyes of the stars' light. |
| 68 | +
|
| 69 | +The boat, a symbol of solitude, |
| 70 | +In the vast expanse of the universe's beauty, |
| 71 | +A lone journey, a solitary quest, |
| 72 | +In the quiet of the night, it finds its rest. |
| 73 | +""" |
| 74 | +``` |
| 75 | + |
| 76 | +示例图片如下: |
| 77 | + |
| 78 | +cat: |
| 79 | + |
| 80 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/cat.png" width="250" style="display: inline-block;"> |
| 81 | + |
| 82 | +animal: |
| 83 | + |
| 84 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png" width="250" style="display: inline-block;"> |
| 85 | + |
| 86 | +math: |
| 87 | + |
| 88 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/math.png" width="250" style="display: inline-block;"> |
| 89 | + |
| 90 | +poem: |
| 91 | + |
| 92 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/poem.png" width="250" style="display: inline-block;"> |
| 93 | + |
| 94 | +**单样本推理** |
| 95 | + |
| 96 | +```python |
| 97 | +import os |
| 98 | +os.environ['CUDA_VISIBLE_DEVICES'] = '0' |
| 99 | + |
| 100 | +from swift.llm import ( |
| 101 | + get_model_tokenizer, get_template, inference, ModelType, |
| 102 | + get_default_template_type, inference_stream |
| 103 | +) |
| 104 | +from swift.utils import seed_everything |
| 105 | +import torch |
| 106 | + |
| 107 | +model_type = ModelType.llava1d6_mistral_7b_chat |
| 108 | +template_type = get_default_template_type(model_type) |
| 109 | +print(f'template_type: {template_type}') |
| 110 | + |
| 111 | +model, tokenizer = get_model_tokenizer(model_type, torch.float16, |
| 112 | + model_kwargs={'device_map': 'auto'}) |
| 113 | +model.generation_config.max_new_tokens = 256 |
| 114 | +template = get_template(template_type, tokenizer) |
| 115 | +seed_everything(42) |
| 116 | + |
| 117 | +images = ['http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/road.png'] |
| 118 | +query = 'How far is it from each city?' |
| 119 | +response, _ = inference(model, template, query, images=images) |
| 120 | +print(f'query: {query}') |
| 121 | +print(f'response: {response}') |
| 122 | + |
| 123 | +# 流式 |
| 124 | +query = 'Which city is the farthest?' |
| 125 | +gen = inference_stream(model, template, query, images=images) |
| 126 | +print_idx = 0 |
| 127 | +print(f'query: {query}\nresponse: ', end='') |
| 128 | +for response, _ in gen: |
| 129 | + delta = response[print_idx:] |
| 130 | + print(delta, end='', flush=True) |
| 131 | + print_idx = len(response) |
| 132 | +print() |
| 133 | +""" |
| 134 | +query: How far is it from each city? |
| 135 | +response: The image shows a road sign indicating the distances to three cities: Mata, Yangjiang, and Guangzhou. The distances are given in kilometers. |
| 136 | +
|
| 137 | +- Mata is 14 kilometers away. |
| 138 | +- Yangjiang is 62 kilometers away. |
| 139 | +- Guangzhou is 293 kilometers away. |
| 140 | +
|
| 141 | +Please note that these distances are as the crow flies and do not take into account the actual driving distance due to road conditions, traffic, or other factors. |
| 142 | +query: Which city is the farthest? |
| 143 | +response: The farthest city listed on the sign is Mata, which is 14 kilometers away. |
| 144 | +""" |
| 145 | +``` |
| 146 | + |
| 147 | +示例图片如下: |
| 148 | + |
| 149 | +road: |
| 150 | + |
| 151 | +<img src="http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/road.png" width="250" style="display: inline-block;"> |
| 152 | + |
| 153 | + |
| 154 | +## 微调 |
| 155 | +多模态大模型微调通常使用**自定义数据集**进行微调. 这里展示可直接运行的demo: |
| 156 | + |
| 157 | +LoRA微调: |
| 158 | + |
| 159 | +(默认只对LLM部分的qkv进行lora微调. 如果你想对所有linear含vision模型部分都进行微调, 可以指定`--lora_target_modules ALL`.) |
| 160 | +```shell |
| 161 | +# Experimental environment: A10, 3090, V100... |
| 162 | +# 21GB GPU memory |
| 163 | +CUDA_VISIBLE_DEVICES=0 swift sft \ |
| 164 | + --model_type llava1d6-mistral-7b-chat \ |
| 165 | + --dataset coco-mini-en-2 \ |
| 166 | +``` |
| 167 | + |
| 168 | +全参数微调: |
| 169 | +```shell |
| 170 | +# Experimental environment: 4 * A100 |
| 171 | +# 4 * 70 GPU memory |
| 172 | +NPROC_PER_NODE=4 CUDA_VISIBLE_DEVICES=0,1,2,3 swift sft \ |
| 173 | + --model_type llava1d6-mistral-7b-chat \ |
| 174 | + --dataset coco-mini-en-2 \ |
| 175 | + --train_dataset_sample -1 \ |
| 176 | + --sft_type full \ |
| 177 | + --deepspeed default-zero2 |
| 178 | +``` |
| 179 | + |
| 180 | + |
| 181 | +[自定义数据集](../LLM/自定义与拓展.md#-推荐命令行参数的形式)支持json, jsonl样式, 以下是自定义数据集的例子: |
| 182 | + |
| 183 | +(只支持单轮对话, 每轮对话必须包含一张图片, 支持传入本地路径或URL) |
| 184 | + |
| 185 | +```jsonl |
| 186 | +{"query": "55555", "response": "66666", "images": ["image_path"]} |
| 187 | +{"query": "eeeee", "response": "fffff", "images": ["image_path"]} |
| 188 | +{"query": "EEEEE", "response": "FFFFF", "images": ["image_path"]} |
| 189 | +``` |
| 190 | + |
| 191 | + |
| 192 | +## 微调后推理 |
| 193 | +直接推理: |
| 194 | +```shell |
| 195 | +CUDA_VISIBLE_DEVICES=0 swift infer \ |
| 196 | + --ckpt_dir output/llava1d6-mistral-7b-chat/vx-xxx/checkpoint-xxx \ |
| 197 | + --load_dataset_config true \ |
| 198 | +``` |
| 199 | + |
| 200 | +**merge-lora**并推理: |
| 201 | +```shell |
| 202 | +CUDA_VISIBLE_DEVICES=0 swift export \ |
| 203 | + --ckpt_dir output/llava1d6-mistral-7b-chat/vx-xxx/checkpoint-xxx \ |
| 204 | + --merge_lora true |
| 205 | + |
| 206 | +CUDA_VISIBLE_DEVICES=0 swift infer \ |
| 207 | + --ckpt_dir output/llava1d6-mistral-7b-chat/vx-xxx/checkpoint-xxx-merged \ |
| 208 | + --load_dataset_config true |
| 209 | +``` |
0 commit comments