Skip to content

Fix exporting streaming zipformer models as non-streaming models#2095

Merged
csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:fix-export
Jun 29, 2026
Merged

Fix exporting streaming zipformer models as non-streaming models#2095
csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:fix-export

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

See errors reported at
#2086 (comment)

cc @nshmyrev

Summary by CodeRabbit

  • Bug Fixes
    • Improved convolution handling for short input sequences so results are now computed correctly when the sequence length is smaller than the kernel size.
    • Preserved the existing behavior for longer sequences, with no change to standard processing.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d3dd37e6-462d-4ebd-b902-1687d1afa41e

📥 Commits

Reviewing files that changed from the base of the PR and between 3bf7070 and bf4992a.

📒 Files selected for processing (1)
  • egs/librispeech/ASR/zipformer/scaling_converter.py

📝 Walkthrough

Walkthrough

In NonStreamingChunkCausalDepthwiseConv1d.forward, the left_edge/right_edge scaling operands now branch on whether seq_len < kernel_size: short sequences slice those tensors to seq_len, while the existing zero-padding and concatenation path runs for seq_len >= kernel_size.

Changes

Short-sequence edge scaling fix

Layer / File(s) Summary
Conditional left/right edge scaling
egs/librispeech/ASR/zipformer/scaling_converter.py
Adds a seq_len < kernel_size branch that slices left_edge and right_edge to seq_len; the original zero-padding concatenation path is retained for seq_len >= kernel_size.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

A kernel so wide, the sequence so short,
The rabbit sliced edges of the proper sort.
No zeros appended where none should go,
Just trim to the length and let tensors flow.
🐇✂️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the change to fix exporting streaming Zipformer models as non-streaming models.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Copy link
Copy Markdown
Contributor

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 modifies the forward method in scaling_converter.py to handle sequence lengths shorter than the kernel size by introducing an if/else conditional block. The reviewer suggests refactoring this to a unified padding and slicing approach using max to eliminate the conditional block, which ensures the code remains ONNX-export friendly and avoids potential issues with ONNX runtimes.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +76 to +86
if seq_len < self.kernel_size:
left_edge = left_edge[:, :seq_len]
right_edge = right_edge[:, -seq_len:]
else:
t = seq_len - self.kernel_size
channels = left_edge.shape[0]
pad = torch.zeros(
channels, t, device=left_edge.device, dtype=left_edge.dtype
)
left_edge = torch.cat((left_edge, pad), dim=-1)
right_edge = torch.cat((pad, right_edge), dim=-1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The class NonStreamingChunkCausalDepthwiseConv1d is specifically designed to be ONNX-export friendly by avoiding conditionals where possible (as noted in its docstring).\n\nBy using a unified padding and slicing approach with max(0, seq_len - self.kernel_size), we can completely eliminate the if/else control flow block. This avoids generating ONNX If nodes, which can be problematic or inefficient for certain ONNX runtimes (e.g., TensorRT or mobile runtimes), while keeping the code much more concise.

        pad_len = max(0, seq_len - self.kernel_size)\n        pad = torch.zeros(\n            left_edge.shape[0], pad_len, device=left_edge.device, dtype=left_edge.dtype\n        )\n        left_edge = torch.cat((left_edge, pad), dim=-1)[:, :seq_len]\n        right_edge = torch.cat((pad, right_edge), dim=-1)[:, -seq_len:]

@csukuangfj csukuangfj merged commit 7a35ca2 into k2-fsa:master Jun 29, 2026
10 of 128 checks passed
@nshmyrev

Copy link
Copy Markdown
Contributor

@csukuangfj wonderful, many thanks!

@csukuangfj csukuangfj deleted the fix-export branch June 30, 2026 04:27
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.

2 participants