pulsatrix 1.1.0
Loading...
Searching...
No Matches
masked_diffusion_lrp.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <cstdint>
18#include <optional>
19#include <vector>
20
21#include "pulsatrix/attnlrp_parity.hpp" // LxtAttnLrpConfig
24
25namespace pulsatrix {
26
32public:
33 virtual ~StepRelevanceModel() = default;
35 [[nodiscard]] virtual std::vector<float> forward(const std::vector<int64_t>& tokens) = 0;
38 [[nodiscard]] virtual std::vector<float> relevance(const std::vector<float>& relevance_logits) = 0;
39};
40
44public:
48 [[nodiscard]] std::vector<float> forward(const std::vector<int64_t>& tokens) override;
49 [[nodiscard]] std::vector<float> relevance(const std::vector<float>& relevance_logits) override;
50
51private:
52 EncoderLM& model_;
53 DeviceBackend* backend_;
54 LRPRuleConfig config_;
55 int64_t length_ = 0;
56};
57
61public:
67 [[nodiscard]] std::vector<float> forward(const std::vector<int64_t>& tokens) override;
68 [[nodiscard]] std::vector<float> relevance(const std::vector<float>& relevance_logits) override;
69
70private:
71 CausalLM& model_;
72 DeviceBackend* backend_;
73 LRPRuleConfig config_;
74 int64_t length_ = 0;
75};
76
78enum class CommitmentMode {
81 Graph,
85};
86
90 int64_t position = 0;
97 std::optional<int64_t> token;
98};
99
103 int64_t step = 0;
105 double seeded = 0;
107 double to_prompt = 0;
108 double to_committed = 0;
109 double to_mask = 0;
111 [[nodiscard]] double leak() const { return seeded - to_prompt - to_committed - to_mask; }
112};
113
123 double value = 0;
126 std::vector<float> prompt;
129 std::vector<float> relayed;
132 std::vector<float> mask;
135 std::vector<CommitmentHop> hops;
138 std::vector<std::vector<float>> step_relevance;
139
140 [[nodiscard]] double prompt_total() const;
141 [[nodiscard]] double relayed_total() const;
142 [[nodiscard]] double mask_total() const;
143 [[nodiscard]] double leak_total() const;
144};
145
159 int64_t mask_token_id, const std::vector<CommitmentTarget>& targets,
161
162// ---- checks: replaying the trajectory -------------------------------------------------------
163
175 const std::vector<int64_t>& input, const MaskedDiffusionConfig& config,
176 const std::vector<int64_t>& skipped = {});
177
185[[nodiscard]] double ReplayCommitmentValue(const DenoiserFn& model, const MaskedDiffusionResult& trajectory,
186 const std::vector<int64_t>& input, const MaskedDiffusionConfig& config,
187 const std::vector<CommitmentTarget>& targets, const std::vector<int64_t>& skipped = {});
188
192 std::vector<float> fractions;
194 std::vector<float> scores;
196 std::vector<float> random_scores;
198 float sign = 1.0f;
201 double auc = 0;
202 double random_auc = 0;
203};
204
215 const MaskedDiffusionConfig& config,
216 const std::vector<CommitmentTarget>& targets,
217 const std::vector<float>& prompt_relevance, int64_t steps = 10,
218 int64_t random_orders = 3, uint64_t seed = 0);
219
224 std::vector<int64_t> positions;
226 std::vector<float> relayed;
228 std::vector<float> effect;
231 double spearman = 0;
232};
233
243 const MaskedDiffusionConfig& config, const std::vector<CommitmentTarget>& targets,
244 const CommitmentRelevance& graph);
245
246} // namespace pulsatrix
AttnLRP parity with LXT on a real language model (LLM-7): pulsatrix's relevance against LXT's own Att...
A CausalLM with bidirectional attention (an A2D diffusion model, DIFF-4) as a StepRelevanceModel,...
Definition masked_diffusion_lrp.hpp:60
std::vector< float > forward(const std::vector< int64_t > &tokens) override
Logits for every position, L * vocab values, row-major.
std::vector< float > relevance(const std::vector< float > &relevance_logits) override
Relevance per input position, L values, from relevance on the last forward()'s logits (L * vocab).
CausalLMStepModel(CausalLM &model, DeviceBackend *backend, LRPRuleConfig config=LxtAttnLrpConfig())
Token embedding, num_hidden_layers pre-norm transformer blocks, a final RMSNorm and a language-model ...
Definition causal_lm.hpp:82
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
An EncoderLM as a StepRelevanceModel: one sequence per pass, AttnLRP with config (LXT's rules by defa...
Definition masked_diffusion_lrp.hpp:43
std::vector< float > relevance(const std::vector< float > &relevance_logits) override
Relevance per input position, L values, from relevance on the last forward()'s logits (L * vocab).
EncoderLMStepModel(EncoderLM &model, DeviceBackend *backend, LRPRuleConfig config=LxtAttnLrpConfig())
std::vector< float > forward(const std::vector< int64_t > &tokens) override
Turns on EncoderLM::set_keep_activations, which relevance() needs.
A masked language model: token embeddings (with ESM's token-dropout scaling, when the config has it),...
Definition encoder_lm.hpp:109
One denoising step's model as the explainer sees it: a forward pass that keeps what the relevance pas...
Definition masked_diffusion_lrp.hpp:31
virtual ~StepRelevanceModel()=default
virtual std::vector< float > forward(const std::vector< int64_t > &tokens)=0
Logits for every position, L * vocab values, row-major.
virtual std::vector< float > relevance(const std::vector< float > &relevance_logits)=0
Relevance per input position, L values, from relevance on the last forward()'s logits (L * vocab).
Masked-language-model encoders: ESM-2 protein language models (PLM-2), Hugging Face EsmForMaskedLM ch...
Sampling from a masked diffusion language model (DIFF-2): start from a sequence with masked positions...
Definition acquisition_functions.hpp:16
double ReplayCommitmentValue(const DenoiserFn &model, const MaskedDiffusionResult &trajectory, const std::vector< int64_t > &input, const MaskedDiffusionConfig &config, const std::vector< CommitmentTarget > &targets, const std::vector< int64_t > &skipped={})
The explained value on a replay (ReplayMaskedDiffusion): each target's logit at the step that wrote i...
CommitmentRelevance ExplainCommitments(StepRelevanceModel &model, const MaskedDiffusionResult &trajectory, int64_t mask_token_id, const std::vector< CommitmentTarget > &targets, CommitmentMode mode=CommitmentMode::Graph)
Explains targets on trajectory, which SampleMaskedDiffusion produced with model's logits....
LRPRuleConfig LxtAttnLrpConfig()
The rule configuration that matches LXT's AttnLRP on a Hugging Face model: the epsilon rule with a ti...
MaskedDiffusionResult ReplayMaskedDiffusion(const DenoiserFn &model, const MaskedDiffusionResult &trajectory, const std::vector< int64_t > &input, const MaskedDiffusionConfig &config, const std::vector< int64_t > &skipped={})
Reruns trajectory from input with its write order frozen: each step writes the same positions as befo...
std::function< std::vector< float >(const std::vector< int64_t > &tokens)> DenoiserFn
The model as the sampler sees it: the current sequence (masked positions included) in,...
Definition masked_diffusion.hpp:25
CommitmentPatchResult CommitmentPatchCheck(const DenoiserFn &model, const MaskedDiffusionResult &trajectory, const MaskedDiffusionConfig &config, const std::vector< CommitmentTarget > &targets, const CommitmentRelevance &graph)
Leaves each committed intermediate token masked in turn, replays the trajectory, and compares the cha...
CommitmentMode
How far relevance travels.
Definition masked_diffusion_lrp.hpp:78
@ Graph
Relevance on a committed token is re-seeded at the step that wrote it, until it reaches the prompt or...
@ StepLocal
Each target is explained at its own step only: relevance on committed tokens stops there....
CommitmentDeletionCurve CommitmentDeletionCheck(const DenoiserFn &model, const MaskedDiffusionResult &trajectory, const MaskedDiffusionConfig &config, const std::vector< CommitmentTarget > &targets, const std::vector< float > &prompt_relevance, int64_t steps=10, int64_t random_orders=3, uint64_t seed=0)
Deletes prompt tokens (replaces them with the mask token) in order of how much they support the value...
A prompt-deletion curve against its random baseline.
Definition masked_diffusion_lrp.hpp:190
float sign
The sign of the undeleted value.
Definition masked_diffusion_lrp.hpp:198
double random_auc
Definition masked_diffusion_lrp.hpp:202
std::vector< float > random_scores
The same in random orders (their mean).
Definition masked_diffusion_lrp.hpp:196
double auc
Trapezoid areas under each curve times sign; a faithful explanation removes the value faster: auc < r...
Definition masked_diffusion_lrp.hpp:201
std::vector< float > fractions
Fractions of the prompt deleted: 0, 1/steps, ..., 1.
Definition masked_diffusion_lrp.hpp:192
std::vector< float > scores
The explained value after deleting that share, most relevant first.
Definition masked_diffusion_lrp.hpp:194
One step's relevance pass: what went in and where it landed.
Definition masked_diffusion_lrp.hpp:101
double to_committed
Definition masked_diffusion_lrp.hpp:108
double to_prompt
Relevance on the step's input, by kind of token.
Definition masked_diffusion_lrp.hpp:107
int64_t step
The index into MaskedDiffusionResult::steps.
Definition masked_diffusion_lrp.hpp:103
double leak() const
What the pass didn't conserve: biases' shares, norms, the stabilizer.
Definition masked_diffusion_lrp.hpp:111
double to_mask
Definition masked_diffusion_lrp.hpp:109
double seeded
Relevance seeded on the step's logits: its targets' logits and what was relayed.
Definition masked_diffusion_lrp.hpp:105
Step-resolved patching against the relevance relayed through each committed token.
Definition masked_diffusion_lrp.hpp:221
std::vector< float > effect
The value minus its value with the position left masked from its write step on.
Definition masked_diffusion_lrp.hpp:228
std::vector< int64_t > positions
The generated positions patched: every non-target position written before the last target's step.
Definition masked_diffusion_lrp.hpp:224
double spearman
Spearman correlation of relayed relevance with the effect; 0 for fewer than 3 positions....
Definition masked_diffusion_lrp.hpp:231
std::vector< float > relayed
The relevance relayed through each (CommitmentRelevance::relayed).
Definition masked_diffusion_lrp.hpp:226
Where the relevance of the explained tokens went. Every vector has one value per position....
Definition masked_diffusion_lrp.hpp:119
std::vector< float > relayed
Relevance that reached each generated position from later steps. Graph: relayed onward from the step ...
Definition masked_diffusion_lrp.hpp:129
std::vector< float > prompt
Relevance on the prompt (positions not masked in the input), from every step. Zero elsewhere.
Definition masked_diffusion_lrp.hpp:126
double value
The explained value: the targets' logits, each at the step that wrote it, summed. The relevance seede...
Definition masked_diffusion_lrp.hpp:123
std::vector< float > mask
Relevance each position absorbed while it was masked, summed over steps: the mask sink.
Definition masked_diffusion_lrp.hpp:132
std::vector< std::vector< float > > step_relevance
For each of the trajectory's steps, the relevance on its input (L values), or empty for a step that w...
Definition masked_diffusion_lrp.hpp:138
std::vector< CommitmentHop > hops
One entry per step explained, in the order explained (the last step first). Steps nothing was seeded ...
Definition masked_diffusion_lrp.hpp:135
CommitmentMode mode
Definition masked_diffusion_lrp.hpp:120
A generated token to explain.
Definition masked_diffusion_lrp.hpp:88
std::optional< int64_t > token
The token whose logit is explained, at the step that last wrote the position. Unset: the token writte...
Definition masked_diffusion_lrp.hpp:97
int64_t position
A position the trajectory wrote.
Definition masked_diffusion_lrp.hpp:90
Configuration for LRP relevance propagation: which rule a module applies and its hyperparameters....
Definition lrp_rule_config.hpp:72
Sampling settings. The defaults fill greedily by confidence.
Definition masked_diffusion.hpp:59
A sampled sequence and how it was produced.
Definition masked_diffusion.hpp:99