Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70702db389 | ||
|
|
89f653e249 | ||
|
|
13b8eeb64e | ||
|
|
9fb9968abd | ||
|
|
570b5eb6c4 | ||
|
|
d9dc03abdb | ||
|
|
6afd3ff4d8 |
No files matched your search
@@ -0,0 +1,16 @@
|
||||
# Keep the image to the source the build needs. third_party/llama.cpp (the
|
||||
# pinned submodule) is NOT ignored: make build compiles it.
|
||||
|
||||
# git metadata (the submodule's .git file points outside the context)
|
||||
.git
|
||||
**/.git
|
||||
|
||||
# cmake output from host builds
|
||||
build
|
||||
build/
|
||||
build*/
|
||||
|
||||
# not needed by the build
|
||||
docs
|
||||
data
|
||||
*.log
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
#
|
||||
# UnbiasedStrata on a Pascal card (Tesla P40 / GTX 10-series, sm_61).
|
||||
#
|
||||
# The engine and the C++ front end are compiled during docker build into one
|
||||
# binary, unbiased-strata, which serves the OpenAI-style API. Nothing is
|
||||
# downloaded at build or run time: the model shards, the pack and the draft
|
||||
# (MTP) head must be mounted. See README.md.
|
||||
#
|
||||
# Build:
|
||||
# docker build -t unbiased-strata:p40 .
|
||||
#
|
||||
# Run (host needs an NVIDIA driver and nvidia-container-toolkit):
|
||||
# docker run --rm --gpus '"device=2"' -p 8082:8080 \
|
||||
# -v /path/models:/models:ro -v /path/Strata-data:/strata:ro \
|
||||
# unbiased-strata:p40 --model /models/...-00001-of-00002.gguf \
|
||||
# --pack /strata/pack-iq3-xxs --mtp /strata/mtp \
|
||||
# --ctx 32768 --host 0.0.0.0 --port 8080
|
||||
#
|
||||
# PASCAL=1 compiles sm_61 and turns on the engine's STRATA_EXPERIMENTAL_SM60
|
||||
# path. It needs a CUDA 12.x toolkit (CUDA 13 dropped sm_60/sm_70); 12.8.1 is
|
||||
# the last 12.x release and still supports Pascal. Override CUDA_ARCHS to build
|
||||
# for another card, e.g. --build-arg CUDA_ARCHS=86 for an RTX 30.
|
||||
|
||||
FROM nvidia/cuda:12.8.1-devel-ubuntu24.04 AS build
|
||||
|
||||
ARG CUDA_ARCHS=61
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
build-essential cmake git ca-certificates \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
|
||||
# third_party/llama.cpp is the pinned submodule; the Makefile applies
|
||||
# patches/llama.cpp-qwen3-coder-any-argument-order.patch if it is not already.
|
||||
# Its .git pointer names a path outside the context, so drop it: git apply then
|
||||
# runs in no-repository mode, which is all the patch step needs.
|
||||
RUN rm -rf third_party/llama.cpp/.git \
|
||||
&& make build PASCAL=1 CUDA_ARCHS="${CUDA_ARCHS}"
|
||||
|
||||
|
||||
FROM nvidia/cuda:12.8.1-runtime-ubuntu24.04
|
||||
|
||||
# ca-certificates/curl are only for the healthcheck.
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates curl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=build /src/build/unbiased-strata /usr/local/bin/unbiased-strata
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
# /health answers 503 while the engine is still loading; the long start period
|
||||
# keeps a slow first load (the model is read into RAM and VRAM) from being
|
||||
# marked unhealthy and restarted mid-load.
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=1800s --retries=3 \
|
||||
CMD curl -fs http://127.0.0.1:8080/health || exit 1
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/unbiased-strata"]
|
||||
@@ -1,7 +1,24 @@
|
||||
# UnbiasedStrata build. Needs CMake 3.24+, a C++20 compiler and the CUDA toolkit. Nothing is downloaded.
|
||||
BUILD_DIR ?= build
|
||||
# Pascal/Volta are a community, experimental build: `make build PASCAL=1` compiles sm_61 (GTX 10-series,
|
||||
# Tesla P40; sm_60/P100 works too, override with CUDA_ARCHS=60) and turns on the engine's
|
||||
# STRATA_EXPERIMENTAL_SM60 path (see README.md and src/core/device.cu). It needs a CUDA 12.x toolkit:
|
||||
# CUDA 13 dropped sm_60/sm_70, so the check below fails early with the reason instead of inside nvcc.
|
||||
PASCAL ?=
|
||||
ifeq ($(PASCAL),1)
|
||||
CUDA_ARCHS ?= 61
|
||||
PASCAL_CMAKE_FLAGS := -DSTRATA_EXPERIMENTAL_SM60=ON
|
||||
NVCC ?= $(shell command -v nvcc 2>/dev/null)
|
||||
NVCC_MAJOR := $(shell $(NVCC) --version 2>/dev/null | sed -n 's/.*release \([0-9]*\).*/\1/p' | head -n1)
|
||||
ifneq ($(NVCC_MAJOR),)
|
||||
ifeq ($(shell [ $(NVCC_MAJOR) -ge 13 ] && echo yes),yes)
|
||||
$(error PASCAL=1 needs a CUDA 12.x toolkit; nvcc $(NVCC_MAJOR) dropped sm_60/sm_70)
|
||||
endif
|
||||
endif
|
||||
else
|
||||
# RTX 30 (86), RTX 40 (89) and RTX 50 (120) in one binary. sm_120 needs CUDA 12.8+, and upstream recommends 13.0+.
|
||||
CUDA_ARCHS ?= 86;89;120
|
||||
endif
|
||||
# capped: inside a container nproc reports the host's CPUs, not the container's quota
|
||||
JOBS ?= $(shell n=$$(nproc 2>/dev/null || echo 4); [ $$n -gt 16 ] && n=16; echo $$n)
|
||||
|
||||
@@ -13,7 +30,8 @@ build:
|
||||
@git -C third_party/llama.cpp apply --check --reverse ../../patches/llama.cpp-qwen3-coder-any-argument-order.patch 2>/dev/null || \
|
||||
git -C third_party/llama.cpp apply ../../patches/llama.cpp-qwen3-coder-any-argument-order.patch
|
||||
cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_CUDA=ON -DSTRATA_NATIVE_EXPERTS=ON \
|
||||
-DSTRATA_PORTABLE=ON -DSTRATA_BUILD_TESTS=OFF "-DCMAKE_CUDA_ARCHITECTURES=$(CUDA_ARCHS)"
|
||||
-DSTRATA_PORTABLE=ON -DSTRATA_BUILD_TESTS=OFF "-DCMAKE_CUDA_ARCHITECTURES=$(CUDA_ARCHS)" \
|
||||
$(PASCAL_CMAKE_FLAGS)
|
||||
cmake --build $(BUILD_DIR) -j $(JOBS) --target unbiased-strata
|
||||
@echo "built: $(BUILD_DIR)/unbiased-strata"
|
||||
|
||||
|
||||
@@ -26,6 +26,12 @@ ssh host 'cd UnbiasedStrata && make build' # produces build/unbiased-strata
|
||||
The tree is 6 MB plus the pinned llama.cpp submodule in `third_party/` (about 170 MB checked out). The
|
||||
build fetches nothing. `make build JOBS=N` sets the job count; `CUDA_ARCHS` defaults to `86;89;120`.
|
||||
|
||||
Pascal is an experimental, community build: `make build PASCAL=1` compiles sm_61 (GTX 10-series, Tesla P40;
|
||||
`CUDA_ARCHS=60` for P100) and turns on the engine's `STRATA_EXPERIMENTAL_SM60` path. It needs a CUDA 12.x
|
||||
toolkit (CUDA 13 dropped sm_60/sm_70) and is outside the validated configuration above: no tensor cores, so
|
||||
expect the portable FP32 attention path and much lower throughput, and a card under 24 GB leans on the expert
|
||||
cache.
|
||||
|
||||
## Run
|
||||
|
||||
```
|
||||
@@ -36,7 +42,8 @@ build fetches nothing. `make build JOBS=N` sets the job count; `CUDA_ARCHS` defa
|
||||
| | |
|
||||
| --- | --- |
|
||||
| Endpoints | `POST /v1/chat/completions` (streaming or not), `GET /v1/models`, `GET /health`, on `127.0.0.1:8080` |
|
||||
| Options | `--ctx N` (32768), `--threads N` (default: physical cores, or half the CPU quota in a container), `--host`, `--port`, `--mmproj PATH`, `--vision-tokens N` (1024); engine flags after `--` |
|
||||
| Options | `--ctx N` (32768), `--threads N` (default: physical cores, or half the CPU quota in a container), `--host`, `--port`, `--mmproj PATH`, `--vision-tokens N` (1024), `--fit-max-tokens` / `--no-fit-max-tokens` (default: on); engine flags after `--` |
|
||||
| Context | a request whose `max_tokens` would run past `--ctx` has it shortened to the room left; with `--no-fit-max-tokens` it is refused (400) instead |
|
||||
| Sampling | greedy unless `temperature` > 0 |
|
||||
| Reasoning | returned as `reasoning_content`; `"reasoning_effort": "none"` turns it off |
|
||||
| Tools | OpenAI format (`tools`, `tool_calls`) |
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
# p40.yml -- run the unbiased-strata container on a Pascal Tesla P40 (sm_61).
|
||||
#
|
||||
# The image is this repository's sm_61 build (make build PASCAL=1), the
|
||||
# experimental path for GTX 10-series / Tesla P40 / P100:
|
||||
# CUDA_ARCHS=61, STRATA_EXPERIMENTAL_SM60=ON.
|
||||
#
|
||||
# Validated on this machine (the P40 host that this example was written on):
|
||||
# GPUs: Tesla P40 24 GB x2 (one is used here), plus a GTX 1060 6 GB
|
||||
# CPU: AMD Ryzen 7 3700X (8 physical cores / 16 threads)
|
||||
# RAM/swap: 62 GiB / 63 GiB
|
||||
# OS: Debian GNU/Linux 13 (trixie)
|
||||
# Driver: NVIDIA 580.178.04
|
||||
# Docker: 26.1.5, Compose v2.26.1, NVIDIA Container Toolkit 1.20.1
|
||||
#
|
||||
# Data you must supply beside this file before `docker compose up`:
|
||||
# ./models/ the model's GGUF shards (shard 2 must sit next to shard 1)
|
||||
# ./strata-data/ the prepared pack and MTP directories, e.g.
|
||||
# strata-data/pack-iq3-xxs/
|
||||
# strata-data/mtp/
|
||||
#
|
||||
# Run with: docker compose -f examples/compose/p40.yml up -d
|
||||
#
|
||||
# Adjust to your host: the GPU device index (device_ids), the cpuset/cpus for
|
||||
# the CPU expert workers, the model/pack/mtp paths, and the published port.
|
||||
|
||||
services:
|
||||
p40:
|
||||
image: unbiased-strata:p40
|
||||
build:
|
||||
context: ../.. # the repository root (the Dockerfile lives there)
|
||||
dockerfile: Dockerfile
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "8082:8080"
|
||||
volumes:
|
||||
- ./models/:/models:ro
|
||||
- ./strata-data/:/strata:ro
|
||||
runtime: nvidia
|
||||
cpuset: 0-5
|
||||
cpus: 6
|
||||
deploy:
|
||||
resources:
|
||||
reservations:
|
||||
devices:
|
||||
- driver: nvidia
|
||||
device_ids: ["0"] # one Tesla P40: set this to your P40's index
|
||||
capabilities: [gpu]
|
||||
command:
|
||||
- --model
|
||||
- /models/ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF/IQ3_XXS/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf
|
||||
- --pack
|
||||
- /strata/pack-iq3-xxs
|
||||
- --mtp
|
||||
- /strata/mtp
|
||||
- --ctx
|
||||
- "262144"
|
||||
- --threads
|
||||
- "6"
|
||||
- --host
|
||||
- "0.0.0.0" # 127.0.0.1 is the default; 0.0.0.0 exposes the port
|
||||
- --port
|
||||
- "8080"
|
||||
@@ -66,7 +66,33 @@ int main(int argc, char** argv) {
|
||||
assert(parsed.tool_calls[0].name == "read");
|
||||
assert(parsed.tool_calls[0].arguments.find("\"path\":\"/tmp/a.txt\"") != std::string::npos);
|
||||
}
|
||||
// #123: reasoning_budget_tokens / reasoning_budget_message - the request's value wins over the command line's,
|
||||
// a bad value is refused, thinking off leaves no budget, and the template's end-of-thinking tags are captured.
|
||||
server.opt.reasoning_budget = 64;
|
||||
server.opt.reasoning_budget_message = "server default";
|
||||
Prepared budgeted;
|
||||
assert(server.prepare(request, budgeted, error));
|
||||
assert(budgeted.thinking);
|
||||
assert(budgeted.reasoning_budget == 64);
|
||||
assert(budgeted.reasoning_budget_message == "server default");
|
||||
assert(!budgeted.thinking_end_tags.empty());
|
||||
request["reasoning_budget_tokens"] = 128;
|
||||
request["reasoning_budget_message"] = "You exhausted your reasoning tokens, come to a conclusion, now!";
|
||||
assert(server.prepare(request, budgeted, error));
|
||||
assert(budgeted.reasoning_budget == 128);
|
||||
assert(budgeted.reasoning_budget_message == "You exhausted your reasoning tokens, come to a conclusion, now!");
|
||||
request.erase("reasoning_budget_message");
|
||||
request.erase("reasoning_budget_tokens");
|
||||
request["reasoning_budget_tokens"] = -1;
|
||||
assert(!server.prepare(request, budgeted, error));
|
||||
request["reasoning_budget_tokens"] = "lots";
|
||||
assert(!server.prepare(request, budgeted, error));
|
||||
request.erase("reasoning_budget_tokens");
|
||||
request["reasoning_effort"] = "none";
|
||||
assert(server.prepare(request, budgeted, error));
|
||||
assert(!budgeted.thinking && budgeted.reasoning_budget == 0);
|
||||
request.erase("reasoning_effort");
|
||||
llama_model_free(server.model);
|
||||
std::puts("PASS: omitted parallel_tool_calls uses the model capability; explicit false remains honored; "
|
||||
"tool arguments parse in any order");
|
||||
"tool arguments parse in any order; reasoning_budget_tokens and its message override the command line");
|
||||
}
|
||||
@@ -18,6 +18,7 @@ struct GenResult {
|
||||
std::string error;
|
||||
std::string finish = "length";
|
||||
long long produced = 0, prompt_n = 0, draft_accepted = 0, draft_offered = 0;
|
||||
long long reasoning_tokens = 0; // #123: tokens generated inside the thinking block (a budget wrap-up included)
|
||||
double prompt_ms = 0, decode_ms = 0;
|
||||
};
|
||||
|
||||
|
||||
+143
-16
@@ -41,6 +41,15 @@ namespace {
|
||||
|
||||
const char* const kModelName = "qwen3.8-flash-next-iq3_xxs";
|
||||
|
||||
// The engine's serve loop refuses a request unless prompt + max_new + this margin fits --ctx
|
||||
// (src/program/generate.cpp:6403): the last cells are the engine's own, not ours to hand out.
|
||||
constexpr long long kEngineReserve = 8;
|
||||
|
||||
// #123: what is injected before the thinking's end tag when reasoning_budget_tokens is reached. The wording is
|
||||
// switchable (--reasoning-budget-message, or the request's reasoning_budget_message); the default is upstream
|
||||
// Strata's own wrap-up line.
|
||||
const char* const kDefaultReasoningBudgetMessage = "I have thought about this long enough; time to give my answer.";
|
||||
|
||||
struct Options {
|
||||
std::string model; // shard 1 of the GGUF
|
||||
std::string pack; // the prepared pack directory
|
||||
@@ -51,6 +60,9 @@ struct Options {
|
||||
int port = 8080;
|
||||
long long ctx = 32768;
|
||||
int threads = 0; // CPU expert workers; 0 = the engine's default (every physical core)
|
||||
bool fit_max_tokens = true; // shorten a request's max_tokens to the room left instead of refusing it
|
||||
long long reasoning_budget = 0; // cap the thinking of every request (0: no cap; a request can set its own)
|
||||
std::string reasoning_budget_message = kDefaultReasoningBudgetMessage; // injected before the end tag
|
||||
std::vector<std::string> extra; // after `--`: passed to the engine as they are
|
||||
};
|
||||
|
||||
@@ -63,6 +75,14 @@ void usage() {
|
||||
" --mmproj PATH enable image input using this vision encoder (CPU)\n"
|
||||
" --vision-tokens N maximum tokens per image (default 1024)\n"
|
||||
" --ctx N context length in tokens (default 32768)\n"
|
||||
" --fit-max-tokens / --no-fit-max-tokens\n"
|
||||
" shorten a request's max_tokens to the room left instead of refusing it (default: on)\n"
|
||||
" --reasoning-budget-tokens N\n"
|
||||
" cap the thinking of every request at N tokens (default: 0, no cap; a request's own\n"
|
||||
" reasoning_budget_tokens wins)\n"
|
||||
" --reasoning-budget-message TEXT\n"
|
||||
" injected before the end-of-thinking tag when the budget is reached (default: a short\n"
|
||||
" wrap-up line; a request's own reasoning_budget_message wins)\n"
|
||||
" --threads N CPU threads for the experts the GPU does not hold (default: every physical core,\n"
|
||||
" or half the CPU quota inside a container that has one)\n"
|
||||
" --host ADDR address to listen on (default 127.0.0.1)\n"
|
||||
@@ -125,8 +145,14 @@ struct Prepared {
|
||||
std::shared_ptr<strata_frontend::ImageFile> images;
|
||||
std::vector<llama_token> ids;
|
||||
std::string header; // "GEN <max_new> key=value ... " - the ids follow
|
||||
std::string gen_prefix; // "GEN " / "GENI ": the header without max_new and the keys
|
||||
std::string gen_suffix; // the sampling keys (and the image path): the rest of the header
|
||||
std::string request_id;
|
||||
common_chat_parser_params parser; // how to split the output into reasoning, answer and tool calls
|
||||
std::vector<std::string> thinking_end_tags; // from the chat template: what ends the reasoning block
|
||||
std::string reasoning_budget_message; // injected before the end tag at the budget
|
||||
bool thinking = true; // the template was rendered with thinking on
|
||||
long long reasoning_budget = 0; // #123: cap the thinking at this many tokens (0: off)
|
||||
bool stream = false;
|
||||
long long max_new = 0;
|
||||
};
|
||||
@@ -186,7 +212,29 @@ struct Server {
|
||||
if (kw.contains("reasoning_effort") && kw.at("reasoning_effort").is_string())
|
||||
in.chat_template_kwargs["reasoning_effort"] = kw.at("reasoning_effort").dump();
|
||||
}
|
||||
// #123: a hard thinking cap. The request's own reasoning_budget_tokens / reasoning_budget_message win; the
|
||||
// command line sets the default for every request. A bad value is a 400 before anything is generated.
|
||||
p.thinking = in.enable_thinking;
|
||||
p.reasoning_budget = opt.reasoning_budget;
|
||||
p.reasoning_budget_message = opt.reasoning_budget_message;
|
||||
if (body.contains("reasoning_budget_tokens")) {
|
||||
const common_json& v = body.at("reasoning_budget_tokens");
|
||||
if (!v.is_number_integer() || v.get<long long>() < 0) {
|
||||
err = "reasoning_budget_tokens must be 0 or more (0: no budget)";
|
||||
return false;
|
||||
}
|
||||
p.reasoning_budget = v.get<long long>();
|
||||
}
|
||||
if (body.contains("reasoning_budget_message")) {
|
||||
if (!body.at("reasoning_budget_message").is_string()) {
|
||||
err = "reasoning_budget_message must be a string";
|
||||
return false;
|
||||
}
|
||||
p.reasoning_budget_message = body.at("reasoning_budget_message").get<std::string>();
|
||||
}
|
||||
if (!p.thinking) p.reasoning_budget = 0; // thinking off: nothing to cap
|
||||
const common_chat_params cp = common_chat_templates_apply(tmpls.get(), in);
|
||||
p.thinking_end_tags = cp.thinking_end_tags;
|
||||
std::string rendered = cp.prompt;
|
||||
if (images.empty()) {
|
||||
p.ids = common_tokenize(vocab, rendered, false, true);
|
||||
@@ -232,16 +280,26 @@ struct Server {
|
||||
// (pi sends max_tokens 32768 on a 64K card) was refused on every prompt longer than ctx - max_tokens.
|
||||
constexpr long long kEngineReserve = 8;
|
||||
const long long n = (long long) p.ids.size();
|
||||
if (n + 1 + kEngineReserve > opt.ctx) {
|
||||
err = "the prompt is " + std::to_string(n) + " tokens, which does not fit the context of " +
|
||||
const long long room = opt.ctx - n - kEngineReserve;
|
||||
if (room < 1) {
|
||||
err = "the prompt is " + std::to_string(n) + " tokens, which leaves no room to generate in the context of " +
|
||||
std::to_string(opt.ctx);
|
||||
return false;
|
||||
}
|
||||
long long max_new = opt.ctx - n - kEngineReserve;
|
||||
long long max_new = room;
|
||||
for (const char* key : {"max_completion_tokens", "max_tokens"})
|
||||
if (body.contains(key) && body.at(key).is_number()) {
|
||||
const long long want = body.at(key).get<long long>();
|
||||
if (want >= 1) max_new = std::min(max_new, want);
|
||||
if (want >= 1) {
|
||||
if (want <= room) max_new = want;
|
||||
else if (opt.fit_max_tokens) max_new = room;
|
||||
else {
|
||||
err = "the request asks for " + std::to_string(want) + " tokens and the prompt is " +
|
||||
std::to_string(n) + ", which does not fit the context of " + std::to_string(opt.ctx) +
|
||||
" (--fit-max-tokens shortens the request to the room left)";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
p.max_new = max_new;
|
||||
@@ -275,8 +333,11 @@ struct Server {
|
||||
keys += b;
|
||||
if (keys.find("penalty_last_n") == std::string::npos) keys += " penalty_last_n=64";
|
||||
}
|
||||
p.header = (p.images ? "GENI " : "GEN ") + std::to_string(max_new) + keys + " " +
|
||||
(p.images ? p.images->path + " " : "");
|
||||
// The header's parts are kept so a continuation (a thinking budget wrap-up) can rebuild it with a smaller
|
||||
// max_new; the assembled header is byte-for-byte the one this fork always sent.
|
||||
p.gen_prefix = p.images ? "GENI " : "GEN ";
|
||||
p.gen_suffix = keys + " " + (p.images ? p.images->path + " " : "");
|
||||
p.header = p.gen_prefix + std::to_string(max_new) + p.gen_suffix;
|
||||
// Opt-in provenance capture: the exact first rendered request this
|
||||
// process serves, including token IDs and the engine sampling header.
|
||||
if (const char* path = std::getenv("SUGMEOFF_CAPTURE_FIRST_PROMPT")) {
|
||||
@@ -300,6 +361,8 @@ struct Server {
|
||||
u["prompt_tokens"] = r.prompt_n;
|
||||
u["completion_tokens"] = r.produced;
|
||||
u["total_tokens"] = r.prompt_n + r.produced;
|
||||
u["completion_tokens_details"] = common_json::object();
|
||||
u["completion_tokens_details"]["reasoning_tokens"] = r.reasoning_tokens;
|
||||
return u;
|
||||
}
|
||||
|
||||
@@ -363,8 +426,20 @@ struct Server {
|
||||
last_write = std::chrono::steady_clock::now();
|
||||
}
|
||||
};
|
||||
GenResult r = engine.generate(p.header, p.ids, [&](llama_token id) {
|
||||
if (llama_vocab_is_eog(vocab, id)) return client_ok;
|
||||
// #123: a hard thinking cap (reasoning_budget_tokens). When the thinking reaches the budget it is closed
|
||||
// with the budget message and the model's end tag, and the model answers from there. The wrap-up is fed
|
||||
// like generated text and the engine is asked again with prompt + generated + wrap-up, continuing from the
|
||||
// prefix it already holds, so nothing is read twice. Off unless thinking is on and a budget is set.
|
||||
bool budget_on = p.thinking && p.reasoning_budget > 0;
|
||||
long long thought = 0; // reasoning tokens generated so far (the budget's count)
|
||||
long long produced = 0; // every generated token, the wrap-up included
|
||||
bool seen_end = false; // an end-of-thinking tag is in `text`
|
||||
auto reasoning_over = [&]() {
|
||||
for (const std::string& tag : p.thinking_end_tags)
|
||||
if (!tag.empty() && text.find(tag) != std::string::npos) return true;
|
||||
return false;
|
||||
};
|
||||
auto feed = [&](llama_token id) {
|
||||
pending += common_token_to_piece(vocab, id, /*special=*/true);
|
||||
const size_t n = utf8_complete_prefix(pending);
|
||||
if (n > 0) {
|
||||
@@ -372,15 +447,59 @@ struct Server {
|
||||
pending.erase(0, n);
|
||||
update(true);
|
||||
}
|
||||
if (client_ok && keepalive) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - last_write >= std::chrono::seconds(15)) {
|
||||
client_ok = keepalive();
|
||||
last_write = now;
|
||||
};
|
||||
GenResult r;
|
||||
r.ok = true;
|
||||
std::vector<llama_token> prompt_ids = p.ids;
|
||||
while (true) {
|
||||
bool budget_reached = false;
|
||||
std::vector<llama_token> pass_ids;
|
||||
const std::string header = p.gen_prefix + std::to_string(p.max_new - produced) + p.gen_suffix;
|
||||
GenResult pass = engine.generate(header, prompt_ids, [&](llama_token id) {
|
||||
if (llama_vocab_is_eog(vocab, id)) return client_ok;
|
||||
pass_ids.push_back(id);
|
||||
if (budget_on && !seen_end) ++thought;
|
||||
feed(id);
|
||||
if (!seen_end && reasoning_over()) seen_end = true;
|
||||
if (budget_on && !seen_end && thought >= p.reasoning_budget && pending.empty()) {
|
||||
budget_reached = true; // engine STOP; the client is still here
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return client_ok;
|
||||
});
|
||||
if (client_ok && keepalive) {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - last_write >= std::chrono::seconds(15)) {
|
||||
client_ok = keepalive();
|
||||
last_write = now;
|
||||
}
|
||||
}
|
||||
return client_ok;
|
||||
});
|
||||
if (!pass.ok) { r = pass; break; }
|
||||
produced += pass.produced;
|
||||
r.ok = true;
|
||||
r.finish = pass.finish;
|
||||
r.prompt_ms += pass.prompt_ms;
|
||||
r.decode_ms += pass.decode_ms;
|
||||
r.draft_accepted += pass.draft_accepted;
|
||||
r.draft_offered += pass.draft_offered;
|
||||
if (!budget_reached || !client_ok) break;
|
||||
budget_on = false; // wrap the thinking up at most once
|
||||
const std::string end_tag = p.thinking_end_tags.empty() ? std::string(" response")
|
||||
: p.thinking_end_tags.front();
|
||||
const std::vector<llama_token> extra =
|
||||
common_tokenize(vocab, "\n\n" + p.reasoning_budget_message + end_tag + "\n\n", false, true);
|
||||
if (p.max_new - produced - (long long) extra.size() < 1) break; // no room to answer: "length"
|
||||
std::fprintf(stderr, "unbiased-strata: request %s thinking budget reached (%lld tokens): wrapping up\n",
|
||||
p.request_id.c_str(), thought);
|
||||
for (llama_token t : extra) { pass_ids.push_back(t); feed(t); }
|
||||
prompt_ids.insert(prompt_ids.end(), pass_ids.begin(), pass_ids.end());
|
||||
produced += (long long) extra.size();
|
||||
}
|
||||
if (r.ok) {
|
||||
r.prompt_n = (long long) p.ids.size(); // usage reports the request's own prompt, not a continuation's
|
||||
r.produced = produced;
|
||||
r.reasoning_tokens = thought;
|
||||
}
|
||||
text += pending;
|
||||
try {
|
||||
update(false);
|
||||
@@ -574,6 +693,10 @@ bool parse_args(int argc, char** argv, Options& o) {
|
||||
else if (a == "--host") o.host = next("--host");
|
||||
else if (a == "--port") o.port = std::atoi(next("--port").c_str());
|
||||
else if (a == "--ctx") o.ctx = std::atoll(next("--ctx").c_str());
|
||||
else if (a == "--fit-max-tokens") o.fit_max_tokens = true;
|
||||
else if (a == "--no-fit-max-tokens") o.fit_max_tokens = false;
|
||||
else if (a == "--reasoning-budget-tokens") o.reasoning_budget = std::atoll(next("--reasoning-budget-tokens").c_str());
|
||||
else if (a == "--reasoning-budget-message") o.reasoning_budget_message = next("--reasoning-budget-message");
|
||||
else if (a == "--threads") o.threads = std::atoi(next("--threads").c_str());
|
||||
else if (a == "--") {
|
||||
for (++i; i < argc; ++i) {
|
||||
@@ -591,6 +714,10 @@ bool parse_args(int argc, char** argv, Options& o) {
|
||||
else if (a == "-h" || a == "--help") return false;
|
||||
else { std::fprintf(stderr, "unbiased-strata: unknown option %s\n\n", a.c_str()); return false; }
|
||||
}
|
||||
if (o.reasoning_budget < 0) {
|
||||
std::fprintf(stderr, "unbiased-strata: --reasoning-budget-tokens must be 0 or more (0: no budget)\n\n");
|
||||
return false;
|
||||
}
|
||||
return !o.model.empty() && !o.pack.empty() && !o.mtp.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
CUDA_VISIBLE_DEVICES=1 ./build/unbiased-strata --model /mnt/nvme/stack/models/ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF/IQ3_XXS/Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf --pack /mnt/nvme/Strata-data/pack-iq3-xxs --mtp /mnt/nvme/Strata-data/mtp --ctx 262144
|
||||
Reference in new issue
Block a user