Skip to content
Merged
61 changes: 46 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -551,21 +551,6 @@ if(ONEBIT_HRX)
# hrx skips without a gfx1151 (ONEBIT_HRX_E2E_TARGETS)
set_tests_properties(serve_e2e_${_dev} PROPERTIES SKIP_RETURN_CODE 77)
endforeach()
# engine#1779 fault 1 (FLM 1.0.12: "a server that stopped accepting connections after clients hung up"): a
# client that disconnects mid-response, repeated, then a fresh client must still be served. The assertion is
# about the LISTENER, so an interrupted request is allowed to get nothing. `cpu` on purpose: the fault is
# server logic, and the issue's rule is that these four are tested without a device where possible.
add_test(NAME serve_client_hangup_cpu
COMMAND ${CMAKE_SOURCE_DIR}/tests/serve_client_hangup_test.sh $<TARGET_FILE:onebit>
${ONEBIT_SERVE_TEST_GGUF} cpu)
set_tests_properties(serve_client_hangup_cpu PROPERTIES SKIP_RETURN_CODE 77)
# engine#1779 fault 2 (FLM 1.0.12: "Qwen3.5 answering `//////` after hitting the context limit"): a prompt that
# fills the context is served, the next turn cannot fit, and the answer must be a refusal or a truncated-but-clean
# reply - never one character repeated. Also `cpu`: the fault is in the request path's clamp, not in a kernel.
add_test(NAME serve_context_limit_cpu
COMMAND ${CMAKE_SOURCE_DIR}/tests/serve_context_limit_test.sh $<TARGET_FILE:onebit>
${ONEBIT_SERVE_TEST_GGUF} cpu)
set_tests_properties(serve_context_limit_cpu PROPERTIES SKIP_RETURN_CODE 77)
# Placement must not change the answer (engine #674): the same greedy prompt through the CPU, through the device
# with every layer assigned to the CPU, and fully offloaded, compared byte for byte.
add_test(NAME hrx_cpu_weight_parity
Expand All @@ -581,8 +566,40 @@ if(ONEBIT_HRX)
COMMAND ${CMAKE_SOURCE_DIR}/tests/hybrid_kvshare_e2e.sh "${ONEBIT_HRX_SERVER}" "${ONEBIT_SERVE_TEST_GGUF}" "${ONEBIT_KVSHARE_PREFILL}")
set_tests_properties(hybrid_kvshare_e2e PROPERTIES SKIP_RETURN_CODE 77)
endif()

endif()

# engine#1779: these two are registered OUTSIDE both `if(ONEBIT_SERVE_TEST_GGUF)` and `if(ONEBIT_HRX)` on purpose.
# Inside the GGUF guard they did not exist in CI at all (a configure-time cache path CI never sets: the build job
# runs 164 tests and none of the serve_* family is among them, so a gap in the family looked like coverage), and
# inside the HRX guard they would only exist for an HRX build while ci.yml configures without ONEBIT_HRX. They skip
# (77) when the model path is empty, which is a visible skip in the CI table, not an absent test. `onebit` is an
# unconditional target (CMakeLists.txt:97), so $<TARGET_FILE:onebit> resolves in any configuration.
# engine#1779 fault 1 (FLM 1.0.12: "a server that stopped accepting connections after clients hung up"): a
# client that disconnects mid-response, repeated, then a fresh client must still be served. The assertion is
# about the LISTENER, so an interrupted request is allowed to get nothing. `cpu` on purpose: the fault is
# server logic, and the issue's rule is that these four are tested without a device where possible.
add_test(NAME serve_client_hangup_cpu
COMMAND ${CMAKE_SOURCE_DIR}/tests/serve_client_hangup_test.sh $<TARGET_FILE:onebit>
"${ONEBIT_SERVE_TEST_GGUF}" cpu)
set_tests_properties(serve_client_hangup_cpu PROPERTIES SKIP_RETURN_CODE 77 RESOURCE_LOCK route_ports)
# engine#1779 fault 2 (FLM 1.0.12: "Qwen3.5 answering `//////` after hitting the context limit"): a prompt that
# fills the context is served, the next turn cannot fit, and the answer must be a refusal or a truncated-but-clean
# reply - never one character repeated. Also `cpu`: the fault is in the request path's clamp, not in a kernel.
add_test(NAME serve_context_limit_cpu
COMMAND ${CMAKE_SOURCE_DIR}/tests/serve_context_limit_test.sh $<TARGET_FILE:onebit>
"${ONEBIT_SERVE_TEST_GGUF}" cpu)
set_tests_properties(serve_context_limit_cpu PROPERTIES SKIP_RETURN_CODE 77 RESOURCE_LOCK route_ports)
# engine#1779 fault 3 ("wrong vectors from embed-gemma"): tested on the route where OUR code computes the vector -
# `serve.cpp:2264 if (embed_serve::container(model))` serves a container directory in-process - not on the
# `rag_launch()` proxy, where the numbers come from a delegated llama-server. Same registration rule as the two
# above (outside both guards, skip 77 when unset); ONEBIT_EMBED_TEST_CONTAINER names a directory holding model.embed.
set(ONEBIT_EMBED_TEST_CONTAINER "" CACHE PATH "1-bit encoder container (.embed dir) the embed-vector test loads")
add_test(NAME serve_embed_vectors_cpu
COMMAND ${CMAKE_SOURCE_DIR}/tests/serve_embed_vectors_test.sh $<TARGET_FILE:onebit>
"${ONEBIT_EMBED_TEST_CONTAINER}" cpu)
set_tests_properties(serve_embed_vectors_cpu PROPERTIES SKIP_RETURN_CODE 77 RESOURCE_LOCK route_ports)

# ── DwarfStar is gone (owner 2026-11-01, removed 2026-11-02) ───────────────
# `--device ds4` ran DwarfStar (third_party/ds4: antirez's CUDA kernels behind a HIP shim plus
# hipBLAS), and its ds4-server was the engine's only 1BP reader. It is out of the shipped builds
Expand Down Expand Up @@ -1315,6 +1332,20 @@ add_executable(npu_design_grid_test tests/npu_design_grid_test.c npu/forward/src
npu/forward/src/q4nx_pack_blocked.cpp npu/forward/src/q4nx_head_image.c)
target_include_directories(npu_design_grid_test PRIVATE npu/forward/include)
target_link_libraries(npu_design_grid_test PRIVATE m)
# engine#1779 fault 4 ("Gemma 4 tool-call parsing"): the answer parser is header-only (app/npu_answer_parser.h), so
# this is a PURE unit test - no server, no model, no device - of the contract that header states: a template's own
# <tool_call>{"name":...,"arguments":{...}}</tool_call> becomes a structured call with the arguments re-dumped as JSON
# text; a call-only reply reports content_null; WITHOUT tools declared the marker stays text in content; and a
# malformed call body is never dropped. The issue asked for name, arguments and a non-tool reply that must not be
# parsed as one; the last two arms are the loss-proofing the header promises at :193 and :216-221.
add_executable(npu_answer_parser_test tests/npu_answer_parser_test.cpp)
target_include_directories(npu_answer_parser_test PRIVATE app)
target_link_libraries(npu_answer_parser_test PRIVATE nlohmann_json::nlohmann_json)
add_test(NAME npu_answer_parser COMMAND npu_answer_parser_test)
# direction B (tools/ci/ci_targets_check.py, engine#1595): a target no ci.yml line names is "never built", and its test
# then reports "Not Run" instead of running. `ci.yml` is a workflow file no lane can patch, so the documented hook is the
# one this tree already uses for onebit_npu_forward_syntax: add_dependencies(onebit <target>), which the checker follows.
add_dependencies(onebit npu_answer_parser_test)
# engine#1781's oracle (merged in #1813) arrived with its test file but no registration, so the comparator behind
# NPU_BATCH_CHECK -- "every drafted position's logits, bitwise" instead of one argmax at the last row -- was neither
# built nor run while every job stayed green. Header-only, so the test needs no link libraries; host-only, so it needs no
Expand Down
11 changes: 10 additions & 1 deletion app/embed_serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ int serve_only(const std::string& model_dir, const std::string& host, int port,
};
srv.Post("/v1/embeddings", handler);
srv.Post("/embeddings", handler);
if (!srv.bind_to_port(host, port)) {
// bind_to_port() answers bool; the port comes back from bind_to_any_port(), which is the same bind_internal(host, 0)
// with the chosen port returned. Same idiom as app/serve.cpp:2023 and :2694 - a caller that asked for port 0 has no
// other way to learn which one it got.
const int bound_port = port == 0 ? srv.bind_to_any_port(host) : (srv.bind_to_port(host, port) ? port : -1);
if (bound_port < 0) {
std::fprintf(stderr, "1bit serve: cannot listen on %s:%d\n", host.c_str(), port);
return 2;
}
Expand All @@ -195,6 +199,11 @@ int serve_only(const std::string& model_dir, const std::string& host, int port,
std::thread listener([&] { srv.listen_after_bind(); });
std::fprintf(stderr, "1bit serve: %s on %s embedding (%s), in-process: /v1/embeddings, dim %d, %d-token cap\n",
id.c_str(), backend.device().c_str(), model_dir.c_str(), backend.dim(), backend.max_tokens());
// engine#1779: the readiness line every other serve route prints, and the one `serve_port_from_log()`
// (tests/serve_port.sh) reads back. The in-process embedding route returned before either of the other two
// "ready on" sites (serve.cpp:2050, :2765), so a caller that asked for --port 0 could not discover the port it was
// listening on - which is what engine #1485 fixed for the other routes and left true only for them.
std::fprintf(stderr, "1bit serve: ready on http://%s:%d\n", host.c_str(), bound_port);
listener.join();
return 0;
}
Expand Down
134 changes: 134 additions & 0 deletions tests/npu_answer_parser_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2026 bong-water-water-bong
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2026 The 1bit-MONSTER Authors.
// SPDX-License-Identifier: Apache-2.0
//
// npu_answer_parser_test -- engine#1779 fault 4: "Gemma 4 tool-call parsing".
//
// The issue's test: "a template's own tool-call emission parsed into a structured call: name, arguments, and a
// non-tool reply that must not be parsed as one". The parser is header-only (app/npu_answer_parser.h), so this is a
// pure unit test: no server, no model, no device, and it runs on any box.
//
// The arms below are the contract that header states rather than just the happy path:
// * a `<tool_call>{"name":…,"arguments":{…}}</tool_call>` block becomes a ToolCall with the arguments re-dumped as
// JSON text (footer note at :49 says `function.arguments` is a string in OpenAI's shape);
// * a call-only reply sets content_null, an answer with prose does not (:279-281);
// * WITHOUT tools declared the marker is text and stays in content, marker and all (:193 "Without tools the tag is
// just text: keep it in content, marker and all") - that is the strongest form of "a non-tool reply must not be
// parsed as one";
// * a malformed call body is not dropped: it goes back into content with its markup (:216-221), because what the
// model wrote must never be lost.

#include <cstdio>
#include <string>

#include "npu_answer_parser.h"

namespace {

int failures = 0;

void check(bool ok, const std::string& what) {
std::printf("%s %s\n", ok ? "ok " : "FAIL", what.c_str());
if (!ok) failures++;
}

bool contains(const std::string& hay, const std::string& needle) {
return hay.find(needle) != std::string::npos;
}

} // namespace

int main() {
using namespace onebit::npu;

// 1. A template's own emission, with reasoning and prose around it.
{
const auto a = parse_answer(
"<think>I should check the weather first.</think>Let me look.<tool_call>{\"name\":\"get_weather\","
"\"arguments\":{\"city\":\"Paris\"}}</tool_call>",
AnswerFamily::qwen3, true);
check(a.calls.size() == 1, "a tool_call block becomes exactly one call");
if (a.calls.size() == 1) {
check(a.calls[0].name == "get_weather", "the call keeps its name");
check(contains(a.calls[0].arguments, "Paris"), "the call keeps its arguments as JSON text");
check(a.calls[0].arguments.size() > 2 && a.calls[0].arguments.front() == '{',
"arguments is a JSON object string, not a quoted blob");
check(!a.calls[0].id.empty(), "the call carries an id");
}
check(a.reasoning == "I should check the weather first.", "<think> goes to reasoning");
check(a.content == "Let me look.", "prose outside the markup goes to content");
check(!a.content_null, "a reply with prose does not report null content");
}

// 2. Call-only: content: null, not "" (the issue's second rule).
{
const auto a = parse_answer("<tool_call>{\"name\":\"ping\",\"arguments\":{}}</tool_call>", AnswerFamily::qwen3, true);
check(a.calls.size() == 1, "a call-only reply still yields the call");
check(a.content.empty(), "a call-only reply has empty content");
check(a.content_null, "a call-only reply reports content_null");
}

// 3. A non-tool reply, tools declared: prose must not become a call.
{
const auto a = parse_answer("The weather in Paris is mild and dry today.", AnswerFamily::qwen3, true);
check(a.calls.empty(), "a plain answer produces no calls");
check(a.content == "The weather in Paris is mild and dry today.", "a plain answer is all content");
}

// 4. The marker with NO tools declared is text, marker and all - the strongest "not parsed as one" case.
{
const std::string text = "Use <tool_call>{\"name\":\"x\",\"arguments\":{}}</tool_call> to emit a call.";
const auto a = parse_answer(text, AnswerFamily::qwen3, false);
check(a.calls.empty(), "without tools declared, tool_call markup is not parsed into a call");
check(contains(a.content, "<tool_call>"), "without tools, the marker stays visible in content");
check(contains(a.content, "to emit a call."), "without tools, the rest of the text is kept");
}

// 5. Malformed call body: never dropped, goes back into content with its markup.
{
const auto a = parse_answer("x<tool_call>not json at all</tool_call>", AnswerFamily::qwen3, true);
check(a.calls.empty(), "a malformed call body produces no call");
check(contains(a.content, "<tool_call>") && contains(a.content, "not json at all"),
"a malformed call body is kept in content, markup and all (nothing is lost)");
}

// 6. Two calls in one reply, in order.
{
const auto a = parse_answer(
"<tool_call>{\"name\":\"first\",\"arguments\":{\"n\":1}}</tool_call>"
"<tool_call>{\"name\":\"second\",\"arguments\":{\"n\":2}}</tool_call>",
AnswerFamily::qwen3, true);
check(a.calls.size() == 2, "two blocks become two calls");
if (a.calls.size() == 2) {
check(a.calls[0].name == "first" && a.calls[1].name == "second", "calls keep their order");
check(a.calls[0].id != a.calls[1].id, "each call gets its own id");
}
check(a.content_null, "a two-call reply with no prose reports content_null");
}

// 7. The family tag set is complete for the one family the header declares, so an unsupported family cannot be
// silently parsed as qwen3.
{
const auto tags = onebit::npu::answer_detail::tags_of(AnswerFamily::qwen3);
check(std::string(tags.call_open) == "<tool_call>" && std::string(tags.call_close) == "</tool_call>",
"the qwen3 family's call tags are the ones this test feeds");
check(std::string(tags.think_open) == "<think>" && std::string(tags.think_close) == "</think>",
"the qwen3 family's reasoning tags are the ones this test feeds");
}

std::printf("%s\n", failures ? "FAILED" : "all cases ok");
return failures ? 1 : 0;
}
3 changes: 2 additions & 1 deletion tests/serve_client_hangup_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ bin=${1:?usage: serve_client_hangup_test.sh path/to/1bit <model> <device> [round
here=$(cd "$(dirname "$0")/.." && pwd)
# shellcheck source=tests/serve_port.sh
. "$(dirname "$0")/serve_port.sh" # serve_port_wait: read back the port serve bound (engine #1485)
model=${2:?}
model=${2:-} # empty in CI unless ONEBIT_SERVE_TEST_GGUF is set: that is a SKIP, not a failure
device=${3:?}
rounds=${4:-6}
[ "$rounds" -ge 1 ] 2>/dev/null || rounds=6
shift 3
[ $# -gt 0 ] && [ "${1#-}" = "$1" ] 2>/dev/null && shift # the optional rounds argument, when given

[ -n "$model" ] || { echo "skip: no model given (set ONEBIT_SERVE_TEST_GGUF - engine#1779: this test is registered in CI and must show as a skip, not as an absent test)"; exit 77; }
[ -x "$bin" ] || { echo "skip: no 1bit at $bin"; exit 77; }
[ -f "$model" ] || { echo "skip: no model at $model"; exit 77; }
command -v curl > /dev/null || { echo "skip: no curl"; exit 77; }
Expand Down
15 changes: 13 additions & 2 deletions tests/serve_context_limit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ try:
except Exception:
text, finish = body.strip(), None
core = text.strip()
if re.fullmatch(r"(.)\1{7,}", core, re.S):
packed = re.sub(r"\s+", "", core)
if len(packed) >= 6 and len(set(packed)) == 1:
# engine#1791's fix, which main's version did not carry: the reported symptom is "//////" (SIX), and the regex
# above needed eight, so a server answering exactly the reported string classified as 'clean'. One character
# repeated >= 6 (whitespace stripped, so "////// //////" is caught too) is the rule; six rather than four because
# a real completion can legitimately be a short repeated token.
print("degenerate")
elif core == "" and finish in ("length", None):
print("clean-empty")
Expand All @@ -96,6 +101,9 @@ self_test() {
local tmp fails=0 got
tmp=$(mktemp -d) || { echo "FAIL self_test: mktemp -d"; return 1; }
printf '%s' '{"choices":[{"message":{"content":"////////////////"},"finish_reason":"stop"}]}' > "$tmp/degenerate.json"
# the EXACT reported symptom is six slashes, not sixteen: a detector with an eight-character floor passed this string
# (engine#1791's first version) and only a six-character case proves the fix covers what was filed (engine#1779).
printf '%s' '{"choices":[{"message":{"content":"//////"},"finish_reason":"stop"}]}' > "$tmp/degenerate6.json"
printf '%s' '{"choices":[{"message":{"content":"The answer is 4."},"finish_reason":"stop"}]}' > "$tmp/clean.json"
printf '%s' '{"choices":[{"message":{"content":""},"finish_reason":"length"}]}' > "$tmp/empty.json"
printf '%s' '{"error":{"message":"context length exceeded"}}' > "$tmp/err.json"
Expand Down Expand Up @@ -130,6 +138,8 @@ self_test() {
expect_pass "an empty answer with finish_reason length" clean-empty yes
# and the fault itself still has to fail
expect "the reported degenerate answer" 200 "$tmp/degenerate.json" degenerate
expect "the reported answer at its real length (six)" 200 "$tmp/degenerate6.json" degenerate
expect_pass "the reported answer at its real length (six)" degenerate no
expect_pass "the reported degenerate answer" degenerate no
# neither outcome: say so rather than pass
expect "a redirect nobody follows" 302 "$tmp/none.body" unclassified-302
Expand All @@ -148,14 +158,15 @@ if [ "${1:-}" = "--self-test" ]; then
self_test
exit $?
fi
model=${2:?}
model=${2:-} # empty in CI unless ONEBIT_SERVE_TEST_GGUF is set: that is a SKIP, not a failure
device=${3:?}
ctx=${4:-512}
[ "$ctx" -ge 64 ] 2>/dev/null || ctx=512
shift 3
if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then shift; fi # the optional ctx argument, when given

[ -x "$bin" ] || { echo "skip: no 1bit at $bin"; exit 77; }
[ -n "$model" ] || { echo "skip: no model given (set ONEBIT_SERVE_TEST_GGUF)"; exit 77; }
[ -f "$model" ] || { echo "skip: no model at $model"; exit 77; }
command -v curl > /dev/null || { echo "skip: no curl"; exit 77; }
command -v python3 > /dev/null || { echo "skip: no python3"; exit 77; }
Expand Down
Loading
Loading