Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

feat(config): let a remote profile declare its own context window (#730)

Krisztian Gajdar530dcde89dd0
diff --git a/packages/sie_server/REMOTE_BACKENDS.md b/packages/sie_server/REMOTE_BACKENDS.md
index e8d4ca8de..dcb63fb6c 100644
--- a/packages/sie_server/REMOTE_BACKENDS.md
+++ b/packages/sie_server/REMOTE_BACKENDS.md
@@ -102,6 +102,14 @@ The declared dimensions and input limit must describe the upstream model.
`remote_backed: true` excludes local weight-source fields such as `hf_id`.
The model configuration can name an upstream; it cannot create one.
+A remote profile's `adapter_options.loadtime` takes `upstream` and
+`upstream_model`, both required. Every other load-time option is refused,
+except `max_seq_length` on a profile other than `default` (see
+[Context window of a remote profile](#context-window-of-a-remote-profile)).
+The `default` profile serves the model's own window, which a remote-backed
+model declares with `max_sequence_length` and, for generation,
+`tasks.generate.context_length`.
+
From the repository root, start the server:
```bash
@@ -176,6 +184,41 @@ The operator may add `set_params` and `strip_params` on the upstream. Reserved
input and response-shaping fields, including `model`, cannot be overridden or
stripped. Callers cannot pass additional upstream fields or credentials.
+## Context window of a remote profile
+
+A generation model with local weights can declare a different window for its
+remote profile than for its local one, for example when the upstream serves a
+longer context than the local accelerators admit:
+
+```yaml
+profiles:
+ remote:
+ adapter_path: sie_server.adapters.remote.openai:OpenAIUpstreamAdapter
+ max_batch_tokens: 16384
+ max_output_tokens: 32768
+ adapter_options:
+ loadtime:
+ upstream: vendor
+ upstream_model: org/model
+ max_seq_length: 131072
+```
+
+A request that names the profile, `org/model:remote`, takes that window as its
+`context_length` and that cap as its `max_output_tokens`, and the usage the
+upstream reports is checked against them. The output cap must fit inside the
+window. A profile without a `max_output_tokens` of its own takes the cap of the
+profile it extends, or else the model's, and that cap must fit too. Load-time
+options are not merged with a parent's, so a profile that extends a remote
+profile and declares a window restates `upstream` and `upstream_model`.
+
+A request for the bare name keeps the model's own `context_length` and
+`max_output_tokens` when its local profile serves it, and when single-node
+fallback sends it to the remote profile. In a cluster, routing sends a
+bare-name request to the remote profile as `org/model:remote`. The gateway
+still holds its output to the model's own `max_output_tokens`, and the remote
+worker checks it against the remote profile's window, as for a request that
+names the profile.
+
## Controls and failure behavior
No upstream is configured by default. Without an upstream and a remote
diff --git a/packages/sie_server/src/sie_server/config/model.py b/packages/sie_server/src/sie_server/config/model.py
index d8adca13d..c1b1891fb 100644
--- a/packages/sie_server/src/sie_server/config/model.py
+++ b/packages/sie_server/src/sie_server/config/model.py
@@ -580,6 +580,7 @@ class ProfileConfig(BaseModel):
REMOTE_ADAPTER_MODULE_PREFIX = "sie_server.adapters.remote."
REMOTE_PROFILE_LOADTIME_KEYS = frozenset({"upstream", "upstream_model"})
+REMOTE_PROFILE_OPTIONAL_LOADTIME_KEYS = frozenset({"max_seq_length"})
UPSTREAM_NAME_PATTERN = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$")
# Every path segment starts with a letter or digit, so '.' and '..' cannot
# appear: the value becomes part of an authenticated URL on the upstream.
@@ -933,11 +934,11 @@ class ModelConfig(BaseModel):
msg = f"Profile '{name}' of a 'remote_backed' model must use a remote adapter"
raise ValueError(msg)
continue
- unsupported = sorted(set(loadtime) - REMOTE_PROFILE_LOADTIME_KEYS)
+ unsupported = sorted(set(loadtime) - REMOTE_PROFILE_LOADTIME_KEYS - REMOTE_PROFILE_OPTIONAL_LOADTIME_KEYS)
if unsupported:
msg = (
- f"Remote profile '{name}' may only name an upstream and its model; "
- f"unsupported load-time options: {', '.join(unsupported)}"
+ f"Remote profile '{name}' may only name an upstream and its model and declare the "
+ f"max_seq_length it serves; unsupported load-time options: {', '.join(unsupported)}"
)
raise ValueError(msg)
missing = [key for key in sorted(REMOTE_PROFILE_LOADTIME_KEYS) if not loadtime.get(key)]
@@ -954,6 +955,17 @@ class ModelConfig(BaseModel):
"or 'org/name:profile'"
)
raise ValueError(msg)
+ if "max_seq_length" in loadtime:
+ if name == "default":
+ msg = (
+ "Remote profile 'default' cannot declare 'max_seq_length': the default profile serves "
+ "the model's own window"
+ )
+ raise ValueError(msg)
+ max_seq_length = loadtime["max_seq_length"]
+ if not isinstance(max_seq_length, int) or isinstance(max_seq_length, bool) or max_seq_length <= 0:
+ msg = f"Remote profile '{name}': 'max_seq_length' must be a positive integer"
+ raise ValueError(msg)
return self
@model_validator(mode="after")
@@ -1185,24 +1197,26 @@ class ModelConfig(BaseModel):
if self.tasks.generate is not None:
for name, profile in self.profiles.items():
parent = self.profiles[profile.extends] if profile.extends is not None else None
- effective_output_cap = (
- profile.max_output_tokens
- if profile.max_output_tokens is not None
- else parent.max_output_tokens
- if parent is not None
- else None
+ effective_loadtime = profile.adapter_options.loadtime or (
+ parent.adapter_options.loadtime if parent is not None else {}
)
+ declared_window = effective_loadtime.get("max_seq_length")
+ if not isinstance(declared_window, int) or declared_window <= 0:
+ declared_window = None
+ effective_output_cap = profile.max_output_tokens
+ if effective_output_cap is None and parent is not None:
+ effective_output_cap = parent.max_output_tokens
+ if effective_output_cap is None and declared_window is not None:
+ effective_output_cap = self.tasks.generate.max_output_tokens
if effective_output_cap is not None:
- effective_loadtime = profile.adapter_options.loadtime or (
- parent.adapter_options.loadtime if parent is not None else {}
- )
- effective_context = effective_loadtime.get(
- "max_seq_length",
- self.tasks.generate.context_length,
- )
+ effective_context = self.tasks.generate.context_length
+ if declared_window is not None:
+ effective_context = (
+ min(declared_window, effective_context) if name == "default" else declared_window
+ )
if effective_output_cap > effective_context:
msg = (
- f"Profile '{name}' on generation model '{self.sie_id}' sets "
+ f"Profile '{name}' on generation model '{self.sie_id}' has "
f"max_output_tokens={effective_output_cap}, exceeding its "
f"context_length={effective_context}"
)
diff --git a/packages/sie_server/tests/config/test_config.py b/packages/sie_server/tests/config/test_config.py
index f4b9c15db..e40da781d 100644
--- a/packages/sie_server/tests/config/test_config.py
+++ b/packages/sie_server/tests/config/test_config.py
@@ -869,6 +869,24 @@ class TestModelConfigProfiles:
},
)
+ def test_default_profile_max_output_tokens_must_fit_the_model_context(self) -> None:
+ """The bare model serves the model's context, whatever window the default profile loads."""
+ with pytest.raises(ValidationError, match="max_output_tokens=16384, exceeding its context_length=8192"):
+ ModelConfig(
+ sie_id="test-model",
+ hf_id="org/model",
+ tasks=Tasks(generate=GenerateTask(context_length=8192, max_output_tokens=4096)),
+ profiles={
+ "default": ProfileConfig(
+ adapter_path="mod:Cls",
+ max_batch_tokens=8192,
+ kv_budget_tokens=8192,
+ max_output_tokens=16384,
+ adapter_options=AdapterOptions(loadtime={"max_seq_length": 32768}),
+ ),
+ },
+ )
+
def test_chat_template_kwargs_require_generation_task(self) -> None:
"""Tokenizer-render presets are invalid on non-generation profiles."""
with pytest.raises(ValidationError, match="without a generation task"):
diff --git a/packages/sie_server/tests/config/test_remote_backed_config.py b/packages/sie_server/tests/config/test_remote_backed_config.py
index ef2f4d1f1..c18128cde 100644
--- a/packages/sie_server/tests/config/test_remote_backed_config.py
+++ b/packages/sie_server/tests/config/test_remote_backed_config.py
@@ -8,6 +8,7 @@ import pytest
from pydantic import ValidationError
from sie_server.config.model import ModelConfig
from sie_server.config.upstreams import Upstream, install_upstreams, validate_profile_upstreams
+from sie_server.core.loader import expand_profile_variants
from sie_server.core.registry import ModelRegistry
from sie_server.ipc_types import ReplaceModelConfigEntry, ReplaceModelConfigsRequest
from sie_server.queue_executor import QueueExecutor
@@ -95,6 +96,63 @@ def test_a_remote_profile_names_both_the_upstream_and_its_model() -> None:
ModelConfig.model_validate(remote_backed(profiles={"default": remote_profile(upstream="team-sie")}))
+def test_a_remote_profile_may_declare_the_window_its_upstream_serves() -> None:
+ config = ModelConfig.model_validate(
+ remote_backed(
+ profiles={
+ "default": remote_profile(),
+ "relay": remote_profile(upstream="team-sie", upstream_model="sie-fake", max_seq_length=8192),
+ }
+ )
+ )
+
+ assert config.resolve_profile("relay").loadtime["max_seq_length"] == 8192
+ assert expand_profile_variants([config])["acme/remote-fake:relay"].max_sequence_length == 8192
+ validate_profile_upstreams(config)
+
+
+def test_the_default_profile_serves_the_models_own_window() -> None:
+ with pytest.raises(ValidationError, match="Remote profile 'default' cannot declare 'max_seq_length'"):
+ ModelConfig.model_validate(
+ remote_backed(
+ profiles={
+ "default": remote_profile(upstream="team-sie", upstream_model="sie-fake", max_seq_length=8192)
+ }
+ )
+ )
+
+
+def test_a_declared_window_does_not_stand_in_for_the_upstream_or_its_model() -> None:
+ with pytest.raises(ValidationError, match="must set load-time options: upstream_model"):
+ ModelConfig.model_validate(
+ remote_backed(
+ profiles={
+ "default": remote_profile(),
+ "relay": remote_profile(upstream="team-sie", max_seq_length=8192),
+ }
+ )
+ )
+ with pytest.raises(ValidationError, match="must set load-time options: upstream, upstream_model"):
+ ModelConfig.model_validate(
+ remote_backed(profiles={"default": remote_profile(), "relay": remote_profile(max_seq_length=8192)})
+ )
+
+
[email protected]("max_seq_length", [0, -1, True, "8192", 8192.0, None])
+def test_a_declared_window_is_a_positive_integer(max_seq_length: Any) -> None:
+ with pytest.raises(ValidationError, match="'max_seq_length' must be a positive integer"):
+ ModelConfig.model_validate(
+ remote_backed(
+ profiles={
+ "default": remote_profile(),
+ "relay": remote_profile(
+ upstream="team-sie", upstream_model="sie-fake", max_seq_length=max_seq_length
+ ),
+ }
+ )
+ )
+
+
@pytest.mark.parametrize(
"upstream_model",
[
diff --git a/packages/sie_server/tests/test_remote_profile_window.py b/packages/sie_server/tests/test_remote_profile_window.py
new file mode 100644
index 000000000..563643cf4
--- /dev/null
+++ b/packages/sie_server/tests/test_remote_profile_window.py
@@ -0,0 +1,402 @@
+"""A remote profile declares the context window its upstream serves.
+
+The model has a local profile at a 16384-token window with a 4096-token output
+cap and a remote profile whose upstream serves 131072 tokens with a
+32768-token output cap. The configs come from the real loader, the remote
+adapter is loaded by the real registry, and only the upstream's HTTP transport
+is replaced.
+"""
+
+from __future__ import annotations
+
+import functools
+import json
+import re
+import time
+from collections.abc import Iterator
+from pathlib import Path
+from typing import Any
+from unittest.mock import AsyncMock, patch
+
+import httpx
+import msgpack
+import pytest
+import yaml
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+from pydantic import ValidationError
+from sie_server.adapters.remote.openai import OpenAIUpstreamAdapter
+from sie_server.api.openai_local import router
+from sie_server.config.upstreams import Upstream, install_upstreams
+from sie_server.core.loader import load_model_configs
+from sie_server.core.registry import ModelRegistry
+from sie_server.core.upstream_client import upstream_client
+from sie_server.processors.streaming import StreamingProcessor
+from tokenizers import Tokenizer, models, pre_tokenizers
+
+MODEL = "acme/agent-chat"
+REMOTE = f"{MODEL}:remote"
+UPSTREAM = "agent-api"
+LOCAL_ADAPTER = "sie_server.adapters.fake.adapter:FakeAdapter"
+REMOTE_ADAPTER = "sie_server.adapters.remote.openai:OpenAIUpstreamAdapter"
+UPSTREAM_LOADTIME = {"upstream": UPSTREAM, "upstream_model": "vendor/agent-chat"}
+IN_WINDOW = {"prompt_tokens": 100_000, "completion_tokens": 20_000, "total_tokens": 120_000}
+OVER_WINDOW = {"prompt_tokens": 131_073, "completion_tokens": 2, "total_tokens": 131_075}
+OVER_REMAINDER = {"prompt_tokens": 120_000, "completion_tokens": 12_000, "total_tokens": 132_000}
+IN_BARE_WINDOW = {"prompt_tokens": 12_000, "completion_tokens": 4_000, "total_tokens": 16_000}
+OVER_BARE_WINDOW = {"prompt_tokens": 16_385, "completion_tokens": 1, "total_tokens": 16_386}
+
+
+def model_config(*, remote_loadtime: dict[str, Any] | None = None, remote_output: int | None = 32768) -> dict[str, Any]:
+ remote: dict[str, Any] = {
+ "adapter_path": REMOTE_ADAPTER,
+ "max_batch_tokens": 16384,
+ "adapter_options": {
+ "loadtime": remote_loadtime
+ if remote_loadtime is not None
+ else {**UPSTREAM_LOADTIME, "max_seq_length": 131072}
+ },
+ }
+ if remote_output is not None:
+ remote["max_output_tokens"] = remote_output
+ return {
+ "sie_id": MODEL,
+ "hf_id": "acme/not-on-any-hub",
+ "hf_revision": "0" * 40,
+ "inputs": {"text": True},
+ "max_sequence_length": 16384,
+ "tasks": {"generate": {"context_length": 16384, "max_output_tokens": 4096, "capabilities": {"tools": True}}},
+ "profiles": {
+ "default": {
+ "adapter_path": LOCAL_ADAPTER,
+ "max_batch_tokens": 16384,
+ "kv_budget_tokens": 16384,
+ "adapter_options": {"loadtime": {"max_seq_length": 16384}},
+ },
+ "remote": remote,
+ },
+ "routing": {"policy": "fallback", "fallback_profile": "remote"},
+ }
+
+
+def remote_backed_config() -> dict[str, Any]:
+ return {
+ "sie_id": "acme/agent-relay",
+ "remote_backed": True,
+ "inputs": {"text": True},
+ "tasks": {"generate": {"context_length": 16384, "max_output_tokens": 4096}},
+ "profiles": {
+ "default": {
+ "adapter_path": REMOTE_ADAPTER,
+ "max_batch_tokens": 16384,
+ "max_output_tokens": 32768,
+ "adapter_options": {"loadtime": {**UPSTREAM_LOADTIME, "max_seq_length": 131072}},
+ }
+ },
+ }
+
+
+def write_models(tmp_path: Path, config: dict[str, Any] | None = None) -> Path:
+ config = config if config is not None else model_config()
+ models_dir = tmp_path / "models"
+ models_dir.mkdir()
+ (models_dir / f"{config['sie_id'].replace('/', '__')}.yaml").write_text(yaml.safe_dump(config), encoding="utf-8")
+ return models_dir
+
+
+def write_tokenizer(path: Path) -> Path:
+ path.mkdir()
+ vocab = {"[UNK]": 0, "<|user|>": 1, "<|assistant|>": 2, "word": 3, "answer": 4}
+ tokenizer = Tokenizer(models.WordLevel(vocab, "[UNK]"))
+ tokenizer.pre_tokenizer = pre_tokenizers.Whitespace()
+ tokenizer.add_special_tokens(["<|user|>", "<|assistant|>"])
+ tokenizer.save(str(path / "tokenizer.json"))
+ template = (
+ "{% for message in messages %}<|{{ message.role }}|>{{ message.content }}{% endfor %}"
+ "{% if add_generation_prompt %}<|assistant|>{% endif %}"
+ )
+ (path / "tokenizer_config.json").write_text(
+ json.dumps({"tokenizer_class": "PreTrainedTokenizerFast", "unk_token": "[UNK]", "chat_template": template}),
+ encoding="utf-8",
+ )
+ return path
+
+
[email protected](autouse=True)
+def _upstreams(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
+ monkeypatch.setenv("HF_HOME", str(tmp_path / "hf-home"))
+ monkeypatch.setenv("HF_HUB_OFFLINE", "1")
+ install_upstreams(
+ {
+ UPSTREAM: Upstream.model_validate(
+ {
+ "kind": "openai",
+ "base_url": "http://127.0.0.1:9/v1",
+ "endpoints": ["chat"],
+ "rate_cap": {"requests_per_minute": 600, "max_concurrency": 8},
+ }
+ )
+ }
+ )
+ yield
+ install_upstreams({})
+
+
+def answer(adapter: OpenAIUpstreamAdapter, usages: list[dict[str, int]]) -> list[httpx.Request]:
+ requests: list[httpx.Request] = []
+
+ def respond(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ usage = usages[len(requests) - 1]
+ if json.loads(request.content)["stream"]:
+ frames = [
+ {"choices": [{"index": 0, "delta": {"role": "assistant", "content": "answer"}, "finish_reason": None}]},
+ {"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]},
+ {"choices": [], "usage": usage},
+ ]
+ body = b"".join(b"data: " + json.dumps(frame).encode() + b"\n\n" for frame in frames)
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ stream=httpx.ByteStream(body + b"data: [DONE]\n\n"),
+ )
+ payload = {
+ "choices": [{"index": 0, "message": {"role": "assistant", "content": "answer"}, "finish_reason": "stop"}],
+ "usage": usage,
+ }
+ return httpx.Response(
+ 200, headers={"content-type": "application/json"}, stream=httpx.ByteStream(json.dumps(payload).encode())
+ )
+
+ assert adapter._upstream is not None
+ adapter._async_client = upstream_client(adapter._upstream, transport=httpx.MockTransport(respond))
+ return requests
+
+
+async def run_queued(
+ registry: ModelRegistry, *, stream: bool, content: str = "question", item: dict[str, Any] | None = None
+) -> dict[str, Any]:
+ nc = AsyncMock()
+ proc = StreamingProcessor(nc=nc, registry=registry, worker_id="w1")
+ msg = AsyncMock()
+ msg.data = msgpack.packb(
+ {
+ "request_id": "req-1",
+ "work_item_id": "req-1.0",
+ "item_index": 0,
+ "total_items": 1,
+ "operation": "generate",
+ "model_id": REMOTE,
+ "profile_id": "default",
+ "pool_name": "default",
+ "router_id": "router-1",
+ "reply_subject": "_INBOX.router-1.req-1",
+ "timestamp": time.time(),
+ "generate": {
+ "messages": [{"role": "user", "content": content}],
+ "max_new_tokens": 32768,
+ "stream": stream,
+ },
+ **(item or {}),
+ },
+ use_bin_type=True,
+ )
+ await proc.process(msg, REMOTE)
+ return msgpack.unpackb(nc.publish.await_args_list[-1].args[1], raw=False)
+
+
+def test_the_remote_variant_carries_the_window_its_profile_declares(tmp_path: Path) -> None:
+ configs = load_model_configs(write_models(tmp_path))
+
+ bare, remote = configs[MODEL], configs[REMOTE]
+ assert bare.tasks.generate is not None
+ assert remote.tasks.generate is not None
+ assert (bare.max_sequence_length, bare.tasks.generate.context_length) == (16384, 16384)
+ assert bare.tasks.generate.max_output_tokens == 4096
+ assert (remote.max_sequence_length, remote.tasks.generate.context_length) == (131072, 131072)
+ assert remote.tasks.generate.max_output_tokens == 32768
+ assert remote.routing is None
+
+
+def test_a_window_without_a_cap_of_its_own_keeps_the_models_cap(tmp_path: Path) -> None:
+ config = model_config(remote_loadtime={**UPSTREAM_LOADTIME, "max_seq_length": 8192}, remote_output=None)
+
+ remote = load_model_configs(write_models(tmp_path, config))[REMOTE].tasks.generate
+
+ assert remote is not None
+ assert (remote.context_length, remote.max_output_tokens) == (8192, 4096)
+
+
+ ("config", "refusal"),
+ [
+ pytest.param(
+ model_config(remote_output=131073),
+ "Profile 'remote' on generation model 'acme/agent-chat' has max_output_tokens=131073, "
+ "exceeding its context_length=131072",
+ id="cap-above-the-profile-window",
+ ),
+ pytest.param(
+ model_config(remote_loadtime=UPSTREAM_LOADTIME),
+ "has max_output_tokens=32768, exceeding its context_length=16384",
+ id="cap-above-the-model-window-without-a-profile-window",
+ ),
+ pytest.param(
+ model_config(remote_loadtime={**UPSTREAM_LOADTIME, "max_seq_length": 2048}, remote_output=None),
+ "has max_output_tokens=4096, exceeding its context_length=2048",
+ id="inherited-cap-above-the-profile-window",
+ ),
+ pytest.param(
+ remote_backed_config(),
+ "Remote profile 'default' cannot declare 'max_seq_length': the default profile serves the model's own window",
+ id="window-on-a-remote-default-profile",
+ ),
+ ],
+)
+def test_the_loader_refuses_an_output_cap_outside_the_window_it_is_served_under(
+ tmp_path: Path, config: dict[str, Any], refusal: str
+) -> None:
+ with pytest.raises(ValidationError, match=re.escape(refusal)):
+ load_model_configs(write_models(tmp_path, config))
+
+
+async def test_the_remote_adapter_receives_the_declared_window(tmp_path: Path) -> None:
+ registry = ModelRegistry(models_dir=write_models(tmp_path), device="cpu", enable_hot_reload=False)
+ original_init = OpenAIUpstreamAdapter.__init__
+ try:
+ with patch.object(OpenAIUpstreamAdapter, "__init__", autospec=True, side_effect=original_init) as init:
+ adapter = await registry.load_async(REMOTE, device="cpu")
+
+ assert isinstance(adapter, OpenAIUpstreamAdapter)
+ assert init.call_args.kwargs["max_seq_length"] == 131072
+ assert init.call_args.kwargs["upstream_model"] == "vendor/agent-chat"
+ finally:
+ await registry.unload_all_async()
+
+
+def direct(tmp_path: Path) -> Iterator[tuple[TestClient, OpenAIUpstreamAdapter]]:
+ registry = ModelRegistry(models_dir=write_models(tmp_path), device="cpu", enable_hot_reload=False)
+ app = FastAPI()
+ app.include_router(router)
+ app.state.registry = registry
+ with TestClient(app) as client:
+ adapter = client.portal.call(functools.partial(registry.load_async, REMOTE, device="cpu"))
+ assert isinstance(adapter, OpenAIUpstreamAdapter)
+ try:
+ yield client, adapter
+ finally:
+ client.portal.call(adapter.aclose_client)
+ client.portal.call(registry.unload_all_async)
+
+
[email protected]("streaming", [False, True])
+def test_the_direct_route_bounds_upstream_usage_by_the_variant_window(direct: tuple, streaming: bool) -> None:
+ client, adapter = direct
+ body = {"model": REMOTE, "messages": [{"role": "user", "content": "question"}], "max_tokens": 32768}
+ if streaming:
+ body.update(stream=True, stream_options={"include_usage": True})
+ usages = [IN_WINDOW, OVER_WINDOW, OVER_REMAINDER]
+ requests = answer(adapter, usages)
+
+ served, *refused = [client.post("/v1/chat/completions", json=body) for _ in usages]
+
+ assert served.status_code == 200, served.text
+ assert served.headers["X-SIE-Served-By"] == "remote"
+ assert '"prompt_tokens":100000' in served.text.replace(" ", "")
+ assert '"error"' not in served.text
+ for response in refused:
+ if streaming:
+ assert response.status_code == 200
+ assert '"code":"upstream_error"' in response.text
+ else:
+ assert response.status_code == 502, response.text
+ assert [json.loads(request.content)["max_tokens"] for request in requests] == [32768] * len(usages)
+
+
+def test_single_node_fallback_holds_a_bare_request_to_the_bare_window(direct: tuple) -> None:
+ client, adapter = direct
+ registry: ModelRegistry = client.app.state.registry
+ body = {"model": MODEL, "messages": [{"role": "user", "content": "question"}]}
+ usages = [IN_BARE_WINDOW, OVER_BARE_WINDOW]
+ requests = answer(adapter, usages)
+
+ with (
+ patch.object(registry, "is_loading", side_effect=lambda name: name == MODEL),
+ patch.object(registry, "start_load_async", AsyncMock(return_value=True)),
+ ):
+ served, refused = [client.post("/v1/chat/completions", json=body) for _ in usages]
+
+ assert served.status_code == 200, served.text
+ assert served.headers["X-SIE-Served-By"] == "remote"
+ assert served.headers["X-SIE-Fallback-Reason"] == "model_loading"
+ assert served.json()["model"] == MODEL
+ assert refused.status_code == 503, refused.text
+ assert refused.headers["X-SIE-Fallback-Reason"] == "model_loading"
+ assert [json.loads(request.content)["max_tokens"] for request in requests] == [4096, 4096]
+
+
+async def test_the_queued_route_bounds_upstream_usage_by_the_variant_window(tmp_path: Path) -> None:
+ registry = ModelRegistry(models_dir=write_models(tmp_path), device="cpu", enable_hot_reload=False)
+ adapter = await registry.load_async(REMOTE, device="cpu")
+ assert isinstance(adapter, OpenAIUpstreamAdapter)
+ usages = [IN_WINDOW, OVER_WINDOW, OVER_REMAINDER]
+ requests = answer(adapter, usages * 2)
+
+ try:
+ terminals = [await run_queued(registry, stream=stream) for stream in (False, True) for _ in usages]
+ finally:
+ await adapter.aclose_client()
+ await registry.unload_all_async()
+
+ assert [terminal["finish_reason"] for terminal in terminals] == ["stop", "error", "error"] * 2
+ assert [terminals[0]["usage"]["prompt_tokens"], terminals[3]["usage"]["prompt_tokens"]] == [100_000, 100_000]
+ assert [json.loads(request.content)["stream"] for request in requests] == [False] * 3 + [True] * 3
+ assert [json.loads(request.content)["max_tokens"] for request in requests] == [32768] * 6
+
+
[email protected]("stream", [False, True])
+async def test_the_queued_route_counts_the_prompt_against_the_variant_window(tmp_path: Path, stream: bool) -> None:
+ config = model_config()
+ del config["hf_id"], config["hf_revision"]
+ config["weights_path"] = str(write_tokenizer(tmp_path / "weights"))
+ registry = ModelRegistry(models_dir=write_models(tmp_path, config), device="cpu", enable_hot_reload=False)
+ adapter = await registry.load_async(REMOTE, device="cpu")
+ assert isinstance(adapter, OpenAIUpstreamAdapter)
+ upstream_counts = {"prompt_tokens": 97_000, "completion_tokens": 3, "total_tokens": 97_003}
+ requests = answer(adapter, [upstream_counts])
+
+ try:
+ served = await run_queued(registry, stream=stream, content=" ".join(["word"] * 98_302))
+ refused = await run_queued(registry, stream=stream, content=" ".join(["word"] * 98_303))
+ finally:
+ await adapter.aclose_client()
+ await registry.unload_all_async()
+
+ assert served["finish_reason"] == "stop", served
+ assert (served["usage"]["prompt_tokens"], served["usage"]["completion_tokens"]) == (98_304, 1)
+ assert served["usage"]["upstream_usage"]["prompt_tokens"] == 97_000
+ assert refused["finish_reason"] == "error"
+ assert refused["error"]["code"] == "context_exceeded"
+ assert "131073 exceeds context_length (131072)" in refused["error"]["message"]
+ assert len(requests) == 1
+ assert json.loads(requests[0].content)["max_tokens"] == 32768
+
+
+async def test_a_queued_bare_request_routed_to_the_remote_profile_takes_its_window(tmp_path: Path) -> None:
+ registry = ModelRegistry(models_dir=write_models(tmp_path), device="cpu", enable_hot_reload=False)
+ adapter = await registry.load_async(REMOTE, device="cpu")
+ assert isinstance(adapter, OpenAIUpstreamAdapter)
+ answer(adapter, [IN_WINDOW, OVER_WINDOW])
+ bridged = {"display_model": MODEL, "fallback_reason": "model_loading"}
+
+ try:
+ served = await run_queued(registry, stream=False, item=bridged)
+ refused = await run_queued(registry, stream=False, item=bridged)
+ finally:
+ await adapter.aclose_client()
+ await registry.unload_all_async()
+
+ assert served["finish_reason"] == "stop", served
+ assert served["usage"]["prompt_tokens"] == 100_000
+ assert refused["finish_reason"] == "error"