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". |
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
Containers and utilities¶
easysteer.steer.StatisticalControlVector
dataclass
¶
Statistical control vector with multi-layer directions
Source code in easysteer/steer/utils.py
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 | |
export_gguf ¶
Export a trained StatisticalControlVector to a llama.cpp .gguf file. Compatible with repeng format.
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. Never modified or rebound by this function. |
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: Optional[str] = None) -> List[Dict[str, Any]]
Search for SAE features based on a semantic query
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Model identifier (e.g., 'gemma-2-9b') |
required |
sae_id
|
str
|
SAE identifier (e.g., '24-gemmascope-res-16k') |
required |
query
|
str
|
Search query |
required |
api_key
|
Optional[str]
|
Optional API key (will use environment variable if not provided) |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of matching features sorted by relevance |
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: Optional[str] = None) -> Dict[str, Any]
Get detailed explanation for a specific feature
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Model identifier (e.g., 'gemma-2-9b') |
required |
sae_id
|
str
|
SAE identifier (e.g., '24-gemmascope-res-16k') |
required |
feature_index
|
int
|
Feature index number |
required |
api_key
|
Optional[str]
|
Optional API key (will use environment variable if not provided) |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing processed feature explanation details |
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: Optional[str] = None) -> Optional[np.ndarray]
Extract decoder vector for a specific feature index from SAE model file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_file
|
str
|
Path to the SAE model file (npz format) |
required |
feature_index
|
int
|
Feature index to extract |
required |
save_path
|
Optional[str]
|
Optional path to save the vector as PyTorch file (.pt) |
None
|
Returns:
| Type | Description |
|---|---|
Optional[ndarray]
|
Decoder vector as numpy 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.from_control_vector ¶
Payload from an easysteer StatisticalControlVector.
The no-disk path: extract with easysteer.steer and steer with
the result directly, no 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 :class:DirectionVector for the
direct algorithm; a LoReFT state dict (rotation + learned
source) becomes a :class: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 — explicitly, instead of the
silent first-vector default the engine loader used to apply.
Source code in easysteer/vectors.py
easysteer.vectors.from_linear_transport ¶
Payload from a LinearTransport pickle (A_ weight, B_ bias).