diff --git a/docs/development/diagnostics.md b/docs/development/diagnostics.md index 8c6d8cc..6dd93d6 100644 --- a/docs/development/diagnostics.md +++ b/docs/development/diagnostics.md @@ -25,6 +25,7 @@ are deprecated spellings of `NEMO_SPEECH_BUILD_NMT` and `NEMO_SPEECH_BUILD_GRPC` | `GGML_CUDA_GRAPH_REINSTANTIATE_AFTER_MS` | `250` on integrated GPUs, `0` on discrete GPUs | After this long without ggml work on the GPU, instantiate cached CUDA graphs again before they run. Prevents Jetson Thor faults (Xid 13) and wrong results after idle power gating. `0` turns it off. | | `GGML_SKINNY_Q8=0` | enabled | Disable the skinny Q8_0 GEMM for block Q8_0 weights. Planar Q8 weights always use it. | | `GGML_SKINNY_Q8_INPLACE=0` | in place; `0` when ASR and NMT share a process | Keep repacked skinny-Q8 weights in a separate buffer. | +| `NEMO_SPEECH_RELPOS_MAX_Q` | `512` | Max query length `q_len` (encoder frames) that uses the fused CUDA relative-position attention kernel; longer sequences take the unfused cuBLAS GEMM path. `0` forces unfused for all lengths; `< 0` (e.g. `-1`) forces always-fused. | The SALM engine reads no environment variables; its settings are `asr.salm.*` keys (see [SALM configuration](../salm/configuration.md)). @@ -37,6 +38,7 @@ Upstream switches that are useful for bisecting a CUDA problem: - `NEMO_SPEECH_MODEL_DIR`, `NEMO_SPEECH_MODEL_INDEX`, `NEMO_SPEECH_HF_BASE_URL`: the CLI model store. - `NEMO_SPEECH_`: overrides one configuration key. +- `NEMO_SPEECH_MAX_OFFLINE_SEC`: maximum audio duration (seconds, default `90`) per `OfflineRunner` encoder pass, clamped against `pos_emb_max_len`. Longer recordings split at quiet points into stateful segments; raise (e.g. `400`) to restore full-budget passes at higher VRAM cost. - `S2S_*`: VoiceChat tuning; see [VoiceChat configuration](../s2s/configuration.md). - `MAGPIETTS_LOGIT_DUMP` and `MAGPIETTS_FORCE_CODES`: file paths used for MagpieTTS parity testing. - `EDGE_SHIM_TRACE_SHAPES`: logs every call in the cuBLAS shim. diff --git a/src/asr/decoders/decoder.h b/src/asr/decoders/decoder.h index 27d2c0e..16a264b 100644 --- a/src/asr/decoders/decoder.h +++ b/src/asr/decoders/decoder.h @@ -124,14 +124,27 @@ real_encoder_frames(int64_t mel_frames, int subsampling_factor) { // Word times are emission frames, and runners flush the decoder through // synthetic zero frames past the end of the audio. Tokens emitted there keep // their text and move onto the last real frame; order and end > start hold. +inline void +bound_word_frame(WordTiming& word, int64_t frame_limit) { + if (frame_limit <= 0) + return; + word.start_frame = std::clamp(word.start_frame, 0, frame_limit - 1); + word.end_frame = std::clamp(word.end_frame, word.start_frame + 1, frame_limit); +} + inline void bound_word_frames(std::vector& words, int64_t frame_limit) { if (frame_limit <= 0) return; - for (auto& word : words) { - word.start_frame = std::clamp(word.start_frame, 0, frame_limit - 1); - word.end_frame = std::clamp(word.end_frame, word.start_frame + 1, frame_limit); - } + for (auto& word : words) bound_word_frame(word, frame_limit); +} + +inline void +bound_decoder_timings( + std::vector& words, WordTiming& cur, bool cur_open, int64_t frame_limit) { + bound_word_frames(words, frame_limit); + if (cur_open) + bound_word_frame(cur, frame_limit); } // SentencePiece word-boundary marker U+2581 ('▁') = 0xE2 0x96 0x81. A piece @@ -258,6 +271,9 @@ class Decoder { static const std::vector kEmpty; return kEmpty; } + // Clamp any word timestamps emitted so far (including an open word) to + // frame_limit, so padded-tail frames from one segment do not overlap the next. + virtual void bound_timings(int64_t /*frame_limit*/) {} // Additional hypotheses beyond the top result, best-first and capped at // max_alternatives - 1. Greedy decoders return none. diff --git a/src/asr/decoders/greedy_ctc_decoder.h b/src/asr/decoders/greedy_ctc_decoder.h index 51f95f5..b0fe20a 100644 --- a/src/asr/decoders/greedy_ctc_decoder.h +++ b/src/asr/decoders/greedy_ctc_decoder.h @@ -86,6 +86,9 @@ class GreedyCtcDecoder : public Decoder { void set_compute_timestamps(bool on) override { compute_ts_ = on; } const std::vector& word_timings() const override { return words_; } + void bound_timings(int64_t frame_limit) override { + bound_decoder_timings(words_, cur_, cur_open_, frame_limit); + } void finalize() override; // flush the trailing in-progress word int64_t last_emit_frame() const override { return last_emit_frame_; } float confidence() const override { diff --git a/src/asr/decoders/rnnt_greedy_decoder.h b/src/asr/decoders/rnnt_greedy_decoder.h index 66809bd..288f147 100644 --- a/src/asr/decoders/rnnt_greedy_decoder.h +++ b/src/asr/decoders/rnnt_greedy_decoder.h @@ -169,6 +169,9 @@ class RnntGreedyDecoder : public Decoder { void set_compute_timestamps(bool on) override { compute_ts_ = on; } const std::vector& word_timings() const override { return words_; } + void bound_timings(int64_t frame_limit) override { + bound_decoder_timings(words_, cur_, cur_open_, frame_limit); + } void finalize() override; // flush the trailing in-progress word int64_t last_emit_frame() const override { return last_emit_frame_; } int64_t last_speech_frame() const override { return last_speech_frame_; } @@ -249,6 +252,9 @@ class TdtGreedyDecoder : public Decoder { const std::vector& vocab() const override { return engine_->vocab(); } void set_compute_timestamps(bool on) override { compute_ts_ = on; } const std::vector& word_timings() const override { return words_; } + void bound_timings(int64_t frame_limit) override { + bound_decoder_timings(words_, cur_, cur_open_, frame_limit); + } void finalize() override; int64_t last_emit_frame() const override { return last_emit_frame_; } const RnntDecodeStats& stats() const { return stats_; } diff --git a/src/asr/encoder/rel_pos_attention.cpp b/src/asr/encoder/rel_pos_attention.cpp index 2e8d2d1..ea7842c 100644 --- a/src/asr/encoder/rel_pos_attention.cpp +++ b/src/asr/encoder/rel_pos_attention.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -244,7 +245,18 @@ RelPositionMultiHeadAttention::build_graph_masked( // ggml_backend_sched_split_graph aborts with no backend able to take the // node. use_gpu=true guarantees the GPU backend exists (BackendManager // throws otherwise), and the scheduler places the op there. - const bool use_fused = session->params.use_gpu; + static const int64_t fused_max_q = [] { + if (const char* s = std::getenv("NEMO_SPEECH_RELPOS_MAX_Q")) { + char* end = nullptr; + const long v = std::strtol(s, &end, 10); + if (end != s) { + return static_cast(v); + } + } + return static_cast(512); + }(); + const int64_t q_len = input_tensor.tensor->ne[1]; + const bool use_fused = session->params.use_gpu && (fused_max_q < 0 || q_len <= fused_max_q); if (use_fused) { // Canonical contiguous [d_k, len, n_head, batch] operands. Bias add and // rel-shift happen inside the kernel, so q_can is passed pre-bias. diff --git a/src/asr/runner.cpp b/src/asr/runner.cpp index 2fd732b..2321d23 100644 --- a/src/asr/runner.cpp +++ b/src/asr/runner.cpp @@ -632,8 +632,18 @@ OfflineRunner::OfflineRunner( // cross the positional-encoding budget; the exact length is always safe. size_t OfflineRunner::max_offline_samples_() const { + static const int max_sec = [] { + if (const char* s = std::getenv("NEMO_SPEECH_MAX_OFFLINE_SEC")) { + const int v = std::atoi(s); + if (v > 0) + return v; + } + return 90; + }(); + const size_t vram_cap = + static_cast(max_sec) * static_cast(model_->sample_rate()); if (!exceeds_offline_position_limit(*model_, audio_.size(), model_->sample_rate())) - return audio_.size(); + return std::min(audio_.size(), vram_cap); size_t lo = static_cast(model_->sample_rate()); size_t hi = audio_.size(); while (hi - lo > static_cast(model_->fe().hop_length())) { @@ -643,7 +653,7 @@ OfflineRunner::max_offline_samples_() const { else lo = mid; } - return lo; + return std::min(lo, vram_cap); } // Center of the quietest 100 ms window in [target - search_span, target], so @@ -724,24 +734,36 @@ OfflineRunner::finalize() { std::unique_ptr owned_decoder; Decoder* decoder = nullptr; std::vector tokens; + const int64_t enc_frame_samples = std::max( + 1, static_cast(model_->subsampling_factor()) * model_->fe().hop_length()); + const auto seg_frame_offset = [&](size_t off) -> int64_t { + return static_cast(off) / enc_frame_samples; + }; // Encoder frames past the real audio (bucket padding and the final // centered frame) can still emit; bound words to the real frames. int64_t frame_limit = 0; - const auto note_real_frames = [&](size_t off, size_t len, int64_t frame_offset) { + const auto note_real_frames = [&](size_t off, size_t len, int64_t frame_offset, bool has_next) { if (off >= real_samples) return; - const auto mel = - static_cast(std::min(len, real_samples - off)) / model_->fe().hop_length(); + const size_t real_len = std::min(len, real_samples - off); + const auto mel = static_cast(real_len) / model_->fe().hop_length(); frame_limit = frame_offset + real_encoder_frames(std::max(1, mel), model_->subsampling_factor()); + if (opts_.enable_word_time_offsets && has_next && off + real_len < real_samples) { + const int64_t next_offset = + std::max(frame_offset + 1, seg_frame_offset(off + real_len)); + decoder->bound_timings(next_offset); + } }; if (model_->head_kind() == HeadKind::Ctc) { auto* ctc = static_cast(model_); decoder = ctc_decoder_.get(); decoder->set_compute_timestamps(opts_.enable_word_time_offsets); decoder->set_request_options(opts_); - int64_t frame_offset = 0; - for (const auto& [off, len] : segments) { + for (size_t i = 0; i < segments.size(); ++i) { + const auto [off, len] = segments[i]; + const int64_t frame_offset = seg_frame_offset(off); + const bool has_next = (i + 1 < segments.size()); std::vector seg_tokens; if (auto* greedy = dynamic_cast(decoder)) { std::vector best_ids; @@ -750,15 +772,13 @@ OfflineRunner::finalize() { ctc->infer_ctc_greedy(audio_.data() + off, len, best_ids, best_probs, T); seg_tokens = greedy->step_compact(best_ids.data(), best_probs.data(), T, frame_offset); - note_real_frames(off, len, frame_offset); - frame_offset += T; + note_real_frames(off, len, frame_offset, has_next); } else { std::vector log_probs; int T = 0, C = 0; ctc->infer_ctc(audio_.data() + off, len, log_probs, T, C); seg_tokens = decoder->step(log_probs.data(), C, T, frame_offset); - note_real_frames(off, len, frame_offset); - frame_offset += T; + note_real_frames(off, len, frame_offset, has_next); } tokens.insert(tokens.end(), seg_tokens.begin(), seg_tokens.end()); } @@ -769,21 +789,24 @@ OfflineRunner::finalize() { decoder->set_compute_timestamps(opts_.enable_word_time_offsets); decoder->set_request_options(opts_); const auto decode_begin = std::chrono::steady_clock::now(); - int64_t frame_offset = 0; - for (const auto& [off, len] : segments) { + int64_t total_frames = 0; + for (size_t i = 0; i < segments.size(); ++i) { + const auto [off, len] = segments[i]; + const int64_t frame_offset = seg_frame_offset(off); + const bool has_next = (i + 1 < segments.size()); std::vector enc; int T = 0; transducer->infer_offline(audio_.data() + off, len, enc, T, prompt_index_); auto seg_tokens = decoder->step(enc.data(), transducer->rnnt_config().joint_dim, T, frame_offset); - note_real_frames(off, len, frame_offset); - frame_offset += T; + note_real_frames(off, len, frame_offset, has_next); + total_frames += T; tokens.insert(tokens.end(), seg_tokens.begin(), seg_tokens.end()); } if (std::getenv("NEMO_SPEECH_TIMING")) { std::fprintf( stderr, "[timing] offline-transducer decode frames=%lld segments=%zu = %.2f ms\n", - static_cast(frame_offset), segments.size(), + static_cast(total_frames), segments.size(), std::chrono::duration( std::chrono::steady_clock::now() - decode_begin) .count());