flm serve: /v1/completions and other routes run outside the NPU queue and race with model swaps

未关闭
#759 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
cpp

调研方向

Start in src/server/server.cpp at requires_npu_access(), then trace the route dispatch in src/src/main.cpp and the handlers in src/server/rest_handler.cpp, especially completion, embeddings, model loading, and /api/ps. Confirm which engine accesses and model-tag reads can overlap, then define done as serialized NPU work and a synchronized /api/ps view without changing unrelated routes.

由索引模型根据 Issue 内容生成。

描述

Everything below comes from reading the source on main at 39ff855. None of it has been measured.

What looks wrong

requires_npu_access() decides which requests go through the single NPU queue (server.cpp:160-170). It lists /api/generate, /api/chat, /v1/chat/completions, /v1/audio/transcriptions and /v1/embeddings. Every other route runs directly on whichever of the 10 I/O threads accepted it (main.cpp:624), concurrently with whatever holds the NPU. Three of those routes touch state that the queued routes change:

  1. POST /v1/completions is missing from the list, but it does inference. handle_openai_completion calls ensure_model_loaded(), insert() and generate() on the shared auto_chat_engine (rest_handler.cpp:1423-1490). A /v1/completions request that arrives while a /v1/chat/completions request is decoding would therefore run on the same engine and the same NPU at the same time. If it names a different model, it would reset the engine that the other request is using (:385-388).

  2. POST /api/embeddings is also missing, while its twin /v1/embeddings is listed. Both call the same handle_embeddings, which runs the embedding model on the NPU.

  3. GET /api/ps reads current_model_tag (:1017-1022). Meanwhile, ensure_model_loaded assigns that std::string on a queued request's thread (:411, :418). A concurrent read of a std::string during assignment is a data race, and undefined behaviour in C++.

    We have measured one consequence on our patched build. The tag stays at the model-faker sentinel until a first load finishes. Polling /api/ps while that load ran hit the sentinel lookup (a separate bug: undefined behaviour in rectify_model_tag) on 19 of 23 polls.

Expected

Anything that uses or swaps auto_chat_engine or the embedding engine goes through the NPU queue. For a read-only route like /api/ps, a snapshot of (engine loaded, tag) published under a mutex, or atomically, would be enough.

Suggested fix
  • Add /v1/completions and /api/embeddings to requires_npu_access().
  • Give /api/ps a synchronised view of the loaded tag.

We have not written a patch for this yet. We found it while fixing /api/ps downstream. That fix (https://github.com/noamsto/nix-amd-ai/blob/46d7a73c1044435cec0ff08410e1a3bc87cbc544/pkgs/fastflowlm/patches/ps-loaded-models.patch) avoids the sentinel lookup, but it does not add synchronisation.

The heap-corruption reports in #608 involve concurrent requests. We haven't connected them to this, so this is only a pointer.

Environment

Found by reading the source; checked against main at 39ff855 and v1.0.6.

主要语言
C++
星标
2.1k
派生
184
平均合并
12 小时 22 分钟
30 天内合并 PR
239

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ROCm/FastFlowLM 的其他 Issue

查看 ROCm/FastFlowLM 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。