Plugins¶
qix has two plug points:
- The C plugin ABI: a set of C99 headers under
include/tnd/plugin/. A plugin is a shared library that fills call tables declared there. The plugin host (csrc/plugin_host/) is the engine code that loads such a library and checks it. - The Python provider registry:
python/tnd/providers.py. A provider is a Python object that replaces one open Python module.
The open engine needs neither: it runs its own built-in code for every plug point. This page says what each plug point is, which parts are frozen, what the plugin host loads, and which kinds the engine calls at this commit.
Terms used on this page:
- Kind: one plug point of the C ABI, with one call table (for example
TND_KIND_KERNELS). - Provider: one implementation of one kind (C) or one slot (Python).
- Builtin: the engine's own provider of a kind, named
builtin.<kind>. - Provider table: the list, one row per kind, of which provider runs it. Each process prints it at start.
- Slot: one replaceable Python module in the provider registry.
The C plugin ABI¶
include/tnd/plugin/plugin.h includes every header. The ABI version is 1.0
(TND_PLUGIN_ABI_MAJOR, TND_PLUGIN_ABI_MINOR in include/tnd/plugin/tnd_plugin.h). A plugin
built for another major version is refused.
Kinds¶
Kind ids are fixed forever (include/tnd/plugin/tnd_plugin.h). "Frozen v1" means the kind's
existing calls never change after the v1 freeze; the kind grows only by appending. "Version 0"
means the kind is in the ABI but its calls may change in a minor release.
| Id | Kind | Status | Header | What it plugs |
|---|---|---|---|---|
| 1 | TND_KIND_TUNING |
frozen v1 | tuning.h |
Choosing the serving plan at start, and the autotune window tick |
| 2 | TND_KIND_KERNELS |
frozen v1 | kernels.h |
Named op points (paged_decode, linear_w8a8, sample, ...) |
| 3 | TND_KIND_ALLOCATOR |
frozen v1 | allocator.h |
Device memory, with a pool handle per model |
| 4 | TND_KIND_COMM |
frozen v1 | comm.h |
Collectives and the expert-parallel exchange |
| 5 | TND_KIND_ROUTING |
frozen v1 | routing.h |
Which copy a request goes to |
| 6 | TND_KIND_FUSED_BLOCK |
frozen v1 | fused_block.h |
A whole chain of a layer's ops as one call |
| 7 | TND_KIND_CONTROL |
version 0 | control.h |
Knobs read and set at run time |
| 8 | TND_KIND_ADMISSION |
frozen v1 | admission.h |
Identity, quota and capacity checks on a request |
| 9 | TND_KIND_EVENTS |
frozen v1 | events.h |
Per-request and security event records |
| 10 | TND_KIND_MODEL_SOURCE |
frozen v1 | model_source.h |
Where model files are read from |
| 11 | TND_KIND_TOKEN_POLICY |
version 0 | token_policy.h |
A per-row mask of tokens a request may sample |
| 12 | TND_KIND_STREAM_FILTER |
version 0 | stream_filter.h |
A filter on streamed text |
| 13 | TND_KIND_KV_LIFECYCLE |
version 0 | kv_lifecycle.h |
KV blocks that change owner |
| 14 | TND_KIND_CHANNEL_SECURITY |
version 0 | channel_security.h |
A hook on the rank-to-rank sockets |
| 15 | TND_KIND_SCHEDULING |
version 0 | scheduling.h |
Queue order and preemption, one call per step |
| 16 | TND_KIND_KV_TIER |
version 0 | kv_tier.h |
KV tiers below the GPU pool (host RAM and lower) |
| 17 | TND_KIND_KV_TRANSFER |
reserved | none | No header yet |
Three more headers carry shared types, all frozen at v1:
tensor.h: a borrowed tensor, layout-compatible with DLPack'sDLTensor;request.h: the 96-byte request context every kind sees;json.h: the rules for the JSON strings that cross the ABI (TUNING and FUSED_BLOCK use them).
Rules every header follows¶
These are stated at the top of include/tnd/plugin/tnd_plugin.h:
- C99, no C++ types. Each header compiles alone as C and as C++.
- Every struct and every call table starts with
uint32_t struct_size. Structs and tables are append-only: a new field or call goes at the end, and a reader reads only what fits in thestruct_sizeit was given. An array element that has nostruct_sizeof its own (tnd_http_header,tnd_event) is stepped through by the stride the host passes. - Fixed-width integers only. Padding is written out as
reservedfields set to zero. Kind ids, status codes and verdicts areuint32_twith#definevalues, not C enums. - A plugin never throws, never calls
exit, and logs only through the host'slogcall. - A version-0 kind may change in a minor release. A frozen kind's calls never change.
- A JSON payload is one object with a required top-level
"schema"key, and its keys are append-only within a schema id (json.h).
What a plugin library exports¶
One symbol, tnd_plugin_get_v1 (TND_PLUGIN_ENTRY_SYMBOL). It returns a tnd_plugin_desc:
| Field | Meaning |
|---|---|
abi_major, abi_minor |
The ABI version the plugin was built against |
name, version, vendor, license_id |
Who the plugin is; license_id is an SPDX id |
init, shutdown |
Called once each. init gets the host's services (tnd_host_api) and may refuse with a reason. |
providers, n_providers |
The providers, one per kind it implements. Read them with tnd_provider_at, which steps by providers[0].struct_size. |
Each tnd_provider names its kind, the kind version it fills (kind_abi_version), a
"vendor.thing" name (builtin.* is reserved for the engine) and a pointer to the kind's call
table.
tests/plugin_abi_null_plugin.c is a complete plugin in plain C99. It fills one call table of
every kind with calls that do nothing. The build compiles it as C99 with -pedantic -Werror
(CMakeLists.txt:1967-1972), and tests/test_plugin_abi.cpp reads its descriptor back.
A compile-time layout check for plugin authors¶
include/tnd/plugin/layout_check.h lets a plugin's own build find a header mismatch before
anything is loaded. It is opt-in: no other header includes it. Put these two lines at file scope
in one .c or .cpp file of the plugin:
#include "tnd/plugin/layout_check.h"
TND_PLUGIN_LAYOUT_CHECK();
The macro fails the compile when a pointer is not 8 bytes, when a struct or call table has a
size other than the v1 size, or when a table does not start with struct_size. It does not check
each field's offset; tests/test_plugin_abi.cpp does that. It works in C99, C11 and C++.
What a provider may and may not do¶
From the kind headers:
- Fallback. A provider replaces the builtin only when it loads and its ABI matches; otherwise
the engine runs the builtin and logs why (
tnd_plugin.h, header comment). The one exception isMODEL_SOURCE: when one is configured and fails to load, start-up stops (model_source.h). - Kernels. A kernel provider enqueues only on the stream it is given, never synchronizes the
device and never allocates inside
run. The engine picks a kernel once per op signature, at load, not per call (kernels.h). - Admission. Each stage writes only its own fields of the request context. The context is
read-only to every kind once the
admit_tokenschain returns (admission.h,request.h). - Driver thread.
KV_LIFECYCLEandSCHEDULINGare called on the per-step driver thread, at most once per step, with an array; never once per block or per request (kv_lifecycle.h,scheduling.h). With no provider loaded, neither makes a call. - Values. A
TUNINGorCONTROLprovider returns each value with its reason; a value with no measurement behind it says UNMEASURED (tuning.h,control.h). - Security mechanisms. The open engine has none on the rank sockets.
CHANNEL_SECURITYis a hook only: authentication and encryption are provider work (channel_security.h). The engine calls the hook after eachconnectand eachaccepton its four rank sockets (see "Which kinds the engine calls" below).
Loading¶
The plugin host reads one JSON configuration file, loads the libraries it names, checks each one
against the ABI, and keeps one provider table per process (csrc/plugin_host/plugin_host.h).
Where the file comes from. --plugin-config FILE on qix-serve
(csrc/serve_auto/tnd_serve_main.cpp:235) or on tnd.serve (python/tnd/serve.py:1867-1874),
else the environment variable TND_PLUGIN_CONFIG (csrc/plugin_host/loader.cpp:607-611). With
neither, the host opens no file and loads no library, and every kind runs its builtin.
qix-serve hands the file's absolute path to each rank and to the node router in
TND_PLUGIN_CONFIG (csrc/serve_auto/tnd_serve_main.cpp:293-300).
Which process loads which kinds (role_hosts, csrc/plugin_host/builtins.cpp:62-69):
| Process | Kinds it resolves |
|---|---|
qix-serve (the launcher) |
MODEL_SOURCE only |
each rank (python -m tnd.serve, through python/tnd/_plugins.py) |
every kind except ROUTING |
tnd-node-router |
ROUTING, and the identity stage of ADMISSION (trusted-network.md) |
A process does not open a library whose entry lists no kind it resolves.
The file (csrc/plugin_host/config.cpp:40-153):
{"version": 1,
"plugins": [{"name": "acme", "path": "/opt/tnd/plugins/libtnd-plugin-acme.so",
"enabled": true, "kinds": ["model_source", "kernels"],
"config": {"key_dir": "/etc/acme/keys"}}],
"prefer": {"allocator": "acme.vmm"}}
| Key | Rule |
|---|---|
version |
must be the integer 1 |
plugins[].name |
required, non-empty, used once |
plugins[].path |
required; the library to load |
plugins[].enabled |
optional, default true; false skips the entry |
plugins[].kinds |
required, non-empty; kind names in lower case (model_source, kernels, ...). A provider of a kind the entry does not list is refused. |
plugins[].config |
optional; an object of strings the plugin reads through the host's config_get call |
prefer |
optional; kind name to provider name, to pick one when two plugins offer the same kind |
What the host checks before it uses a library:
- The configuration file and each of its parent directories are owned by root or by the server's
user, with no group or world write (
csrc/plugin_host/hygiene.cpp:22-66). The check follows symbolic links first and reads the resolved path. - The library path is absolute, the process does not run set-uid, and the library passes the same
owner and mode rule (
csrc/plugin_host/hygiene.cpp:68-72). This is a guard against accidents and shared directories. It checks no hash and no signature. - The library exports
tnd_plugin_get_v1, and its descriptor has the engine's ABI major version and at least one provider (csrc/plugin_host/loader.cpp:277-303). - Each provider names a kind the engine knows, has a name that does not start with
builtin., fills a kind version the engine reads, and has a call table of at least this version's size (check_provider,csrc/plugin_host/loader.cpp:183-230). - The plugin's
initreturns success (csrc/plugin_host/loader.cpp:357-368).
What a refused plugin does. Its kinds keep the builtin, and the provider table says why. The
process starts. Two kinds are the exception, listed below: a refused model_source or admission
entry stops start-up.
What stops start-up. The process prints the reason and exits with status 2
(qix-serve: csrc/serve_auto/tnd_serve_main.cpp:289-292; a rank:
python/tnd/_plugins.py:108-110; the router: csrc/distributed/node_router_main.cpp:226-229):
- the configuration file cannot be read, fails the owner and mode rule, is not valid JSON, has an unknown key, has one key twice in an object, or has a field of the wrong type. The file may name a model source, so a bad file never falls back to the builtins.
- an entry that lists
model_sourcefails any check above, or itsopencall refuses the model. A configured model source has no fallback to the builtin reader. - an entry that lists
admissionfails any check above, offers no admission provider, or names a stage outside 1 to 3; orprefer.admissionnames a provider that did not load. A configured admission provider has no fallback:builtin.admissionadmits every request, so falling back would open every route the operator meant to guard. An entry with"enabled": falseis not configured. - two plugins offer the same kind, the kind takes one provider, and
prefernames neither. The same holds for twoADMISSIONproviders of one stage.
The provider table. qix-serve prints a header line, one provider <kind> = ... row for each
of the 16 kinds, and one line per refusal (csrc/plugin_host/loader.cpp:496-536). Each rank logs
its own table. The router logs the header, its routing row and any refusal. With no
configuration the header reads plugins: none configured.
Which kinds the engine calls¶
A plugin of any kind is loaded and checked. The engine calls a provider of only four kinds at this
commit. For the other twelve, the header and the load check exist and the engine's own code still
runs: the table row reads loaded; not called in this build.
| Kind | What the engine calls | Where |
|---|---|---|
MODEL_SOURCE |
open before the first read of the model, in qix-serve and again in each rank; the rank then reads every model file under the directory the provider returned |
csrc/plugin_host/loader.cpp:686-769, python/tnd/_plugins.py:113-153 |
ADMISSION |
the identity stage only: admit on every request of all 10 HTTP routes, before the handler, and release when an inference request ends. The quota and capacity stages are not called. |
csrc/server/http_server.cpp:372-457, :953-1030 |
EVENTS |
drain, when the provider's own thread calls the host's events_pump. The engine writes request, usage, security and admin events for the classes the provider subscribed to. |
csrc/plugin_host/events.h, csrc/plugin_host/events.cpp:300-303 |
CHANNEL_SECURITY |
on_connect and on_accept on the four rank sockets, before the first byte, and close when a connection with a session ends. A provider that asks for the per-frame seal is refused and the socket is closed: the sealed path is not built. |
csrc/distributed/channel_hook.h, csrc/distributed/channel_hook.cpp:70-100 |
Not called at this commit: TUNING, KERNELS, ALLOCATOR, COMM, ROUTING, FUSED_BLOCK,
CONTROL, TOKEN_POLICY, STREAM_FILTER, KV_LIFECYCLE, SCHEDULING, KV_TIER.
Three more facts about the kinds that are called:
- Identity verdicts.
ADMITgoes on.DEGRADEgoes on and may lower the request'smax_tokens. Any other verdict refuses the request with the status the provider names, else403forREJECTand503for the rest. A provider call that fails answers500(csrc/server/http_server.cpp:408-427). The identity check runs before each route's own checks, for example before the loopback rule of the sleep routes. - The
EVENTStable row. The row of a loadedEVENTSprovider also saysloaded; not called in this build(csrc/plugin_host/builtins.cpp:30-31marks the kind as not wired), although the host does call itsdrain. - With no provider of a kind, the engine makes no call for that kind. The HTTP server and the rank sockets read their provider pointer once at start and test it afterwards.
The Python provider registry¶
python/tnd/providers.py lets a Python module be replaced without the open code importing the
replacement.
- Open code declares a slot with
declare_slot(Slot(name, version, open_module, required))and asks for it withresolve(name). - A provider wheel offers a provider through the
tnd.providersentry-point group. Each entry point is a callableregister(registry)that callsregistry.offer(slot, provider, name=..., version=...). resolvereturns the first offered provider whoseversionequals the slot's and that has everyrequiredname. Otherwise it imports the slot's open module. The result is cached; nothing here runs per step.- Provider names starting with
open.are reserved for the open modules.
Which wheels are loaded¶
Only a distribution the plugin configuration names is loaded. The configuration is a JSON file
given by TND_PLUGIN_CONFIG (PLUGIN_CONFIG_ENV), or by a path passed to configure(). An
enabled entry of its plugins array names a Python distribution with
config.python_distribution:
{"version": 1,
"plugins": [{"name": "acme", "path": "/opt/tnd/plugins/libtnd-plugin-acme.so",
"kinds": ["kernels"], "config": {"python_distribution": "acme-tnd"}}]}
- Every other installed
tnd.providersentry point is refused without being imported. - Before a named distribution is imported, its metadata,
entry_points.txtand module files must be owned by root or by the engine's user, with no group or world write, along their whole path. A distribution that fails is refused and not imported. This rule checks file ownership only; it checks no signature. - With no configuration, no distribution is named, so installing a package never changes which module runs.
The provider table¶
table() returns the Python provider table as lines: the configuration line, one line per
declared slot (python provider <slot> = <chosen> -- <origin>, with any refusals), one line per
named distribution that is not installed, and one line per refused entry point.
At this commit no open module declares a slot yet, so the table holds only the configuration line and any refusals, and every module the engine runs is the open one.