diff --git a/include/engine/community_models/fish_drama3/generator.h b/include/engine/community_models/fish_drama3/generator.h index 199422ce..6e88ae72 100644 --- a/include/engine/community_models/fish_drama3/generator.h +++ b/include/engine/community_models/fish_drama3/generator.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "engine/community_models/fish_drama3/prompt.h" #include "engine/framework/runtime/streaming_audio.h" @@ -14,6 +16,11 @@ struct Drama3Sampling { int top_k = 4097; int64_t max_frames = 1024; uint32_t seed = 0; + // Pause clamp (drama3-small / drama3-expressive-medium); unset values fall back to AUDIOCPP_DRAMA3_* env vars. + int64_t max_pause_frames = -1; // -1 unset, 0 off + std::string silence_codes; // list or file path; empty = unset + double pause_end_ratio = 0; // 0 = unset (2.0) + double pause_cap_ratio = 0; // 0 = unset (3.0) }; struct Drama3Generation { diff --git a/model_specs/fish_drama3.json b/model_specs/fish_drama3.json index c6e7a7ba..fae01094 100644 --- a/model_specs/fish_drama3.json +++ b/model_specs/fish_drama3.json @@ -122,6 +122,33 @@ "description": "Reproducible native sampling seed.", "required": false, "min": 0 + }, + { + "name": "max_pause_frames", + "type": "int", + "description": "Pause clamp: after this many consecutive silence-code frames the next main token must be speech or EOS (21.5 frames = 1 s). 0 disables; unset uses AUDIOCPP_DRAMA3_MAX_PAUSE_FRAMES.", + "required": false, + "min": 0 + }, + { + "name": "silence_codes", + "type": "string", + "description": "Semantic codes treated as silence for the pause clamp: a comma/space/JSON list or a path to a file holding one. Unset uses AUDIOCPP_DRAMA3_SILENCE_CODES.", + "required": false + }, + { + "name": "pause_end_ratio", + "type": "float", + "description": "With the pause clamp: a pause after this multiple of the text's expected frames ends the take (default 2.0).", + "required": false, + "min": 0 + }, + { + "name": "pause_cap_ratio", + "type": "float", + "description": "With the pause clamp: stop at this multiple of the text's expected frames regardless (default 3.0).", + "required": false, + "min": 0 } ], "session": [ diff --git a/src/community_models/fish_drama3/generator.cpp b/src/community_models/fish_drama3/generator.cpp index d7ce7d00..d3c61a45 100644 --- a/src/community_models/fish_drama3/generator.cpp +++ b/src/community_models/fish_drama3/generator.cpp @@ -6,6 +6,13 @@ #include "engine/framework/sampling/hf_sampler.h" #include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -83,6 +90,96 @@ int32_t sample(const std::vector & logits, float temperature, int top_k, return order[choice(random)]; } +// drama3-small pause clamp (request options max_pause_frames / silence_codes / pause_end_ratio / +// pause_cap_ratio, else AUDIOCPP_DRAMA3_* env). After max_pause_frames consecutive silence-code +// frames, silence codes are masked from the next main-token draw, so the model must resume speech +// or emit EOS (21.5 frames = 1 s). 0 = off. +struct PauseClamp { + std::unordered_set codes; + int64_t max_pause = 0; + // Length guard (with the clamp): a pause after end_ratio x the text's expected frames ends the + // take instead of forcing speech; cap_ratio x expected ends it regardless (babble backstop). + double end_ratio = 2.0; + double cap_ratio = 3.0; +}; + +// Expected frames for the spoken text, fitted on clean takes (21.5 frames/s): +// 3.88 per CJK/kana character + 2.00 per ASCII letter or digit + 23.9 per control span ([...], <...>, +// each opening bracket) + 8.5. A "Direction: ...\nLine:" prefix is not spoken. +double expected_frames(const std::string & text) { + std::string spoken = text; + if (spoken.rfind("Direction:", 0) == 0) { + const auto line = spoken.find("\nLine:"); + if (line != std::string::npos) spoken = spoken.substr(line + 6); + } + double cjk = 0, latin = 0, spans = 0; + int depth_square = 0, depth_angle = 0; + for (size_t i = 0; i < spoken.size();) { + const unsigned char c = static_cast(spoken[i]); + uint32_t cp = c; + size_t len = 1; + if (c >= 0xF0 && i + 3 < spoken.size()) { cp = ((c & 0x07) << 18) | ((spoken[i + 1] & 0x3F) << 12) | ((spoken[i + 2] & 0x3F) << 6) | (spoken[i + 3] & 0x3F); len = 4; } + else if (c >= 0xE0 && i + 2 < spoken.size()) { cp = ((c & 0x0F) << 12) | ((spoken[i + 1] & 0x3F) << 6) | (spoken[i + 2] & 0x3F); len = 3; } + else if (c >= 0xC0 && i + 1 < spoken.size()) { cp = ((c & 0x1F) << 6) | (spoken[i + 1] & 0x3F); len = 2; } + i += len; + if (cp == '[') { ++depth_square; ++spans; continue; } + if (cp == ']') { if (depth_square > 0) --depth_square; continue; } + if (cp == '<') { ++depth_angle; ++spans; continue; } + if (cp == '>') { if (depth_angle > 0) --depth_angle; continue; } + if (depth_square > 0 || depth_angle > 0) continue; + const bool cjk_punct = cp == 0x3002 || cp == 0x3001 || cp == 0xFF01 || cp == 0xFF1F || cp == 0x300C || + cp == 0x300D || cp == 0x2026 || cp == 0x30FB || cp == 0xFF08 || cp == 0xFF09; + if (cp >= 0x3000 && !cjk_punct) cjk += 1; + else if (cp < 0x80 && std::isalnum(static_cast(cp))) latin += 1; + } + return 3.88 * cjk + 2.00 * latin + 23.9 * spans + 8.5; +} + +std::unordered_set parse_silence_codes(const std::string & spec) { + static std::mutex mutex; + static std::unordered_map> cache; + std::lock_guard lock(mutex); + if (const auto it = cache.find(spec); it != cache.end()) return it->second; + std::string text = spec; + if (std::ifstream file(spec); file) { + std::stringstream buffer; + buffer << file.rdbuf(); + text = buffer.str(); + } + for (char & c : text) { + if (!(c >= '0' && c <= '9')) c = ' '; + } + std::unordered_set codes; + std::istringstream values(text); + for (int32_t code; values >> code;) codes.insert(code); + if (codes.empty()) throw std::runtime_error("Drama 3 silence_codes is empty"); + return cache.emplace(spec, std::move(codes)).first->second; +} + +// Request options override the AUDIOCPP_DRAMA3_* environment defaults. +PauseClamp resolve_pause_clamp(const Drama3Sampling & sampling) { + PauseClamp clamp; + const char * env_frames = std::getenv("AUDIOCPP_DRAMA3_MAX_PAUSE_FRAMES"); + clamp.max_pause = sampling.max_pause_frames >= 0 ? sampling.max_pause_frames + : (env_frames != nullptr ? std::atoll(env_frames) : 0); + if (clamp.max_pause <= 0) { + clamp.max_pause = 0; + return clamp; + } + std::string spec = sampling.silence_codes; + if (spec.empty()) { + const char * env_codes = std::getenv("AUDIOCPP_DRAMA3_SILENCE_CODES"); + if (env_codes == nullptr) throw std::runtime_error("Drama 3 pause clamp needs silence_codes or AUDIOCPP_DRAMA3_SILENCE_CODES"); + spec = env_codes; + } + clamp.codes = parse_silence_codes(spec); + if (const char * v = std::getenv("AUDIOCPP_DRAMA3_END_RATIO")) clamp.end_ratio = std::atof(v); + if (const char * v = std::getenv("AUDIOCPP_DRAMA3_CAP_RATIO")) clamp.cap_ratio = std::atof(v); + if (sampling.pause_end_ratio > 0) clamp.end_ratio = sampling.pause_end_ratio; + if (sampling.pause_cap_ratio > 0) clamp.cap_ratio = sampling.pause_cap_ratio; + return clamp; +} + std::vector last_hidden(const std::vector & values, int64_t width) { if (values.size() < static_cast(width)) throw std::runtime_error("Drama 3 backbone returned too few hidden values"); return {values.end() - width, values.end()}; @@ -369,6 +466,31 @@ Drama3Generation Drama3Generator::generate(const std::string & text, const std:: } }); } + const PauseClamp clamp = resolve_pause_clamp(sampling); + int64_t silent_run = 0; + const double expected = expected_frames(text); + const auto end_frames = static_cast(clamp.end_ratio * expected); + const auto cap_frames = static_cast(clamp.cap_ratio * expected); + // Offline takes decode through the streaming codec graph in codec_batch_frames batches, so peak VRAM no + // longer grows with output length (a 32 s take otherwise needs 2.7-3.8 GB more on a 24 GB card). + // AUDIOCPP_DRAMA3_FULL_DECODE=1 restores the single whole-take decode graph. + const char * full_decode_env = std::getenv("AUDIOCPP_DRAMA3_FULL_DECODE"); + const bool full_decode = full_decode_env != nullptr && std::string(full_decode_env) == "1"; + const bool offline_stream_decode = !streaming && !full_decode; + std::vector offline_pending; + auto flush_offline = [&] { + if (offline_pending.empty()) return; + codecs::FishDacCodes codes; + codes.codebooks = 10; + codes.frames = static_cast(offline_pending.size()); + codes.codes.resize(offline_pending.size() * 10); + for (size_t book = 0; book < 10; ++book) { + for (size_t frame = 0; frame < offline_pending.size(); ++frame) codes.codes[book * offline_pending.size() + frame] = offline_pending[frame][book + 1]; + } + runtime::append_audio_buffer(result.audio, codec_->decode_stream(codes)); + offline_pending.clear(); + }; + if (offline_stream_decode) codec_->reset_decode_stream(); for (int64_t step = 0; step < sampling.max_frames; ++step) { component_started = ProfileClock::now(); auto logits = backbone_->lm_head(hidden); @@ -377,6 +499,17 @@ Drama3Generation Drama3Generator::generate(const std::string & text, const std:: // Both drama3 (lm_head_token_ids) and drama3-small (lm_head_rows) return // compact [EOS, semantic 0..N-1] logits from the backbone. auto candidates = std::move(logits); + if (clamp.max_pause > 0 && step >= cap_frames) { + std::cerr << "[fish_drama3] length guard: stopped at " << step << " frames (expected " << expected << ")\n"; + break; + } + if (clamp.max_pause > 0 && silent_run >= clamp.max_pause && step >= end_frames) break; // pause after the text is done + if (clamp.max_pause > 0 && silent_run >= clamp.max_pause) { + // Long enough silence: the next frame must be speech or EOS (candidate 0). + for (const int32_t code : clamp.codes) { + if (code + 1 < static_cast(candidates.size())) candidates[static_cast(code) + 1] = -std::numeric_limits::infinity(); + } + } const int32_t first = sample(candidates, sampling.temperature, sampling.top_k, sampling.top_p, random, sampler_scratch); const int32_t second = sample(candidates, 1.0F, 30, 0.95F, random, sampler_scratch); const int32_t first_token = first == 0 ? assets_->eos : assets_->tower.semantic_begin + first - 1; @@ -416,6 +549,11 @@ Drama3Generation Drama3Generator::generate(const std::string & text, const std:: } component_ms[6] += debug::elapsed_ms(tower_started) - (component_ms[5] - sampling_before_tower); output_frames.push_back(next); + silent_run = clamp.codes.count(next[1]) ? silent_run + 1 : 0; + if (offline_stream_decode) { + offline_pending.push_back(next); + if (static_cast(offline_pending.size()) >= std::max(1, codec_batch_frames_)) flush_offline(); + } if (step == 0) mark_phase("tower_first_frame_ms"); recent.push_back(next[0]); if (recent.size() > 16) recent.pop_front(); @@ -470,7 +608,12 @@ Drama3Generation Drama3Generator::generate(const std::string & text, const std:: backbone_->release_prefill_graphs(); tower_->release_prefill_graphs(); } - result.audio = codec_->decode_codes(result.codes); + if (offline_stream_decode) { + flush_offline(); + codec_->reset_decode_stream(); + } else { + result.audio = codec_->decode_codes(result.codes); + } codec_->release_runtime_graphs(); component_ms[8] += debug::elapsed_ms(component_started); } diff --git a/src/community_models/fish_drama3/session.cpp b/src/community_models/fish_drama3/session.cpp index bf3203ca..0e0b5e9e 100644 --- a/src/community_models/fish_drama3/session.cpp +++ b/src/community_models/fish_drama3/session.cpp @@ -138,6 +138,10 @@ runtime::TaskResult Drama3Session::synthesize(const runtime::TaskRequest & reque sampling.top_p = runtime::parse_finite_float_option(effective.options, {"top_p"}).value_or(0.9F); sampling.top_k = runtime::parse_int_option(effective.options, {"top_k"}).value_or(4097); sampling.max_frames = runtime::parse_positive_i64_option(effective.options, {"max_tokens", "max_new_tokens"}, 1024); + if (const auto v = runtime::parse_int_option(effective.options, {"max_pause_frames"})) sampling.max_pause_frames = *v; + if (const auto v = runtime::find_option(effective.options, {"silence_codes"})) sampling.silence_codes = *v; + if (const auto v = runtime::parse_finite_float_option(effective.options, {"pause_end_ratio"})) sampling.pause_end_ratio = *v; + if (const auto v = runtime::parse_finite_float_option(effective.options, {"pause_cap_ratio"})) sampling.pause_cap_ratio = *v; seed = runtime::parse_u32_option(effective.options, {"seed"}); } catch (const std::runtime_error & error) { throw runtime::InvalidRequestError(error.what());