cosyvoice.cpp icon indicating copy to clipboard operation
cosyvoice.cpp copied to clipboard

运行bug

Open mcmingchang opened this issue 3 months ago • 4 comments

(py312) ligong@ligong-System-Product-Name:~/chenmc/cosyvoice.cpp$ ./build-cuda/bin/cosyvoice-server --model CosyVoice3-2512_Q8_0.gguf --voice-prompt alloy=1.gguf --host 0.0.0.0 --port 8080 --backend cuda0 --stream --chunk-tokens 200 [KVDBG] LLM kv_cache raw=0x800008C2 separate=1 k=2 v=6 fallback=2 fattn=1 allow_fallback=1 [KVDBG] DIT kv_cache raw=0x5481AC01 separate=0 k=1 v=0 fallback=11 fattn=1 allow_fallback=0 [KVDBG] cosyvoice_kv_cache_type_to_ggml: bad type=1417784321 (0x5481AC01) /home/ligong/chenmc/cosyvoice.cpp/src/cosyvoice-loader.cpp:483: unexpected kv cache type [New LWP 62851] [New LWP 62852] [New LWP 62859] [New LWP 62860] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". 0x00007f4c546ea3ef in __GI___wait4 (pid=62861, stat_loc=0x0, options=0, usage=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30 30 ../sysdeps/unix/sysv/linux/wait4.c: No such file or directory. #0 0x00007f4c546ea3ef in __GI___wait4 (pid=62861, stat_loc=0x0, options=0, usage=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30 30 in ../sysdeps/unix/sysv/linux/wait4.c #1 0x00007f4c54d4e683 in ggml_print_backtrace () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libggml-base.so.0 #2 0x00007f4c54d4e823 in ggml_abort () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libggml-base.so.0 #3 0x00007f4c54ee7972 in cosyvoice_kv_cache_type_to_ggml(cosyvoice_kv_cache_type) [clone .part.0] () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so #4 0x00007f4c54ee8570 in cosyvoice_check_kv_cache_types(ggml_context*, ggml_backend*, bool&, int, int, Linear const&, Linear const&, Linear const&, bool, kv_cache_type_union&) () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so #5 0x00007f4c54eef763 in cosyvoice_model_3::load(gguf_loader&) () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so #6 0x00007f4c54f23053 in cosyvoice_load_ext () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so #7 0x00007f4c54f232b0 in cosyvoice_load_from_file_ext () from /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so #8 0x0000599845393737 in build_runtime(server_options const&, server_runtime*) () #9 0x0000599845396cf0 in tool_entry(int, char**) () #10 0x00007f4c54629d90 in __libc_start_call_main (main=main@entry=0x5998453918a0

, argc=argc@entry=14, argv=argv@entry=0x7fff1e76db28) at ../sysdeps/nptl/libc_start_call_main.h:58 58 ../sysdeps/nptl/libc_start_call_main.h: No such file or directory. #11 0x00007f4c54629e40 in __libc_start_main_impl (main=0x5998453918a0
, argc=14, argv=0x7fff1e76db28, init=, fini=, rtld_fini=, stack_end=0x7fff1e76db18) at ../csu/libc-start.c:392 392 ../csu/libc-start.c: No such file or directory. #12 0x00005998453918d5 in _start () [Inferior 1 (process 62850) detached]

======================================== FATAL: Caught signal 6 (SIGABRT) Backtrace (16 frames):

./build-cuda/bin/cosyvoice-server(+0xf9ee)[0x5998453919ee] /lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7f4c54642520] /lib/x86_64-linux-gnu/libc.so.6(pthread_kill+0x12c)[0x7f4c546969bc] /lib/x86_64-linux-gnu/libc.so.6(raise+0x16)[0x7f4c54642476] /lib/x86_64-linux-gnu/libc.so.6(abort+0xd3)[0x7f4c546287f3] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libggml-base.so.0(ggml_abort+0x12e)[0x7f4c54d4e7fe] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so(+0xe7972)[0x7f4c54ee7972] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so(+0xe8570)[0x7f4c54ee8570] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so(+0xef763)[0x7f4c54eef763] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so(cosyvoice_load_ext+0x213)[0x7f4c54f23053] /home/ligong/chenmc/cosyvoice.cpp/build-cuda/bin/../lib/libcosyvoice.so(cosyvoice_load_from_file_ext+0x60)[0x7f4c54f232b0] ./build-cuda/bin/cosyvoice-server(+0x11737)[0x599845393737] ./build-cuda/bin/cosyvoice-server(+0x14cf0)[0x599845396cf0] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7f4c54629d90] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7f4c54629e40] ./build-cuda/bin/cosyvoice-server(+0xf8d5)[0x5998453918d5]

Aborted (core dumped) (py312) ligong@ligong-System-Product-Name:~/chenmc/cosyvoice.cpp$

我直接下拉合并主分支的代码,然后发现v100和4090都是一样的报错,运行命令添加--llm-kv-cache-type f32 --dit-kv-cache-type f32 这样可以成功运行推理,但是推理完后会崩掉

mcmingchang avatar Aug 04 '26 02:08 mcmingchang

我在主线提交了两个补丁,你拉取 build 试试

Lourdle avatar Aug 04 '26 06:08 Lourdle

能运行推理,但是无论是长音频还是短音频都在播放完成后云端就崩掉了,关闭流式也是一样崩溃,我ds解决不了,我把问题贴上来

[Bug] Server SIGSEGV after a successful streaming TTS request (heap corruption in the TTS core)

English summary

RTX 4090 + Ubuntu 24.04 + gcc-13 + CUDA 13.3 + ggml 0.18.0. A streaming request (/v1/audio/speech with --stream --chunk-tokens 200) completes and the client receives the full audio, but the server then crashes with SIGSEGV while destroying the HTTP response: the heap-allocated content-provider lambda inside httplib::Response is overwritten during inference (its inner std::function _M_manager becomes an unmapped address 0x5556efd0), so the destructor jumps to invalid code. Valgrind reports nothing; ASan cannot start on this kernel (vm.mmap_rnd_bits=32 + ASLR shadow conflict). Two related bugs were found and fixed locally (not yet in a PR): a GCC-only layout mismatch between the C++ params class and the C params struct, and a flash-attention KV-cache support probe that hardcodes F32 instead of probing the requested K/V types.


环境

  • 系统:Ubuntu 24.04(kernel 6.8+,vm.mmap_rnd_bits=32),gcc-13 / g++-13
  • GPU:RTX 4090(sm_89),CUDA 13.3
  • ggml:0.18.0(构建时从 GitHub 克隆的最新版,commit 78de606 附近)
  • 模型:CosyVoice3-2512_Q8_0.gguf
  • 服务:cosyvoice-server --model CosyVoice3-2512_Q8_0.gguf --voice-prompt alloy=1.gguf --host 0.0.0.0 --port 8080 --backend cuda0 --stream --chunk-tokens 200

复现步骤

  1. 按上述命令启动 server(-DCOSYVOICE_SERVER_NO_WEBUI=ON 构建)。
  2. 发送一次 /v1/audio/speech 请求(response_format: "pcm" 或 "wav",默认即可)。
  3. 请求正常完成:日志输出 [REQ] id=1 done status=ok http=200 ... note=stream_speech,客户端能收到完整音频。
  4. 请求结束后 server 立即段错误(SIGSEGV, core dumped)。

崩溃栈(带调试符号的 build-dbg)

Thread 6 "cosyvoice-serve" received signal SIGSEGV
#0  0x000000005556efd0 in ?? ()                      <- 跳转到未映射地址(损坏的 _M_manager)
#1  std::_Function_base::~_Function_base (this=0x555556547be0) std_function.h:244
#2  std::function<bool (unsigned long, httplib::DataSink&)>::~function() std_function.h:334
#3  httplib::detail::ContentProviderAdapter::~ContentProviderAdapter (this=0x555556547be0) httplib.h:8852
#4  std::_Function_base::_Base_manager<...ContentProviderAdapter>::_M_destroy
#5  ...::_M_manager
#6  std::_Function_handler<...>::_M_manager
#7  std::_Function_base::~_Function_base (this=0x7fffac64afd8)
#8  std::function<bool (unsigned long, unsigned long, httplib::DataSink&)>::~function()
#9  httplib::Response::~Response httplib.h:1458
#10 httplib::Server::process_request httplib.h:12665

崩溃时 rax = 0x5556efd0(被调用的“管理器”指针),但 this 指向的内存 0x555556547be0 处的内容是 0x7fffa42f28e0, 0, 0x5556efd0, 0 —— 已经不是合法的 std::function 布局(正常应为 [_M_manager=代码地址, _M_functor=堆指针, _M_invoker=代码地址])。 即:provider lambda / ContentProviderAdapter 的堆块在推理过程中被覆盖,析构时跳转 到无效地址崩溃。

排查过程与关键证据

  1. 崩溃地址每次运行完全一致(0x555556547be0、0x5556efd0,gdb 默认关闭 ASLR)—— 损坏值是确定性的,不是随机垃圾。
  2. valgrind memcheck:零报错、零崩溃(请求正常完成)—— 说明越界写落在了一个 “活的”相邻堆块上(memcheck 对“写进另一个合法块”不可见),或与线程调度/时序相关 (memcheck 不检测竞态)。
  3. ASan 无法启动:Shadow memory range interleaves with an existing memory mapping。 这是 Ubuntu 24.04 内核高 ASLR 熵(vm.mmap_rnd_bits=32)与 ASAN 影子内存冲突的已知 问题(google/sanitizers#856)。本机无 root 权限改 vm.mmap_rnd_bits,-no-pie + setarch -R 也未能解决(共享库仍随机映射进影子区间)。
  4. 硬件 watchpoint(gdb)未能捕获写入者(观察点成功设置但从未触发,环境可能不支持)。
  5. 非流式路径(无 --stream)也复现同样的 Response 析构崩溃(经确认仍走 chunked provider 路径),问题出在流式/分块 TTS 的核心路径。
  6. 上游最近两个提交(5eef380 零初始化 worker 内存、129845d server 初始化 dit_allow_kv_cache_fallback)已合并测试,崩溃依旧。

已发现并修复的问题(本地修复,尚未提交 PR,也未合入 main)

1. GCC 下 C++ 参数类与 C 结构体布局不一致(会导致启动期 "unexpected kv cache type")

include/cosyvoice.h 中:

  • cosyvoice_context_params_v3_t(C 结构体):dit_kv_cache_type 偏移 0x38,sizeof 80
  • cosyvoice_context_params_v3_cpp(C++ 继承类):GCC 下 dit_kv_cache_type 偏移 0x34, sizeof 72(MSVC 下两者一致,均为 0x38/80)

原因:GCC 在派生类中复用了基类的尾部填充(C++ 标准允许),而 MSVC 会先补齐基类。 cosyvoice_load_ext 里 reinterpret_cast<const cosyvoice_context_params_v3_t*>(params) 按 C 结构偏移读,server 却按 C++ 类偏移写 → DIT 的 kv cache 字段全部错位 4 字节 (读到的 pv3_dit 低字节恒为 allow=1 等),直接触发 GGML_ABORT("unexpected kv cache type")。

mcmingchang avatar Aug 04 '26 06:08 mcmingchang

修复:给 cosyvoice_context_params_v2_cpp 增加显式尾部填充字段,使 C++ 类与 C 结构 在所有编译器下布局一致:

struct cosyvoice_context_params_v2_cpp : cosyvoice_context_params_t
{
    uint32_t n_workers;
    uint32_t reserved_tail_padding;  // 显式填充,保证与 v2_t/v3_t 布局一致
};

建议:上游应考虑用 static_assert(sizeof/offsetof) 保护这些跨编译器布局假设, 或让 loader 直接按 C++ 类读取(不要 reinterpret_cast 到 C 结构)。

2. Flash Attention KV cache 支持性探测用 F32 硬编码(导致运行期 fattn.cu:574 abort)

src/cosyvoice-loader.cpp 的 cosyvoice_check_kv_cache_types 中 fattn_check lambda:

auto cached_k = ggml_new_tensor(ctx, GGML_TYPE_F32, GGML_MAX_DIMS, k->ne);  // 原来硬编码 F32
auto cached_v = ggml_new_tensor(ctx, GGML_TYPE_F32, GGML_MAX_DIMS, v->ne);

探测永远按 F32 验证,导致 fallback 逻辑永不触发。默认 K=Q8_0/V=Q4_0(类型不同)在 ggml CUDA flash attention 下没有内核(未开 GGML_CUDA_FA_ALL_QUANTS 时 K/V 必须同类型), 运行时直接 vendor/ggml/src/ggml-cuda/fattn.cu:574: GGML_ABORT("fatal error")。

修复(已本地验证有效):

auto cached_k = ggml_new_tensor(ctx, check_k, GGML_MAX_DIMS, k->ne);
auto cached_v = ggml_new_tensor(ctx, check_v, GGML_MAX_DIMS, v->ne);

修复后 fallback 正确降级到 Q8_0/Q8_0(4090 上 VEC 内核支持),推理正常。

剩余问题(本次崩溃):请求成功完成后 server SIGSEGV

现象

流式请求完整跑完、音频正常、HTTP 200,随后在 Response 析构时崩溃(provider lambda 堆块被覆盖)。合并上游 memset 修复后依旧。

怀疑范围与可能原因(供作者排查)

损坏发生在推理过程中、写入了一个“活的”相邻堆块。结合流式分块路径,重点怀疑以下宿主 内存写入的字节计算:

  1. src/cosyvoice-token2wav.cpp 的 HiFT overlap / flow_cache 拷贝(约 394–431 行): ggml_backend_tensor_get_async(backend, feat, worker->flow_cache.data(), byte_offset, byte_size),其中 byte_size = frames_to_keep * feat->nb[1],而 flow_cache 容量为 frames_to_keep * feat->ne[0] * sizeof(float)。若 feat 的 nb[1] != ne[0]*type_size (非连续/带 padding/类型非 F32),会越界写宿主堆。 同区域 speech_feat->nb[2] - feat->nb[2] 的 set_async 也有类似假设。
  2. src/cosyvoice-kv-cache.cpp 的 offload/load(155–256 行):BALANCED 策略下每个 分块结束把 LLM KV cache 拷贝到宿主 std::vector<char>,字节数由 k->nb[1] * n_tokens 等计算;量化 K/V(Q8_0)下的行大小/对齐假设若与 ggml 实际 布局不符会越界。get_offloaded_kv_cache_struct_size 的柔性数组结构也值得复查。
  3. 未初始化/部分初始化的 worker 字段:上游已加 memset 零初始化,但建议复查 cosyvoice_worker_context 中所有 POD 字段(如 offset、chunk_size 等)在加载与 流式首块调用之间是否都被确定性赋值。
  4. 线程竞态:server 存在 httplib 线程、stop_thread_pool 轮询线程、OpenMP 线程。 valgrind 时序不同导致不复现,无法排除竞态。建议用 helgrind 或 TSAN 检查 cosyvoice_request_stop / connection_monitor / empty_buffer_cache 与推理线程的 并发访问。
  5. ggml 版本差异:本项目非 Metal 构建直接克隆 ggml 最新 master(0.18.0),若上游 实际按较旧的 ggml 开发/测试,建议在非 Metal 构建也固定一个已知可用的 ggml commit, 避免行为漂移(本次已出现一次 ggml 0.18.0 的 flash attention 行为差异)。

调试环境限制(供参考)

  • ASan 无法运行(内核 ASLR 与影子内存冲突,且无 root)。
  • valgrind memcheck 对“写进合法相邻块”和竞态不可见。
  • gdb 硬件 watchpoint 未触发。
  • 因此本环境只能靠代码审查 + 确定性崩溃复现(gdb 关 ASLR 后每次地址一致)。

对作者的请求

  1. 请重点检查上面“怀疑范围”第 1、2 点的宿主缓冲字节计算(flow_cache、 KV cache offload/load)。
  2. 建议为 cosyvoice_context_params_v3_cpp 与 cosyvoice_context_params_v3_t 增加 布局一致性保护(static_assert),并在 Linux GCC 上回归测试(MSVC 无此问题)。
  3. 建议修复 fattn_check 用真实 K/V 类型探测(否则默认配置在多数 CUDA 卡上 运行期 abort)。
  4. 建议非 Metal 构建固定 ggml 版本或增加版本兼容性检查。

(本地两个修复 + 上游 5eef380/129845d 均已在此环境验证过;剩余崩溃仍可稳定复现, 欢迎作者协助定位。)

mcmingchang avatar Aug 04 '26 06:08 mcmingchang

@mcmingchang 别的问题我修复了,server 崩溃的问题我在 WSL2 上没复现出来。我看 issue 关了,是现在没问题了吗?

Lourdle avatar Aug 05 '26 02:08 Lourdle