Fix the issue that csm model cannot work with pipeline mode. - #39349
Conversation
Signed-off-by: yuanwu <yuan.wu@intel.com>
|
Hi @yuanwu2017 , thank you for this PR. Let's add some description in |
|
eustlb
left a comment
There was a problem hiding this comment.
Thanks a lot 🤗
Do we need the codec batching here? It should be in another PR
| # 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.
We don't need this, that is exactly what is done below no?
There was a problem hiding this comment.
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.
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.
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
There was a problem hiding this comment.
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. |
eustlb
left a comment
There was a problem hiding this comment.
One last iteration and we'll be good to merge 🤗
| """ | ||
|
|
||
| audio: Optional[list[torch.Tensor]] = None | ||
| waveform: Optional[list[torch.Tensor]] = None |
There was a problem hiding this comment.
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. |
Signed-off-by: yuanwu <yuanwu@habana.ai>
|
@ydshieh The failed case has nothing to do with this patch. |
|
@eustlb , could you help do a final review of this PR? Thx very much. |
|
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. |
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto |
…face#39349) * Fix the issue that csm model cannot work with pipeline mode. Signed-off-by: yuanwu <yuan.wu@intel.com> * Remove batching inference Signed-off-by: yuanwu <yuan.wu@intel.com> * csm output is list of tensor Signed-off-by: yuanwu <yuan.wu@intel.com> * Update src/transformers/pipelines/text_to_audio.py Co-authored-by: eustlb <94853470+eustlb@users.noreply.github.com> * Use different waveform key for different model Signed-off-by: yuanwu <yuan.wu@intel.com> * Fix make style errors Signed-off-by: yuanwu <yuan.wu@intel.com> * Add csm tests Signed-off-by: yuanwu <yuanwu@habana.ai> * Update src/transformers/models/auto/tokenization_auto.py --------- Signed-off-by: yuanwu <yuan.wu@intel.com> Signed-off-by: yuanwu <yuanwu@habana.ai> Co-authored-by: eustlb <94853470+eustlb@users.noreply.github.com>
…face#39349) * Fix the issue that csm model cannot work with pipeline mode. Signed-off-by: yuanwu <yuan.wu@intel.com> * Remove batching inference Signed-off-by: yuanwu <yuan.wu@intel.com> * csm output is list of tensor Signed-off-by: yuanwu <yuan.wu@intel.com> * Update src/transformers/pipelines/text_to_audio.py Co-authored-by: eustlb <94853470+eustlb@users.noreply.github.com> * Use different waveform key for different model Signed-off-by: yuanwu <yuan.wu@intel.com> * Fix make style errors Signed-off-by: yuanwu <yuan.wu@intel.com> * Add csm tests Signed-off-by: yuanwu <yuanwu@habana.ai> * Update src/transformers/models/auto/tokenization_auto.py --------- Signed-off-by: yuanwu <yuan.wu@intel.com> Signed-off-by: yuanwu <yuanwu@habana.ai> Co-authored-by: eustlb <94853470+eustlb@users.noreply.github.com>




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.