Skip to content

Granite speech - minor fixes to support training with the HF trainer #38833

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

avihu111
Copy link
Contributor

@avihu111 avihu111 commented Jun 15, 2025

What does this PR do?

Minor updates to granite_speech to enable finetuning it with HF trainers.

  • avoids a crash when trainers pass padding=True to the processor
  • ensure all trainable parameters have gradients (bugfix) - remove .data from a forward call
  • rename melspec to mel_filters to leverage this, which avoids a crash on save_pretrained

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

CC: @ArthurZucker @eustlb @alex-jw-brooks @avishaiElmakies @gsaon

avihu111 added 2 commits June 15, 2025 16:50
avoid unused parameters that DDP does not like
trainers often pass this argument automatically
@avihu111 avihu111 marked this pull request as draft June 15, 2025 14:19
@@ -50,15 +50,16 @@ def __init__(
**kwargs,
):
super().__init__(**kwargs)
self.sampling_rate = sampling_rate
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this isn't used currently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, it's not used. I added it to stay consistent with other audio feature extractors that have this property.

@avihu111 avihu111 marked this pull request as ready for review June 16, 2025 14:22
Copy link
Contributor Author

@avihu111 avihu111 left a comment

Choose a reason for hiding this comment

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

Added some comments on each change, giving relevant context

@@ -88,7 +88,9 @@ def __call__(
else:
audio_inputs = {}

text_inputs = self.tokenizer(prompt_strings, padding=True, **kwargs)
if "padding" not in kwargs:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

avoids a crash when trainers pass padding=True to the processor

@@ -92,7 +92,7 @@ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
hidden_states = hidden_states.view(batch_size * nblocks, self.window_size, dim)

query_output = self.qformer(
query_embeds=self.query.data,
query_embeds=self.query,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bugfix. When using .data this trainable parameter did not receive gradients.

# Currently lazily initialized
self.melspec = None
requires_backends(self, ["torchaudio"])
self.mel_filters = torchaudio.transforms.MelSpectrogram(**self.melspec_kwargs)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed the lazy init, and renamed it to mel_filters. This specific name avoids a crash when serializing the processor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

2 participants