/* cvec.h -- control vectors: one GGUF direction per layer, projected out of the residual stream. * * ============================== what it is ============================== * * The package at huggingface.co/Cudecnik/Qwen3.8-Flash-Next-refusal-projection carries one unit * direction per layer (`direction.1` .. `direction.47`, 2560 f32 each) and the configuration its * authors selected: the llama.cpp `--cvec-mode project` operation * * h <- h - s_l (h . v_l) v_l v_l = d_l / |d_l|, s_l = |d_l| * * over layers 4..44, applied to every stream of the gated residual. Strata ports the same * package (`data/experimental-speed-projection`) with the same operation, range and per-request * switch; this is that port for radiance, which has its own graph and cannot use llama.cpp's * patch (arch/qwen4exp_fp8 issues `hc_project` from the op vocabulary instead). * * The vector is a refusal-direction projection: with it on the model declines far fewer requests. * Removing refusals removes a safety behaviour, so the default is OFF and every request names its * own choice, and the file is only read when the engine finds one for this container. * * ============================== compatibility, checked, not assumed ============================== * * Nothing here trusts the file's provenance. A vector is refused unless * * - `general.architecture` is `controlvector`, * - `controlvector.model_hint`, when present, names this model's architecture plugin * (`qwen4exp`), since a direction from another architecture's residual basis is noise, * - every tensor is `direction.`, F32, one dimension, and exactly the model's `n_embd`, * - the layer range lies inside the model and every layer in it has a direction. * * The published Flash-Next vector passes all four against a Flash-Next container: 47 tensors of * 2560 f32, hint `qwen4exp`, range 4..44 of 48 layers. A 27B, Gemma or Qwen3.6 container refuses * it at startup with the reason, rather than serving a model whose hidden states are moved along * an unrelated direction. */ #pragma once #include "rad_internal.h" #include #include namespace rad { /* What a loaded vector keeps on the host, before it is uploaded per rank. `dir` is * [n_layers * n_embd] f32, unit rows, row 0 unused (the format has no direction for layer 0); * `scales[l]` is |d_l| * user scale, or 0 where the layer is outside the loaded range. */ struct ControlVector { std::vector dir; std::vector scales; int64_t n_embd = 0; int64_t n_layers = 0; int64_t n_dirs = 0; /* directions the file carried (47 for the published one) */ int first = -1, last = -1; /* inclusive layer range */ float user_scale = 1.0f; std::string path; bool loaded() const { return n_layers > 0 && n_embd > 0; } /* Whether layer `l` is steered, i.e. a kernel has something to project. Mirror of the loader's * range check, used by the architecture plugin at declare. */ bool covers(int64_t l) const { return loaded() && l >= first && l <= last && l < (int64_t) scales.size() && scales[(size_t) l] != 0.0f; } }; /* Read and validate `path` against a model of `n_embd` and `n_model_layers`. * * The operation's parameters come with the package, never from a command line: `path`'s own * `.json` (the JSON the vector is published with) supplies `layer_range` and `scale` when it * says `"mode": "project"`. Without one, the range is the published Qwen3.8-Flash-Next selection * (4..44) where the file has directions there, else every layer the file carries, at scale 1. */ int control_vector_load(const std::string& path, const std::string& arch, int64_t n_embd, int64_t n_model_layers, ControlVector* out, std::string* err); /* Where a vector for `model_path` is looked for: * the container's sidecar, then one generic name in the container's directory, then each * $RADIANCE_HOME's `vectors/` directory. Mirrors gen_sidecar_path's naming so an operator who * already knows the generation-config convention knows this one. */ std::string control_vector_sidecar_path(const std::string& model_path); /* Resolve `auto`: sets *found to the first candidate that exists and is non-empty, or to "" when * none does. *tried receives the candidates, for the log line that says why the feature is * unavailable on this deployment. Discovery is a filesystem test only; whether the file is * compatible is control_vector_load's answer. */ int control_vector_discover(const std::string& model_path, const std::string& arch, const std::string& radiance_home, std::string* found, std::string* tried); /* Build the ABI view for one rank. `dir_device` is that rank's [n_layers, n_embd] f32 copy. */ RadControlVector control_vector_view(const ControlVector& cv, const float* dir_device); /* Upload the host table to a fresh device buffer on rank `rank`, or null on failure. The caller * owns the buffer and frees it with rad_dev_free. */ const float* control_vector_upload(const ControlVector& cv, int rank); /* The layer salt a cache uses so that blocks and linear-state snapshots computed with the mode on * are never handed to a request with it off (or to another vector's). 0 for "off", which is also * the value the engine uses with no vector loaded, so a deployment without the feature hashes * exactly as it did before it existed. */ uint64_t control_vector_salt(const ControlVector& cv); } /* namespace rad */