-
Notifications
You must be signed in to change notification settings - Fork 30k
Fix the issue that csm model cannot work with pipeline mode. #39349
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: yuanwu <yuan.wu@intel.com>
Hi @yuanwu2017 , thank you for this PR. Let's add some description in |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot 🤗
Do we need the codec batching here? It should be in another PR
src/transformers/audio_utils.py
Outdated
# If it's a torch tensor with more than one dimension, convert it to a list of tensors | ||
if is_torch_tensor(audio) and len(audio.shape) > 1: | ||
return list(audio) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this, that is exactly what is done below no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As described below, because pipeline requires that the return value of generate is a tensor, it needs to be converted to list here so that the subsequent processor.save_audio can work normally.
# ======================================= | ||
# TODO: @eustlb, this should be batched !!! | ||
# but requires making sure batched inference of the codec model works as intended | ||
for audio_codes_batch in generated_audio_codes: | ||
eos_idxs = (audio_codes_batch == self.config.codebook_eos_token_id).all(dim=-1).nonzero() | ||
generated_audio_codes = generate_output.sequences if generate_returned_dict else generate_output | ||
|
||
# Find EOS positions for all batches at once | ||
eos_mask = (generated_audio_codes == self.config.codebook_eos_token_id).all(dim=-1) | ||
eos_positions = torch.zeros( | ||
generated_audio_codes.shape[0], dtype=torch.long, device=generated_audio_codes.device | ||
) | ||
|
||
for i in range(generated_audio_codes.shape[0]): | ||
eos_idxs = eos_mask[i].nonzero() | ||
if eos_idxs.numel() != 0: | ||
cutoff_idx = eos_idxs.min() | ||
eos_positions[i] = eos_idxs.min() | ||
else: | ||
cutoff_idx = audio_codes_batch.shape[0] | ||
|
||
audio_codes_batch = audio_codes_batch[:cutoff_idx] | ||
codec_decode_output = self.codec_model.decode(audio_codes_batch.transpose(0, 1).unsqueeze(0)) | ||
audio.append(codec_decode_output.audio_values[0, 0]) | ||
# ======================================= | ||
eos_positions[i] = generated_audio_codes.shape[1] | ||
|
||
# Create a mask for valid positions | ||
max_len = eos_positions.max().item() | ||
valid_mask = torch.arange(max_len, device=generated_audio_codes.device).unsqueeze( | ||
0 | ||
) < eos_positions.unsqueeze(1) | ||
|
||
# Truncate and pad audio codes | ||
truncated_codes = generated_audio_codes[:, :max_len] | ||
masked_codes = truncated_codes * valid_mask.unsqueeze(-1) | ||
|
||
# Decode all batches at once | ||
transposed_codes = masked_codes.transpose(1, 2) | ||
codec_decode_output = self.codec_model.decode(transposed_codes) | ||
audio = codec_decode_output.audio_values.squeeze(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary to this PR (I mean enabling pipeline usage?)
The codec is not inferred batched for the moment because there is no equivalence yet between batch/sequential for Mimi. I would expect this to break slow tests. This should be in another PR/ issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of it may be needed because pipeline requires that the return value of generate is a tensor, otherwise the postprocessing will cause an error.
transformers/src/transformers/pipelines/base.py
Line 1463 in d9b35c6
def run_single(self, inputs, preprocess_params, forward_params, postprocess_params): |
output_dict["audio"] = waveform.to(device="cpu", dtype=torch.float).numpy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I can divide this patch into two.
Signed-off-by: yuanwu <yuan.wu@intel.com>
Signed-off-by: yuanwu <yuan.wu@intel.com>
@eustlb I have removed the codec batching. Please help to review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last iteration and we'll be good to merge 🤗
@@ -68,7 +68,7 @@ class CsmGenerateOutput(GenerateDecoderOnlyOutput): | |||
The generated audio. | |||
""" | |||
|
|||
audio: Optional[list[torch.Tensor]] = None | |||
waveform: Optional[list[torch.Tensor]] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't merge this as it is breaking, can you rather update postprocess
in text_to_audio.py
pipeline? with something like
if self.model.config.model_type == "csm":
waveform_key = "audio"
else:
waveform_key = "waveform"
Co-authored-by: eustlb <94853470+eustlb@users.noreply.github.com>
Done. |
Signed-off-by: yuanwu <yuan.wu@intel.com>
@yuanwu2017 , pls run |
Done. |
Signed-off-by: yuanwu <yuan.wu@intel.com>
Hi @yuanwu2017 I will let @eustlb to finalize the review. From the screenshot you shared, I saw, in |
Done. |
[For maintainers] Suggested jobs to run (before merge) run-slow: auto |
Signed-off-by: yuanwu <yuanwu@habana.ai>
What does this PR do?
Fix the csm model(text_to_audio) cannot work with pipeline mode.
example:
Before patch:


After patch:
Batch inference with original example:
Result:

Fixes # (issue)
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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.