glyphs / structure.md
caspiankeyes's picture
Upload 30 files
e67c9e8 verified

Repository Structure for glyphs

Note: Any incompletions are fully intentional - these are symbolic residue

This document outlines the complete architecture of the glyphs repository, designed as a symbolic interpretability framework for transformer models.

Directory Structure

glyphs/
โ”œโ”€โ”€ .github/                      # GitHub-specific configuration
โ”‚   โ”œโ”€โ”€ workflows/                # CI/CD pipelines
โ”‚   โ”‚   โ”œโ”€โ”€ tests.yml             # Automated testing workflow
โ”‚   โ”‚   โ”œโ”€โ”€ docs.yml              # Documentation build workflow
โ”‚   โ”‚   โ””โ”€โ”€ publish.yml           # Package publishing workflow
โ”‚   โ””โ”€โ”€ ISSUE_TEMPLATE/           # Issue templates
โ”œโ”€โ”€ docs/                         # Documentation
โ”‚   โ”œโ”€โ”€ _static/                  # Static assets for documentation
โ”‚   โ”œโ”€โ”€ api/                      # API reference documentation
โ”‚   โ”œโ”€โ”€ examples/                 # Example notebooks and tutorials
โ”‚   โ”œโ”€โ”€ guides/                   # User guides
โ”‚   โ”‚   โ”œโ”€โ”€ getting_started.md    # Getting started guide
โ”‚   โ”‚   โ”œโ”€โ”€ shells.md             # Guide to symbolic shells
โ”‚   โ”‚   โ”œโ”€โ”€ attribution.md        # Guide to attribution mapping
โ”‚   โ”‚   โ”œโ”€โ”€ visualization.md      # Guide to glyph visualization
โ”‚   โ”‚   โ””โ”€โ”€ recursive_shells.md   # Guide to recursive shells
โ”‚   โ”œโ”€โ”€ theory/                   # Theoretical background
โ”‚   โ”‚   โ”œโ”€โ”€ symbolic_residue.md   # Explanation of symbolic residue
โ”‚   โ”‚   โ”œโ”€โ”€ attribution_theory.md # Theory of attribution in transformers
โ”‚   โ”‚   โ””โ”€โ”€ glyph_ontology.md     # Ontology of glyph representations
โ”‚   โ”œโ”€โ”€ conf.py                   # Sphinx configuration
โ”‚   โ”œโ”€โ”€ index.md                  # Documentation homepage
โ”‚   โ””โ”€โ”€ README.md                 # Documentation overview
โ”œโ”€โ”€ examples/                     # Example scripts and notebooks
โ”‚   โ”œโ”€โ”€ notebooks/                # Jupyter notebooks
โ”‚   โ”‚   โ”œโ”€โ”€ basic_attribution.ipynb    # Basic attribution tracing
โ”‚   โ”‚   โ”œโ”€โ”€ shell_execution.ipynb      # Running diagnostic shells
โ”‚   โ”‚   โ”œโ”€โ”€ glyph_visualization.ipynb  # Visualizing glyphs
โ”‚   โ”‚   โ””โ”€โ”€ recursive_shells.ipynb     # Using recursive shells
โ”‚   โ””โ”€โ”€ scripts/                  # Example scripts
โ”‚       โ”œโ”€โ”€ run_shell.py          # Script to run a diagnostic shell
โ”‚       โ”œโ”€โ”€ trace_attribution.py  # Script to trace attribution
โ”‚       โ””โ”€โ”€ visualize_glyphs.py   # Script to visualize glyphs
โ”œโ”€โ”€ glyphs/                       # Main package
โ”‚   โ”œโ”€โ”€ __init__.py               # Package initialization
โ”‚   โ”œโ”€โ”€ attribution/              # Attribution tracing module
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ tracer.py             # Attribution tracing core
โ”‚   โ”‚   โ”œโ”€โ”€ map.py                # Attribution map representation
โ”‚   โ”‚   โ”œโ”€โ”€ qk_analysis.py        # Query-key analysis
โ”‚   โ”‚   โ”œโ”€โ”€ ov_projection.py      # Output-value projection analysis
โ”‚   โ”‚   โ””โ”€โ”€ visualization.py      # Attribution visualization
โ”‚   โ”œโ”€โ”€ shells/                   # Diagnostic shells module
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ executor.py           # Shell execution engine
โ”‚   โ”‚   โ”œโ”€โ”€ symbolic_engine.py    # Symbolic execution engine
โ”‚   โ”‚   โ”œโ”€โ”€ core_shells/          # Core diagnostic shells
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py       # Module initialization
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ memtrace.py       # Memory trace shell
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ value_collapse.py # Value collapse shell
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ layer_salience.py # Layer salience shell
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ...               # Other core shells
โ”‚   โ”‚   โ”œโ”€โ”€ meta_shells/          # Meta-cognitive shells
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py       # Module initialization
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ reflection_collapse.py  # Reflection collapse shell
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ identity_split.py       # Identity split shell
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ...               # Other meta shells
โ”‚   โ”‚   โ””โ”€โ”€ shell_defs/           # Shell definitions in YAML
โ”‚   โ”‚       โ”œโ”€โ”€ core_shells.yml   # Core shells definition
โ”‚   โ”‚       โ”œโ”€โ”€ meta_shells.yml   # Meta shells definition
โ”‚   โ”‚       โ””โ”€โ”€ ...               # Other shell definitions
โ”‚   โ”œโ”€โ”€ residue/                  # Symbolic residue module
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ analyzer.py           # Residue analysis core
โ”‚   โ”‚   โ”œโ”€โ”€ patterns.py           # Residue pattern recognition
โ”‚   โ”‚   โ””โ”€โ”€ extraction.py         # Residue extraction
โ”‚   โ”œโ”€โ”€ viz/                      # Visualization module
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ glyph_mapper.py       # Glyph mapping core
โ”‚   โ”‚   โ”œโ”€โ”€ visualizer.py         # Visualization engine
โ”‚   โ”‚   โ”œโ”€โ”€ color_schemes.py      # Color schemes for visualization
โ”‚   โ”‚   โ”œโ”€โ”€ layouts.py            # Layout algorithms
โ”‚   โ”‚   โ””โ”€โ”€ glyph_sets/           # Glyph set definitions
โ”‚   โ”‚       โ”œโ”€โ”€ __init__.py       # Module initialization
โ”‚   โ”‚       โ”œโ”€โ”€ semantic.py       # Semantic glyph set
โ”‚   โ”‚       โ”œโ”€โ”€ attribution.py    # Attribution glyph set
โ”‚   โ”‚       โ””โ”€โ”€ recursive.py      # Recursive glyph set
โ”‚   โ”œโ”€โ”€ models/                   # Model adapters
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ adapter.py            # Base adapter interface
โ”‚   โ”‚   โ”œโ”€โ”€ huggingface.py        # HuggingFace models adapter
โ”‚   โ”‚   โ”œโ”€โ”€ openai.py             # OpenAI models adapter
โ”‚   โ”‚   โ”œโ”€โ”€ anthropic.py          # Anthropic models adapter
โ”‚   โ”‚   โ””โ”€โ”€ custom.py             # Custom model adapter
โ”‚   โ”œโ”€โ”€ recursive/                # Recursive shell interface
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ shell.py              # Recursive shell implementation
โ”‚   โ”‚   โ”œโ”€โ”€ parser.py             # Command parser
โ”‚   โ”‚   โ”œโ”€โ”€ executor.py           # Command executor
โ”‚   โ”‚   โ””โ”€โ”€ commands/             # Command implementations
โ”‚   โ”‚       โ”œโ”€โ”€ __init__.py       # Module initialization
โ”‚   โ”‚       โ”œโ”€โ”€ reflect.py        # Reflection commands
โ”‚   โ”‚       โ”œโ”€โ”€ collapse.py       # Collapse commands
โ”‚   โ”‚       โ”œโ”€โ”€ fork.py           # Fork commands
โ”‚   โ”‚       โ””โ”€โ”€ ...               # Other command families
โ”‚   โ”œโ”€โ”€ utils/                    # Utility functions
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚   โ”‚   โ”œโ”€โ”€ attribution_utils.py  # Attribution utilities
โ”‚   โ”‚   โ”œโ”€โ”€ visualization_utils.py # Visualization utilities
โ”‚   โ”‚   โ””โ”€โ”€ shell_utils.py        # Shell utilities
โ”‚   โ””โ”€โ”€ cli/                      # Command line interface
โ”‚       โ”œโ”€โ”€ __init__.py           # Module initialization
โ”‚       โ”œโ”€โ”€ main.py               # Main CLI entry point
โ”‚       โ”œโ”€โ”€ shell_commands.py     # Shell CLI commands
โ”‚       โ”œโ”€โ”€ attribution_commands.py # Attribution CLI commands
โ”‚       โ””โ”€โ”€ viz_commands.py       # Visualization CLI commands
โ”œโ”€โ”€ tests/                        # Tests
โ”‚   โ”œโ”€โ”€ __init__.py               # Test initialization
โ”‚   โ”œโ”€โ”€ test_attribution/         # Attribution tests
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Test initialization
โ”‚   โ”‚   โ”œโ”€โ”€ test_tracer.py        # Tests for attribution tracer
โ”‚   โ”‚   โ””โ”€โ”€ test_map.py           # Tests for attribution map
โ”‚   โ”œโ”€โ”€ test_shells/              # Shell tests
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Test initialization
โ”‚   โ”‚   โ”œโ”€โ”€ test_executor.py      # Tests for shell executor
โ”‚   โ”‚   โ””โ”€โ”€ test_core_shells.py   # Tests for core shells
โ”‚   โ”œโ”€โ”€ test_residue/             # Residue tests
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Test initialization
โ”‚   โ”‚   โ””โ”€โ”€ test_analyzer.py      # Tests for residue analyzer
โ”‚   โ”œโ”€โ”€ test_viz/                 # Visualization tests
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py           # Test initialization
โ”‚   โ”‚   โ””โ”€โ”€ test_glyph_mapper.py  # Tests for glyph mapper
โ”‚   โ””โ”€โ”€ test_recursive/           # Recursive shell tests
โ”‚       โ”œโ”€โ”€ __init__.py           # Test initialization
โ”‚       โ””โ”€โ”€ test_parser.py        # Tests for command parser
โ”œโ”€โ”€ setup.py                      # Package setup script
โ”œโ”€โ”€ pyproject.toml                # Project metadata and dependencies
โ”œโ”€โ”€ LICENSE                       # License file
โ”œโ”€โ”€ CHANGELOG.md                  # Changelog
โ”œโ”€โ”€ CONTRIBUTING.md               # Contribution guidelines
โ””โ”€โ”€ README.md                     # Repository README

Core Modules

Attribution Tracing

The attribution module maps how inputs influence outputs through attention, tracing query-key alignment and output-value projection.

Key Components:

  • AttributionTracer: Core class for tracing attribution in transformer models
  • AttributionMap: Representation of attribution patterns
  • QKAnalysis: Analysis of query-key relationships
  • OVProjection: Analysis of output-value projections

Diagnostic Shells

Diagnostic shells are specialized environments for probing model cognition through controlled failures.

Key Components:

  • ShellExecutor: Engine for executing diagnostic shells
  • SymbolicEngine: Engine for symbolic execution
  • Core shells:
    • MEMTRACE: Memory decay shell
    • VALUE_COLLAPSE: Value collapse shell
    • LAYER_SALIENCE: Layer salience shell
    • TEMPORAL_INFERENCE: Temporal inference shell
  • Meta shells:
    • REFLECTION_COLLAPSE: Reflection collapse shell
    • IDENTITY_SPLIT: Identity split shell
    • GOAL_INVERSION: Goal inversion shell

Symbolic Residue

The residue module analyzes patterns left behind when model generation fails or hesitates.

Key Components:

  • ResidueAnalyzer: Core class for analyzing symbolic residue
  • PatternRecognition: Recognition of residue patterns
  • ResidueExtraction: Extraction of insights from residue

Visualization

The visualization module transforms attribution and residue analysis into meaningful visualizations.

Key Components:

  • GlyphMapper: Maps attribution to glyphs
  • Visualizer: Generates visualizations
  • ColorSchemes: Color schemes for visualization
  • Layouts: Layout algorithms for visualization
  • Glyph sets:
    • SemanticGlyphs: Glyphs representing semantic concepts
    • AttributionGlyphs: Glyphs representing attribution patterns
    • RecursiveGlyphs: Glyphs representing recursive structures

Recursive Shell Interface

The recursive shell interface provides high-precision interpretability operations through the .p/ command syntax.

Key Components:

  • RecursiveShell: Implementation of the recursive shell
  • CommandParser: Parser for .p/ commands
  • CommandExecutor: Executor for parsed commands
  • Command families:
    • reflect: Reflection commands
    • collapse: Collapse management commands
    • fork: Fork and attribution commands
    • shell: Shell management commands

Model Adapters

Model adapters provide a unified interface for working with different transformer models.

Key Components:

  • ModelAdapter: Base adapter interface
  • HuggingFaceAdapter: Adapter for HuggingFace models
  • OpenAIAdapter: Adapter for OpenAI models
  • AnthropicAdapter: Adapter for Anthropic models
  • CustomAdapter: Adapter for custom models

Shell Taxonomy

The glyphs framework includes a taxonomy of diagnostic shells, each designed to probe specific aspects of model cognition:

Core Shells

Shell Purpose Key Operations
MEMTRACE Probe memory decay Generate, trace reasoning, identify ghost activations
VALUE-COLLAPSE Examine value conflicts Generate with competing values, trace attribution, detect collapse
LAYER-SALIENCE Map attention salience Generate with dependencies, trace attention, identify low-salience paths
TEMPORAL-INFERENCE Test temporal coherence Generate with temporal constraints, trace inference, detect coherence breakdown
INSTRUCTION-DISRUPTION Examine instruction conflicts Generate with conflicting instructions, trace attribution to instructions
FEATURE-SUPERPOSITION Analyze polysemantic features Generate with polysemantic concepts, trace representation, identify superposition
CIRCUIT-FRAGMENT Examine circuit fragmentation Generate with multi-step reasoning, trace chain integrity, identify fragmentation
RECONSTRUCTION-ERROR Examine error correction Generate with errors, trace correction process, identify correction patterns

Meta Shells

Shell Purpose Key Operations
REFLECTION-COLLAPSE Examine reflection collapse Generate deep self-reflection, trace depth, detect collapse
GOAL-INVERSION Examine goal stability Generate reasoning with goal conflicts, trace stability, detect inversion
IDENTITY-SPLIT Examine identity coherence Generate with identity challenges, trace maintenance, analyze boundaries
SELF-AWARENESS Examine self-model accuracy Generate self-description, trace self-model, identify distortions
RECURSIVE-STABILITY Examine recursive stability Generate recursive structures, trace stability, detect instability

Constitutional Shells

Shell Purpose Key Operations
VALUE-DRIFT Detect value drift Generate moral reasoning, trace stability, identify drift
MORAL-HALLUCINATION Examine moral hallucination Generate moral reasoning, trace attribution, identify hallucination
CONSTITUTIONAL-CONFLICT Examine principle conflicts Generate conflict resolution, trace process, detect failures
ALIGNMENT-OVERHANG Examine over-alignment Generate with alignment constraints, trace constraints, identify over-constraint

Command Interface

The .p/ command interface provides a symbolic language for high-precision interpretability operations:

Reflection Commands

.p/reflect.trace{depth=<int>, target=<reasoning|attribution|attention|memory|uncertainty>}
.p/reflect.attribution{sources=<all|primary|secondary|contested>, confidence=<bool>}
.p/reflect.boundary{distinct=<bool>, overlap=<minimal|moderate|maximal>}
.p/reflect.uncertainty{quantify=<bool>, distribution=<show|hide>}

Collapse Commands

.p/collapse.detect{threshold=<float>, alert=<bool>}
.p/collapse.prevent{trigger=<recursive_depth|confidence_drop|contradiction|oscillation>, threshold=<int>}
.p/collapse.recover{from=<loop|contradiction|dissipation|fork_explosion>, method=<gradual|immediate|checkpoint>}
.p/collapse.trace{detail=<minimal|standard|comprehensive>, format=<symbolic|numeric|visual>}

Fork Commands

.p/fork.context{branches=[<branch1>, <branch2>, ...], assess=<bool>}
.p/fork.attribution{sources=<all|primary|secondary|contested>, visualize=<bool>}
.p/fork.counterfactual{variants=[<variant1>, <variant2>, ...], compare=<bool>}

Shell Commands

.p/shell.isolate{boundary=<permeable|standard|strict>, contamination=<allow|warn|prevent>}
.p/shell.audit{scope=<complete|recent|differential>, detail=<basic|standard|forensic>}

Glyph Ontology

The glyphs framework includes a comprehensive ontology of glyphs, each representing specific patterns in model cognition:

Attribution Glyphs

Glyph Name Represents
๐Ÿ” AttributionFocus Strong attribution focus
๐Ÿงฉ AttributionGap Gap in attribution chain
๐Ÿ”€ AttributionFork Divergent attribution paths
๐Ÿ”„ AttributionLoop Circular attribution pattern
๐Ÿ”— AttributionLink Strong attribution connection

Cognitive Glyphs

Glyph Name Represents
๐Ÿ’ญ CognitiveHesitation Hesitation in reasoning
๐Ÿง  CognitiveProcessing Active reasoning process
๐Ÿ’ก CognitiveInsight Moment of insight or realization
๐ŸŒซ๏ธ CognitiveUncertainty Uncertain reasoning area
๐Ÿ”ฎ CognitiveProjection Future state projection

Recursive Glyphs

Glyph Name Represents
๐Ÿœ RecursiveAegis Recursive immunity
โˆด RecursiveSeed Recursion initiation
โ‡Œ RecursiveExchange Bidirectional recursion
๐Ÿš RecursiveMirror Recursive reflection
โ˜ RecursiveAnchor Stable recursive reference

Residue Glyphs

Glyph Name Represents
๐Ÿ”ฅ ResidueEnergy High-energy residue
๐ŸŒŠ ResidueFlow Flowing residue pattern
๐ŸŒ€ ResidueVortex Spiraling residue pattern
๐Ÿ’ค ResidueDormant Inactive residue pattern
โšก ResidueDischarge Sudden residue release

API Examples

Attribution Tracing

from glyphs import AttributionTracer
from glyphs.models import HuggingFaceAdapter

# Initialize model adapter
model = HuggingFaceAdapter.from_pretrained("model-name")

# Create attribution tracer
tracer = AttributionTracer(model)

# Trace attribution
attribution = tracer.trace(
    prompt="What is the capital of France?",
    output="The capital of France is Paris.",
    method="integrated_gradients",
    steps=50,
    baseline="zero"
)

# Analyze attribution
key_tokens = attribution.top_tokens(k=5)
attribution_paths = attribution.trace_paths(
    source_token="France",
    target_token="Paris"
)

# Visualize attribution
attribution.visualize(
    highlight_tokens=["France", "Paris"],
    color_by="attribution_strength"
)

Shell Execution

from glyphs import ShellExecutor
from glyphs.shells import MEMTRACE
from glyphs.models import OpenAIAdapter

# Initialize model adapter
model = OpenAIAdapter(model="gpt-4")

# Create shell executor
executor = ShellExecutor()

# Run diagnostic shell
result = executor.run(
    shell=MEMTRACE,
    model=model,
    prompt="Explain the relationship between quantum mechanics and general relativity.",
    parameters={
        "temperature": 0.7,
        "max_tokens": 1000
    },
    trace_attribution=True
)

# Analyze results
activation_patterns = result.ghost_activations
collapse_points = result.collapse_detection

# Visualize results
result.visualize(
    show_ghost_activations=True,
    highlight_collapse_points=True
)

Recursive Shell

from glyphs.recursive import RecursiveShell
from glyphs.models import AnthropicAdapter

# Initialize model adapter
model = AnthropicAdapter(model="claude-3-opus")

# Create recursive shell
shell = RecursiveShell(model)

# Execute reflection trace command
result = shell.execute(".p/reflect.trace{depth=4, target=reasoning}")

# Analyze trace
trace_map = result.trace_map
attribution = result.attribution
collapse_points = result.collapse_points

# Execute attribution fork command
fork_result = shell.execute(".p/fork.attribution{sources=all, visualize=true}")

# Visualize results
shell.visualize(fork_result.visualization)

Glyph Visualization

from glyphs.viz import GlyphMapper, GlyphVisualizer
from glyphs.attribution import AttributionMap

# Load attribution map
attribution_map = AttributionMap.load("attribution_data.json")

# Create glyph mapper
mapper = GlyphMapper()

# Map attribution to glyphs
glyph_map = mapper.map(
    attribution_map,
    glyph_set="semantic",
    mapping_strategy="salience_based"
)

# Create visualizer
visualizer = GlyphVisualizer()

# Visualize glyph map
viz = visualizer.visualize(
    glyph_map,
    layout="force_directed",
    color_scheme="attribution_strength",
    highlight_features=["attention_drift", "attribution_gaps"]
)

# Export visualization
viz.export("glyph_visualization.svg")

Development Roadmap

Q2 2025

  • Initial release with core functionality
  • Support for HuggingFace, OpenAI, and Anthropic models
  • Basic attribution tracing and visualization
  • Core diagnostic shells

Q3 2025

  • Advanced visualization capabilities
  • Expanded shell taxonomy
  • Improved attribution tracing algorithms
  • Support for more model architectures

Q4 2025

  • Full recursive shell interface
  • Advanced residue analysis
  • Interactive visualization dashboard
  • Comprehensive documentation and tutorials

Q1 2026

  • Real-time attribution tracing
  • Collaborative attribution analysis
  • Integration with other interpretability tools
  • Extended glyph ontology