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

Word-level timestamps broken for short-form audio #30325

Conversation

kamilakesbi
Copy link
Contributor

What does this PR do?

This PR aims at fixing issue #30224: word-level timestamps is currently broken in Whisper large/large-v2/large-v3/distil-large-v3. The problem comes from the fact that num_frames isn't passed to the generate method when stride is None.

I suggest simply adding a new extra argument to the output of the preprocess method, called segment_size, which will store the input length in the case of short form audios. It will be passed to the _forward method and used when stride is None to compute generate_kwargs["num_frames"] = segment_size // self.feature_extractor.hop_length.

This seems to fix the problem. Is there any particular test that should be run regarding this issue?

Who can review?

@sanchit-gandhi

@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.

@kamilakesbi
Copy link
Contributor Author

Hi @sanchit-gandhi,

I've added slow tests that pass. With this PR we should be able to compute word-level timestamps for short form audio using the automatic-speech-recognition pipeline.

I've noticed that word-level timestamps cannot be computed using WhisperForConditionalGeneration instead of the pipeline. I'll open an issue so we can fix this in another PR.

Copy link
Contributor

@sanchit-gandhi sanchit-gandhi left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for fixing @kamilakesbi! Let's indeed do the model + processor API in a follow-up PR

cc @ylacombe and @xenova for info

@kamilakesbi
Copy link
Contributor Author

Hey @amyeroberts! would appreciate a final review here when you have time.
It should be pretty quick to review ;)

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for working on fixing this!

Just a few small questions/comments.

if isinstance(segment_size, int):
generate_kwargs["num_frames"] = segment_size // self.feature_extractor.hop_length
else:
generate_kwargs["num_frames"] = segment_size[0] // self.feature_extractor.hop_length
Copy link
Collaborator

Choose a reason for hiding this comment

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

Under what cases would segment_size be a iterable? And will all the values of segment_size in the iterable be the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

segment_size would be an iterable when using the batch_size argument in the pipeline (with bs >1), as in here: in this case, we get an iterable of size 1. If I increase the batch size, I still get an iterable with size 1.

@@ -459,6 +460,7 @@ def preprocess(self, inputs, chunk_length_s=0, stride_length_s=None):
def _forward(self, model_inputs, return_timestamps=False, **generate_kwargs):
attention_mask = model_inputs.pop("attention_mask", None)
stride = model_inputs.pop("stride", None)
segment_size = model_inputs.pop("segment_size", None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there any cases when stride is None and we want to use segment_size?

Because we're popping both from model_inputs here, technically, the user can pass this in directly as an argument when calling pipeline.forward. In which case, it would be good to have input validation on these argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will get stride is None when using this pipeline:

pipe = pipeline(
            task="automatic-speech-recognition",
            model="openai/whisper-large-v3",
            return_timestamps="word",
        )

--> segment_size is the alternative to compute and add num_frames to the generate_kwargs to be passed to the generate method here.

Copy link
Collaborator

@amyeroberts amyeroberts Apr 30, 2024

Choose a reason for hiding this comment

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

Sorry, I mistyped. I meant to say when stride is not None

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want to use segment_size only when stride is None.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK. Let's add a quick input validation so the argument isn't just silently ignored then if stride is not None

Copy link
Contributor Author

@kamilakesbi kamilakesbi May 2, 2024

Choose a reason for hiding this comment

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

I've added the following lines:

if stride is not None and segment_size is not None: 
            raise ValueError("segment_size must be used only when stride is None")

],
}

# batch size 1: copy the audio sample since pipeline consumes it
Copy link
Collaborator

Choose a reason for hiding this comment

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

👀 - as in, mutates it? It shouldn't do that....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure to know why it does that, I've simply copied the logic from another test (this one) here!

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, let's open an issue to address this, so the work isn't forgotten but can be done in an separate PR. We should definitely avoid side-effect like this 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll open an issue!

kamilakesbi and others added 2 commits April 30, 2024 16:41
Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
@kamilakesbi kamilakesbi changed the title [WIP] - Word-level timestamps broken for short-form audio Word-level timestamps broken for short-form audio Apr 30, 2024
Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for fixing and iterating on this!

@kamilakesbi Do you have permissions to merge in the PR? If not, let me know and I can merge it it for you

@kamilakesbi
Copy link
Contributor Author

Hi @amyeroberts, I don't! Could you merge it ?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants