zhangyue66 commited on
Commit
90dbf48
·
1 Parent(s): a431606

update chat_template

Browse files
Files changed (2) hide show
  1. README.md +19 -12
  2. chat_template.jinja +27 -3
README.md CHANGED
@@ -171,18 +171,25 @@ model = AutoModelForCausalLM.from_pretrained(
171
  ).to(DEVICE).eval()
172
  processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
173
 
174
- messages = [{"role": "user", "content": PROMPTS[CHOSEN_TASK]}]
175
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
176
-
177
- inputs = processor(text=[text], images=[image], return_tensors="pt")
178
- inputs = {k: (v.to(DEVICE) if isinstance(v, torch.Tensor) else v) for k, v in inputs.items()}
179
-
180
- with torch.inference_mode():
181
- generated = model.generate(**inputs, max_new_tokens=1024, do_sample=False, use_cache=True)
182
-
183
- resp = processor.batch_decode(generated, skip_special_tokens=True)[0]
184
- answer = resp.split(text)[-1].strip()
185
- print(answer)
 
 
 
 
 
 
 
186
  ```
187
 
188
  ## Performance
 
171
  ).to(DEVICE).eval()
172
  processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
173
 
174
+ messages = [
175
+ {"role": "user",
176
+ "content": [
177
+ {"type": "image", "image": image},
178
+ {"type": "text", "text": PROMPTS[CHOSEN_TASK]},
179
+ ]
180
+ }
181
+ ]
182
+ inputs = processor.apply_chat_template(
183
+ messages,
184
+ tokenize=True,
185
+ add_generation_prompt=True,
186
+ return_dict=True,
187
+ return_tensors="pt"
188
+ ).to(DEVICE)
189
+
190
+ outputs = model.generate(**inputs, max_new_tokens=1024)
191
+ outputs = processor.batch_decode(outputs, skip_special_tokens=True)[0]
192
+ print(outputs)
193
  ```
194
 
195
  ## Performance
chat_template.jinja CHANGED
@@ -7,14 +7,38 @@
7
  {%- if not sep_token is defined -%}
8
  {%- set sep_token = "<|end_of_sentence|>" -%}
9
  {%- endif -%}
 
 
 
10
  {{- cls_token -}}
11
  {%- for message in messages -%}
12
  {%- if message["role"] == "user" -%}
13
- {{- "User: <|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>" + message["content"] + "\n" -}}
 
 
 
 
 
 
 
 
 
 
 
14
  {%- elif message["role"] == "assistant" -%}
15
- {{- "Assistant: " + message["content"] + sep_token -}}
 
 
 
 
 
 
16
  {%- elif message["role"] == "system" -%}
17
- {{- message["content"] -}}
 
 
 
 
18
  {%- endif -%}
19
  {%- endfor -%}
20
  {%- if add_generation_prompt -%}
 
7
  {%- if not sep_token is defined -%}
8
  {%- set sep_token = "<|end_of_sentence|>" -%}
9
  {%- endif -%}
10
+ {%- if not image_token is defined -%}
11
+ {%- set image_token = "<|IMAGE_START|><|IMAGE_PLACEHOLDER|><|IMAGE_END|>" -%}
12
+ {%- endif -%}
13
  {{- cls_token -}}
14
  {%- for message in messages -%}
15
  {%- if message["role"] == "user" -%}
16
+ {{- "User: " -}}
17
+ {%- for content in message["content"] -%}
18
+ {%- if content["type"] == "image" -%}
19
+ {{ image_token }}
20
+ {%- endif -%}
21
+ {%- endfor -%}
22
+ {%- for content in message["content"] -%}
23
+ {%- if content["type"] == "text" -%}
24
+ {{ content["text"] }}
25
+ {%- endif -%}
26
+ {%- endfor -%}
27
+ {{ "\n" -}}
28
  {%- elif message["role"] == "assistant" -%}
29
+ {{- "Assistant: " -}}
30
+ {%- for content in message["content"] -%}
31
+ {%- if content["type"] == "text" -%}
32
+ {{ content["text"] + "\n" }}
33
+ {%- endif -%}
34
+ {%- endfor -%}
35
+ {{ sep_token -}}
36
  {%- elif message["role"] == "system" -%}
37
+ {%- for content in message["content"] -%}
38
+ {%- if content["type"] == "text" -%}
39
+ {{ content["text"] + "\n" }}
40
+ {%- endif -%}
41
+ {%- endfor -%}
42
  {%- endif -%}
43
  {%- endfor -%}
44
  {%- if add_generation_prompt -%}