Skip to content

Conversation

@alien-0119
Copy link
Collaborator

What does this PR do?

Fixes # (issue)

Adds # (feature)

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?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • 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.

@xxx

@alien-0119 alien-0119 requested a review from vigo999 as a code owner October 28, 2025 12:14
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @alien-0119, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly expands the mindone/transformers library by introducing the Conditional Speech Model (CSM), a sophisticated architecture designed for conditional audio generation. The changes encompass the full implementation of the CSM model, its unique generation strategy that combines a backbone model with a depth decoder, and necessary adaptations to the existing Mimi codec model for seamless audio processing. Furthermore, core MindSpore-specific utilities have been refined, and a new suite of tests has been added to validate the new model's functionality.

Highlights

  • New CSM Model Integration: Introduced the Conditional Speech Model (CSM) architecture, including its backbone, depth decoder, and conditional generation capabilities, along with its configuration and auto-modeling integration.
  • Custom Generation Logic: Implemented specialized generation methods within CsmGenerationMixin to handle the unique multi-stage sampling process of CSM, involving both the backbone and depth decoder for audio generation.
  • Mimi Codec Model Enhancements: Updated the Mimi codec model with new utility functions (_get_output_length, get_audio_codes_mask) to facilitate its use as an audio encoder/decoder within the CSM framework, and refactored scaled_dot_product_attention.
  • MindSpore Adaptations: Refactored internal utility functions for model introspection (mindone/transformers/utils/generic.py) to better align with MindSpore's nn.Cell and construct method conventions.
  • Comprehensive Testing: Added new unit tests for the CsmBackboneModel and CsmForConditionalGeneration to ensure correctness and compatibility of the newly integrated models.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the CSM (Compound Speech Model) to the library, including its modeling, generation logic, and configuration. The implementation is a significant addition, enabling text-to-waveform generation.

My review focuses on performance optimizations in the new model code. I've identified a few areas where loops can be vectorized for significant performance gains, particularly in audio encoding/decoding paths and logit computation. These are marked with TODOs in the code, and my comments provide suggestions on how to address them.

Overall, the PR is well-structured, and the addition of the CSM model is a valuable contribution. Addressing the performance points will make it even better.

Comment on lines +470 to +479
for audio_codes_batch in generated_audio_codes:
eos_idxs = (audio_codes_batch == self.config.codebook_eos_token_id).all(dim=-1).nonzero()
if eos_idxs.numel() != 0:
cutoff_idx = 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])
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The audio decoding loop iterates over each item in the batch, which is inefficient. This should be vectorized to process the entire batch at once for better performance. The TODO comment indicates awareness of this, but it's a significant performance bottleneck that should be addressed.

For example, you could pad the audio_codes_batch tensors to the same length, stack them into a single batch tensor, and then call self.codec_model.decode once.

Comment on lines +811 to +819
for batch_input_values, batch_input_values_cutoffs in zip(input_values, input_values_cutoffs):
batch_input_values_cutoffs = batch_input_values_cutoffs[batch_input_values_cutoffs >= 0]
for i in range(batch_input_values_cutoffs.shape[0] - 1):
start_idx = batch_input_values_cutoffs[i]
end_idx = batch_input_values_cutoffs[i + 1]
audio_batch = batch_input_values[..., start_idx:end_idx]
codec_outputs = self.codec_model.encode(audio_batch.unsqueeze(0))
codebook_ids = codec_outputs.audio_codes.transpose(1, -1)
audio_tokens_list.append(codebook_ids[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The audio token encoding loop iterates over each item in the batch, which is inefficient. This should be vectorized to process the entire batch at once for better performance. The TODO comment indicates awareness of this, but it's a significant performance bottleneck that should be addressed.

This would likely involve padding the audio segments to a uniform length before passing them to self.codec_model.encode in a single batch.

Comment on lines +494 to +498
hidden_states = [
mint.nn.functional.linear(hidden_states[:, codebook_idx, :], codebook_weight[codebook_idx].T)
for codebook_idx in range(codebook_weight.shape[0])
]
hidden_states = mint.stack(hidden_states, dim=1)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The list comprehension in CsmCodebooksHead.construct iterates over the sequence length to compute logits. This can be optimized by using a vectorized operation like mindspore.ops.bmm (batched matrix multiplication) for better performance, especially with longer sequences.

        hidden_states = mint.bmm(hidden_states.transpose(0, 1), codebook_weight).transpose(0, 1)

@alien-0119 alien-0119 closed this Oct 28, 2025
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.

1 participant