GeorgeBredis commited on
Commit
560a301
1 Parent(s): 7e7f221

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +63 -3
README.md CHANGED
@@ -34,11 +34,11 @@ This is the model card of a 🤗 transformers model that has been pushed on the
34
  - **Model type:** ruIdefics2
35
  - **Language(s) (NLP):** Russian
36
  - **License:** Apache-2.0
37
- - **Finetuned from model [optional]:** Idefics2
38
 
39
  # How to Get Started
40
 
41
- This section shows snippets of code for generation for `idefics2-8b-base` and `idefics2-8b`. The codes only differ by the input formatting. Let's first define some common imports and inputs.
42
 
43
  ```python
44
  import requests
@@ -60,7 +60,6 @@ model = AutoModelForVision2Seq.from_pretrained(
60
  "GeorgeBredis/ruIdefics2-ruLLaVA-merged",
61
  ).to(DEVICE)
62
 
63
- # Create inputs
64
  messages = [
65
  {
66
  "role": "user",
@@ -81,3 +80,64 @@ generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True
81
  print(generated_texts)
82
  ```
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  - **Model type:** ruIdefics2
35
  - **Language(s) (NLP):** Russian
36
  - **License:** Apache-2.0
37
+ - **Finetuned from model:** Idefics2
38
 
39
  # How to Get Started
40
 
41
+ ## Запуск в fp16
42
 
43
  ```python
44
  import requests
 
60
  "GeorgeBredis/ruIdefics2-ruLLaVA-merged",
61
  ).to(DEVICE)
62
 
 
63
  messages = [
64
  {
65
  "role": "user",
 
80
  print(generated_texts)
81
  ```
82
 
83
+ Вполне возможно что это не влезет в вашу GPU (если будете загружать на gpu), так что ниже вариант с bnb для запуска в colab'e.
84
+
85
+ ## Запуск в int4/int8 c bnb.
86
+
87
+ Требует установки peft
88
+
89
+ ```python
90
+ import requests
91
+ import torch
92
+ from PIL import Image
93
+ from io import BytesIO
94
+
95
+ from peft import LoraConfig
96
+ from transformers import AutoProcessor, BitsAndBytesConfig, Idefics2ForConditionalGeneration
97
+ from transformers.image_utils import load_image
98
+
99
+ DEVICE = "cuda:0"
100
+
101
+ image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
102
+ image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
103
+ image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")
104
+
105
+ processor = AutoProcessor.from_pretrained(
106
+ "GeorgeBredis/ruIdefics2-ruLLaVA-merged",
107
+ do_image_splitting=False
108
+ )
109
+
110
+ quantization_config = BitsAndBytesConfig(
111
+ load_in_4bit=True,
112
+ bnb_4bit_quant_type="nf4",
113
+ bnb_4bit_use_double_quant=True,
114
+ bnb_4bit_compute_dtype=torch.float16
115
+ )
116
+ model = Idefics2ForConditionalGeneration.from_pretrained(
117
+ "GeorgeBredis/ruIdefics2-ruLLaVA-merged",
118
+ torch_dtype=torch.float16,
119
+ quantization_config=quantization_config,
120
+ )
121
+ # не нужно переносить на карту, так как в int4/8 заводятся сразу на них
122
+
123
+ messages = [
124
+ {
125
+ "role": "user",
126
+ "content": [
127
+ {"type": "image"},
128
+ {"type": "text", "text": "Что изображено на данной картинке?"},
129
+ ]
130
+ }
131
+ ]
132
+ prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
133
+ inputs = processor(text=prompt, images=[image1, image2], return_tensors="pt")
134
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
135
+
136
+
137
+ generated_ids = model.generate(**inputs, max_new_tokens=500)
138
+ generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
139
+
140
+ print(generated_texts)
141
+
142
+ ```
143
+