pulsatrix 1.1.0
Loading...
Searching...
No Matches
linear_probe.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <cmath>
9#include <cstdint>
10#include <random>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
18
19namespace pulsatrix {
20
53public:
66 : LinearProbe(activation_dim, backend, static_cast<unsigned>(next_seed())) {}
67
68 LinearProbe(int64_t activation_dim, DeviceBackend* backend, unsigned seed)
69 : activation_dim_(activation_dim),
70 // Clamped only so a rejected dimension can't reach LinearModule/Shape and throw
71 // *their* message before the check below throws this class's own, clearer one --
72 // member initialization necessarily runs before the constructor body.
73 classifier_(activation_dim > 0 ? activation_dim : 1, 1, backend),
74 loss_(backend) {
75 if (activation_dim <= 0) {
76 throw std::invalid_argument("LinearProbe: activation_dim must be positive");
77 }
78 std::mt19937 rng(seed);
79 std::vector<float> weights(static_cast<size_t>(activation_dim));
80 for (float& w : weights) {
81 w = uniform_symmetric(rng, 0.01f);
82 }
83 classifier_.set_weight(weights);
84 classifier_.set_bias(std::vector<float>{uniform_symmetric(rng, 0.01f)});
85 }
86
104 template <typename OptimizerT>
105 float train_step(const Tensor& activation_batch, const Tensor& label_batch, OptimizerT& optimizer) {
106 validate_batch(activation_batch, label_batch, "LinearProbe::train_step");
107
108 optimizer.zero_grad(classifier_);
109
110 Tensor logits = classifier_.forward(activation_batch);
111 const float loss_value = loss_.forward(logits, label_batch);
112
113 Tensor grad_logits = loss_.backward();
114 (void)classifier_.backward(grad_logits); // the activations have no upstream to receive this
115
116 optimizer.step(classifier_);
117 return loss_value;
118 }
119
140 [[nodiscard]] float accuracy(const Tensor& activation_batch, const Tensor& label_batch) const {
141 validate_batch(activation_batch, label_batch, "LinearProbe::accuracy");
142
143 const Tensor logits = classifier_.forward(activation_batch);
144 const int64_t n = logits.numel();
145 int64_t correct = 0;
146 for (int64_t i = 0; i < n; ++i) {
147 const bool predicted_positive = logits.data()[i] >= 0.0f;
148 const bool labeled_positive = label_batch.data()[i] >= 0.5f;
149 if (predicted_positive == labeled_positive) {
150 ++correct;
151 }
152 }
153 return static_cast<float>(correct) / static_cast<float>(n);
154 }
155
157 [[nodiscard]] int64_t activation_dim() const { return activation_dim_; }
158
164 [[nodiscard]] LinearModule& classifier() { return classifier_; }
165
167 [[nodiscard]] const LinearModule& classifier() const { return classifier_; }
168
169private:
171 static float uniform_symmetric(std::mt19937& rng, float scale) {
172 // std::uniform_real_distribution's mapping is implementation-defined, so identical
173 // seeds would not give identical weights across standard libraries. This mapping is
174 // fixed here, making a seeded probe reproducible everywhere.
175 const float unit = static_cast<float>(rng() - std::mt19937::min()) /
176 static_cast<float>(std::mt19937::max() - std::mt19937::min() + 1ull);
177 return (unit * 2.0f - 1.0f) * scale;
178 }
179
195 void validate_batch(const Tensor& activation_batch, const Tensor& label_batch, const char* method) const {
196 const std::string where(method);
197 if (activation_batch.rank() != 2) {
198 throw std::invalid_argument(where + ": activation_batch must be rank-2 (N, activation_dim)");
199 }
200 if (label_batch.rank() != 2 || label_batch.shape().dim(1) != 1) {
201 throw std::invalid_argument(where + ": label_batch must be rank-2 (N, 1)");
202 }
203 if (activation_batch.shape().dim(1) != activation_dim_) {
204 throw std::invalid_argument(where + ": activation_batch width must equal activation_dim");
205 }
206 if (activation_batch.shape().dim(0) != label_batch.shape().dim(0)) {
207 throw std::invalid_argument(where + ": activation_batch and label_batch must have the same batch size");
208 }
209 if (activation_batch.shape().dim(0) <= 0) {
210 throw std::invalid_argument(where + ": batch must not be empty");
211 }
212 }
213
214 int64_t activation_dim_;
215 mutable LinearModule classifier_;
216 BCEWithLogitsLoss loss_;
217};
218
219} // namespace pulsatrix
Binary cross-entropy on raw logits – combined sigmoid + BCE, numerically stable.
Tensor backward() const
Gradient w.r.t. the logits: grad[i] = (sigmoid(x[i]) - y[i]) / numel.
float forward(const Tensor &logits, const Tensor &target)
Computes the loss value and caches logits/target for backward().
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
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
Tensor backward(const Tensor &grad_output) override
Computes the gradient w.r.t. this module's input, and accumulates the weight/bias gradients internall...
void set_bias(std::initializer_list< float > values)
Overwrites the bias buffer – test/initialization use only.
void set_weight(std::initializer_list< float > values)
Overwrites the weight buffer – test/initialization use only.
A linear probe: LinearModule(activation_dim, 1) + BCEWithLogitsLoss, trained on (activation,...
Definition linear_probe.hpp:52
LinearProbe(int64_t activation_dim, DeviceBackend *backend)
Constructs a probe over activations of a given dimension.
Definition linear_probe.hpp:65
int64_t activation_dim() const
Width of the activation vectors this probe reads.
Definition linear_probe.hpp:157
float train_step(const Tensor &activation_batch, const Tensor &label_batch, OptimizerT &optimizer)
Runs one training step: forward, BCE-with-logits loss, backward, one optimizer update of the probe's ...
Definition linear_probe.hpp:105
const LinearModule & classifier() const
Const overload of classifier().
Definition linear_probe.hpp:167
LinearProbe(int64_t activation_dim, DeviceBackend *backend, unsigned seed)
Definition linear_probe.hpp:68
LinearModule & classifier()
The underlying linear classifier – inspection (learned weights are the whole point of a probe: their ...
Definition linear_probe.hpp:164
float accuracy(const Tensor &activation_batch, const Tensor &label_batch) const
Fraction of the batch the probe classifies correctly.
Definition linear_probe.hpp:140
Tensor forward(const Tensor &input)
Runs this module's forward computation.
Definition module.hpp:93
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
int64_t numel() const
Total element count – shape().numel().
Definition tensor.hpp:116
const float * data() const
Raw buffer access. nullptr iff numel() == 0.
Definition tensor.hpp:167
One global seed for everything that isn't given its own, and a deterministic mode that forbids nondet...
Dense/fully-connected layer – the reference Module implementation.
Definition acquisition_functions.hpp:16
uint64_t next_seed()
The next seed in the global stream: a distinct, well-mixed 64-bit value per call, reproducible for a ...