easysteer.steer¶
Analysis-based extraction of steering vectors from captured hidden states.
Unified extraction interface¶
easysteer.steer.extract_statistical_control_vector ¶
extract_statistical_control_vector(
method: str,
all_hidden_states,
positive_indices,
negative_indices=None,
**kwargs,
) -> StatisticalControlVector
Unified control vector extraction interface.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Method name; one of "diffmean", "pca", "lat", "linear_probe", "iti". ITI also requires validation captures, validation labels and query head counts. |
required |
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None, every sample index not in
|
None
|
**kwargs
|
Any
|
Method-specific options. Unknown options raise
ValueError naming the accepted ones for |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
StatisticalControlVector |
StatisticalControlVector
|
The extracted control vector. |
Source code in easysteer/steer/unified_interface.py
easysteer.steer.extract_diffmean_control_vector ¶
extract_diffmean_control_vector(
all_hidden_states,
positive_indices,
negative_indices=None,
**kwargs,
) -> StatisticalControlVector
Extract a DiffMean control vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None, every sample index not in
|
None
|
**kwargs
|
Any
|
Options accepted by DiffMeanExtractor:
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
StatisticalControlVector |
StatisticalControlVector
|
The DiffMean control vector. |
Source code in easysteer/steer/unified_interface.py
easysteer.steer.extract_pca_control_vector ¶
extract_pca_control_vector(
all_hidden_states,
positive_indices,
negative_indices=None,
**kwargs,
) -> StatisticalControlVector
Extract a PCA control vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None, every sample index not in
|
None
|
**kwargs
|
Any
|
Options accepted by PCAExtractor:
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
StatisticalControlVector |
StatisticalControlVector
|
The PCA control vector. |
Examples:
>>> # Plain PCA over positive samples only
>>> pca_vector = extract_pca_control_vector(
... all_hidden_states, positive_indices,
... method="standard"
... )
>>>
>>> # PCA over pair differences with direction correction
>>> pca_diff_vector = extract_pca_control_vector(
... all_hidden_states, positive_indices, negative_indices,
... method="diff", correct_direction=True
... )
>>>
>>> # PCA over pair differences without direction correction
>>> pca_diff_no_correct = extract_pca_control_vector(
... all_hidden_states, positive_indices, negative_indices,
... method="diff", correct_direction=False
... )
Source code in easysteer/steer/unified_interface.py
easysteer.steer.extract_lat_control_vector ¶
extract_lat_control_vector(
all_hidden_states,
positive_indices,
negative_indices=None,
**kwargs,
) -> StatisticalControlVector
Extract a LAT control vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None and |
None
|
**kwargs
|
Any
|
Options accepted by LATExtractor:
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
StatisticalControlVector |
StatisticalControlVector
|
The LAT control vector. |
Examples:
>>> # Positive samples only (traditional LAT)
>>> lat_vector = extract_lat_control_vector(
... all_hidden_states, positive_indices,
... use_positive_only=True
... )
>>>
>>> # Positive and negative samples with direction correction
>>> lat_mixed_vector = extract_lat_control_vector(
... all_hidden_states, positive_indices, negative_indices,
... use_positive_only=False, correct_direction=True
... )
Source code in easysteer/steer/unified_interface.py
easysteer.steer.extract_linear_probe_control_vector ¶
extract_linear_probe_control_vector(
all_hidden_states,
positive_indices,
negative_indices=None,
**kwargs,
) -> StatisticalControlVector
Extract a Linear Probe control vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None, every sample index not in
|
None
|
**kwargs
|
Any
|
Options accepted by LinearProbeExtractor:
|
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
StatisticalControlVector |
StatisticalControlVector
|
The Linear Probe control vector. |
Examples:
>>> # L2 regularization (recommended)
>>> linear_probe_vector = extract_linear_probe_control_vector(
... all_hidden_states, positive_indices, negative_indices,
... regularization="l2", C=1.0
... )
>>>
>>> # L1 regularization (feature selection)
>>> linear_probe_l1 = extract_linear_probe_control_vector(
... all_hidden_states, positive_indices, negative_indices,
... regularization="l1", C=10.0
... )
Source code in easysteer/steer/unified_interface.py
ITI attention extraction¶
easysteer.steer.ITIExtractor ¶
Select heads with probes and build mean-difference ITI directions.
Probes fit training rows and rank heads by validation accuracy. Following ITI, the final direction and its projection standard deviation use both development splits. Split by question before capture; no held-out test answers should appear in either input.
Source code in easysteer/steer/iti.py
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
extract
staticmethod
¶
extract(
all_hidden_states,
positive_indices,
negative_indices=None,
*,
validation_hidden_states,
validation_positive_indices,
validation_negative_indices=None,
num_heads: dict[int, int],
top_k: int = 1,
token_pos: int | str = -1,
) -> StatisticalControlVector
Return concatenated per-layer sigma * direction vectors.
Inputs use the other extractors' CaptureResult or nested-list layout.
num_heads maps layer ids to query head counts; for capture input,
use {layer: layout['num_heads'] for layer, layout in result.layouts.items()}.
Each row must contain contiguous head outputs. Unselected head slices
remain zero; layers with no selected head are omitted. Apply with
attention_add and no normalization, using scale for ITI's alpha.
Source code in easysteer/steer/iti.py
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
Containers and utilities¶
easysteer.steer.StatisticalControlVector
dataclass
¶
Statistical control vector with per-layer directions.
Source code in easysteer/steer/utils.py
13 14 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 | |
export_gguf ¶
Export directions and metadata to a repeng-compatible GGUF file.
Source code in easysteer/steer/utils.py
import_gguf
classmethod
¶
Import a StatisticalControlVector from a GGUF file.
Source code in easysteer/steer/utils.py
easysteer.steer.extract_token_hiddens ¶
extract_token_hiddens(
all_hidden_states,
positive_indices,
negative_indices=None,
token_pos=-1,
) -> tuple[dict, dict]
Extract hidden states of one token position per sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_hidden_states
|
list | CaptureResult
|
Nested
|
required |
positive_indices
|
list[int]
|
Indices of positive samples. |
required |
negative_indices
|
list[int] | None
|
Indices of negative
samples. If None, every sample index not in
|
None
|
token_pos
|
int | str
|
Token position to extract: an int index (-1 selects the last token, the default), "first", "last", "mean" (average over tokens), "max" or "min" (token with the largest/smallest L2 norm). |
-1
|
Returns:
| Type | Description |
|---|---|
tuple[dict, dict]
|
tuple[dict, dict]: |
Source code in easysteer/steer/utils.py
SAE helpers¶
easysteer.steer.search_sae_features ¶
search_sae_features(
model_id: str,
sae_id: str,
query: str,
api_key: str | None = None,
) -> list[dict[str, Any]]
Call SAEFeatureExplorer.search_features without retaining a client.
api_key defaults to the NP_API_KEY environment variable.
Source code in easysteer/steer/sae.py
easysteer.steer.get_sae_feature_explanation ¶
get_sae_feature_explanation(
model_id: str,
sae_id: str,
feature_index: int,
api_key: str | None = None,
) -> dict[str, Any]
Call SAEFeatureExplorer.get_feature_explanation with a temporary client.
api_key defaults to the NP_API_KEY environment variable.
Source code in easysteer/steer/sae.py
easysteer.steer.extract_sae_decoder_vector ¶
extract_sae_decoder_vector(
model_file: str,
feature_index: int,
save_path: str | None = None,
) -> np.ndarray | None
Call SAEFeatureExplorer.extract_decoder_vector and return its array.
Source code in easysteer/steer/sae.py
Payload adapters (easysteer.vectors)¶
Client-side adapters from third-party checkpoint formats to the canonical
steering payloads passed via VectorSpec(data=...).
easysteer.vectors.load ¶
Load an explicitly identified checkpoint schema into a canonical payload.
Formats: gguf, concept_pair (named h1/h2 GGUF directory),
moe_router (JSON), pt_direction, pyreft, lm_steer and
linear_transport. Options go to the chosen adapter; for example
layers=[10] for pt_direction or vector_index=1 for lm_steer.
A suffix such as .pt never selects or guesses a checkpoint schema.
Source code in easysteer/vectors.py
easysteer.vectors.to_json_payload ¶
Encode a canonical payload for JSON files or OpenAI HTTP requests.
Tensor bytes become base64 strings; shapes, metadata and the content
hash remain identical to payload.to_wire(). This runs in the
producing EasySteer environment, so JSON consumers need no engine
or tensor dependencies.
Source code in easysteer/vectors.py
easysteer.vectors.from_control_vector ¶
Convert a StatisticalControlVector directly, without a GGUF round-trip.
Source code in easysteer/vectors.py
easysteer.vectors.from_gguf ¶
Payload from an EasySteer GGUF export (direction.<layer>).
easysteer.vectors.from_pt_direction ¶
Payload from a bare direction tensor saved with torch.save.
The file holds one vector (tensor or numpy array); it is applied to each listed layer.
Source code in easysteer/vectors.py
easysteer.vectors.from_pyreft ¶
Payload from a pyreft checkpoint directory.
Reads the single *.bin + config pair. A BiasIntervention-style
state dict (one vector) becomes a DirectionVector for the
direct algorithm; a LoReFT state dict (rotation + learned
source) becomes a ReftIntervention for loreft. The
checkpoint's layer index is preserved.
Source code in easysteer/vectors.py
easysteer.vectors.from_lm_steer ¶
Payload from an LM-Steer checkpoint (.pt).
Handles the published gpt2.pt layout (a list whose second entry
is the parameter dict). Multi-vector checkpoints stack steer
vectors; vector_index selects one and defaults to the first.
Source code in easysteer/vectors.py
easysteer.vectors.from_linear_transport ¶
Payload from a LinearTransport pickle (A_ weight, B_ bias).