ruvector-coherence
Quantitative coherence metrics for comparing attention mechanisms — measure what gating costs and what it preserves.
| Metric | What It Measures | Use Case |
|---|---|---|
contradiction_rate |
Semantic inversion (negative dot product) | Detect gating failures |
entailment_consistency |
Adjacent-output alignment (cosine) | Detect erratic swings |
delta_behavior |
Direction + magnitude drift | Full coherence profile |
jaccard_similarity |
Mask overlap (intersection/union) | Compare sparsity patterns |
quality_check |
Cosine similarity pass/fail gate | CI/CD quality guardrail |
evaluate_batch |
Aggregate stats with 95% CI | Statistical significance |
Overview
When replacing softmax attention with a gated alternative (such as min-cut gating), the central question is: does the output stay coherent? This crate provides a suite of metrics, comparison utilities, quality guardrails, and batched evaluation tools to answer that question quantitatively.
"Coherence" here means the degree to which gated attention outputs preserve the semantic and structural properties of baseline softmax outputs. The crate measures this through vector similarity, contradiction detection, mask overlap analysis, and statistical aggregation with confidence intervals.
Modules
| Module | Purpose |
|---|---|
metrics |
contradiction_rate, entailment_consistency, delta_behavior |
comparison |
compare_attention_masks, edge_flip_count, jaccard_similarity |
quality |
quality_check with cosine_similarity and l2_distance |
batch |
evaluate_batch with mean, std, 95% CI, and pass rate |
Metrics Explained
contradiction_rate
Measures the fraction of output pairs where the dot product between prediction and reference vectors is negative. A high contradiction rate signals that gating has inverted the semantic direction of outputs.
use contradiction_rate;
let predictions = vec!;
let references = vec!;
let rate = contradiction_rate;
// rate = 0.5 (second pair contradicts)
entailment_consistency
Computes mean pairwise cosine similarity between consecutive output vectors. High values (close to 1.0) indicate that adjacent outputs remain aligned -- useful for detecting whether gating introduces erratic token-to-token swings.
use entailment_consistency;
let outputs = vec!;
let consistency = entailment_consistency;
// consistency close to 1.0 (outputs smoothly evolve)
delta_behavior (DeltaMetric)
Compares baseline and gated attention outputs element-by-element, returning:
| Field | Meaning |
|---|---|
coherence_delta |
Cosine similarity minus 1.0 (0.0 = identical direction) |
decision_flips |
Count of sign disagreements between baseline and gated values |
path_length_change |
Relative change in L2 norm (magnitude drift) |
use delta_behavior;
let baseline = vec!;
let gated = vec!;
let delta = delta_behavior;
println!;
println!;
println!;
Mask Comparison
compare_attention_masks (ComparisonResult)
Provides a full comparison between two boolean attention masks:
| Field | Meaning |
|---|---|
jaccard |
Jaccard similarity (intersection / union) |
edge_flips |
Number of positions where masks disagree |
baseline_edges |
Count of true entries in baseline mask |
gated_edges |
Count of true entries in gated mask |
sparsity_ratio |
Ratio of gated sparsity to baseline sparsity |
use compare_attention_masks;
let baseline = vec!;
let gated = vec!;
let cmp = compare_attention_masks;
println!; // 0.500
println!; // 2
println!;
Standalone helpers jaccard_similarity and edge_flip_count are also available
for use outside of the full comparison struct.
Quality Guardrails
quality_check (QualityResult)
A pass/fail gate that checks whether gated output stays close enough to baseline output. The check passes when cosine similarity meets or exceeds a configurable threshold.
use quality_check;
let baseline_out = vec!;
let gated_out = vec!;
let result = quality_check;
println!;
println!;
println!;
Batch Evaluation
evaluate_batch (BatchResult)
Runs delta_behavior and quality_check across an array of sample pairs,
aggregating results with standard statistics.
| Field | Meaning |
|---|---|
mean_coherence_delta |
Average coherence delta across samples |
std_coherence_delta |
Standard deviation |
ci_95_lower / ci_95_upper |
95% confidence interval (z = 1.96) |
n_samples |
Number of evaluated pairs |
pass_rate |
Fraction of samples passing the quality threshold |
use evaluate_batch;
let baselines = vec!;
let gated = vec!;
let batch = evaluate_batch;
println!;
println!;
println!;
println!;
Typical Workflow
1. Run attn_softmax() --> baseline outputs
2. Run attn_mincut() --> gated outputs + keep_mask
3. quality_check() --> per-sample pass/fail
4. compare_attention_masks() --> mask overlap analysis
5. evaluate_batch() --> aggregate stats with 95% CI
6. Export via ruvector-profiler CSV emitters
Step 1: Run baseline and gated attention
use ;
let = ;
let q = vec!;
let k = vec!;
let v = vec!;
let baseline = attn_softmax;
let gated = attn_mincut;
Step 2: Individual metrics
use *;
let delta = delta_behavior;
println!;
println!;
let quality = quality_check;
println!;
Step 3: Batch evaluation with confidence intervals
let baselines = vec!;
let gateds = vec!;
let batch = evaluate_batch;
println!;
println!;
println!;
Step 4: Success criteria
| Criterion | Threshold | Check |
|---|---|---|
| Coherence delta | < 5% | batch.mean_coherence_delta < 0.05 |
| Accuracy loss | < 1% | batch.pass_rate > 0.99 |
| Contradiction rate | < 0.1% | contradiction_rate(...) < 0.001 |
Related Crates
| Crate | Role |
|---|---|
ruvector-attn-mincut |
Provides gated attention operators |
ruvector-profiler |
Exports results to CSV for analysis |
ruvector-solver |
Sublinear solvers for graph analytics |
License
Licensed under the MIT License.