pulsatrix 1.1.0
Loading...
Searching...
No Matches
featurizer_metrics.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <cstdint>
16#include <functional>
17#include <utility>
18#include <vector>
19
20#include "pulsatrix/causal_lm.hpp" // HiddenStateHook
23
24namespace pulsatrix {
25
28 int64_t inputs = 0;
30 double mse = 0;
35 double cosine = 0;
37 double norm_ratio = 0;
39 double l0 = 0;
41 double dead_fraction = 0;
43 double dense_fraction = 0;
45 std::vector<double> firing_rates;
46};
47
53[[nodiscard]] ReconstructionMetrics EvaluateReconstruction(Featurizer& featurizer, const Tensor& x, double dense_rate = 0.1,
54 int64_t batch = 4096);
60[[nodiscard]] ReconstructionMetrics EvaluatePrediction(Featurizer& featurizer, const Tensor& x, const Tensor& target,
61 double dense_rate = 0.1, int64_t batch = 4096);
62
71[[nodiscard]] HiddenStateHook SpliceHook(Featurizer& featurizer, int64_t position, std::vector<bool> rows = {});
73[[nodiscard]] HiddenStateHook AblationHook(int64_t position, std::vector<bool> rows = {});
74
81[[nodiscard]] MlpHook MlpSpliceHook(Featurizer& transcoder, std::vector<bool> rows = {});
83[[nodiscard]] MlpHook MlpAblationHook(std::vector<bool> rows = {});
84
87 double clean = 0;
88 double spliced = 0;
89 double ablated = 0;
94 double recovered = 0;
95};
96
103[[nodiscard]] LossRecovered MeasureLossRecovered(Featurizer& featurizer, int64_t position,
104 const std::function<double(const HiddenStateHook&)>& loss,
105 const std::vector<bool>& rows = {});
106
112[[nodiscard]] LossRecovered MeasureMlpLossRecovered(Featurizer& transcoder, const std::function<double(const MlpHook&)>& loss,
113 const std::vector<bool>& rows = {});
114
118 double train_fraction = 0.5;
119 uint64_t seed = 0;
122 double min_f1_gain = 0.03;
123 int64_t max_main_features = 10;
126 double min_cosine = 0.025;
129 double min_share = 0.4;
130 int64_t probe_epochs = 30;
131 float probe_learning_rate = 1e-2f;
132 int64_t batch = 4096;
133};
134
137 int64_t test_inputs = 0;
138 int64_t positives = 0;
140 double probe_f1 = 0;
142 std::vector<int64_t> main_features;
143 double main_f1 = 0;
145 int64_t probe_hits = 0;
147 int64_t missed = 0;
149 int64_t absorbed = 0;
150 double miss_rate = 0;
151 double absorption_rate = 0;
153 std::vector<std::pair<int64_t, int64_t>> absorbing_features;
154};
155
177[[nodiscard]] AbsorptionResult FeatureAbsorption(Featurizer& featurizer, const Tensor& x, const std::vector<int>& labels,
178 const AbsorptionOptions& options = {});
179
180} // namespace pulsatrix
A Llama-family decoder-only language model built from a Hugging Face config, and loading its Hugging ...
A featurizer maps activations x, (N, input_dim), to codes f = encode(x), (N, num_features),...
Definition featurizer.hpp:45
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
The featurizer family's common interface (FEAT-1): models that rewrite activations as sparse codes ov...
Reads or replaces a transformer block's MLP output during forward() (FEAT-5), for collecting a transc...
Definition acquisition_functions.hpp:16
LossRecovered MeasureLossRecovered(Featurizer &featurizer, int64_t position, const std::function< double(const HiddenStateHook &)> &loss, const std::vector< bool > &rows={})
Loss recovered, SAEBench's main downstream measure.
HiddenStateHook AblationHook(int64_t position, std::vector< bool > rows={})
Zero-ablates one position: the same rows set to 0 instead of reconstructed.
AbsorptionResult FeatureAbsorption(Featurizer &featurizer, const Tensor &x, const std::vector< int > &labels, const AbsorptionOptions &options={})
Feature absorption (Chanin et al.; SAEBench's version, in outline). A concept is absorbed when the fe...
std::function< Tensor(int64_t position, const Tensor &hidden)> HiddenStateHook
Reads or replaces a language model's residual stream during forward() (FEAT-3): called with a positio...
Definition causal_lm.hpp:37
MlpHook MlpSpliceHook(Featurizer &transcoder, std::vector< bool > rows={})
Splices a transcoder in for an MLP (install it with EncoderBlock::set_mlp_hook() or TransformerBlock:...
HiddenStateHook SpliceHook(Featurizer &featurizer, int64_t position, std::vector< bool > rows={})
Splices the featurizer in at one position: a hook that replaces the hidden states there,...
std::function< Tensor(const Tensor &mlp_input, const Tensor &mlp_output)> MlpHook
Called with an MLP's input (after the block's norm, where it has one) and its output (the branch adde...
Definition mlp_hook.hpp:23
ReconstructionMetrics EvaluatePrediction(Featurizer &featurizer, const Tensor &x, const Tensor &target, double dense_rate=0.1, int64_t batch=4096)
EvaluateReconstruction for a transcoder: predict(x) against target, (N, output_dim)....
LossRecovered MeasureMlpLossRecovered(Featurizer &transcoder, const std::function< double(const MlpHook &)> &loss, const std::vector< bool > &rows={})
Loss recovered for a transcoder spliced in for an MLP: loss installs the MlpHook it is given on the M...
MlpHook MlpAblationHook(std::vector< bool > rows={})
Zero-ablates an MLP: its output rows set to 0.
ReconstructionMetrics EvaluateReconstruction(Featurizer &featurizer, const Tensor &x, double dense_rate=0.1, int64_t batch=4096)
Encodes and reconstructs x, (N, input_dim), in batches of batch rows.
Definition featurizer_metrics.hpp:115
double train_fraction
The share of inputs used to fit the probe and pick the main features; the rest are scored.
Definition featurizer_metrics.hpp:118
double min_f1_gain
Main features are added, best first, while each raises F1 by at least this much (SAEBench's k-sparse ...
Definition featurizer_metrics.hpp:122
int64_t max_main_features
Definition featurizer_metrics.hpp:123
double min_share
And absorbing features must carry at least this share of the input's projection onto the probe direct...
Definition featurizer_metrics.hpp:129
float probe_learning_rate
Definition featurizer_metrics.hpp:131
uint64_t seed
Definition featurizer_metrics.hpp:119
int64_t probe_epochs
Definition featurizer_metrics.hpp:130
int64_t batch
Definition featurizer_metrics.hpp:132
double min_cosine
An absorbing feature's decoder direction must have at least this cosine with the probe direction (SAE...
Definition featurizer_metrics.hpp:126
FeatureAbsorption()'s result, on the held-out inputs.
Definition featurizer_metrics.hpp:136
double absorption_rate
absorbed / probe_hits
Definition featurizer_metrics.hpp:151
int64_t positives
Definition featurizer_metrics.hpp:138
int64_t probe_hits
Positives the probe finds.
Definition featurizer_metrics.hpp:145
double miss_rate
missed / probe_hits
Definition featurizer_metrics.hpp:150
std::vector< int64_t > main_features
The features that stand for the concept, and their F1 when any one firing predicts it.
Definition featurizer_metrics.hpp:142
int64_t absorbed
Of those, the ones that other features, aligned with the probe, carry instead.
Definition featurizer_metrics.hpp:149
int64_t test_inputs
Definition featurizer_metrics.hpp:137
std::vector< std::pair< int64_t, int64_t > > absorbing_features
The absorbing features and on how many inputs each absorbed the concept, most first.
Definition featurizer_metrics.hpp:153
int64_t missed
Of those, the ones on which no main feature fires.
Definition featurizer_metrics.hpp:147
double main_f1
Definition featurizer_metrics.hpp:143
double probe_f1
The baseline: a logistic probe on the raw activations.
Definition featurizer_metrics.hpp:140
MeasureLossRecovered()'s result: the model's loss three ways.
Definition featurizer_metrics.hpp:86
double spliced
with the reconstructions spliced in
Definition featurizer_metrics.hpp:88
double recovered
(ablated - spliced) / (ablated - clean): 1 when splicing costs nothing, 0 when it is as bad as deleti...
Definition featurizer_metrics.hpp:94
double clean
unchanged
Definition featurizer_metrics.hpp:87
double ablated
with the position zeroed
Definition featurizer_metrics.hpp:89
EvaluateReconstruction()'s result.
Definition featurizer_metrics.hpp:27
double dense_fraction
The share that fired on more than the dense rate of them.
Definition featurizer_metrics.hpp:43
std::vector< double > firing_rates
Each feature's (or block's) share of inputs it fired on.
Definition featurizer_metrics.hpp:45
double l0
Active features per input: blocks, for a block-sparse featurizer.
Definition featurizer_metrics.hpp:39
double dead_fraction
The share of features that fired on none of these inputs.
Definition featurizer_metrics.hpp:41
double explained_variance
1 - Σ|x - x̂|² / Σ|x - mean(x)|²: the share of the inputs' variance, about their mean,...
Definition featurizer_metrics.hpp:33
double mse
Mean squared error over every element.
Definition featurizer_metrics.hpp:30
double cosine
The mean cosine similarity of each input and its reconstruction.
Definition featurizer_metrics.hpp:35
int64_t inputs
Definition featurizer_metrics.hpp:28
double norm_ratio
The mean of |x̂| / |x|: below 1 means codes are shrunk.
Definition featurizer_metrics.hpp:37