pulsatrix 1.1.0
Loading...
Searching...
No Matches
attribution_graph.hpp
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdint>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <vector>
14
16
17namespace pulsatrix {
18
27 struct Node {
29 std::string node_id;
31 std::optional<int64_t> feature;
33 std::string layer;
34 int64_t ctx_idx = 0;
37 std::string feature_type;
39 std::string js_node_id;
41 std::string clerp;
43 std::optional<double> influence;
44 std::optional<double> activation;
45 double token_prob = 0.0;
46 bool is_target_logit = false;
47 };
48 struct Link {
49 std::string source;
50 std::string target;
51 double weight = 0.0;
52 };
53
54 std::string slug;
56 std::string scan;
57 std::string prompt;
58 std::vector<std::string> prompt_tokens;
60 std::optional<double> node_threshold;
62 std::string description;
63 std::vector<Node> nodes;
64 std::vector<Link> links;
65};
66
74[[nodiscard]] std::string ToNeuronpediaJson(const AttributionGraph& graph);
75
78[[nodiscard]] AttributionGraph ParseNeuronpediaGraph(std::string_view json);
79
88void ComputeInfluence(AttributionGraph& graph, const std::vector<double>& scores);
89
96void PruneLinks(AttributionGraph& graph, double edge_threshold);
97
106[[nodiscard]] AttributionGraph FromCircuitGraph(const CircuitGraphDocument& doc, const std::string& slug,
107 const std::string& scan);
108
109} // namespace pulsatrix
Versioned JSON documents (pulsatrix.<kind>.v1) that every renderer reads.
Definition acquisition_functions.hpp:16
void PruneLinks(AttributionGraph &graph, double edge_threshold)
Keeps the fewest links, largest |weight| first, whose |weight| sums to at least edge_threshold of the...
std::string ToNeuronpediaJson(const AttributionGraph &graph)
The graph as Neuronpedia/circuit-tracer JSON, including circuit-tracer's optional node fields (token_...
AttributionGraph ParseNeuronpediaGraph(std::string_view json)
Reads a graph written by ToNeuronpediaJson, circuit-tracer or Neuronpedia. Unknown fields are ignored...
AttributionGraph FromCircuitGraph(const CircuitGraphDocument &doc, const std::string &slug, const std::string &scan)
A circuit graph (a chain of operations scored by ablation) for the viewer: two columns,...
void ComputeInfluence(AttributionGraph &graph, const std::vector< double > &scores)
Sets each node's influence the way circuit-tracer's pruning does: nodes other than embeddings and log...
Definition attribution_graph.hpp:27
double token_prob
Definition attribution_graph.hpp:45
std::optional< int64_t > feature
The feature's index; the viewer groups nodes with the same layer and feature.
Definition attribution_graph.hpp:31
bool is_target_logit
Definition attribution_graph.hpp:46
std::string feature_type
"embedding" and "logit" are special; a type containing "error" is drawn as an error node; anything el...
Definition attribution_graph.hpp:37
std::optional< double > influence
Cumulative influence (0 to 1); the viewer hides nodes above its pruning threshold.
Definition attribution_graph.hpp:43
std::string node_id
Unique id, for example "3_17_5" (layer, feature, position). Links refer to it.
Definition attribution_graph.hpp:29
int64_t ctx_idx
Definition attribution_graph.hpp:34
std::string js_node_id
The viewer's id for hover state across prompts.
Definition attribution_graph.hpp:39
std::string layer
"E" for an embedding, a layer number, or the logit layer.
Definition attribution_graph.hpp:33
std::optional< double > activation
Definition attribution_graph.hpp:44
std::string clerp
The node's label. A logit's must hold (p=PROBABILITY): the viewer reads it.
Definition attribution_graph.hpp:41
An attribution graph as Neuronpedia's schema (graph-schema.json) and circuit-tracer's create_graph_fi...
Definition attribution_graph.hpp:26
std::string description
Optional metadata.info text.
Definition attribution_graph.hpp:62
std::string prompt
Definition attribution_graph.hpp:57
std::string scan
The model id, for example "gemma-2-2b"; Neuronpedia uses it to find the model.
Definition attribution_graph.hpp:56
std::vector< std::string > prompt_tokens
Definition attribution_graph.hpp:58
std::string slug
Definition attribution_graph.hpp:54
std::vector< Link > links
Definition attribution_graph.hpp:64
std::vector< Node > nodes
Definition attribution_graph.hpp:63
std::optional< double > node_threshold
The viewer's default pruning threshold (circuit-tracer uses 0.8).
Definition attribution_graph.hpp:60
pulsatrix.circuit_graph.v1: a CircuitGraph's scored nodes and weighted edges.
Definition document.hpp:135