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

A Llama-family decoder-only language model built from a Hugging Face config, and loading its Hugging Face weights by name (IO-4). More...

#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>
#include "pulsatrix/embedding_module.hpp"
#include "pulsatrix/generation.hpp"
#include "pulsatrix/hf_model.hpp"
#include "pulsatrix/linear_module.hpp"
#include "pulsatrix/module.hpp"
#include "pulsatrix/rms_norm_module.hpp"
#include "pulsatrix/tied_lm_head_module.hpp"
#include "pulsatrix/transformer_block.hpp"
Include dependency graph for causal_lm.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  pulsatrix::ResidualIntervention
 A trainable edit of the residual stream after a transformer block (TRN-9: LoReFT), run by CausalLM::set_intervention(). Maps hidden states (N, L, hidden_size) to the same shape and, as a Module, trains and explains with the model. More...
 
class  pulsatrix::CausalLM
 Token embedding, num_hidden_layers pre-norm transformer blocks, a final RMSNorm and a language-model head (tied to the embedding or its own bias-free Linear): the architecture of Llama, SmolLM2, Qwen2/2.5 and Qwen3. Maps (N, L) token ids to (N, L, vocab) logits. More...
 
struct  pulsatrix::WeightMapping
 One entry of a name-mapping manifest. More...
 
struct  pulsatrix::WeightLoadOptions
 Loading settings. More...
 
struct  pulsatrix::WeightLoadReport
 What a load did. More...
 

Namespaces

namespace  pulsatrix
 
namespace  pulsatrix::detail
 

Typedefs

using pulsatrix::HiddenStateHook = std::function< Tensor(int64_t position, const Tensor &hidden)>
 Reads or replaces a language model's residual stream during forward() (FEAT-3): called with a position, 0 for the embeddings entering layer 0 and i for layer i's output, and the hidden states there, (N, L, hidden_size) on the model's device. What it returns, the same shape, is what the model continues with: return the input unchanged to only read it. Splicing a featurizer's reconstruction back in uses it (MeasureLossRecovered, featurizer_metrics.hpp).
 

Enumerations

enum class  pulsatrix::WeightTransform { pulsatrix::Identity , pulsatrix::Transpose }
 How a source tensor becomes a target parameter. More...
 

Functions

Tensor pulsatrix::detail::ApplyHiddenStateHook (const HiddenStateHook &hook, int64_t position, const Tensor &x)
 Runs hook and checks its result keeps the shape.
 
WeightLoadReport pulsatrix::LoadWeights (Module &model, const HfCheckpoint &checkpoint, const std::vector< WeightMapping > &mapping, const WeightLoadOptions &options={})
 Loads checkpoint tensors into model's named parameters and buffers by mapping. Every tensor is read, converted to fp32 (IO-6), sliced and transformed, and checked against its target's shape before anything is written, so a failed load leaves the model unchanged.
 
std::vector< WeightMapping > pulsatrix::HuggingFaceMapping (const HfModelConfig &config)
 The manifest for a Llama-family Hugging Face checkpoint (model.embed_tokens.weight, model.layers.<i>.self_attn.q_proj.weight, ..., model.norm.weight, lm_head.weight) onto CausalLM's parameters: Linear weights transposed, biases and norms as they are.
 
std::unique_ptr< CausalLM > pulsatrix::LoadCausalLM (const std::string &directory, DeviceBackend *backend)
 Reads config.json and the weights in a downloaded Hugging Face model directory into a new CausalLM, strictly: a tied checkpoint's lm_head.weight is the only tensor allowed to go unused.
 

Detailed Description

A Llama-family decoder-only language model built from a Hugging Face config, and loading its Hugging Face weights by name (IO-4).