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

Support longer audio contexts #110

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Changes from 2 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
30 changes: 23 additions & 7 deletions ultravox/model/ultravox_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def __init__(self, config: UltravoxConfig):
self.vocab_size = config.vocab_size

self.audio_tower = self._create_audio_tower(config)
self.audio_tower_context_length = (
3000 # the context window for the whisper model
)
liPatrick marked this conversation as resolved.
Show resolved Hide resolved

self.multi_modal_projector = UltravoxProjector(config)
self.language_model = self._create_language_model(config)

Expand Down Expand Up @@ -186,17 +190,29 @@ def forward(
len(audio_token_start_idx) == len(audio_token_len) == len(audio_values)
), "audio_token_start_idx, audio_token_len, and audio_values must have the same batch size."

# B x A/3200 x D
audio_tower_output = self.audio_tower.forward(
audio_values
).last_hidden_state
audio_tower_output = audio_tower_output.to(inputs_embeds.dtype)
# Check if the audio_values T dimension is greater than whisper encoder's context window.
if audio_values.shape[2] > self.audio_tower_context_length:
audio_values_chunks = torch.split(
audio_values, self.audio_tower_context_length, dim=2
)
liPatrick marked this conversation as resolved.
Show resolved Hide resolved
else:
audio_values_chunks = (audio_values,)

rebuilt_audio_embeds = []
for audio_chunk in audio_values_chunks:
liPatrick marked this conversation as resolved.
Show resolved Hide resolved
# B x A/3200 x D
audio_tower_output = self.audio_tower.forward(
audio_chunk
).last_hidden_state
audio_tower_output = audio_tower_output.to(inputs_embeds.dtype)
audio_embeds = self.multi_modal_projector.forward(audio_tower_output)
rebuilt_audio_embeds.append(audio_embeds)

audio_embeds = self.multi_modal_projector.forward(audio_tower_output)
rebuilt_audio_embeds_tensor = torch.cat(rebuilt_audio_embeds, dim=1)

# combine audio and text embeddings
for i, (audio, start, length) in enumerate(
zip(audio_embeds, audio_token_start_idx, audio_token_len)
zip(rebuilt_audio_embeds_tensor, audio_token_start_idx, audio_token_len)
):
length = min(length, audio.shape[0])
inputs_embeds[i, start : start + length] = audio[:length]
Expand Down
Loading