New models. Thinking Machines' Inkling 2 Mini by gostar (#584-#586): its text backbone and loop, the release's NVFP4 and MXFP8 checkpoint read by the converter, an int8 trunk, the vision tower, tensor parallelism, tool calls (with Warzuponus) and an image processor that matches transformers bit for bit (millaguie). Inkling's routed experts are served in IQR-V4's four-bit trellis (moe_gemm_iqr), fitted by GPTQ against Hessians the plugin now records (`--calib-dir`), its token embedding in int8 (read in place by embed_lookup), and incoai's DFlash2 drafter in four bits in place of the release's DSpark. That container, 14.4 GiB with the vision tower, decodes 190 / 427 / 658 tokens a second at 1 / 4 / 8 streams at tp 2 on two R9700 with `draft-window: adaptive` (code 240 / 550 / 790, step-by-step arithmetic 358 / 811 / 1116): 19-43% ahead of the same target with the release's DSpark drafter and 45-83% ahead of the all-int8 container (26.1 GiB, 124 / 263 / 386), at a mean KL of 0.0066 against the release's weights (int8: 0.0042). DeepSeek-V4.1-Flash on 2.x by baKon (#583), with its vision tower, its IQ1_S and IQ2_XXS containers and linears and experts served in llama.cpp's own blocks; parts ported from mikoskinen/r9700-exact-serving by Dad_Vibes' fork. GLM-5.3-Flash on 2.x with its IQR / IQ8R container, an int8 latent cache and four-card serving by AnimusNull (#566). Gemma 4 dense on EXL3-V3 by keptn42 (#537), on the EXL3 dense rows and qwen35_exl3 tensor parallelism by Hero422. NVIDIA's 2-bit Nemotron-3.5-Bolt-74B-A9B on one card by keptn42 (#477). Wan 2.2 TI2V-5B text-to-video and first-frame video by rkcth (#535). LFM3-1.7B on RDNA3 iGPUs by DokterSmol (#578). Qwen3.8-27B in bf16 with the DFlash2 drafter, and bf16x12, bf16 weights packed losslessly in 12.25 bits, by eddie168 (#577). Int8 experts for drama-3 by morevgalex (#598). Speculative decoding. Draft trees for Qwen3.8-27B's DFlash2 drafts by Kezi (#450, opt-in): one card, +17% at one stream and +14% at two. --draft-max-lanes and --draft-min-accept, two gates on a block drafter, and --draft-window-ckpt, its window KV kept in the checkpoint slots, by rkcth (#600-#602). `draft-window: adaptive` verifies each sequence's drafts up to the first one unlikely to be kept. A verify's rows share each K and V tile in Inkling's attention by gostar (#608): 93 to 118 tokens a second at a 70k-token prompt on one card. Generation. Krea 2 instruction edits with one or two references and registered outfill, LoRA on the image generators with bias deltas and row-mixer adapters applied per request, and MiniMax-H3's model-declared sparse attention profiles, by rkcth (#534, #542, #543, #605-#609). MiniMax-H3's audio reference guide pinned from a frame by 1beb (#595). LTX-2's chunk ceiling raised to 40 by Itoash (#570). Fleets and memory. memory.queue-reserve, the hardware queues a card keeps ready, by kevinjd (#589). memory.pinned-numa-node by Foundry (#580). memory.card-headroom-mib on single-model tensor-parallel budgets by zihaomu (#624). Parked weights rebuilt on the CPU, memory ownership kept across a failed handoff and quiescent callbacks serialised, by rkcth (#557-#561). A warm load may unload a parked model (#592); the first card listed chooses the architecture (#458). Kernels. libr3d's packed dense IQR GEMM and routed IQR expert rows on gfx1100, and libhalo's GEMV decode for Qwen3.8-Flash-Next's MoE down, by zihaomu (#562, #620, #622). GGML IQ2_XXS and IQ3_XXS packed on the card by baKon. Dashboard. KV cache in tokens (in use, capacity now and with all flexible space), live figures through long prefill steps, step / decode / prefill / acceptance lines without holes, and expert hit rates only on tiered models. Seven tile and history defects fixed by Noblistic (#597), vision encoder and per-card hardware figures by davetha (#574), per-LoRA attribution by dowjames (#552). Fixed. return_tokens / return_token_ids on the OpenAI endpoints (#571). reasoning_effort "none" and "minimal" reach a template that takes them as levels, by millaguie (#608). Misaligned loads in the windowed paged attention over an int8 cache (#540). A retired 27B head refused by name (#541). rad-convert's GPU trellis jobs kept fed, and --reuse-only, by gostar (#575). jinja's tojson takes sort_keys by Warzuponus (#582). hfget measures file sizes with stat, by kepler_62f (#623). The tested C++ compiler is stated in the build guide by lowell80 (#551). The codebase was cleaned up: shared helpers instead of repeated blocks, and the int8 linear, activation and GEMM code no longer named after the retired fp8 formats. Inkling 2 Mini is served in one form: IQR-V4 experts with the DFlash2 drafter (StillDeadcode/inkling-2-mini-iq4r-v4 on Hugging Face, or data/recipes/inkling2-mini-iq4r-v4.recipe). A container with the release's DSpark drafter, or with NVFP4, int8 or 4-bit experts, is refused by name and needs converting again. Plugin ABI 44: out-of-tree plugins need to be rebuilt. Thanks to @gostar, @baKon, @Dad_Vibes, @AnimusNull, @keptn42, @Hero422, @rkcth, @DokterSmol, @eddie168, @morevgalex, @Kezi, @1beb, @Itoash, @kevinjd, @Foundry, @zihaomu, @Noblistic, @davetha, @dowjames, @Warzuponus, @millaguie, @kepler_62f, @lowell80 and @Code000.
822 lines
39 KiB
C++
822 lines
39 KiB
C++
/* loader.cpp -- dlopen, the ABI gate, the configured hierarchy, and the registration of one
|
|
* plugin's schemas and kernel rows. spec.md §2, §18.
|
|
*/
|
|
#include "registry.h"
|
|
#include "../device/device.h"
|
|
#include "../format/encoding.h"
|
|
#include "rad_abi_layout.h"
|
|
|
|
#include <dlfcn.h>
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include <algorithm>
|
|
#include <climits>
|
|
#include <cerrno>
|
|
#include <cstdarg>
|
|
#include <cstring>
|
|
|
|
namespace rad {
|
|
|
|
namespace {
|
|
|
|
/* dlsym returns void*; converting it to a function pointer is conditionally supported in
|
|
* standard C++ and works on every target here. The cast is isolated in one place. */
|
|
template <class Fn>
|
|
Fn dl_fn(void* h, const char* name) {
|
|
dlerror();
|
|
return reinterpret_cast<Fn>(dlsym(h, name));
|
|
}
|
|
|
|
std::string stem_of(const std::string& path) {
|
|
size_t slash = path.find_last_of('/');
|
|
std::string base = slash == std::string::npos ? path : path.substr(slash + 1);
|
|
if (base.size() > 3 && base.compare(base.size() - 3, 3, ".so") == 0)
|
|
base.resize(base.size() - 3);
|
|
if (base.compare(0, 3, "lib") == 0 && base.size() > 3) base.erase(0, 3);
|
|
return base;
|
|
}
|
|
|
|
const char* or_empty(const char* s) { return s ? s : ""; }
|
|
|
|
/* Whether a plugin's declared build target list (separated by spaces, commas or semicolons)
|
|
* names this device's architecture. */
|
|
bool target_covers(const std::string& declared, const char* arch) {
|
|
for (size_t i = 0; i < declared.size(); ) {
|
|
size_t e = declared.find_first_of(" ,;", i);
|
|
if (e == std::string::npos) e = declared.size();
|
|
if (e > i && declared.compare(i, e - i, arch) == 0) return true;
|
|
i = e + 1;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/* Two schemas for one op are the same when every positional slot means the same thing: parameter
|
|
* keys, types, requiredness (what the selector matches on) and roles (the core reads the first
|
|
* registrant's: a chunk or a capacity named by one plugin applies to every plugin's rows), and
|
|
* operand count, roles and optionality (arguments are positional). Operand names are for
|
|
* diagnostics only (ABI), so "b" and "w_gate" in slot 1 agree. */
|
|
bool schema_same(const RadOpSchema& a, const RadOpSchema& b, std::string* diff) {
|
|
if (a.n_params != b.n_params) {
|
|
*diff = fmt("%d parameters vs %d", a.n_params, b.n_params);
|
|
return false;
|
|
}
|
|
for (int i = 0; i < a.n_params; ++i) {
|
|
const RadParamSpec& x = a.params[i];
|
|
const RadParamSpec& y = b.params[i];
|
|
if (std::strcmp(or_empty(x.key), or_empty(y.key)) != 0) {
|
|
*diff = fmt("parameter %d is '%s' vs '%s'", i, or_empty(x.key), or_empty(y.key));
|
|
return false;
|
|
}
|
|
if (x.type != y.type) {
|
|
*diff = fmt("parameter '%s' is %s vs %s", or_empty(x.key), param_kind_name(x.type),
|
|
param_kind_name(y.type));
|
|
return false;
|
|
}
|
|
if (x.required != y.required) {
|
|
*diff = fmt("parameter '%s' is %s vs %s", or_empty(x.key),
|
|
x.required ? "required" : "optional", y.required ? "required" : "optional");
|
|
return false;
|
|
}
|
|
if (x.role != y.role) {
|
|
*diff = fmt("parameter '%s' has role %d vs %d", or_empty(x.key), x.role, y.role);
|
|
return false;
|
|
}
|
|
}
|
|
if (a.n_operands != b.n_operands) {
|
|
*diff = fmt("%d operands vs %d", a.n_operands, b.n_operands);
|
|
return false;
|
|
}
|
|
for (int i = 0; i < a.n_operands; ++i) {
|
|
if (a.operands[i].role != b.operands[i].role ||
|
|
a.operands[i].optional != b.operands[i].optional) {
|
|
*diff = fmt("operand %d ('%s' / '%s') has a different role", i,
|
|
or_empty(a.operands[i].name), or_empty(b.operands[i].name));
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} /* namespace */
|
|
|
|
/* ------------------------------------------------------------------ diagnostics */
|
|
void Registry::err_(const char* f, ...) {
|
|
char msg[2048];
|
|
va_list ap;
|
|
va_start(ap, f);
|
|
vsnprintf(msg, sizeof msg, f, ap);
|
|
va_end(ap);
|
|
set_->load_errors.emplace_back(msg);
|
|
RAD_ERR("%s", msg);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ staging */
|
|
/* dlopen and read the plugin's identity without registering anything. Loading has two phases
|
|
* because the hierarchy is ordered by plugin name, which lives inside the .so: the whole directory
|
|
* is opened before anything registers, and registration order fixes an op's schema. */
|
|
int Registry::stage_(const std::string& path, Staged* out) {
|
|
out->path = path;
|
|
out->file = stem_of(path);
|
|
|
|
/* RTLD_LOCAL is required: every kernel plugin exports the same symbol names (rad_kernel_count
|
|
* and friends), so with RTLD_GLOBAL the first plugin loaded would answer for all later ones.
|
|
* Symbols are looked up per handle. */
|
|
out->code_from = rad::dev_code_mark();
|
|
out->handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
|
out->code_to = rad::dev_code_mark();
|
|
if (!out->handle) {
|
|
err_("plugin '%s' failed to load: %s", path.c_str(), or_empty(dlerror()));
|
|
return RAD_E_IO;
|
|
}
|
|
|
|
auto abi = dl_fn<uint32_t (*)(void)>(out->handle, "rad_plugin_abi_version");
|
|
if (!abi) {
|
|
err_("'%s' is not a radiance plugin: it exports no rad_plugin_abi_version", path.c_str());
|
|
unstage_(*out);
|
|
return RAD_E_ABI;
|
|
}
|
|
const uint32_t v = abi();
|
|
if (v != RAD_ABI_VERSION) {
|
|
/* Named by file, not RadPluginInfo::name: that struct's layout may differ across ABI
|
|
* versions. Both versions are printed so the operator can tell which side is stale. */
|
|
err_("plugin '%s' was built against ABI version %u; this engine is version %u -- refused",
|
|
path.c_str(), v, (unsigned)RAD_ABI_VERSION);
|
|
unstage_(*out);
|
|
return RAD_E_ABI;
|
|
}
|
|
/* One version can name two layouts (two trees bumping to the same number): the fingerprint
|
|
* of the structs as the plugin was compiled is checked against the engine's own. */
|
|
auto layout = dl_fn<uint64_t (*)(void)>(out->handle, "rad_plugin_abi_layout");
|
|
if (!layout) {
|
|
err_("plugin '%s' reports ABI version %u but exports no rad_plugin_abi_layout: rebuild it "
|
|
"against this engine's abi/ headers, with RAD_PLUGIN_ABI_LAYOUT_EXPORT once in it "
|
|
"(rad_abi_layout.h) -- refused", path.c_str(), v);
|
|
unstage_(*out);
|
|
return RAD_E_ABI;
|
|
}
|
|
static const uint64_t mine = rad_abi_layout();
|
|
if (const uint64_t theirs = layout(); theirs != mine) {
|
|
err_("plugin '%s' reports ABI version %u, this engine's, but was built against another "
|
|
"layout of it (fingerprint %016llx, this engine's %016llx): its abi/ headers and the "
|
|
"engine's disagree on a struct; rebuild it against this engine's -- refused",
|
|
path.c_str(), v, (unsigned long long)theirs, (unsigned long long)mine);
|
|
unstage_(*out);
|
|
return RAD_E_ABI;
|
|
}
|
|
|
|
auto info_fn = dl_fn<const RadPluginInfo* (*)(void)>(out->handle, "rad_plugin_info");
|
|
if (!info_fn || !(out->info = info_fn())) {
|
|
err_("plugin '%s' exports no usable rad_plugin_info", path.c_str());
|
|
unstage_(*out);
|
|
return RAD_E_INVAL;
|
|
}
|
|
out->name = out->info->name && out->info->name[0] ? out->info->name : out->file;
|
|
if (auto ref = dl_fn<RadPluginReferenceFn>(out->handle, "rad_plugin_reference"))
|
|
out->reference = out->info->kind == RAD_PLUGIN_KERNEL && ref() != 0;
|
|
return RAD_OK;
|
|
}
|
|
|
|
void Registry::unstage_(Staged& s) {
|
|
if (s.handle) dlclose(s.handle);
|
|
s.handle = nullptr;
|
|
s.info = nullptr;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ registration */
|
|
int Registry::commit_(Staged& s, int order) {
|
|
for (const Plugin& p : set_->plugins) {
|
|
if (p.name == s.name) {
|
|
err_("two plugins claim the name '%s': %s and %s -- refusing the second",
|
|
s.name.c_str(), p.path.c_str(), s.path.c_str());
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
}
|
|
|
|
Plugin p;
|
|
p.name = s.name;
|
|
p.file = s.file;
|
|
p.path = s.path;
|
|
p.version = or_empty(s.info->version);
|
|
p.description = or_empty(s.info->description);
|
|
p.build_target = or_empty(s.info->build_target);
|
|
p.kind = s.info->kind;
|
|
p.reference = s.reference;
|
|
p.order = order;
|
|
p.handle = s.handle;
|
|
p.close = dl_fn<RadPluginCloseFn>(s.handle, "rad_plugin_close");
|
|
p.freed = dl_fn<RadPluginFreedFn>(s.handle, "rad_plugin_freed");
|
|
|
|
std::vector<const RadOpSchema*> staged_schemas;
|
|
std::vector<std::pair<const RadKernelInfo*, int>> staged_rows; /* the row and its index */
|
|
std::vector<const RadQuantizerInfo*> staged_quants;
|
|
int n_declared_schemas = 0;
|
|
|
|
if (p.kind == RAD_PLUGIN_ARCH) {
|
|
auto id_fn = dl_fn<const char* (*)(void)>(s.handle, "rad_arch_id");
|
|
p.arch_declare = dl_fn<int (*)(RadBuilder*, const RadModelMeta*, const RadBuildCtx*)>(
|
|
s.handle, "rad_arch_declare");
|
|
p.arch_step = dl_fn<void (*)(RadCtx*, const RadBatch*)>(s.handle, "rad_arch_step");
|
|
p.arch_pass_key = dl_fn<uint64_t (*)(RadCtx*, const RadBatch*, const int32_t*)>(
|
|
s.handle, "rad_arch_pass_key");
|
|
if (!id_fn || !p.arch_declare || !p.arch_step) {
|
|
err_("architecture plugin '%s' is missing rad_arch_id, rad_arch_declare or "
|
|
"rad_arch_step", p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
p.arch_id = or_empty(id_fn());
|
|
if (p.arch_id.empty()) {
|
|
err_("architecture plugin '%s' returns an empty architecture id", p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* The second half of the key. Optional: a plugin without it serves an unquantised
|
|
* container, which "" means. */
|
|
if (auto q_fn = dl_fn<const char* (*)(void)>(s.handle, "rad_arch_quant"))
|
|
p.arch_quant = or_empty(q_fn());
|
|
/* Optional: absent means the plugin may serve an imported quantisation (startup.h). */
|
|
if (auto i_fn = dl_fn<int (*)(void)>(s.handle, "rad_arch_serves_imports"))
|
|
p.arch_serves_imports = i_fn() != 0;
|
|
/* Optional: absent means one engine at a time (rad_engine_claim). */
|
|
if (auto m_fn = dl_fn<int (*)(void)>(s.handle, "rad_arch_multi_instance"))
|
|
p.arch_multi_instance = m_fn() != 0;
|
|
|
|
/* Optional: answers what the core needs before it builds the RadBuildCtx for declare.
|
|
* Absent means every answer is zero (rad_builder.h). */
|
|
p.arch_probe = dl_fn<int (*)(const RadModelMeta*, RadArchProbe*)>(s.handle,
|
|
"rad_arch_probe");
|
|
/* Optional: absent means the reply format is read off the chat template. */
|
|
p.arch_chat_format = dl_fn<const RadChatFormat* (*)(const RadModelMeta*)>(
|
|
s.handle, "rad_arch_chat_format");
|
|
/* Optional: absent means the model serves no speech route. */
|
|
p.arch_audio_format = dl_fn<const RadAudioFormat* (*)(const RadModelMeta*)>(
|
|
s.handle, "rad_arch_audio_format");
|
|
/* Optional: absent means an audio generator's prompt takes a request's texts as given. */
|
|
p.arch_text_prepare = dl_fn<int64_t (*)(int, const char*, char*, int64_t)>(
|
|
s.handle, "rad_arch_text_prepare");
|
|
/* A video generator's rotary positions; the driver refuses to start without it. */
|
|
p.arch_video_positions = dl_fn<int64_t (*)(int, const RadVideoGeom*, float*, int64_t)>(
|
|
s.handle, "rad_arch_video_positions");
|
|
/* Its reference conditioning, which the video driver refuses to serve without. */
|
|
p.arch_video_pack = dl_fn<int64_t (*)(const RadVideoGeom*, const int32_t*, int32_t*,
|
|
int32_t*, int32_t*, int32_t*, float*, int64_t)>(
|
|
s.handle, "rad_arch_video_pack");
|
|
p.arch_encode_geom = dl_fn<int64_t (*)(int32_t, const int64_t*, int32_t*, int64_t)>(
|
|
s.handle, "rad_arch_encode_geom");
|
|
|
|
/* The (architecture id, quantisation) pair selects the plugin, so no two plugins may claim
|
|
* the same pair (§2.4). Sharing an id at different quantisations is normal (qwen35_bf16
|
|
* and qwen35_fp8). */
|
|
for (const Plugin& q : set_->plugins) {
|
|
if (q.arch_id == p.arch_id && q.arch_quant == p.arch_quant) {
|
|
err_("architecture id '%s' at quantisation '%s' is claimed by both '%s' and '%s' "
|
|
"-- refusing the second", p.arch_id.c_str(),
|
|
p.arch_quant.empty() ? "(unquantised)" : p.arch_quant.c_str(),
|
|
q.name.c_str(), p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
}
|
|
} else if (p.kind == RAD_PLUGIN_KERNEL) {
|
|
auto sc_n = dl_fn<int (*)(void)>(s.handle, "rad_kernel_schema_count");
|
|
auto sc_at = dl_fn<const RadOpSchema* (*)(int)>(s.handle, "rad_kernel_schema_at");
|
|
auto k_n = dl_fn<int (*)(void)>(s.handle, "rad_kernel_count");
|
|
auto k_at = dl_fn<const RadKernelInfo* (*)(int)>(s.handle, "rad_kernel_at");
|
|
if (!sc_n || !sc_at || !k_n || !k_at) {
|
|
err_("kernel plugin '%s' is missing rad_kernel_schema_count/at or "
|
|
"rad_kernel_count/at", p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
|
|
/* All schemas are checked before any is committed, so a plugin refused on one op leaves
|
|
* nothing registered. */
|
|
n_declared_schemas = sc_n();
|
|
for (int i = 0; i < n_declared_schemas; ++i) {
|
|
const RadOpSchema* sch = sc_at(i);
|
|
if (!sch || !sch->op || !sch->op[0]) {
|
|
err_("kernel plugin '%s' declares a schema with no op name at index %d",
|
|
p.name.c_str(), i);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* The counts are the plugin's word; the tables they index are walked at every
|
|
* selection and every issue, so a null table or an operand count past what an issue
|
|
* can carry (MAX_OPERANDS) is refused here, by name, rather than read there. */
|
|
if (sch->n_params < 0 || (sch->n_params && !sch->params) || sch->n_operands < 0 ||
|
|
sch->n_operands > MAX_OPERANDS || (sch->n_operands && !sch->operands)) {
|
|
err_("kernel plugin '%s': op '%s' declares %d parameters and %d operands, a table "
|
|
"missing, or more operands than an issue carries (%d)", p.name.c_str(),
|
|
sch->op, sch->n_params, sch->n_operands, MAX_OPERANDS);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
for (int q = 0; q < sch->n_params; ++q) {
|
|
const RadParamSpec& ps = sch->params[q];
|
|
if (!ps.key || !ps.key[0] || ps.role < RAD_PROLE_NONE ||
|
|
ps.role > RAD_PROLE_CAPACITY) {
|
|
err_("kernel plugin '%s': op '%s' parameter %d has no key or an unknown role "
|
|
"(%d)", p.name.c_str(), sch->op, q, ps.role);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
}
|
|
std::string diff;
|
|
auto it = set_->schemas.find(sch->op);
|
|
if (it != set_->schemas.end()) {
|
|
if (!schema_same(*it->second.schema, *sch, &diff)) {
|
|
/* Disagreeing positional schemas would silently change a call's meaning
|
|
* (§2.3). The whole plugin is refused, not just the op: it was built against
|
|
* a different vocabulary. */
|
|
err_("op '%s' has two different schemas: '%s' fixed it first, '%s' disagrees "
|
|
"(%s) -- refusing '%s'",
|
|
sch->op, it->second.plugin.c_str(), p.name.c_str(), diff.c_str(),
|
|
p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
continue; /* same-shape re-declaration: expected, every plugin declares its ops */
|
|
}
|
|
for (const RadOpSchema* q : staged_schemas) {
|
|
if (std::strcmp(q->op, sch->op) == 0 && !schema_same(*q, *sch, &diff)) {
|
|
err_("kernel plugin '%s' declares op '%s' twice with different schemas (%s)",
|
|
p.name.c_str(), sch->op, diff.c_str());
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
}
|
|
staged_schemas.push_back(sch);
|
|
}
|
|
|
|
for (int i = 0, n = k_n(); i < n; ++i) {
|
|
const RadKernelInfo* k = k_at(i);
|
|
if (!k || !k->name || !k->op || !k->op[0]) {
|
|
err_("kernel plugin '%s' declares a kernel with no name or op at index %d",
|
|
p.name.c_str(), i);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* A row with no entry point would resolve and then fail to run. */
|
|
if (!k->launch) {
|
|
err_("kernel '%s' in plugin '%s' has no launch function", k->name, p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
if (k->domain != RAD_DOMAIN_DEVICE && k->domain != RAD_DOMAIN_HOST) {
|
|
err_("kernel '%s' in plugin '%s' declares domain %d, which is neither device nor "
|
|
"host", k->name, p.name.c_str(), k->domain);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* The selector reads every constraint of every row at every query, and the tuner every
|
|
* tunable, so their tables must exist wherever a count says they do. */
|
|
if (k->n_tunables < 0 || (k->n_tunables && !k->tunables) || k->n_constraints < 0 ||
|
|
(k->n_constraints && !k->constraints)) {
|
|
err_("kernel '%s' in plugin '%s' declares %d tunables and %d constraints with a "
|
|
"missing table", k->name, p.name.c_str(), k->n_tunables, k->n_constraints);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
for (int c = 0; c < k->n_constraints; ++c)
|
|
if (!k->constraints[c].key || !k->constraints[c].key[0] ||
|
|
(k->constraints[c].op == RAD_C_IN && !k->constraints[c].sval)) {
|
|
err_("kernel '%s' in plugin '%s' declares constraint %d with no key, or an IN "
|
|
"with no set", k->name, p.name.c_str(), c);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* Every tunable axis must carry a default that is one of its own values. Untuned
|
|
* shapes (no cache, an unmeasured shape, another card's cache) run the defaults, with
|
|
* no other fallback (spec §15). Checked at load rather than first use so a bad axis
|
|
* fails at startup, not when some shape first misses the cache. */
|
|
for (int v = 0; v < k->n_tunables; ++v) {
|
|
const RadTunable& t = k->tunables[v];
|
|
if (!t.key || !t.key[0] || !t.values || t.n_values <= 0) {
|
|
err_("kernel '%s' in plugin '%s' declares tunable %d with no key or no legal "
|
|
"values", k->name, p.name.c_str(), v);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
bool ok = false;
|
|
for (int j = 0; j < t.n_values; ++j) if (t.values[j] == t.deflt) ok = true;
|
|
if (!ok) {
|
|
err_("kernel '%s' in plugin '%s': tunable '%s' defaults to %lld, which is not "
|
|
"one of its declared values",
|
|
k->name, p.name.c_str(), t.key, (long long)t.deflt);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
for (int w = 0; w < v; ++w)
|
|
if (k->tunables[w].key && std::strcmp(k->tunables[w].key, t.key) == 0) {
|
|
err_("kernel '%s' in plugin '%s' declares tunable '%s' twice", k->name,
|
|
p.name.c_str(), t.key);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
}
|
|
staged_rows.emplace_back(k, i);
|
|
}
|
|
|
|
/* A kernel library with no code for this card has its device rows dropped: constraints
|
|
* do not say which card a row was compiled for, so the rows would resolve and then fail
|
|
* to launch, and an installation with libraries for several cards would serve only the
|
|
* first. Coverage is read from the fat binaries its dlopen registered, which the launch
|
|
* consults (core/device/aql_code.cpp), not from build_target. Host rows stay. On the host
|
|
* backend no GPU code runs at all (set_no_device_code). */
|
|
int n_device = 0;
|
|
for (const auto& r : staged_rows) n_device += r.first->domain == RAD_DOMAIN_DEVICE;
|
|
std::string why;
|
|
if (set_->no_device_code)
|
|
why = "no card is visible, so this process runs on the host backend and no GPU code "
|
|
"object runs here";
|
|
/* The code range decides only the per-card test: on the host backend the rows go even when
|
|
* this dlopen registered no fat binary (no HIP in the build, or the library already
|
|
* mapped by another registry). */
|
|
const bool no_code_here =
|
|
n_device > 0 &&
|
|
(set_->no_device_code ||
|
|
(s.code_to > s.code_from &&
|
|
rad::dev_code_runs(s.code_from, s.code_to, set_->code_device, &why) == 0));
|
|
if (no_code_here) {
|
|
std::vector<std::pair<const RadKernelInfo*, int>> keep;
|
|
for (const auto& r : staged_rows)
|
|
if (r.first->domain != RAD_DOMAIN_DEVICE) keep.push_back(r);
|
|
RAD_WARN("kernel plugin '%s' has no device code for device %d: %s. Its %d device "
|
|
"kernel(s) are left out of selection here%s.", p.name.c_str(), set_->code_device,
|
|
why.c_str(), n_device,
|
|
keep.empty() ? "" : fmt(" and its %zu host kernel(s) stay", keep.size()).c_str());
|
|
staged_rows.swap(keep);
|
|
p.device_code_absent = true;
|
|
}
|
|
} else if (p.kind == RAD_PLUGIN_QUANT) {
|
|
auto q_n = dl_fn<int (*)(void)>(s.handle, "rad_quant_count");
|
|
auto q_at = dl_fn<const RadQuantizerInfo* (*)(int)>(s.handle, "rad_quant_at");
|
|
if (!q_n || !q_at) {
|
|
err_("quantiser plugin '%s' is missing rad_quant_count or rad_quant_at",
|
|
p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* All checked before any is registered, as with kernel schemas. */
|
|
for (int i = 0, n = q_n(); i < n; ++i) {
|
|
const RadQuantizerInfo* q = q_at(i);
|
|
if (!q || !q->name || !q->name[0] || !q->encoding || !q->quantize) {
|
|
err_("quantiser plugin '%s' declares a quantiser with no name, no encoding hook or "
|
|
"no quantize hook at index %d", p.name.c_str(), i);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
/* Quantiser names are unique, so a recipe means the same thing whatever the load
|
|
* order. */
|
|
auto clash = [&](const char* other) { return std::strcmp(other, q->name) == 0; };
|
|
for (const QuantRow& r : set_->quants)
|
|
if (clash(r.info->name)) {
|
|
err_("quantiser '%s' is offered by both '%s' and '%s' -- refusing '%s'",
|
|
q->name, r.plugin.c_str(), p.name.c_str(), p.name.c_str());
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
for (const RadQuantizerInfo* o : staged_quants)
|
|
if (clash(o->name)) {
|
|
err_("quantiser plugin '%s' declares '%s' twice", p.name.c_str(), q->name);
|
|
unstage_(s);
|
|
return RAD_E_DUPLICATE;
|
|
}
|
|
staged_quants.push_back(q);
|
|
}
|
|
} else {
|
|
err_("plugin '%s' declares kind %u, which is not a kernel, architecture or quantiser "
|
|
"plugin", p.name.c_str(), p.kind);
|
|
unstage_(s);
|
|
return RAD_E_INVAL;
|
|
}
|
|
|
|
/* Called once after load, before any selection (rad_abi.h). A plugin that cannot open is
|
|
* refused. */
|
|
if (auto open = dl_fn<RadPluginOpenFn>(s.handle, "rad_plugin_open")) {
|
|
int st = open();
|
|
/* RAD_E_UNSUPPORTED: the plugin does not serve this machine (e.g. a library for another
|
|
* card). A skip, not a failure. */
|
|
if (st == RAD_E_UNSUPPORTED) {
|
|
RAD_INFO("plugin '%s' declines this machine and is not loaded", p.name.c_str());
|
|
unstage_(s);
|
|
return 1;
|
|
}
|
|
if (st < 0) {
|
|
err_("plugin '%s' refused to open: %s", p.name.c_str(), rad_strerror(st));
|
|
unstage_(s);
|
|
return st;
|
|
}
|
|
}
|
|
|
|
/* What the plugin declares, including ops an earlier plugin already fixed; schema_owner()
|
|
* answers which plugin fixed an op. */
|
|
p.n_schemas = n_declared_schemas;
|
|
p.n_kernels = (int)staged_rows.size();
|
|
for (const RadOpSchema* sch : staged_schemas) set_->schemas[sch->op] = SchemaEntry{sch, p.name};
|
|
for (const RadQuantizerInfo* q : staged_quants) {
|
|
set_->quants.push_back(QuantRow{ q, p.name });
|
|
/* Register its decoder so every weight reader can decode its schemes. */
|
|
if (q->decode) enc_add_decoder(q->decode);
|
|
}
|
|
const auto concurrent = dl_fn<RadKernelConcurrentFn>(s.handle, "rad_kernel_concurrent");
|
|
const auto collective = dl_fn<RadKernelCollectiveFn>(s.handle, "rad_kernel_collective");
|
|
const auto chainable = dl_fn<RadKernelChainFn>(s.handle, "rad_kernel_chain");
|
|
const auto fetches = dl_fn<RadKernelFetchesRowsFn>(s.handle, "rad_kernel_fetches_rows");
|
|
/* Per row when exported, else the library's own answer: a plugin that ships a device row and
|
|
* its host oracle in one library must not have the oracle's status leak onto the device row. */
|
|
const auto kern_ref = dl_fn<RadKernelReferenceFn>(s.handle, "rad_kernel_reference");
|
|
for (const auto& sr : staged_rows) {
|
|
KernelRow row;
|
|
row.info = sr.first;
|
|
row.plugin = p.name;
|
|
row.plugin_order = order;
|
|
row.index = sr.second;
|
|
row.concurrent = concurrent && concurrent(sr.second) != 0;
|
|
const int coll = collective ? collective(sr.second) : 0;
|
|
row.collective = coll != 0;
|
|
row.releases_link = coll == 2;
|
|
row.reference = kern_ref ? kern_ref(sr.second) != 0 : p.reference;
|
|
const int chains = chainable ? chainable(sr.second) : 0;
|
|
row.chain = chains != 0;
|
|
row.chain_fenced = chains == 2;
|
|
row.chain = chainable && chainable(sr.second) != 0;
|
|
row.fetches_rows = fetches && fetches(sr.second) != 0;
|
|
set_->rows.push_back(row);
|
|
}
|
|
|
|
/* A library built for another card fails at every launch with an unhelpful driver error;
|
|
* warn once here instead. Only a warning: build_target is author-written text, not derived
|
|
* from the binary, so it cannot justify a refusal. "host" (CPU kernels) and "" (no claim) say
|
|
* nothing about a device. Applies to libraries whose dlopen registered no fat binaries. */
|
|
if (p.kind == RAD_PLUGIN_KERNEL && s.code_to == s.code_from && !p.build_target.empty() &&
|
|
p.build_target != "host") {
|
|
RadDeviceProps dp{};
|
|
if (rad_dev_count() > set_->code_device && rad_dev_props(set_->code_device, &dp) == RAD_OK &&
|
|
dp.arch[0] && !target_covers(p.build_target, dp.arch))
|
|
RAD_WARN("kernel plugin '%s' declares it was built for %s and this machine reports %s. "
|
|
"Its kernels will fail to launch; build it for %s, or leave it out with "
|
|
"--kernels.",
|
|
p.name.c_str(), p.build_target.c_str(), dp.arch, dp.arch);
|
|
}
|
|
|
|
RAD_DEBUG("plugin %d: %s %s (%s) -- %d kernels, %d schemas, %zu fat binaries, %s", order,
|
|
p.name.c_str(), p.version.c_str(), p.path.c_str(), p.n_kernels, p.n_schemas,
|
|
s.code_to - s.code_from, p.build_target.empty() ? "portable" : p.build_target.c_str());
|
|
if (p.freed) rad_dev_on_free(p.freed);
|
|
set_->plugins.push_back(std::move(p));
|
|
index_rows_();
|
|
s.handle = nullptr; /* the Plugin owns it now */
|
|
return RAD_OK;
|
|
}
|
|
|
|
/* The per-op index select() walks, in (hierarchy order, declaration order). Rebuilt whole on
|
|
* every registration; there are only hundreds of rows, at startup. */
|
|
void Registry::index_rows_() {
|
|
set_->by_op.clear();
|
|
for (const KernelRow& r : set_->rows) set_->by_op[r.info->op].push_back(&r);
|
|
for (auto& kv : set_->by_op) {
|
|
std::stable_sort(kv.second.begin(), kv.second.end(),
|
|
[](const KernelRow* a, const KernelRow* b) {
|
|
if (a->plugin_order != b->plugin_order)
|
|
return a->plugin_order < b->plugin_order;
|
|
return a->index < b->index;
|
|
});
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ the public loading API */
|
|
Registry::Registry() : set_(std::make_shared<Set>()) {}
|
|
|
|
std::unique_ptr<Registry> Registry::share() const {
|
|
return std::unique_ptr<Registry>(new Registry(set_));
|
|
}
|
|
|
|
int Registry::load_plugin(const std::string& path, int order) {
|
|
if (shared()) {
|
|
err_("%s: the plugin set is shared by a running engine; it is loaded once, before the first", path.c_str());
|
|
return RAD_E_STATE;
|
|
}
|
|
Staged s;
|
|
int st = stage_(path, &s);
|
|
if (st < 0) return st;
|
|
return commit_(s, order);
|
|
}
|
|
|
|
int Registry::load_dir(const std::string& dir, const std::vector<std::string>& hierarchy) {
|
|
return load_dirs({ dir }, hierarchy);
|
|
}
|
|
|
|
int Registry::load_dirs(const std::vector<std::string>& dirs,
|
|
const std::vector<std::string>& hierarchy) {
|
|
if (shared()) {
|
|
err_("the plugin set is shared by a running engine; it is loaded once, before the first");
|
|
return RAD_E_STATE;
|
|
}
|
|
int first_error = RAD_OK;
|
|
|
|
/* Directories in the given order, files in filename order, so an unconfigured set loads
|
|
* reproducibly. A file name an earlier directory already supplied is shadowed: the operator's
|
|
* own plugins come ahead of an installation's, and loading both would be a duplicate name. */
|
|
std::vector<std::string> files, stems;
|
|
std::string where;
|
|
for (const std::string& dir : dirs) {
|
|
DIR* d = opendir(dir.c_str());
|
|
if (!d) {
|
|
err_("plugin directory '%s': %s", dir.c_str(), std::strerror(errno));
|
|
return RAD_E_IO;
|
|
}
|
|
where += (where.empty() ? "" : ", ") + dir;
|
|
std::vector<std::string> here;
|
|
while (const dirent* e = readdir(d)) {
|
|
const std::string n = e->d_name;
|
|
if (n.size() < 4 || n.compare(n.size() - 3, 3, ".so") != 0) continue;
|
|
const std::string path = dir + "/" + n;
|
|
struct stat st {};
|
|
if (stat(path.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) continue;
|
|
here.push_back(path);
|
|
}
|
|
closedir(d);
|
|
std::sort(here.begin(), here.end());
|
|
for (const std::string& f : here) {
|
|
const std::string stem = stem_of(f);
|
|
auto at = std::find(stems.begin(), stems.end(), stem);
|
|
if (at != stems.end()) {
|
|
RAD_INFO("plugin %s is shadowed by %s, which comes first on the search path",
|
|
f.c_str(), files[(size_t)(at - stems.begin())].c_str());
|
|
continue;
|
|
}
|
|
files.push_back(f);
|
|
stems.push_back(stem);
|
|
}
|
|
}
|
|
|
|
std::vector<Staged> staged;
|
|
for (const std::string& f : files) {
|
|
Staged s;
|
|
int st = stage_(f, &s);
|
|
if (st < 0) {
|
|
if (first_error == RAD_OK) first_error = st;
|
|
continue;
|
|
}
|
|
staged.push_back(s);
|
|
}
|
|
|
|
/* An entry matches a library's declared name or its file: `libr4d`, `r4d`, `libr4d.so` and a
|
|
* path to it are one library. */
|
|
auto names = [](const std::string& h, const Staged& s) {
|
|
return h == s.name || h == s.file || stem_of(h) == s.file;
|
|
};
|
|
std::string found; /* every kernel library on the search path, for a list that names none */
|
|
for (const Staged& s : staged)
|
|
if (s.info && s.info->kind == RAD_PLUGIN_KERNEL)
|
|
found += (found.empty() ? "" : ", ") + s.name;
|
|
|
|
/* A non-empty hierarchy is exclusive. Ordering alone cannot keep a library out: selection
|
|
* runs per domain, so an op the named library has no device row for would fall to an unnamed
|
|
* one. Unnamed kernel libraries are closed before registering anything. Architecture and
|
|
* quantiser plugins are never ranked. */
|
|
if (!hierarchy.empty()) {
|
|
std::vector<Staged> kept;
|
|
std::string left_out;
|
|
for (Staged& s : staged) {
|
|
const bool kernel = s.info && s.info->kind == RAD_PLUGIN_KERNEL;
|
|
const bool named = std::find_if(hierarchy.begin(), hierarchy.end(),
|
|
[&](const std::string& h) {
|
|
return names(h, s);
|
|
}) != hierarchy.end();
|
|
if (!kernel || named) {
|
|
kept.push_back(s);
|
|
continue;
|
|
}
|
|
left_out += (left_out.empty() ? "" : ", ") + s.name;
|
|
unstage_(s);
|
|
}
|
|
staged.swap(kept);
|
|
if (!left_out.empty())
|
|
RAD_INFO("kernel libraries on the search path that --kernels (or RADIANCE_KERNELS) "
|
|
"does not name, and so not loaded: %s", left_out.c_str());
|
|
}
|
|
|
|
/* A named library takes its position in the list; unnamed ones follow. A reference library
|
|
* (RadPluginReferenceFn) is pinned below every other unnamed plugin, because it matches every
|
|
* op it declares and would make anything behind it unreachable. Naming it places it where
|
|
* named, e.g. ahead of a library to compare against. */
|
|
const size_t kUnnamed = (size_t)INT_MAX - 1;
|
|
std::vector<size_t> rank(staged.size());
|
|
for (size_t i = 0; i < staged.size(); ++i)
|
|
rank[i] = staged[i].reference ? (size_t)INT_MAX : kUnnamed;
|
|
for (size_t h = 0; h < hierarchy.size(); ++h) {
|
|
bool hit = false;
|
|
for (size_t i = 0; i < staged.size(); ++i) {
|
|
if (names(hierarchy[h], staged[i])) {
|
|
rank[i] = h;
|
|
hit = true;
|
|
}
|
|
}
|
|
/* The list is what loads: an entry that matches nothing loads nothing in its place. */
|
|
if (!hit)
|
|
RAD_WARN("--kernels (or RADIANCE_KERNELS) names '%s', which is no kernel library in "
|
|
"%s, so nothing loads in its place; the kernel libraries there are: %s",
|
|
hierarchy[h].c_str(), where.c_str(), found.empty() ? "none" : found.c_str());
|
|
}
|
|
std::vector<size_t> idx(staged.size());
|
|
for (size_t i = 0; i < idx.size(); ++i) idx[i] = i;
|
|
std::stable_sort(idx.begin(), idx.end(),
|
|
[&](size_t a, size_t b) { return rank[a] < rank[b]; });
|
|
|
|
/* Continue from what is already loaded, so loading kernels/ then architectures/ does not put
|
|
* two plugins at position 0. */
|
|
int order = 0;
|
|
for (const Plugin& p : set_->plugins) order = std::max(order, p.order + 1);
|
|
|
|
for (size_t i : idx) {
|
|
int st = commit_(staged[i], order);
|
|
if (st < 0) {
|
|
if (first_error == RAD_OK) first_error = st;
|
|
continue; /* refused, and the next plugin still gets its chance */
|
|
}
|
|
if (st > 0) continue; /* declined this machine */
|
|
++order;
|
|
}
|
|
|
|
int st = check_complete();
|
|
if (st < 0 && first_error == RAD_OK) first_error = st;
|
|
return first_error;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ the tuning cache (§15) */
|
|
int Registry::load_tune(const std::string& radiance_home, const std::string& machine) {
|
|
const std::string key = machine.empty() ? rad_machine_key() : machine;
|
|
const int s = set_->tune.load(radiance_home, key);
|
|
if (s < 0) return s;
|
|
/* `load` refuses a file whose machine key (line 2, tunecache.h) is another machine's, so
|
|
* these rows are this machine's. */
|
|
return (int)set_->tune.rows().size();
|
|
}
|
|
|
|
/* A row whose op has no schema in the loaded set cannot have its parameters checked, so a typo
|
|
* would become a silent miss. Checked once the set is complete, because an override plugin may
|
|
* rely on the schema of a plugin below it, which loads later. */
|
|
int Registry::check_complete() {
|
|
int st = RAD_OK;
|
|
for (const KernelRow& r : set_->rows) {
|
|
if (set_->schemas.find(r.info->op) == set_->schemas.end()) {
|
|
err_("kernel '%s' in plugin '%s' implements op '%s', which no loaded plugin declares a "
|
|
"schema for", r.info->name, r.plugin.c_str(), r.info->op);
|
|
st = RAD_E_NOSCHEMA;
|
|
}
|
|
}
|
|
return st;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ lookup and teardown */
|
|
const Plugin* Registry::plugin(std::string_view name) const {
|
|
for (const Plugin& p : set_->plugins)
|
|
if (p.name == name || p.file == name) return &p;
|
|
return nullptr;
|
|
}
|
|
|
|
const Registry::QuantRow* Registry::quantizer(std::string_view name) const {
|
|
for (const QuantRow& q : set_->quants)
|
|
if (name == q.info->name) return &q;
|
|
return nullptr;
|
|
}
|
|
|
|
const std::vector<const KernelRow*>* Registry::rows_for(std::string_view op) const {
|
|
auto it = set_->by_op.find(std::string(op));
|
|
return it == set_->by_op.end() ? nullptr : &it->second;
|
|
}
|
|
|
|
void Registry::close() {
|
|
fini_instances();
|
|
clear_selections();
|
|
device_facts_ = Geometry{};
|
|
/* Another registry still selects from the set: leave it loaded and take an empty one. Engines
|
|
* are created and destroyed by one thread (the fleet), so the count cannot race a share(). */
|
|
if (shared()) {
|
|
set_ = std::make_shared<Set>();
|
|
return;
|
|
}
|
|
for (const QuantRow& q : set_->quants)
|
|
if (q.info && q.info->decode) enc_remove_decoder(q.info->decode);
|
|
/* Reverse load order: a later plugin may depend on state an earlier one established. */
|
|
for (auto it = set_->plugins.rbegin(); it != set_->plugins.rend(); ++it) {
|
|
if (it->freed) rad_dev_off_free(it->freed);
|
|
if (it->close) it->close();
|
|
if (it->handle) dlclose(it->handle);
|
|
}
|
|
set_->plugins.clear();
|
|
set_->rows.clear();
|
|
set_->quants.clear();
|
|
set_->by_op.clear();
|
|
set_->schemas.clear();
|
|
set_->load_errors.clear();
|
|
}
|
|
|
|
Registry::~Registry() { close(); }
|
|
|
|
} /* namespace rad */
|