Skip to content

[Qwen2Audio] handle input ids expansion during processing#35534

Merged
eustlb merged 12 commits intohuggingface:mainfrom
eustlb:refactor-qwenaudio
Jan 7, 2025
Merged

[Qwen2Audio] handle input ids expansion during processing#35534
eustlb merged 12 commits intohuggingface:mainfrom
eustlb:refactor-qwenaudio

Conversation

@eustlb
Copy link
Contributor

@eustlb eustlb commented Jan 6, 2025

What does this PR do?

This PR adds input_id expansion to the processor.

Before:

  1. embedding of the input_ids"<|audio_bos|><|AUDIO|><|audio_eos|>Generate the caption in English:"inputs_embeds tensor
  2. creation of a new embedding tensor here, taking into account the number of audio tokens to be merged with inputs_embeds
  3. replacement of audio embedding vectors

Now:

  1. expansion of input ids at the processing stage: "<|audio_bos|><|AUDIO|><|audio_eos|>Generate the caption in English:""<|audio_bos|><|AUDIO|>...<|AUDIO|><|audio_eos|>Generate the caption in English:" with as many <|AUDIO|> tokens as the number of audio embedded vectors after encoding and projection
  2. creation of inputs_embeds, directly with the correct shape
  3. replacement of the audio embedding vectors

This approach allows to remove the unnecessary step of creating a new embedding tensor in the model forward. Moreover, it simplifies the code as correct padding is directly handled at the processing stage.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🤗

Comment on lines +298 to +339
# test the error when incorrect number of audio tokens
inputs["input_ids"] = torch.tensor(
[
[
151644,
8948,
198,
2610,
525,
264,
10950,
17847,
13,
151645,
198,
151644,
872,
198,
14755,
220,
16,
25,
220,
151647,
]
+ [151646] * 200
+ [
151648,
198,
3838,
594,
429,
5112,
30,
151645,
198,
151644,
77091,
198,
]
]
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use # fmt: skip

@eustlb eustlb merged commit 7f76773 into huggingface:main Jan 7, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants