Algorithm extension API¶
An algorithm transforms selected rows of one model component. The controller handles hooks, row selection, request slots, and conflict resolution. Payload loading and validation happen before execution. The contribution guide shows the registration and capability declaration needed to add an algorithm.
from vllm.model_hooks.steering.algorithms.base import BaseSteerVectorAlgorithm
from vllm.model_hooks.steering.algorithms.registry import register_algorithm
Transformation and graph contract¶
| Member | Extension contract |
|---|---|
_transform(hidden_state, params) |
Required. Return transformed selected rows with the same shape. params is one layer's prepared payload. |
set_payload(payload, scale_factor=1.0) |
Shared preparation: scale tensor payloads or attach scale_factor to a dictionary without mutating cached source tensors. |
_renormalize(original, transformed) |
Optional helper for algorithms declaring normalization support. Returns transformed rows rescaled to the original norms. |
graph_family |
None uses eager/split execution. A supported family declares that the algorithm can lower to persistent in-graph tables. |
graph_lower(payload, scale) |
Required for an in-graph family. Map the unscaled layer payload and request scale to that family's table keys. Its math must match _transform. |
graph_payload_problem(request=None) |
Return a restriction/rejection reason, or None when admissible. With no request, describe conditions that cannot be guaranteed from the algorithm name. |
wire_rank(wire) |
Low-rank families report payload rank so admission can enforce the engine's rank capacity. |
The eager preparation and graph-lowering inputs differ: tensor payloads passed
to _transform have already been scaled, while graph_lower receives the scale
separately. Handle scaling once in each path. For dictionary payloads, the
algorithm reads the prepared scale_factor as appropriate to its transformation.
normalize is a constructor option, not an automatic postprocessing step for
every algorithm. The algorithm must implement it, and its capability declaration
must permit it. For components whose decoder output is split into hidden and
residual tensors, the shared controller presents the complete selected rows to
the transformation and writes the resulting change back in the model's format.
BaseSteerVectorAlgorithm ¶
Bases: ABC
Base class for steering algorithms (see module docstring).
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/base.py
graph_payload_problem
classmethod
¶
Payload restriction, or its condition when only a name is known.
graph_lower
staticmethod
¶
Lower one layer's (payload, scale) to family slot tensors.
Returns {table_key: tensor-or-None}; None entries keep the table's zero default (e.g. an absent bias).
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/base.py
wire_rank
staticmethod
¶
set_payload ¶
Set a private scaled payload without mutating cached source tensors.
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/base.py
_transform
abstractmethod
¶
Transform the selected token rows (the algorithm's core math).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_state
|
Tensor
|
[num_positions, hidden_dim] selected rows |
required |
params
|
Any
|
this intervention's scaled payload |
required |
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/base.py
_renormalize ¶
Rescale transformed rows to the norms of original rows.
Computed in float32: hidden-state norms can reach ~1e4, so the intermediate product overflows float16 (max 65504).
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/base.py
Registration and construction¶
The registered name is used by VectorSpec.algorithm and the engine's
steer_algorithms declaration. Import the implementation in the algorithms
package so every engine process registers it.
model_hooks.steering.algorithms.registry.register_algorithm ¶
Class decorator registering an algorithm under a unique name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name of the algorithm (e.g., "direct", "loreft"). |
required |
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/registry.py
model_hooks.steering.algorithms.registry.get_algorithm ¶
Look up a registered algorithm class by name.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the algorithm name is not registered. |
Source code in vllm-steer/vllm/model_hooks/steering/algorithms/registry.py
model_hooks.steering.algorithms.registry.create_algorithm ¶
Create an algorithm instance by registered name.
register_algorithm(name) returns a class decorator and rejects duplicate
names. get_algorithm(name) returns the class; create_algorithm(name,
normalize=False) constructs it. Neither function loads weights.
Authoring capabilities¶
The entry in ALGORITHM_CAPABILITIES defines accepted authoring inputs and
the component an algorithm edits. The runtime class above owns graph behavior.
After changing a capability, regenerate the packaged client contracts with
python tools/export_client_contracts.py.
AlgorithmCapabilities
dataclass
¶
Source code in vllm-steer/vllm/model_hooks/steering/capabilities.py
payload_kind identifies a canonical payload; source declares
native-source support; normalize and params constrain request fields;
target_component selects hidden_states, attention_heads, or router_logits.
See the algorithm table for current entries.