57 const LRPRuleConfig& config = {});
67 const LRPRuleConfig& config = {});
91 [[nodiscard]] std::vector<TokenShift>
top(int64_t n,
bool decrease =
false, int64_t min_count = 1)
const;
93 [[nodiscard]] std::vector<TokenShift>
all()
const;
98 double tuned = 0, base = 0;
100 std::map<int64_t, Sum> sums_;
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
Collects RelevanceDiff results over a dataset by input token: which tokens a fine-tune made the model...
Definition fine_tune_diff.hpp:83
std::vector< TokenShift > top(int64_t n, bool decrease=false, int64_t min_count=1) const
The n tokens with the largest mean increase (or, with decrease, the largest mean decrease),...
std::vector< TokenShift > all() const
Every token seen, by id.
void add(const Tensor &token_ids, const RelevanceDiff &diff)
Adds one batch: token_ids (N, L) and a diff whose relevance is (N, L). Each score is divided by its s...
Selects which LRP rule variant a Module::propagate_relevance() call uses.
Abstract base every layer subclasses – NVI forward(), pure-virtual LRP contract.
Definition acquisition_functions.hpp:16
IntruderResult IntruderDimensions(const Tensor &base, const Tensor &tuned, int64_t k=10, double epsilon=0.6)
Counts the top-k singular vectors of tuned that no singular vector of base resembles: |cos| < epsilon...
RelevanceDiff DiffAdapterRelevance(Module &model, const Tensor &input, const DiffTarget &target={}, const LRPRuleConfig &config={})
DiffRelevance() for a fine-tune held as adapters on model: the tuned run with its LoRA adapters,...
@ Sum
Σ share × score. Conserves the total: the words' scores plus the unassigned score add up to the token...
std::vector< LayerIntruders > LoraIntruderDimensions(Module &model, int64_t k=10, double epsilon=0.6)
Intruder dimensions of every LoRA layer: its base weight W against W + (alpha / r) A B.
RelevanceDiff DiffRelevance(Module &base, Module &tuned, const Tensor &input, const DiffTarget &target={}, const LRPRuleConfig &config={})
Explains the same prediction in both models: forward() on input, then relevance seeded with each mode...
Which output both models are asked to explain.
Definition fine_tune_diff.hpp:25
std::vector< int64_t > classes
One class or token per sample. Empty: the fine-tuned model's top prediction, so both models explain t...
Definition fine_tune_diff.hpp:31
int64_t position
For outputs shaped (N, L, C), such as a language model's logits: the position, with negative values c...
Definition fine_tune_diff.hpp:28
Intruder dimensions of one weight matrix (Shuttleworth et al., 2024, Definition 3....
Definition fine_tune_diff.hpp:104
std::vector< double > max_similarity
For each of the tuned matrix's top-k singular vectors, the largest |cosine similarity| with any of th...
Definition fine_tune_diff.hpp:107
int64_t count
How many fall below epsilon: the intruder dimensions.
Definition fine_tune_diff.hpp:111
std::vector< double > singular_values
Their singular values, largest first.
Definition fine_tune_diff.hpp:109
IntruderDimensions() for one named layer.
Definition fine_tune_diff.hpp:132
std::string name
Definition fine_tune_diff.hpp:133
IntruderResult result
Definition fine_tune_diff.hpp:134
The two explanations of the same prediction, and their difference.
Definition fine_tune_diff.hpp:35
Tensor tuned
The fine-tuned model's input relevance, as its propagate_relevance() returns it ((N,...
Definition fine_tune_diff.hpp:38
std::vector< int64_t > classes
The class explained for each sample.
Definition fine_tune_diff.hpp:44
std::vector< double > tuned_scores
Each model's output at that class, per sample: the relevance each one distributes.
Definition fine_tune_diff.hpp:46
Tensor base
The base model's, for the same class at the same position.
Definition fine_tune_diff.hpp:40
Tensor difference
tuned - base.
Definition fine_tune_diff.hpp:42
std::vector< double > base_scores
Definition fine_tune_diff.hpp:47
One token's relevance shift, averaged over every occurrence seen.
Definition fine_tune_diff.hpp:70
double tuned
Definition fine_tune_diff.hpp:73
double difference
The mean of tuned - base.
Definition fine_tune_diff.hpp:76
int64_t count
Definition fine_tune_diff.hpp:72
double base
Definition fine_tune_diff.hpp:74
int64_t token
Definition fine_tune_diff.hpp:71
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).