Routing Analysis
Routing statistics, donor matching, and causal routing interventions for MoE models (issue #42): gate entropy and routing churn between correct/incorrect answers, matched donor/random token swaps, and the CETT × routing overlap cross-check.
hprobes.routing
Routing-pattern analysis for MoE probes (issue #42).
Pure analysis over decision-token routing tables captured by
:class:hprobes.moe.MoECapture -- no additional forwards. Compares routing
of correct vs incorrect samples on selection frequencies, selected-set
overlap (Jaccard), and gate-mass entropy, each guarded by a permutation
null over labels. The counterfactual-routing machinery builds on the same
captured tables.
cett_routing_overlap(cett_feature_names, stats)
Cross-check CETT-selected experts against routing-flagged experts (#42).
cett_feature_names are the selected feature names from
fit_moe's probe (L{layer}:E{expert}:N{neuron}); stats is the
routing_stats output. Agreement = routing-flagged experts hosting
CETT-selected neurons -- overlap above chance argues routing and neurons
capture the same signal; low overlap argues dissociation. Shared-expert
features (L{layer}:S:N{neuron}) have no routing analog and are
counted separately in n_shared_features.
Source code in src/hprobes/routing.py
dose_rows(own_experts, own_weights, donor_experts, donor_weights, dose)
Replace the first dose routing slots with the donor's (#42 standard 1).
Dose is capped at the row length (top-k). dose >= k is a full routing
takeover; dose == 0 is a no-op returning the own row unchanged.
Source code in src/hprobes/routing.py
frequency_sampled_experts(usage, dose, k, seed)
dose distinct experts drawn by empirical usage frequency (#42 standard 4-c).
The random control draws from exactly the experts that were ever routed
(positive usage), weighted by their usage counts -- frequency-matched by
construction. Deterministic for a fixed seed.
Source code in src/hprobes/routing.py
match_donors(labels, lengths, entropies)
Nearest opposite-label donor per sample (#42 standard 3).
Donor = argmin over samples of the opposite label of
(|length diff|, |entropy diff|, index) -- a documented proxy for
question difficulty (option count is already fixed per dataset here;
extend the key when datasets mix option counts). Samples without any
opposite-label candidate are simply absent from the result.
Source code in src/hprobes/routing.py
routing_stats(routings, labels, gates=None, n_experts=None, n_perm=200, seed=0, top_flagged=10, alpha=0.05)
Compare decision-token routing between correct and incorrect samples.
Parameters
routings : per-sample (n_layers, k) int tensors of selected expert
indices (MoECapture.routing_table() at the decision position).
labels : 1 = incorrect (hallucination), 0 = correct.
gates : optional per-sample (n_layers, k) routing weights.
n_experts : population size for the indicator space (default: max index + 1).
n_perm : permutation draws for the nulls.
Returns per-layer frequencies, usage deltas with two-sided permutation
p-values and flagged experts, within/between-group Jaccard over
(layer, expert) pairs, and gate-mass entropy comparison if gates
were passed.
Source code in src/hprobes/routing.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |