Example

Run one conformance check against three backend names

Pass the same generate contract to a toy backend and two unavailable stubs, and see why a passing check is not a model equivalence proof.

A multi-backend runtime needs one request contract and several adapters. The contract can be checked with a conformance suite; the suite can tell you that an adapter is callable and returns the expected shape. It cannot tell you that two models produce the same answers, or that an unavailable engine would pass if it were installed.

This example runs the same check against three names. pytorch-toy is available and passes because it returns hixx for hi plus two tokens. vllm and sglang are unavailable stubs, so they are recorded as available: false, passed: false with a reason. The output is an eligibility matrix, not a benchmark. The check is deliberately weak: a constant function could pass it.

Run it

From the canonical repository at commit f958967d70992d7b09d9c75000bd8c543f1644d4:

python examples/ch43/backends.py --json

Python 3.11 or later, standard library only. No engine installation, model, network, GPU or credentials. It finishes immediately.

Implementation

conformance returns early when a backend is unavailable, and otherwise calls generate("hi", 2) and compares the string. StubBackend.generate raises if it is called, so an unavailable row cannot accidentally produce a passing result.

class Backend(Protocol):
    def generate(self, prompt: str, max_new_tokens: int) -> str: ...


class PyTorchToyBackend:
    def __init__(self) -> None:
        self.available = True

    def generate(self, prompt: str, max_new_tokens: int) -> str:
        return prompt + ("x" * max_new_tokens)


class StubBackend:
    def __init__(self, name: str, reason: str) -> None:
        self.name = name
        self.reason = reason
        self.available = False

    def generate(self, prompt: str, max_new_tokens: int) -> str:
        raise NotImplementedError(self.reason)


def conformance(backend: Backend, name: str, available: bool, reason: str = "") -> ConformanceResult:
    if not available:
        return ConformanceResult(backend=name, available=False, passed=False, reason=reason)
    try:
        result = backend.generate("hi", 2)
        passed = result == "hixx"
    except Exception as exc:
        passed = False
        reason = str(exc)
    return ConformanceResult(backend=name, available=available, passed=passed, reason=reason)

Expected outcome

Observed with the command above (exit 0):

{
  "results": [
    {
      "backend": "pytorch-toy",
      "available": true,
      "passed": true,
      "reason": "",
      "metadata": {}
    },
    {
      "backend": "vllm",
      "available": false,
      "passed": false,
      "reason": "vLLM not installed",
      "metadata": {}
    },
    {
      "backend": "sglang",
      "available": false,
      "passed": false,
      "reason": "SGLang not installed",
      "metadata": {}
    }
  ]
}

Only the toy backend passed. The two external rows are absence records, not failures of vLLM or SGLang. The reasons name the missing installation, which is the information a reader needs to distinguish “not run” from “ran and failed”.

Mechanism and limitations

Documented: conformance returns available: false without calling an unavailable backend, and catches an exception from an available one. The toy backend appends one character per requested token. run_demo names three backends and records a reason for each. The tests check the toy pass, the stub unavailability, the stub’s NotImplementedError, the three-result demo and JSON serialisation.

Observed: the command exited 0 with the three result objects above. The targeted tests.test_ch43_backends is part of the canonical suite; no independent test count is claimed here.

Not shown: vLLM or SGLang behaviour, numerical equivalence between backends, batching, streaming, memory, or a real model. The conformance check expects one hard-coded string, so it tests the test’s expectation as much as the backend. A backend that always returns hixx would pass without doing any inference.

Challenge this result

Add a ConstantBackend that always returns hixx and run conformance. It should pass. Explain why that result is a flaw in the check, then design a conformance suite that would reject it: at least two prompts, a shape check, a tokenizer round-trip and a numerical comparison against a reference. Finally, state what must be true before two backends can be called equivalent.

Source and verification

Canonical source: examples/ch43/backends.py at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests: tests/test_ch43_backends.py . Chapter: Adding Multiple Model Backends . The run above is a fresh local execution on Windows 11 with Python 3.13.15; no external engine was installed or executed.

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.