-
Notifications
You must be signed in to change notification settings - Fork 3k
2x more memory efficient Graph-based RNN-T #11169
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ab46c7a
Optimized Graph-Transducer implementation
artbataev 4b1f5c8
Apply isort and black reformatting
artbataev f558312
Add copyright
artbataev 99121ad
Tests for bfloat16
artbataev 2e30a57
Respect lengths
artbataev 27dca45
refactor: x -> logits
artbataev 554bd07
refactor: x -> logits
artbataev 4821863
Fix tests
artbataev f6a33f0
Merge branch 'main' into optimize_graph_rnnt
artbataev 20b9291
Faster library check
artbataev 9024312
Refactor optional libs
artbataev 9b10cc2
Apply isort and black reformatting
artbataev 3c1ecd0
Merge branch 'main' into optimize_graph_rnnt
artbataev e1b9a61
Clean up code and add docstrings
artbataev 6f3d90b
Apply isort and black reformatting
artbataev 46d8d55
Add comments
artbataev afb1469
Fix k2 check
artbataev 0347d71
Apply isort and black reformatting
artbataev 9e4ed89
Fix pylint
artbataev e6c06dc
Fix CPU-only tests
artbataev 7251abb
Fix docstring
artbataev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
|
||
def rnnt_logprobs_torch( | ||
logits: torch.Tensor, targets: torch.Tensor, blank_id: int | ||
) -> tuple[torch.Tensor, torch.Tensor]: | ||
""" | ||
Given logits, calculate log probabilities for blank and target labels needed for transducer loss calculation. | ||
Naive implementation in PyTorch, for testing and prototyping purposes. | ||
|
||
Args: | ||
logits: Joint tensor of size [B, T, U+1, D] | ||
targets: Targets of size [B, U] | ||
blank_id: id of the blank output | ||
|
||
Returns: | ||
Tuple of tensors with log probabilities for targets and blank labels, both of size [B, T, U+1]. | ||
For the last non-existent target (U+1) output is zero. | ||
""" | ||
device = logits.device | ||
batch_size = logits.shape[0] | ||
log_probs = F.log_softmax(logits, dim=-1) | ||
blank_scores = log_probs[..., blank_id] | ||
targets = torch.cat((targets, torch.zeros([batch_size], dtype=targets.dtype, device=device).unsqueeze(1)), dim=-1) | ||
target_scores = torch.gather( | ||
log_probs, dim=-1, index=targets.unsqueeze(1).expand(log_probs.shape[:-1]).unsqueeze(-1) | ||
).squeeze(-1) | ||
target_scores[:, :, -1] = 0.0 | ||
return target_scores, blank_scores |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be made into a function, similar to Numba, instead of a hardcoded constant var