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

[Fix] Qwen prompt for alignment #360

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
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
29 changes: 18 additions & 11 deletions lmms_eval/models/qwen2_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
use_cache=True,
add_generation_prompt: bool = True,
add_system_prompt: bool = True,
simple_prompt: bool = False,
**kwargs,
) -> None:
super().__init__()
Expand All @@ -41,6 +42,9 @@ def __init__(
accelerator = Accelerator()
self.add_generation_prompt = add_generation_prompt
self.add_system_prompt = add_system_prompt
# If using simple prompt, only add "<|audio_bos|><|AUDIO|><|audio_eos|>"
# and then prompt to align with original Qwen2 Audio
self.simple_prompt = simple_prompt
if accelerator.num_processes > 1:
self._device = torch.device(f"cuda:{accelerator.local_process_index}")
self.device_map = f"cuda:{accelerator.local_process_index}"
Expand Down Expand Up @@ -206,17 +210,20 @@ def _collate(x):
if isinstance(contexts, tuple):
contexts = list(contexts)

conversations = []
for idx, context in enumerate(contexts):
conv = [{"role": "user", "content": []}]
for _ in batched_audios[idx]:
# This placeholder is just use to make chat template work
# We already have the sampled audio array
conv[0]["content"].append({"type": "audio", "audio_url": "placeholder.wav"})
conv[0]["content"].append({"type": "text", "text": context})
conversations.append(conv)

text = [self.processor.apply_chat_template(conversation, add_generation_prompt=self.add_generation_prompt, tokenize=False) for conversation in conversations]
if not self.simple_prompt:
conversations = []
for idx, context in enumerate(contexts):
conv = [{"role": "user", "content": []}]
for _ in batched_audios[idx]:
# This placeholder is just use to make chat template work
# We already have the sampled audio array
conv[0]["content"].append({"type": "audio", "audio_url": "placeholder.wav"})
conv[0]["content"].append({"type": "text", "text": context})
conversations.append(conv)

text = [self.processor.apply_chat_template(conversation, add_generation_prompt=self.add_generation_prompt, tokenize=False) for conversation in conversations]
else:
text = ["<|audio_bos|><|AUDIO|><|audio_eos|>" + context for context in contexts]
audios = [downsample_audio(audio["array"], audio["sampling_rate"], self.processor.feature_extractor.sampling_rate) for audio in flattened_audios]

inputs = self.processor(text=text, audios=audios, return_tensors="pt", padding=True, sampling_rate=self.processor.feature_extractor.sampling_rate)
Expand Down
Loading