-
Notifications
You must be signed in to change notification settings - Fork 34k
Add TDT loss kernel #46048
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
Draft
ebezzam
wants to merge
82
commits into
huggingface:main
Choose a base branch
from
ebezzam:tdt_loss_kernel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add TDT loss kernel #46048
Changes from all commits
Commits
Show all changes
82 commits
Select commit
Hold shift + click to select a range
fa7d6e0
parakeet tdt intergration
f2b4938
Add TDT decoder support for Parakeet ASR models
lmaksym fa36657
Add expected outputs for TDT, small fixes.
ebezzam 05e2e34
Separate CTC and TDT generate outputs.
ebezzam bb5ff33
Work with auto device, better init,
ebezzam 9ec79b0
Test timestamps and expose token duration.
ebezzam 33f128e
Add reproducer link.
ebezzam 760b4b6
fix: align TDT training and decoding with NeMo implementation
lmaksym b33002f
revert: restore lasr generated files to original state
lmaksym 48b39dd
warn: torchaudio rnnt_loss does not train duration head
lmaksym e9f23ab
Relax timestamp test, and test nits.
ebezzam e2b97aa
feat: TDT training
lmaksym 6b9fc73
chore: for cuda detection and run without patching
lmaksym 6c879bc
Equivalent timestamp processing as Nemo, and various nits/cleanup.
ebezzam 149e17f
Merge branch 'parakeet-tdt' of github.com:lmaksym/transformers into p…
ebezzam 36bfa63
Simplify durations config.
ebezzam 2df0ccc
Update training examples.
ebezzam 388c6d3
chore: enable parralelism
lmaksym 08b2b55
chore: performance optimization
lmaksym 0c4e05a
fix: formatting
lmaksym 1ddd804
Doc and testing nits
ebezzam f512670
Use active mask from current step, and nits.
ebezzam 07d8e35
Better pre-allocate.
ebezzam fab050a
TDT has separate pad token and blank token.
ebezzam c438565
Merge branch 'main' into parakeet-tdt
ebezzam 86d980c
Regenerate lasr.
ebezzam 895c4a0
Merge branch 'parakeet-tdt' of github.com:lmaksym/transformers into p…
ebezzam ab21380
Style checks and nits
ebezzam d0141d5
Nits, put back ctc loss test
ebezzam f7529d4
More standard model output.
ebezzam 77b95d7
Style
ebezzam 94eae66
Remove compute_loss flag and allow monkey patching to tdt loss
ebezzam f7d4067
Update src/transformers/models/parakeet/modular_parakeet.py
ebezzam f75c17b
Address various comments.
ebezzam 5a49b65
More compatible with Transformers forward/generate approach
ebezzam 881233f
compile option for generation and decoder cache
ebezzam b41a8ee
Cleaner, better conventions.
ebezzam 897753a
Merge branch 'main' into parakeet-tdt
ebezzam 6c914db
Update with main.
ebezzam 756cee1
doc nits
ebezzam f30c536
Imitate whisper for encoder outputs as input
ebezzam fa95fc8
Address tests and nits.
ebezzam 5df7f28
Inherit from GenerateMixIn for get_compiled_call
ebezzam cd706d4
Comment nit
ebezzam a47ed8a
forward cleanup
eustlb 13b68ce
generate cleanup + separate generation file
eustlb 72c1ad0
generate: add _supported_generation_modes
eustlb 8e23b3d
automatic init of the loss
eustlb 1cc39fd
modular cleanups
eustlb 531f297
use is_encoder_decoder
eustlb 2c0f23a
timestamp processing fully from tokens + durations
eustlb cef6639
convertion script update
eustlb fd3cf9b
test update
eustlb e63a5bf
make
eustlb f9d1a4f
Merge branch 'main' into parakeet-tdt
eustlb 43ee7cd
test update
eustlb c2a0f78
test update
eustlb 1fd7ed7
ensure correct loss computation
eustlb 7cc9d2e
kernel loss
eustlb e753eab
test loss integration
eustlb ed3fa4d
push to hub pr
eustlb ab66b23
integration tests to rely fully on transcripts
eustlb a5ba0c6
udpate fixtures
eustlb 48279a6
we don't need to monkey patch with numba anymore!
eustlb 1d7680d
fix pipeline usage
eustlb 59ddced
nit
eustlb 31490d1
fix usage
eustlb d8eb1b6
Pass through tests and examples: improve kernel fallback, update with…
ebezzam 1f1b912
Update checkpoint
ebezzam 9ab08d1
Merge branch 'main' into parakeet-tdt
ebezzam fd9f8b1
Add TDT to mapping after merge.
ebezzam 136f676
Fix lasr generate test.
ebezzam 833d289
Output attention mask if labels provided for computing loss.
ebezzam a1c62a1
Apply suggestion from @ArthurZucker
ebezzam 8683570
Improve ParakeetTDTDecoderCache definition and usage.
ebezzam 1d4b0f4
Remove tuple parsing.
ebezzam a418eca
processor refactor
ebezzam 5d0c631
Merge branch 'parakeet-tdt' of github.com:lmaksym/transformers into p…
ebezzam 5c603c1
Update conversion.
ebezzam 09ba99c
Merge branch 'main' into tdt_loss_kernel
ebezzam e743b2d
Modular after merge.
ebezzam 8d09cb6
Don't allow all kernels.
ebezzam 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
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 |
|---|---|---|
|
|
@@ -16,7 +16,9 @@ | |
| import json | ||
| import tempfile | ||
| import unittest | ||
| from contextlib import nullcontext | ||
| from pathlib import Path | ||
| from unittest.mock import patch | ||
|
|
||
| from transformers import is_datasets_available, is_torch_available | ||
| from transformers.testing_utils import cleanup, require_torch, slow, torch_device | ||
|
|
@@ -731,9 +733,12 @@ def test_tdt_model_integration_timestamps(self): | |
| @slow | ||
| def test_tdt_model_integration_loss(self): | ||
| """ | ||
| Verify that ParakeetForTDT loss matches NeMo's TDT loss (sigma=0). | ||
| Verify that ParakeetForTDT loss matches NeMo's TDT loss (sigma=0) for both | ||
| the CUDA kernel and the pure PyTorch implementation. | ||
| reproducer: https://gist.github.com/883ea42bf7d8ce2af42f3055627476a7 | ||
|
Comment on lines
+736
to
738
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also test for sigma != 0? |
||
| """ | ||
| from transformers.loss.loss_tdt import _load_tdt_kernel | ||
|
|
||
| RESULTS_PATH = Path(__file__).parent.parent.parent / "fixtures/parakeet/expected_loss_tdt.json" | ||
| with open(RESULTS_PATH, "r") as f: | ||
| raw_data = json.load(f) | ||
|
|
@@ -754,20 +759,33 @@ def test_tdt_model_integration_loss(self): | |
| ) | ||
| inputs.to(model.device) | ||
|
|
||
| # Forward in eval mode — check loss matches NeMo | ||
| model.eval() | ||
| with torch.no_grad(): | ||
| outputs = model(**inputs) | ||
| self.assertIsNotNone(outputs.loss, "Loss must be computed when labels are provided") | ||
| self.assertEqual(outputs.logits.dim(), 4, "Training logits must be 4D (B, T, U+1, V+D)") | ||
| torch.testing.assert_close(outputs.loss.cpu(), EXPECTED_MEAN_LOSS, rtol=1e-3, atol=1e-3) | ||
|
|
||
| # Backward — verify gradients flow | ||
| del outputs | ||
| torch.cuda.empty_cache() | ||
| model.train() | ||
| model.zero_grad() | ||
| outputs = model(**inputs) | ||
| outputs.loss.backward() | ||
| n_with_grad = sum(1 for p in model.parameters() if p.grad is not None) | ||
| self.assertGreater(n_with_grad, 0, "No gradients after backward") | ||
| # Test both backends: kernel (if available) and pure PyTorch | ||
| has_kernel = _load_tdt_kernel() is not None | ||
| backends = [ | ||
| ("kernel", None), | ||
| ("torch", patch("transformers.loss.loss_tdt._load_tdt_kernel", return_value=None)), | ||
| ] | ||
| if not has_kernel: | ||
| backends = backends[1:] # skip kernel test when not installed | ||
|
|
||
| for backend_name, ctx in backends: | ||
| with self.subTest(backend=backend_name): | ||
| ctx_manager = ctx if ctx is not None else nullcontext() | ||
| with ctx_manager: | ||
| # Forward in eval mode — check loss matches NeMo | ||
| model.eval() | ||
| with torch.no_grad(): | ||
| outputs = model(**inputs) | ||
| self.assertIsNotNone(outputs.loss, "Loss must be computed when labels are provided") | ||
| self.assertEqual(outputs.logits.dim(), 4, "Training logits must be 4D (B, T, U+1, V+D)") | ||
| torch.testing.assert_close(outputs.loss.cpu(), EXPECTED_MEAN_LOSS, rtol=1e-3, atol=1e-3) | ||
|
|
||
| # Backward — verify gradients flow | ||
| del outputs | ||
| torch.cuda.empty_cache() | ||
| model.train() | ||
| model.zero_grad() | ||
| outputs = model(**inputs) | ||
| outputs.loss.backward() | ||
| n_with_grad = sum(1 for p in model.parameters() if p.grad is not None) | ||
| self.assertGreater(n_with_grad, 0, "No gradients after backward") | ||
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.
Related comment: https://github.com/huggingface/transformers/pull/44171/changes#r3094638013