pulsatrix 1.1.0
Loading...
Searching...
No Matches
crosscoder.hpp
Go to the documentation of this file.
1
13#pragma once
14
15#include <cstdint>
16#include <optional>
17#include <vector>
18
21
22namespace pulsatrix {
23
28 L1,
33};
34
38 float l1_coefficient = 1e-3f;
40 int64_t k = 32;
43 int64_t k_aux = 0;
44 float aux_coefficient = 1.0f / 32.0f;
45 int64_t dead_after = 10'000'000;
48 float threshold_decay = 0.99f;
51 float decoder_init_norm = 0.0f;
61 bool delta = false;
62 double shared_fraction = 0.2;
64 int64_t k_shared = 0;
65 float delta_coefficient = 0.005f;
66 uint64_t seed = 0;
67};
68
93class Crosscoder : public Module, public Featurizer {
94public:
99
101 void initialize_bias(const Tensor& x);
102
103 [[nodiscard]] int64_t num_sources() const { return S_; }
104 [[nodiscard]] int64_t source_dim() const { return d_; }
106 [[nodiscard]] int64_t num_shared() const { return shared_; }
107
108 // ---- Featurizer ----------------------------------------------------------------------
109 [[nodiscard]] int64_t input_dim() const override { return S_ * d_; }
110 [[nodiscard]] int64_t num_features() const override { return m_; }
112 [[nodiscard]] Tensor encode(const Tensor& x) override;
113 [[nodiscard]] Tensor decode(const Tensor& codes) override;
117 FeaturizerLoss loss_and_backward(const Tensor& x, std::vector<float>* codes = nullptr) override;
118 void normalize_decoder() override;
120 [[nodiscard]] std::vector<float> decoder_direction(int64_t i) override;
121 [[nodiscard]] Module& parameters_module() override { return *this; }
122
124 [[nodiscard]] std::vector<float> decoder_direction(int64_t i, int64_t s);
126 [[nodiscard]] float delta_loss() const { return last_delta_loss_; }
127
128 [[nodiscard]] std::vector<int64_t> dead_latents() const;
129 [[nodiscard]] const std::vector<int64_t>& inputs_since_fired() const { return since_fired_; }
131 void set_inputs_since_fired(const std::vector<int64_t>& since);
134 [[nodiscard]] std::vector<float> thresholds() const;
136 void set_thresholds(const std::vector<float>& thresholds);
137 [[nodiscard]] const CrosscoderOptions& options() const { return options_; }
138 [[nodiscard]] LinearModule& encoder() { return encoder_; }
139 [[nodiscard]] LinearModule& decoder() { return decoder_; }
140
141 // ---- Module: input to reconstruction -------------------------------------------------
142 [[nodiscard]] Tensor backward(const Tensor& grad_output) override;
145 [[nodiscard]] Tensor propagate_relevance(const Tensor& relevance_out, const LRPRuleConfig& config) override;
146 [[nodiscard]] OpType op_type() const override { return OpType::Composite; }
147 [[nodiscard]] std::vector<NamedParamRef> named_parameters() override;
148 [[nodiscard]] std::vector<NamedBufferRef> named_buffers() override;
149 [[nodiscard]] std::optional<DeviceType> compute_device() const override { return backend_->device(); }
150 void set_training(bool training) override;
151 void release_activations() override;
152
153protected:
154 [[nodiscard]] Tensor forward_impl(const Tensor& input) override;
155
156private:
157 struct Pass {
158 std::vector<float> pre;
159 std::vector<float> codes;
160 std::vector<float> min_kept;
161 };
162 Pass Encode(const Tensor& x, bool training);
163 void Check(const Tensor& x, const char* who) const;
165 [[nodiscard]] Tensor EncoderInput(const Tensor& x) const;
167 [[nodiscard]] std::vector<double> NormSums() const;
168
169 int64_t S_, d_, m_, shared_ = 0;
170 DeviceBackend* backend_;
171 CrosscoderOptions options_;
172 LinearModule encoder_;
173 LinearModule decoder_;
174 std::vector<int64_t> since_fired_;
175 Tensor threshold_;
176 float last_delta_loss_ = 0;
177 std::vector<float> last_codes_;
178 int64_t last_rows_ = 0;
179};
180
183 double norm_a = 0, norm_b = 0;
185 double relative_norm = 0;
188 double delta_norm = 0;
190 double cosine = 0;
191};
192
195[[nodiscard]] std::vector<CrosscoderLatentStats> CrosscoderLatents(Crosscoder& crosscoder, int64_t a = 0, int64_t b = 1);
196
197enum class LatentClass {
198 AOnly,
199 BOnly,
200 Shared,
201 Other,
202};
205
219 int64_t latent = 0;
223 int64_t active = 0;
224};
228[[nodiscard]] std::vector<LatentScaling> MeasureLatentScaling(Crosscoder& crosscoder, const Tensor& x, const std::vector<int64_t>& latents,
229 int64_t a = 0, int64_t b = 1, int64_t batch = 4096);
230
232[[nodiscard]] std::vector<double> ExplainedVarianceBySource(Crosscoder& crosscoder, const Tensor& x, int64_t batch = 4096);
233
234} // namespace pulsatrix
A crosscoder over S sources of the same size d. Its input is the sources side by side,...
Definition crosscoder.hpp:93
Module & parameters_module() override
The trainable parameters, for an optimizer's step() and zero_grad(), and for checkpoints.
Definition crosscoder.hpp:121
int64_t num_shared() const
With delta: latents [0, num_shared()) are shared, the rest delta latents.
Definition crosscoder.hpp:106
std::optional< DeviceType > compute_device() const override
The device this module computes on, so forward() can reject an input on another device before any ker...
Definition crosscoder.hpp:149
Tensor forward_impl(const Tensor &input) override
The actual forward computation. Called by forward() after precondition checks.
Tensor encode(const Tensor &x) override
The codes: BatchTopK by threshold once one is set, else across this batch.
void normalize_decoder() override
Rescales every feature so its decoder direction has unit L2 norm, scaling its code by the inverse whe...
FeaturizerLoss loss_and_backward(const Tensor &x, std::vector< float > *codes=nullptr) override
The loss and its gradients: reconstruction is the mean squared error over every source,...
std::vector< float > thresholds() const
BatchTopK's inference thresholds on f_i · Σ_s |d_i^s| (shared, then delta, with delta); negative unti...
void set_training(bool training) override
Sets this module's training/eval mode. Defaults to training (matches every mainstream framework's Mod...
LinearModule & encoder()
Definition crosscoder.hpp:138
std::vector< NamedParamRef > named_parameters() override
This module's trainable parameters, each with its hierarchical name – the one place a module declares...
int64_t source_dim() const
Definition crosscoder.hpp:104
int64_t num_features() const override
Definition crosscoder.hpp:110
std::vector< int64_t > dead_latents() const
const std::vector< int64_t > & inputs_since_fired() const
Definition crosscoder.hpp:129
Tensor decode(const Tensor &codes) override
The reconstruction from codes, (N, output_dim).
LinearModule & decoder()
Definition crosscoder.hpp:139
void release_activations() override
Frees the tensors forward() kept for backward() and the relevance passes; containers release their ch...
Crosscoder(int64_t num_sources, int64_t source_dim, int64_t num_features, DeviceBackend *backend, CrosscoderOptions options={})
std::vector< float > decoder_direction(int64_t i, int64_t s)
Latent i's decoder in source s, d values.
void initialize_bias(const Tensor &x)
Sets b_dec to the mean of x, (N, S·d).
Tensor propagate_relevance(const Tensor &relevance_out, const LRPRuleConfig &config) override
Relevance of every source's input: the decoder's and the encoder's rules, through the selected latent...
Tensor backward(const Tensor &grad_output) override
Computes the gradient w.r.t. this module's input, given the gradient w.r.t. its output....
OpType op_type() const override
This module's operation-category tag, for ComputationGraph node tagging.
Definition crosscoder.hpp:146
void set_inputs_since_fired(const std::vector< int64_t > &since)
const CrosscoderOptions & options() const
Definition crosscoder.hpp:137
int64_t num_sources() const
Definition crosscoder.hpp:103
float delta_loss() const
The last loss_and_backward()'s weighted delta loss (0 without delta).
Definition crosscoder.hpp:126
int64_t input_dim() const override
Definition crosscoder.hpp:109
std::vector< NamedBufferRef > named_buffers() override
This module's buffers: state a checkpoint must save that no optimizer updates.
void set_thresholds(const std::vector< float > &thresholds)
std::vector< float > decoder_direction(int64_t i) override
Latent i's decoder in every source, S·d values.
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
virtual DeviceType device() const noexcept=0
Which device this backend's buffers reside on.
A featurizer maps activations x, (N, input_dim), to codes f = encode(x), (N, num_features),...
Definition featurizer.hpp:45
y = x @ W + b, batched (x is (N, in_features), y is (N, out_features)) – migrated from the original u...
Definition linear_module.hpp:43
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
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...
Dense/fully-connected layer – the reference Module implementation.
Definition acquisition_functions.hpp:16
std::vector< double > ExplainedVarianceBySource(Crosscoder &crosscoder, const Tensor &x, int64_t batch=4096)
Each source's explained variance on x: 1 - Σ|x^s - x̂^s|² / Σ|x^s - mean(x^s)|².
CrosscoderSparsity
Definition crosscoder.hpp:24
@ BatchTopK
Minder et al.: the batch's N · k largest values of f_i · Σ_s |d_i^s| are kept (the unscaled f_i is th...
@ L1
Lindsey et al.: λ · Σ_i f_i Σ_s |d_i^s|, an L1 penalty on each code weighted by the sum (not the L2 n...
LatentClass ClassifyLatent(const CrosscoderLatentStats &stats)
Minder et al.'s bins on Δnorm.
std::vector< LatentScaling > MeasureLatentScaling(Crosscoder &crosscoder, const Tensor &x, const std::vector< int64_t > &latents, int64_t a=0, int64_t b=1, int64_t batch=4096)
Latent Scaling of each of latents on x, (N, S·d), in batches of batch. β is 0 (and ν 0) for a latent ...
OpType
The op-type tag a Node carries. Charter Part 2 §3: nodes are tagged by a small closed set of op types...
Definition op_type.hpp:19
LatentClass
Definition crosscoder.hpp:197
@ AOnly
Δnorm below 0.1.
@ BOnly
Δnorm above 0.9.
@ Shared
Δnorm in [0.4, 0.6].
std::vector< CrosscoderLatentStats > CrosscoderLatents(Crosscoder &crosscoder, int64_t a=0, int64_t b=1)
Every latent's stats for sources a and b.
One latent across two sources a and b.
Definition crosscoder.hpp:182
double cosine
The cosine between the two decoders (0 if either is zero).
Definition crosscoder.hpp:190
double norm_b
Definition crosscoder.hpp:183
double norm_a
Definition crosscoder.hpp:183
double relative_norm
|d^b| / (|d^a| + |d^b|) (Lindsey et al., Kassem et al.): 0 a only, ½ even, 1 b only.
Definition crosscoder.hpp:185
double delta_norm
Minder et al.'s Δnorm, ½ (1 + (|d^b| - |d^a|) / max(|d^a|, |d^b|)): the same ends and middle,...
Definition crosscoder.hpp:188
Definition crosscoder.hpp:35
double shared_fraction
Definition crosscoder.hpp:62
uint64_t seed
Definition crosscoder.hpp:66
int64_t k
BatchTopK: mean latents kept per input (the delta partition's, with delta).
Definition crosscoder.hpp:40
bool delta
The Delta-Crosscoder (Kassem et al.), for two sources: source 0 the base model and source 1 the fine-...
Definition crosscoder.hpp:61
float l1_coefficient
L1: λ.
Definition crosscoder.hpp:38
int64_t k_aux
AuxK, as TopKSparseAutoencoder: the k_aux largest dead latents reconstruct the error (0: half the inp...
Definition crosscoder.hpp:43
float aux_coefficient
Definition crosscoder.hpp:44
CrosscoderSparsity sparsity
Definition crosscoder.hpp:36
int64_t k_shared
The shared partition's latents per input (0: twice k, the paper's multiplier).
Definition crosscoder.hpp:64
float threshold_decay
BatchTopK's inference threshold: a moving average of the smallest kept scaled value per training batc...
Definition crosscoder.hpp:48
float delta_coefficient
Definition crosscoder.hpp:65
int64_t dead_after
Definition crosscoder.hpp:45
float decoder_init_norm
Every latent's decoder starts at this norm in every source (0: 0.05 for L1, 1 for BatchTopK,...
Definition crosscoder.hpp:51
One batch's loss, split into its parts.
Definition featurizer.hpp:18
Configuration for LRP relevance propagation: which rule a module applies and its hyperparameters....
Definition lrp_rule_config.hpp:72
Latent Scaling (Minder et al., Eq. 4): how much of latent j's b-side decoder direction each source ac...
Definition crosscoder.hpp:218
double beta_error_b
Definition crosscoder.hpp:220
int64_t active
Inputs where the latent fired.
Definition crosscoder.hpp:223
double nu_reconstruction
Definition crosscoder.hpp:221
double nu_error
Definition crosscoder.hpp:221
int64_t latent
Definition crosscoder.hpp:219
double beta_reconstruction_b
Definition crosscoder.hpp:220
double beta_error_a
Definition crosscoder.hpp:220
double beta_reconstruction_a
Definition crosscoder.hpp:220