easysteer.hidden_states¶
Hidden-state, attention-head, and MoE router-logit capture from a running vLLM engine. The capture guide explains selection, graph execution, and prefix-cache behavior.
Capture (recommended)¶
easysteer.hidden_states.capture ¶
capture(
llm: Any,
prompts: Any,
max_tokens: int = 1,
layers: list[int] | None = None,
dtype: str | None = None,
select: Any | None = None,
per_prompt_selects: list[Any | None] | None = None,
stream: str = "hidden_states",
steering: Any | None = None,
**generate_kwargs,
) -> CaptureResult
Capture intermediate state for a batch of prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Any
|
Single-worker vLLM LLM instance (compiled or eager, prefix caching on or off). Tensor-parallel capture is not supported. |
required |
prompts
|
Any
|
prompt list (text or multimodal dicts). |
required |
max_tokens
|
int
|
tokens to generate (1 = prompt-only forward). |
1
|
layers
|
list[int] | None
|
layer-id subset (None = all hooked layers). |
None
|
dtype
|
str | None
|
engine-side storage dtype (e.g. 'float16'). |
None
|
select
|
Any | None
|
global SelectSpec (or wire dict) row selection. |
None
|
per_prompt_selects
|
list[Any | None] | None
|
one SelectSpec (or wire dict) per prompt, overriding the global selection for that prompt; None entries keep the global selection. The helper returns selected rows without engine-side reduction. |
None
|
stream
|
str
|
'hidden_states', 'router_logits', or 'attention_heads'. |
'hidden_states'
|
steering
|
Any | None
|
SteeringSpec (or per-prompt list), as in LLM.generate(). |
None
|
**generate_kwargs
|
Any
|
forwarded into SamplingParams. |
{}
|
Returns:
| Type | Description |
|---|---|
CaptureResult
|
CaptureResult with exact per-sample views. |
Source code in easysteer/hidden_states/capture_result.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
easysteer.hidden_states.CaptureResult ¶
CaptureResult(
layers: dict[int, Tensor],
meta: dict[int, Any],
outputs: Any,
layouts: dict[int, dict[str, int]] | None = None,
)
Result of one capture call.
Attributes:
| Name | Type | Description |
|---|---|---|
layers |
{true_layer_id: Tensor(total_rows, dim)} in fetch order. |
|
outputs |
the vLLM RequestOutput list, prompt order. |
|
layouts |
component dimensions by layer. Attention-head outputs include width, query num_heads, and per-head value-output head_size. |
Source code in easysteer/hidden_states/capture_result.py
rows ¶
meta ¶
sample ¶
One sample's rows for every layer: {layer_id: (rows, dim)}.
Source code in easysteer/hidden_states/capture_result.py
sample_positions ¶
Absolute sequence positions of sample i's rows (row order).
Source code in easysteer/hidden_states/capture_result.py
sample_token_ids ¶
Input token ids of sample i's rows (row order).
Source code in easysteer/hidden_states/capture_result.py
to_nested ¶
Return [sample][layer_pos] tensors with layers sorted by model id.
Source code in easysteer/hidden_states/capture_result.py
capture() returns a CaptureResult. Its methods select rows by true layer ID
or by input sample; len(result) is the number of generated request outputs.
An empty selection still preserves the outputs and empty per-sample views.
Row-label types, component constants, serialization, and worker stream/session
classes are documented in the fork capture API.
Compatibility wrappers¶
Nested-list wrappers over capture(); prefer capture() for new code.
easysteer.hidden_states.get_all_hidden_states_generate ¶
get_all_hidden_states_generate(
llm: Any,
prompts: list[str] | list[dict[str, Any]],
max_tokens: int = 1,
split_by_samples: bool = True,
token_ids: list[int] | None = None,
positions: list[int] | None = None,
layers: list[int] | None = None,
dtype: str | None = None,
select: dict | Any | None = None,
**generate_kwargs,
) -> (
tuple[list[list[torch.Tensor]], Any]
| tuple[list[torch.Tensor], Any]
)
Capture every layer's hidden states while running generate.
Works for any generate-capable model, including multimodal models
(Qwen-VL, LLaVA, ...) that do not support the embed task. With the
default max_tokens=1 only the prompt forward is captured,
matching what an embed task would produce.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Any
|
Single-worker vLLM LLM instance (compiled or eager, prefix caching on or off). |
required |
prompts
|
list[str] | list[dict[str, Any]]
|
text prompts, or multimodal dicts with |
required |
max_tokens
|
int
|
tokens to generate (1 = prompt-only forward). |
1
|
split_by_samples
|
bool
|
if True return |
True
|
token_ids
|
list[int] | None
|
only capture rows whose input token id is in this
list (source-side filter; unions with |
None
|
positions
|
list[int] | None
|
only capture these prompt positions (negatives
resolve from the prompt end; unions with |
None
|
layers
|
list[int] | None
|
layer-id subset (None = all hooked layers). |
None
|
dtype
|
str | None
|
engine-side storage dtype (e.g. |
None
|
select
|
dict | Any | None
|
SelectSpec (or wire dict) — the full where-clause selection language; cannot combine with the shortcuts. |
None
|
**generate_kwargs
|
Any
|
forwarded into SamplingParams. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[list[list[Tensor]], Any] | tuple[list[Tensor], Any]
|
|
tuple[list[list[Tensor]], Any] | tuple[list[Tensor], Any]
|
|
tuple[list[list[Tensor]], Any] | tuple[list[Tensor], Any]
|
layers ordered by layer id. |
Source code in easysteer/hidden_states/capture_generate.py
easysteer.hidden_states.get_moe_router_logits_generate ¶
get_moe_router_logits_generate(
llm: Any,
prompts: list[str] | list[dict[str, Any]],
max_tokens: int = 1,
split_by_samples: bool = False,
**generate_kwargs,
) -> (
tuple[dict[int, torch.Tensor], Any]
| tuple[list[dict[int, torch.Tensor]], Any]
)
Capture MoE router logits while running generate.
Works for any generate-capable MoE model, including multimodal
ones (e.g. Qwen3-VL). With the default max_tokens=1 only the
prompt forward is captured. When router-logits steering is active,
the captured logits are the post-steering ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Any
|
Single-worker vLLM LLM instance (compiled or eager, prefix caching on or off). |
required |
prompts
|
list[str] | list[dict[str, Any]]
|
text prompts, or multimodal dicts with |
required |
max_tokens
|
int
|
tokens to generate (1 = prompt-only forward). |
1
|
split_by_samples
|
bool
|
if True return one |
False
|
**generate_kwargs
|
Any
|
forwarded into SamplingParams. |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[dict[int, Tensor], Any] | tuple[list[dict[int, Tensor]], Any]
|
|
tuple[dict[int, Tensor], Any] | tuple[list[dict[int, Tensor]], Any]
|
|
tuple[dict[int, Tensor], Any] | tuple[list[dict[int, Tensor]], Any]
|
|