Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions egs/librispeech/ASR/zipformer/scaling_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ def forward(self, x: torch.Tensor, chunk_size: int = -1) -> torch.Tensor:

left_edge = self.chunkwise_conv_scale[0]
right_edge = self.chunkwise_conv_scale[1]
# seq_len >= kernel_size in non-streaming mode, so we pad with zeros
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)

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)
Comment on lines +76 to +86

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:]

chunk_scale = 1.0 + (left_edge + right_edge)

x_chunk = x_chunk * chunk_scale
Expand Down
Loading