Archived
- Gemma 4 31B (PRs #74, #75, #81, #86; sfjes, keptn42): sliding and global attention with their own kernels, the assistant head as a drafter that reads the trunk's cache, the vision tower, a fused w4a8 decode step, bf16 and compressed-tensors int4 checkpoints, and window snapshots so a prefix hit on a sliding-window model resumes instead of prefilling again. deploy/compose/gemma4-31b-w4a8.yaml serves keptn42's container on two cards - ParoQuant (PRs #65, #66, #67; malic): Qwen3.8-27B checkpoints with learned Givens rotations, as int4, int5 and MXFP4, imported without re-quantising, the prefill's rotations on the matrix cores. deploy/compose/qwen3.8-27b-paro.yaml serves them on one card. A ParoQuant container made with the PR builds is refused: convert it again with --reuse <old container> - Gemma 4 26B-A4B with z-lab's DFlash drafter (PR #88; Perire): arch/gemma4 serves Google's bf16 checkpoint with the routed experts in block fp8 or bf16 (data/recipes/gemma4-26b-a4b-df*.recipe), and libr4d gains its windowed paged attention, the fused sandwich-norm joins, expert_scale and the sharded draft head's pair_argmax. A prefill chunk runs as two slices with its all-reduces beside the compute, bit-exact with the whole step. The 31B's global layers take the windowed attention too: an 11.5K-token prefill on two R9700s in 3.1 s instead of 4.9 - a startup that logs nothing for two minutes says where it waits (#89): the system call (a KFD request by name), the library that made it, and its stack, which addr2line resolves against the binary - the prefix cache is kept in VRAM and host memory only (#70): the disk tier wrote every block and snapshot a prompt produced, and its copies never outlived the process. --prefix-cache-host-mib is renamed --prefix-cache-mib; --prefix-cache-dir and --prefix-cache-disk-mib are removed, and startup refuses all three with what replaced them. The compose files no longer mount /data, and .env no longer needs RADIANCE_STATE - --prefix-cache-ttl S (default 3600, 0 never): a conversation no request has used for that long leaves the cache, whichever tier holds it; radiance:prefix_cache_expired_total counts them. A full host tier now drops its least recently used conversations - --vision-tower-placement vram|ram (#84): ram keeps an encoder tower in pinned host memory and gives its VRAM to the KV cache, read over the link once an image - timings carry ttft_ms and total_ms (#85): the server's own time from the request body's arrival to the first and the last token, queueing and tokenisation included - a KV-cache recall that found a moved expert with nowhere to go stopped halfway and leaked the slots it had already freed: after it every recall failed, the cache stayed short until a restart, and requests failed with HTTP 500. A recall now waits for the slots it is owed and always finishes - a tool result or tool-call arguments that are not JSON are no longer refused as nesting past 1024 levels for their unmatched brackets (#90): only text that parses as JSON is held to the bound, since nothing else is parsed into a tree - the host-pool refusal names the limit that applies to pinned memory (RAM less 1/64 and 1.5 GiB), not ttm.pages_limit - --embedding-placement auto, removed in 1.3.0, is refused with what replaced it - for out-of-tree kernel libraries: gemm_nt_q gains two operands (pb1, pb2, the part bounds), and the loader requires identical op schemas, so a library built against 1.4.0 must be rebuilt. For architecture plugins, rad_op_declared(b, op) asks whether an op is in the loaded vocabulary without declaring it
167 lines
7.6 KiB
C++
167 lines
7.6 KiB
C++
/* core/server/http.h -- the transport seam.
|
|
*
|
|
* Everything above this line (routing, OpenAI translation, metrics) is written against these
|
|
* plain structs and never sees a socket. That is what lets tests/server_test.cpp drive the real
|
|
* handlers in-process: `Router::dispatch` is the same call the transport makes, minus the port.
|
|
* The alternative -- testing by binding a loopback socket -- tests cpp-httplib, which is already
|
|
* tested, and makes the suite depend on a free port.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace rad {
|
|
namespace server {
|
|
|
|
/* Header names are case-insensitive per RFC 9110, and clients disagree about capitalisation of
|
|
* Authorization and Content-Type in particular. */
|
|
struct CiLess {
|
|
bool operator()(const std::string& a, const std::string& b) const;
|
|
};
|
|
|
|
struct HttpRequest {
|
|
std::string method = "GET";
|
|
std::string path; /* no query string */
|
|
std::string body;
|
|
std::map<std::string, std::string, CiLess> headers;
|
|
std::map<std::string, std::string> query;
|
|
std::map<std::string, std::string> params; /* from a ":name" route segment */
|
|
std::string peer; /* "addr:port"; empty in-process */
|
|
/* WHEN THE WHOLE BODY HAD ARRIVED, in steady-clock nanoseconds: where the server's own time
|
|
* for the request starts, before it is parsed, rendered or tokenised, so the time the body
|
|
* spent on the network is not in it. -1 for a request made in-process. */
|
|
int64_t t_received_ns = -1;
|
|
|
|
/* True once the peer is gone. The transport supplies it; in-process it is absent and the
|
|
* waiting paths simply never see a drop. A dropped connection is a cancellation (spec §14),
|
|
* so every path that waits polls this. */
|
|
std::function<bool()> closed;
|
|
|
|
std::string header(const std::string& k, const std::string& def = "") const;
|
|
std::string param(const std::string& k, const std::string& def = "") const;
|
|
bool peer_gone() const { return closed && closed(); }
|
|
/* True when the peer is this machine: 127.0.0.0/8, ::1, or either written as an IPv4-mapped
|
|
* IPv6 address -- and for a request made in-process, which has no peer. */
|
|
bool peer_is_loopback() const;
|
|
};
|
|
|
|
struct HttpResponse {
|
|
int status = 200;
|
|
std::string content_type = "application/json; charset=utf-8";
|
|
std::string body;
|
|
std::vector<std::pair<std::string, std::string>> headers;
|
|
|
|
/* Streaming. When `next` is set, `body` is ignored and the transport pulls chunks until next()
|
|
* returns false. A chunk may legitimately be empty (nothing produced this poll), in which case
|
|
* nothing is written but the connection is checked. */
|
|
std::function<bool(std::string&)> next;
|
|
|
|
/* Runs exactly once when the connection ends, with `ok` false if the peer went away before the
|
|
* stream finished. This is where a dropped stream becomes a scheduler cancellation. */
|
|
std::function<void(bool ok)> on_close;
|
|
|
|
bool streaming() const { return next != nullptr; }
|
|
|
|
void set_header(std::string k, std::string v) { headers.emplace_back(std::move(k), std::move(v)); }
|
|
|
|
static HttpResponse json_body(int status, std::string body);
|
|
static HttpResponse text(int status, std::string body);
|
|
};
|
|
|
|
using Handler = std::function<HttpResponse(const HttpRequest&)>;
|
|
|
|
/* Exact-match routing plus a single ":name" wildcard segment, which is all the OpenAI surface
|
|
* needs (/v1/models/:id). No regex, no priority rules, nothing to reason about. */
|
|
class Router {
|
|
public:
|
|
void add(std::string method, std::string path, Handler h);
|
|
void get(std::string path, Handler h) { add("GET", std::move(path), std::move(h)); }
|
|
void post(std::string path, Handler h) { add("POST", std::move(path), std::move(h)); }
|
|
|
|
/* 404 with a body naming the path, 405 when the path exists under another method. By value,
|
|
* so the transport can move a request -- body included -- into the handler rather than copy
|
|
* a body that may be hundreds of megabytes. */
|
|
HttpResponse dispatch(HttpRequest req) const;
|
|
|
|
/* Convenience for tests and for anything that speaks in whole request lines. */
|
|
HttpResponse call(const std::string& method, const std::string& path,
|
|
const std::string& body = "") const;
|
|
|
|
std::vector<std::string> routes() const;
|
|
|
|
private:
|
|
struct Route {
|
|
std::string method;
|
|
std::vector<std::string> segs; /* ":name" segments are wildcards */
|
|
Handler h;
|
|
};
|
|
std::vector<Route> routes_;
|
|
};
|
|
|
|
/* Split "/v1/models/foo?x=1" into path and decoded query parameters. */
|
|
void parse_target(const std::string& target, std::string* path,
|
|
std::map<std::string, std::string>* query);
|
|
/* Percent-decode. `plus_is_space` is the form encoding, which applies to a query string and
|
|
* not to a path -- see the definition. */
|
|
std::string url_decode(const std::string& s, bool plus_is_space = true);
|
|
|
|
/* ================================================================== the cpp-httplib binding */
|
|
|
|
/* ONE WORKER PER CONNECTION, for the connection's whole life: httplib reads a request, runs the
|
|
* handler and writes the response on the same thread, and a generation's handler runs until the
|
|
* generation ends. So the pool bounds are connection counts, and Server::run derives them from
|
|
* the admission ceiling rather than from the core count. */
|
|
struct TransportOptions {
|
|
std::string host = "0.0.0.0";
|
|
int port = 8000; /* 0 -> any free port; see HttpTransport::port */
|
|
int n_threads = 0; /* kept alive while idle. 0 -> hardware_concurrency,
|
|
* floor 4 */
|
|
int64_t max_threads = 0; /* what the pool may grow to. 0 -> n_threads */
|
|
int64_t max_queued = 0; /* accepted connections that may wait for a worker once
|
|
* it has grown to max_threads; past that they are
|
|
* closed. 0 -> unbounded */
|
|
/* The longest the peer may go silent while its REQUEST is being read. It bounds nothing about
|
|
* generation, which is written, not read. A connection that sends part of a request and then
|
|
* goes quiet holds a worker until this runs out, so it is short. */
|
|
int read_timeout_s = 30;
|
|
int write_timeout_s = 600;
|
|
int keep_alive_timeout_s = 5; /* an idle connection's hold on a worker between requests */
|
|
size_t max_body_bytes = 512ull << 20; /* images arrive base64 inside the JSON body */
|
|
std::string api_key; /* "" disables the check */
|
|
bool cors = true;
|
|
};
|
|
|
|
/* The workers the pool keeps while idle for TransportOptions::n_threads: that many, one a
|
|
* hardware thread for 0, and never fewer than 4. One definition, so the number the startup
|
|
* report states is the number the transport runs. */
|
|
int idle_workers(int n_threads);
|
|
|
|
class HttpTransport {
|
|
public:
|
|
HttpTransport();
|
|
~HttpTransport();
|
|
HttpTransport(const HttpTransport&) = delete;
|
|
HttpTransport& operator=(const HttpTransport&) = delete;
|
|
|
|
/* Blocks until stop(). Returns RAD_OK, or negative if the bind failed. */
|
|
int serve(const Router& r, const TransportOptions& o);
|
|
void stop();
|
|
|
|
/* The port serve() bound, which is the only way to learn it when it was asked for port 0.
|
|
* 0 until the server is accepting connections, so a non-zero answer also means stop() will
|
|
* take effect. */
|
|
int port() const;
|
|
|
|
private:
|
|
struct Impl;
|
|
std::unique_ptr<Impl> impl_;
|
|
};
|
|
|
|
} /* namespace server */
|
|
} /* namespace rad */
|