easysteer.hidden_states¶
Hidden-state and MoE router-logit capture from a running vLLM engine.
Capture (recommended)¶
easysteer.hidden_states.capture ¶
capture(llm: Any, prompts: Any, max_tokens: int = 1, layers: Optional[List[int]] = None, dtype: Optional[str] = None, select: Optional[Any] = None, per_prompt_selects: Optional[List[Optional[Any]]] = None, stream: str = 'hidden_states', **generate_kwargs) -> CaptureResult
Capture intermediate state for a batch of prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
Any
|
vLLM LLM instance (any engine config: compiled or eager, prefix caching on or off). |
required |
prompts
|
Any
|
prompt list (text or multimodal dicts). |
required |
max_tokens
|
int
|
tokens to generate (1 = prompt-only forward). |
1
|
layers
|
Optional[List[int]]
|
layer-id subset (None = all hooked layers). |
None
|
dtype
|
Optional[str]
|
engine-side storage dtype (e.g. 'float16'). |
None
|
select
|
Optional[Any]
|
global SelectSpec (or wire dict) row selection. |
None
|
per_prompt_selects
|
Optional[List[Optional[Any]]]
|
one SelectSpec (or wire dict) per prompt, overriding the global selection for that prompt; None entries keep the global selection. Requires positions='all' semantics (no reductions). |
None
|
stream
|
str
|
'hidden_states' or 'router_logits'. |
'hidden_states'
|
**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
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
easysteer.hidden_states.CaptureResult ¶
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. |
Source code in easysteer/hidden_states/capture_result.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
meta ¶
Row labels (req_ids/positions/token_ids) for a layer.
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).
sample_token_ids ¶
Input token ids of sample i's rows (row order).
to_nested ¶
Legacy extractor shape: [sample][layer_pos] (layers sorted by id).
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: Union[List[str], List[Dict[str, Any]]], max_tokens: int = 1, split_by_samples: bool = True, token_ids: Optional[List[int]] = None, positions: Optional[List[int]] = None, layers: Optional[List[int]] = None, dtype: Optional[str] = None, select: Optional[Union[dict, Any]] = None, **generate_kwargs) -> Union[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
|
vLLM LLM instance (any engine config: compiled or eager, prefix caching on or off). |
required |
prompts
|
Union[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
|
Optional[List[int]]
|
only capture rows whose input token id is in this
list (source-side filter; unions with |
None
|
positions
|
Optional[List[int]]
|
only capture these absolute positions (negatives
resolve from the prompt end; unions with |
None
|
layers
|
Optional[List[int]]
|
layer-id subset (None = all hooked layers). |
None
|
dtype
|
Optional[str]
|
engine-side storage dtype (e.g. |
None
|
select
|
Optional[Union[dict, Any]]
|
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 |
|---|---|
Union[Tuple[List[List[Tensor]], Any], Tuple[List[Tensor], Any]]
|
|
Union[Tuple[List[List[Tensor]], Any], Tuple[List[Tensor], Any]]
|
|
Union[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: Union[List[str], List[Dict[str, Any]]], max_tokens: int = 1, split_by_samples: bool = False, **generate_kwargs) -> Union[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
|
vLLM LLM instance (any engine config: compiled or eager, prefix caching on or off). |
required |
prompts
|
Union[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 |
|---|---|
Union[Tuple[Dict[int, Tensor], Any], Tuple[List[Dict[int, Tensor]], Any]]
|
|
Union[Tuple[Dict[int, Tensor], Any], Tuple[List[Dict[int, Tensor]], Any]]
|
|
Union[Tuple[Dict[int, Tensor], Any], Tuple[List[Dict[int, Tensor]], Any]]
|
|