Skip to content

MJ-Ref/DCTT

Repository files navigation

DCTT: Discrete-to-Continuous Transition Testing

Python 3.11+ License: MIT Code style: ruff

A rigorous framework for diagnosing token-level embedding geometry pathologies in Large Language Models.

DCTT provides tools to identify tokens with problematic local geometry in LLM embedding spaces. The core contribution is a diagnostic and causal validation framework; repair methods are exploratory, with a current finding that single-token local optimization is insufficient when entire neighborhoods exhibit pathological geometry.

Key Features

  • Multi-stage diagnostics: Fast screening (Stage 1) followed by detailed spectral analysis (Stage 2)
  • Spectral geometry metrics: Participation ratio, condition number, effective dimension, log-determinant
  • Constrained repair optimization: Fix geometry while preserving semantics
  • Causal validation: Stress tests and matched control experiments
  • Apple Silicon optimized: USearch HNSW with ARM NEON acceleration
  • Reproducibility first: W&B tracking, config snapshots, seed management

Installation

# Clone the repository
git clone https://github.com/MJ-Ref/DCTT.git
cd DCTT

# Install with development dependencies
pip install -e ".[dev]"

# For Apple Silicon with MLX support
pip install -e ".[dev,mlx]"

# For cloud GPU with Modal
pip install -e ".[dev,modal]"

Requirements

  • Python 3.11+
  • PyTorch 2.3+
  • 16GB+ RAM (96GB recommended for full vocabulary analysis)

Quick Start

1. Extract Embeddings

# Extract and normalize embeddings from a model
dctt extract --model Qwen/Qwen2.5-Coder-7B --output outputs/embeddings.npy

2. Run Diagnostic Census

# Analyze geometry for all tokens
python experiments/run_census.py model=qwen2_5_coder_7b

# Or sample 1000 tokens for quick analysis
python experiments/run_census.py model=qwen2_5_coder_7b \
    experiment.tokens.mode=sample \
    experiment.tokens.sample_size=1000

3. Run Causal Repair Experiment

# Repair high-severity tokens and compare to matched controls
python experiments/run_causal_repair.py model=qwen2_5_coder_7b

Architecture

dctt/
├── core/           # Types, exceptions, registry pattern
├── embeddings/     # Extraction, normalization, caching
├── neighbors/      # USearch HNSW index (M3-optimized)
├── metrics/        # Stage 1 & 2 diagnostic metrics
├── repair/         # Constrained optimization repair
├── stress_tests/   # Code syntax, math formatting tests
├── evaluation/     # Statistical analysis, causal inference
└── tracking/       # W&B integration, reproducibility

Diagnostic Pipeline

Stage 1: Basic Local Outlier Checks

Fast, cheap metrics for initial screening:

  • μ_k: Mean k-NN distance
  • med_k: Median k-NN distance
  • spread_q: Quantile spread ratio (q90/q10)

Stage 2: Spectral Geometry Analysis

Core contribution - displacement matrix-based metrics:

  • dim95: Effective dimension at 95% variance
  • PR: Participation ratio (eigenvalue spread)
  • cond: Local condition number
  • logdet: Log-determinant of covariance
  • anisotropy: Dominant direction ratio

Severity Scoring

Robust z-scores within (frequency_tier, token_type) buckets ensure fair comparison across token categories.

Configuration

DCTT uses Hydra for configuration management:

# Override model
python experiments/run_census.py model=llama3_8b

# Override compute settings
python experiments/run_census.py compute=modal_gpu

# Override multiple settings
python experiments/run_census.py \
    model=qwen2_5_coder_7b \
    neighbors.k=100 \
    seed=123

Key Configuration Files

File Purpose
configs/config.yaml Root configuration
configs/model/*.yaml Model-specific settings
configs/compute/*.yaml Hardware optimization
configs/pipeline/*.yaml Pipeline stages
configs/experiment/*.yaml Experiment configurations

Metrics Reference

Participation Ratio (PR)

PR = (Σλ)² / Σ(λ²)

Measures effective dimensionality. PR = d for uniform eigenvalues, PR ≈ 1 for single dominant direction.

Condition Number

cond = (λ₁ + ε) / (λₘ + ε)

High condition numbers indicate ill-conditioned local geometry that may cause gradient instabilities.

Log-Determinant

logdet = Σ log(λᵢ + ε)

Measures local "volume" or dispersion. Very negative values indicate collapsed geometry.

Repair Methods

Single-Token Repair (Negative Result)

The initial repair optimizer uses projected gradient descent with tangent space projection:

for outer_iter in range(max_outer_iters):
    neighbors = knn(index, embedding, k)  # Recompute neighbors
    for step in range(inner_steps):
        loss = geometry_loss + λ_anchor * anchor_loss + λ_nn * nn_preserve_loss
        grad_tangent = gradient - (gradient · embedding) * embedding
        embedding = embedding - lr * grad_tangent
        embedding = project_to_constraints(embedding)  # Unit norm, max delta

Finding: Single-token local optimization preserves semantics (Jaccard > 0.7) but does NOT improve geometry metrics. This occurs because centered displacement covariance makes the loss independent of a single moving token when neighbors are fixed.

Cluster-Level Repair (Positive Result) ✓

To address the single-token limitation, we implemented cluster-level repair that jointly optimizes connected components of pathological tokens:

python experiments/run_cluster_repair.py model=qwen2_5_coder_7b \
    cluster_repair.mutual_k=50 cluster_repair.min_cluster_size=2

Algorithm:

  1. Build mutual k-NN graph on high-severity tokens
  2. Find connected components (clusters)
  3. Jointly optimize all tokens in each cluster
  4. Centered covariance CAN change when multiple reference points shift together

Results on Qwen2.5-Coder-7B:

Metric Value Status
Clusters found 69 With mutual_k=50, min_size=2
Clusters improved 5/5 (100%) All clusters show improvement
Condition number reduction 0.427 ± 0.157 10-17% improvement
Jaccard overlap 0.836 ± 0.030 Excellent semantic preservation
Similarity to original 0.992 Minimal movement required

Key Finding: Cluster-level repair successfully improves geometry (condition number decreases) while preserving semantics, validating the hypothesis that pathological tokens need to move together.

Development

# Run tests
make test

# Run fast tests only
make test-fast

# Lint and format
make format
make lint

# Type check
make typecheck

# Install pre-commit hooks
make pre-commit

Experimental Results

Qwen2.5-Coder-7B Analysis

Full vocabulary census on 152,064 tokens (3,584 dimensions):

Metric Result
Tokens analyzed 152,064
Flagged tokens (poor geometry) 3,325 (2.19%)
Processing speed ~330 tokens/sec
Total census time 7.5 minutes

High-severity token types:

  • Nested punctuation: ))):\n, ))))\n\n
  • Mixed quotes/escapes: ...\', "For, (", …"
  • Special characters: (Chinese), (full-width)
  • Line ending variants: ),\r\n, ',\r\r\n

Single-Token Repair Validation

Criterion Status Value
Embeddings moved ✓ YES similarity = 0.98
Semantic preservation ✓ PASS Jaccard 0.75 - 1.0
Geometry improved ✗ NO cond/PR unchanged

Finding: Single-token repair preserves semantics but doesn't improve geometry when neighborhoods are uniformly pathological.

Cluster-Level Repair Validation ✓

Criterion Status Value
Clusters detected ✓ YES 69 clusters found
Geometry improved ✓ YES cond reduced 0.43 ± 0.16
Semantic preservation ✓ PASS Jaccard = 0.836
Improvement rate ✓ 100% 5/5 clusters improved

Causal Experiment Results

Claim Status Evidence
Geometry improves vs placebo ✓ Supported Treatment cond -0.27 vs control +0.04
Behavior improves causally ✗ Not yet DiD not significant (p=0.81), simulated outcomes

Supported Claim: "Cluster-level repair improves local geometry (cond) relative to placebo with minimal embedding movement."

Not Yet Supported: "Repair causally improves downstream behavior." Requires real stress tests with model inference, better matching, and larger samples.

Project Status

This is a research codebase under active development. Current status:

  • Core types and abstractions
  • Stage 1 & 2 metrics
  • USearch index integration
  • Single-token repair optimizer
  • Stress test framework
  • Statistical evaluation
  • W&B integration
  • Benchmark wrappers (HumanEval, GSM8k)
  • Stage 3 TDA metrics
  • Paper figures generation
  • Full census on Qwen2.5-Coder-7B
  • Single-token repair validation (negative result)
  • Cluster-level repair (positive result - geometry improves!)
  • Predictive validity analysis with confound controls
  • Causal experiment framework (mechanistic claim validated)
  • Causal behavioral evidence (needs real stress tests)
  • Multi-model comparison (Llama, Mistral)

Citation

If you use DCTT in your research, please cite:

@software{dctt2024,
  title = {DCTT: Discrete-to-Continuous Transition Testing for LLM Embedding Geometry},
  year = {2024},
  url = {https://github.com/MJ-Ref/DCTT}
}

License

MIT License - see LICENSE for details.

Acknowledgments

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •