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
18 changes: 18 additions & 0 deletions funasr/models/paraformer/cif_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ def cif_v1_export(hidden, alphas, threshold: float):
device = hidden.device
dtype = hidden.dtype
batch_size, len_time, hidden_size = hidden.size()
if fire_idxs.sum() == 0:
# No integration boundaries fired (e.g. a very short / silent
# segment): return zero frames instead of indexing into the
# resulting empty tensors, which raised an IndexError.
max_label_len = torch.round(alphas.sum(-1)).int().max()
return (
torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device),
fires,
)
Comment on lines +619 to +627

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.

critical

In cif_v1_export, fire_idxs and fires are referenced before they are defined. This will raise a NameError (or fail TorchScript compilation) at runtime.

Since alphas are non-negative, the condition fire_idxs.sum() == 0 is mathematically equivalent to torch.floor(alphas.sum(-1)).sum() == 0. We can use this check to avoid any undefined variable issues and completely bypass the need to compute fire_idxs beforehand in the early-return case. In this case, fires is simply the cumulative sum of alphas cast to float32.

Suggested change
if fire_idxs.sum() == 0:
# No integration boundaries fired (e.g. a very short / silent
# segment): return zero frames instead of indexing into the
# resulting empty tensors, which raised an IndexError.
max_label_len = torch.round(alphas.sum(-1)).int().max()
return (
torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device),
fires,
)
if torch.floor(alphas.sum(-1)).sum() == 0:
# No integration boundaries fired (e.g. a very short / silent
# segment): return zero frames instead of indexing into the
# resulting empty tensors, which raised an IndexError.
max_label_len = torch.round(alphas.sum(-1)).int().max()
fires = torch.cumsum(alphas, dim=1, dtype=torch.float64).to(torch.float32)
return (
torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device),
fires,
)

threshold = torch.tensor([threshold], dtype=alphas.dtype).to(alphas.device)

frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device)
Expand Down Expand Up @@ -853,6 +862,15 @@ def cif_v1(hidden, alphas, threshold):
device = hidden.device
dtype = hidden.dtype
batch_size, len_time, hidden_size = hidden.size()
if fire_idxs.sum() == 0:
# No integration boundaries fired (e.g. a very short / silent
# segment): return zero frames instead of indexing into the
# resulting empty tensors, which raised an IndexError.
max_label_len = torch.round(alphas.sum(-1)).int().max()
return (
torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device),
fires,
)
# frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device)
# prefix_sum_hidden = torch.cumsum(alphas.unsqueeze(-1).tile((1, 1, hidden_size)) * hidden, dim=1)
frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device)
Expand Down