Skip to content

Conversation

@dengcunqin
Copy link

This PR provides a code reproduction of Paraformer-v2: An Improved Non-Autoregressive Transformer for Noise-Robust Speech Recognition, as described in the paper. The implementation may not be fully consistent with the official code but aims to replicate the core concepts and methods presented in the paper.

This PR provides a code reproduction of Paraformer-v2: An Improved Non-Autoregressive Transformer for Noise-Robust Speech Recognition, as described in the paper. The implementation may not be fully consistent with the official code but aims to replicate the core concepts and methods presented in the paper.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @dengcunqin, 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 introduces a complete offline implementation of the Paraformer-v2 model, a non-autoregressive transformer architecture tailored for noise-robust speech recognition. The core changes involve a specialized SANM-based decoder and a comprehensive model class that integrates CTC for alignment and prediction, enabling efficient and accurate speech-to-text conversion.

Highlights

  • New Decoder Architecture: Introduced DecoderLayerSANM and ParaformerSANMDecoder classes, specifically designed for the Paraformer-v2 model, extending BaseTransformerDecoder to handle self-attention and cross-attention mechanisms.
  • Paraformer-v2 Model Implementation: Added the main Paraformer class, which integrates the encoder, the new SANM decoder, and CTC components for end-to-end speech recognition, enabling a complete offline reproduction of the model.
  • CTC Alignment and Averaging Utilities: Implemented map_alignment_to_target_index, force_align, average_repeats_training, and average_repeats_inference methods to robustly handle CTC-based token alignment and aggregation for both training and inference processes.
  • Model Configuration File: Included a template.yaml file to define the model's configuration, specifying parameters for the encoder, decoder, frontend, specaug, and various training settings.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 an offline implementation of Paraformer-v2. The code is generally well-structured, but I've identified a few issues that need attention. There's a critical bug in decoder.py that would lead to a runtime error, and a logic bug in handling decoder blocks which could affect model performance. Additionally, model.py has some inconsistencies in return types and unused variables. I've also pointed out a consistent typo (sanm_shfit instead of sanm_shift) in both the Python code and the YAML configuration, which should be corrected for better maintainability. My detailed comments and suggestions are provided below.

memory_mask = myutils.sequence_mask(hlens, device=memory.device)[:, None, :]

tgt, tgt_mask, memory, memory_mask, _ = self.decoders[0](tgt, tgt_mask, memory, memory_mask)
attn_mat = self.model.decoders[1].get_attn_mat(tgt, tgt_mask, memory, memory_mask)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The code uses self.model.decoders, but self.model is not an attribute of the ParaformerSANMDecoder class. This will raise an AttributeError at runtime. It should probably be self.decoders.

Suggested change
attn_mat = self.model.decoders[1].get_attn_mat(tgt, tgt_mask, memory, memory_mask)
attn_mat = self.decoders[1].get_attn_mat(tgt, tgt_mask, memory, memory_mask)

look_back=cache["decoder_chunk_look_back"],
)

if self.num_blocks - self.att_layer_num > 1:
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 condition if self.num_blocks - self.att_layer_num > 1: seems incorrect. If self.num_blocks - self.att_layer_num is exactly 1, self.decoders2 would be initialized with one layer, but this block would be skipped. This is likely a bug. You should probably check if the value is greater than 0, or more robustly, check if self.decoders2 is not None.

Suggested change
if self.num_blocks - self.att_layer_num > 1:
if self.decoders2 is not None:

)
new_cache.append(c_ret)

if self.num_blocks - self.att_layer_num > 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

high

Similar to forward_chunk, the condition if self.num_blocks - self.att_layer_num > 1: seems incorrect and would cause the decoders2 block to be skipped if it contains exactly one layer. This should likely be if self.decoders2 is not None: to ensure the block is executed whenever it's initialized.

Suggested change
if self.num_blocks - self.att_layer_num > 1:
if self.decoders2 is not None:

timestamps: List[Tuple[int, int]] -> [(start_frame, end_frame), ...]
"""
if greedy_path.numel() == 0:
return torch.zeros((0, ctc_probs.size(1)), device=ctc_probs.device), []
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This function has inconsistent return types. Here, it returns a tuple (torch.Tensor, []), but in other branches (lines 438, 440) it returns a single tensor. The caller at line 508 expects a single tensor. This will cause a TypeError if this branch is taken. The empty list seems to be related to timestamps which is mentioned in the docstring but is otherwise unused. You should probably remove the empty list from the return value to make it consistent and update the docstring to reflect the actual return value.

Suggested change
return torch.zeros((0, ctc_probs.size(1)), device=ctc_probs.device), []
return torch.zeros((0, ctc_probs.size(1)), device=ctc_probs.device)

concat_after: bool = False,
att_layer_num: int = 6,
kernel_size: int = 21,
sanm_shfit: int = 0,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo in the parameter name sanm_shfit. It should be sanm_shift for better readability and consistency. This typo is also present in its usage within the __init__ method and in the template.yaml file.

Suggested change
sanm_shfit: int = 0,
sanm_shift: int = 0,

Comment on lines +301 to +302
if sanm_shfit is None:
sanm_shfit = (kernel_size - 1) // 2
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo here, sanm_shfit should be sanm_shift.

Suggested change
if sanm_shfit is None:
sanm_shfit = (kernel_size - 1) // 2
if sanm_shift is None:
sanm_shift = (kernel_size - 1) // 2

lambda lnum: DecoderLayerSANM(
attention_dim,
MultiHeadedAttentionSANMDecoder(
attention_dim, self_attention_dropout_rate, kernel_size, sanm_shfit=sanm_shfit
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo here, sanm_shfit should be sanm_shift.

Suggested change
attention_dim, self_attention_dropout_rate, kernel_size, sanm_shfit=sanm_shfit
attention_dim, self_attention_dropout_rate, kernel_size, sanm_shift=sanm_shift

# Handling Noise/Silence (Empty Output)
if compressed_prob.size(0) == 0:
token_int = []
timestamp_list = []
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 variable timestamp_list is initialized here but it is never used. This appears to be dead code and should be removed to improve clarity.

pos_enc_class: SinusoidalPositionEncoder
normalize_before: true
kernel_size: 11
sanm_shfit: 0
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo in the configuration key sanm_shfit. It should be sanm_shift to match the corrected parameter name in the model implementation.

    sanm_shift: 0

src_attention_dropout_rate: 0.1
att_layer_num: 16
kernel_size: 11
sanm_shfit: 0
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There's a typo in the configuration key sanm_shfit. It should be sanm_shift.

    sanm_shift: 0

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