pulsatrix 1.1.0
Loading...
Searching...
No Matches
loreft.hpp File Reference

LoReFT (Wu et al., 2024): fine-tune a frozen language model by editing its hidden states in a learned low-rank subspace, at a few positions after chosen layers, and explain the edit with LRP. More...

#include <cstdint>
#include <string>
#include <vector>
#include "pulsatrix/causal_lm.hpp"
Include dependency graph for loreft.hpp:

Go to the source code of this file.

Classes

struct  pulsatrix::LoReFTConfig
 Where and how large LoReFT's interventions are. More...
 
class  pulsatrix::LoReFTIntervention
 One block's LoReFT edit. Parameters, named as pyreft's state dict: More...
 

Namespaces

namespace  pulsatrix
 

Functions

std::vector< LoReFTIntervention * > pulsatrix::ApplyLoReFT (CausalLM &model, const LoReFTConfig &config={})
 Installs a LoReFTIntervention after each of config.layers' blocks and freezes the rest of the model: only the interventions train.
 
void pulsatrix::OrthonormalizeLoReFT (CausalLM &model)
 orthonormalize() on every LoReFT intervention of the model: call it after each optimizer step.
 
void pulsatrix::SaveLoReFT (CausalLM &model, const std::string &path)
 Saves the model's LoReFT interventions as safetensors: per layer i, pyreft's state dict under layer.<i>.rotate_layer, layer.<i>.learned_source.weight and layer.<i>.learned_source.bias, in pyreft's layouts, with metadata first and last.
 
std::vector< LoReFTIntervention * > pulsatrix::LoadLoReFT (CausalLM &model, const std::string &path)
 Installs the interventions in a file SaveLoReFT() wrote, or one built from pyreft's per-intervention state dicts under those keys (convert its .bin files with tools/convert/pickle_to_safetensors.py), and freezes the rest of the model.
 

Detailed Description

LoReFT (Wu et al., 2024): fine-tune a frozen language model by editing its hidden states in a learned low-rank subspace, at a few positions after chosen layers, and explain the edit with LRP.

At each chosen position, the residual stream h after a block becomes

h' = h + R^T (W h + b - R h)

with R (r, d) orthonormal rows. Inside the r-dimensional subspace R spans, the edit replaces the hidden state's coordinates R h by the learned W h + b; outside it, h is untouched. The model's own weights stay frozen. The edit is affine in h, so the epsilon rule propagates relevance through it exactly, and LoReFTIntervention splits the relevance into the part the edit carries and the part that passes through untouched.