-
Notifications
You must be signed in to change notification settings - Fork 1.5k
paraformer_v2_community offline #2785
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
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.
Summary of ChangesHello @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 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 AssistThe 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
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 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
|
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.
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) |
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.
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.
| 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: |
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.
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.
| 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: |
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.
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.
| 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), [] |
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.
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.
| 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, |
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.
| if sanm_shfit is None: | ||
| sanm_shfit = (kernel_size - 1) // 2 |
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.
| lambda lnum: DecoderLayerSANM( | ||
| attention_dim, | ||
| MultiHeadedAttentionSANMDecoder( | ||
| attention_dim, self_attention_dropout_rate, kernel_size, sanm_shfit=sanm_shfit |
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.
| # Handling Noise/Silence (Empty Output) | ||
| if compressed_prob.size(0) == 0: | ||
| token_int = [] | ||
| timestamp_list = [] |
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.
| pos_enc_class: SinusoidalPositionEncoder | ||
| normalize_before: true | ||
| kernel_size: 11 | ||
| sanm_shfit: 0 |
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.
| src_attention_dropout_rate: 0.1 | ||
| att_layer_num: 16 | ||
| kernel_size: 11 | ||
| sanm_shfit: 0 |
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.
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.