-
Notifications
You must be signed in to change notification settings - Fork 1k
Add tdt duration to APIs #2514
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
csukuangfj
merged 4 commits into
k2-fsa:master
from
bradmurray-dt:Add-tdt-duration-to-APIs
Aug 21, 2025
Merged
Add tdt duration to APIs #2514
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,89 @@ | ||
| // c-api-examples/nemo-parakeet-c-api.c | ||
| // Example using the C API and sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8 model | ||
| // Prints recognized text, per-token timestamps, and durations | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #include "sherpa-onnx/c-api/c-api.h" | ||
|
|
||
| int32_t main() { | ||
| const char *wav_filename = | ||
| "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/test_wavs/en.wav"; | ||
| const char *encoder_filename = | ||
| "sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/encoder.int8.onnx"; | ||
| const char *decoder_filename = | ||
| "sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/decoder.int8.onnx"; | ||
| const char *joiner_filename = | ||
| "sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/joiner.int8.onnx"; | ||
| const char *tokens_filename = | ||
| "sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/tokens.txt"; | ||
| const char *provider = "cpu"; | ||
|
|
||
| if (!SherpaOnnxFileExists(wav_filename)) { | ||
| fprintf(stderr, "File not found: %s\n", wav_filename); | ||
| return -1; | ||
| } | ||
| const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename); | ||
| if (wave == NULL) { | ||
| fprintf(stderr, "Failed to read or parse %s (not a valid mono 16-bit WAVE file)\n", wav_filename); | ||
| return -1; | ||
| } | ||
|
|
||
| SherpaOnnxOfflineModelConfig offline_model_config; | ||
| memset(&offline_model_config, 0, sizeof(offline_model_config)); | ||
| offline_model_config.debug = 0; | ||
| offline_model_config.num_threads = 1; | ||
| offline_model_config.provider = provider; | ||
| offline_model_config.tokens = tokens_filename; | ||
| offline_model_config.transducer.encoder = encoder_filename; | ||
| offline_model_config.transducer.decoder = decoder_filename; | ||
| offline_model_config.transducer.joiner = joiner_filename; | ||
|
|
||
| SherpaOnnxOfflineRecognizerConfig recognizer_config; | ||
| memset(&recognizer_config, 0, sizeof(recognizer_config)); | ||
| recognizer_config.decoding_method = "greedy_search"; | ||
| recognizer_config.model_config = offline_model_config; | ||
|
|
||
| const SherpaOnnxOfflineRecognizer *recognizer = | ||
| SherpaOnnxCreateOfflineRecognizer(&recognizer_config); | ||
| if (recognizer == NULL) { | ||
| fprintf(stderr, "Please check your config!\n"); | ||
| SherpaOnnxFreeWave(wave); | ||
| return -1; | ||
| } | ||
|
|
||
| const SherpaOnnxOfflineStream *stream = | ||
| SherpaOnnxCreateOfflineStream(recognizer); | ||
| if (stream == NULL) { | ||
| fprintf(stderr, "Failed to create offline stream.\n"); | ||
| SherpaOnnxDestroyOfflineRecognizer(recognizer); | ||
| SherpaOnnxFreeWave(wave); | ||
| return -1; | ||
| } | ||
|
|
||
| SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples, | ||
| wave->num_samples); | ||
| SherpaOnnxDecodeOfflineStream(recognizer, stream); | ||
| const SherpaOnnxOfflineRecognizerResult *result = | ||
| SherpaOnnxGetOfflineStreamResult(stream); | ||
|
|
||
| printf("Recognized text: %s\n", result->text); | ||
|
|
||
| if (result->tokens_arr && result->timestamps && result->durations) { | ||
| printf("Token\tTimestamp\tDuration\n"); | ||
| for (int32_t i = 0; i < result->count; ++i) { | ||
| printf("%s\t%.2f\t%.2f\n", result->tokens_arr[i], result->timestamps[i], result->durations[i]); | ||
| } | ||
| } else { | ||
| printf("Timestamps or durations not available.\n"); | ||
| } | ||
|
|
||
| SherpaOnnxDestroyOfflineRecognizerResult(result); | ||
| SherpaOnnxDestroyOfflineStream(stream); | ||
| SherpaOnnxDestroyOfflineRecognizer(recognizer); | ||
| SherpaOnnxFreeWave(wave); | ||
|
|
||
| return 0; | ||
| } |
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,46 @@ | ||
| # Example using the sherpa-onnx Python API and sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8 model | ||
| # Prints recognized text, per-token timestamps, and durations | ||
|
|
||
| import os | ||
| import sys | ||
| import sherpa_onnx | ||
| import soundfile as sf | ||
|
|
||
| wav_filename = "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/test_wavs/en.wav" | ||
| encoder = "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/encoder.int8.onnx" | ||
| decoder = "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/decoder.int8.onnx" | ||
| joiner = "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/joiner.int8.onnx" | ||
| tokens = "./sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8/tokens.txt" | ||
|
|
||
| if not os.path.exists(wav_filename): | ||
| print(f"File not found: {wav_filename}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| recognizer = sherpa_onnx.OfflineRecognizer.from_transducer( | ||
| encoder, | ||
| decoder, | ||
| joiner, | ||
| tokens, | ||
| num_threads=1, | ||
| provider="cpu", | ||
| debug=False, | ||
| decoding_method="greedy_search", | ||
| model_type="nemo_transducer" | ||
| ) | ||
|
|
||
| audio, sample_rate = sf.read(wav_filename, dtype="float32", always_2d=True) | ||
| audio = audio[:, 0] # use first channel if multi-channel | ||
| stream = recognizer.create_stream() | ||
| stream.accept_waveform(sample_rate, audio) | ||
| recognizer.decode_stream(stream) | ||
| result = stream.result | ||
|
|
||
| print(f"Recognized text: {result.text}") | ||
|
|
||
| if hasattr(result, "tokens") and hasattr(result, "timestamps") and hasattr(result, "durations"): | ||
| print("Token\tTimestamp\tDuration") | ||
| for token, ts, dur in zip(result.tokens, result.timestamps, result.durations): | ||
| print(f"{token}\t{ts:.2f}\t{dur:.2f}") | ||
| else: | ||
| print("Timestamps or durations not available.") |
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
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
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -130,23 +130,31 @@ static OfflineTransducerDecoderResult DecodeOneTDT( | |||||||||||||||||||
| p_logit[blank_id] -= blank_penalty; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto y = static_cast<int32_t>(std::distance( | ||||||||||||||||||||
| static_cast<const float *>(p_logit), | ||||||||||||||||||||
| std::max_element(static_cast<const float *>(p_logit), | ||||||||||||||||||||
| static_cast<const float *>(p_logit) + vocab_size))); | ||||||||||||||||||||
| int32_t output_size = shape.back(); | ||||||||||||||||||||
| int32_t num_durations = output_size - vocab_size; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| skip = static_cast<int32_t>(std::distance( | ||||||||||||||||||||
| static_cast<const float *>(p_logit) + vocab_size, | ||||||||||||||||||||
| std::max_element(static_cast<const float *>(p_logit) + vocab_size, | ||||||||||||||||||||
| static_cast<const float *>(p_logit) + shape.back()))); | ||||||||||||||||||||
| // Split logits into token and duration logits | ||||||||||||||||||||
| const float* token_logits = p_logit; | ||||||||||||||||||||
| const float* duration_logits = p_logit + vocab_size; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (skip == 0) { | ||||||||||||||||||||
| skip = 1; | ||||||||||||||||||||
| auto y = static_cast<int32_t>(std::distance( | ||||||||||||||||||||
| token_logits, | ||||||||||||||||||||
| std::max_element(token_logits, token_logits + vocab_size))); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| skip = 1; | ||||||||||||||||||||
| int32_t duration = 1; | ||||||||||||||||||||
| if (num_durations > 0) { | ||||||||||||||||||||
| duration = static_cast<int32_t>(std::distance( | ||||||||||||||||||||
| duration_logits, | ||||||||||||||||||||
| std::max_element(duration_logits, duration_logits + num_durations))); | ||||||||||||||||||||
| skip = duration; | ||||||||||||||||||||
| if (skip == 0) skip = 1; | ||||||||||||||||||||
|
||||||||||||||||||||
| if (skip == 0) skip = 1; | |
| skip = kDefaultDuration; | |
| int32_t duration = kDefaultDuration; | |
| if (num_durations > 0) { | |
| duration = static_cast<int32_t>(std::distance( | |
| duration_logits, | |
| std::max_element(duration_logits, duration_logits + num_durations))); | |
| skip = duration; | |
| if (skip == 0) skip = kDefaultDuration; |
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
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.
The variable
skipis unconditionally set to 1 on line 144, but then conditionally overridden on line 150. This creates confusion as the initial assignment appears unnecessary. Consider moving theskip = 1assignment inside the else clause or after the duration calculation.