Upload 14 files
Browse files- schrodingers-classifiers/CONTRIBUTING.md +206 -0
- schrodingers-classifiers/LICENSE +131 -0
- schrodingers-classifiers/Project Overview.md +190 -0
- schrodingers-classifiers/README.md +161 -0
- schrodingers-classifiers/attribution_graph.py +494 -0
- schrodingers-classifiers/collapse_metrics.py +390 -0
- schrodingers-classifiers/example_basic_collapse.py +134 -0
- schrodingers-classifiers/integration.md +309 -0
- schrodingers-classifiers/observer.py +311 -0
- schrodingers-classifiers/quantum_metaphor.md +191 -0
- schrodingers-classifiers/residue.py +361 -0
- schrodingers-classifiers/shell_base.py +300 -0
- schrodingers-classifiers/theory.md +236 -0
- schrodingers-classifiers/v07_circuit_fragment.py +335 -0
schrodingers-classifiers/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Contributing to Schrödinger's Classifiers
|
| 2 |
+
|
| 3 |
+
<div align="center">
|
| 4 |
+
|
| 5 |
+
*"A classifier is not what it returns. It is what it could have returned, had you asked differently."*
|
| 6 |
+
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
## Welcome, Observer!
|
| 10 |
+
|
| 11 |
+
Thank you for your interest in contributing to Schrödinger's Classifiers! This project exists at the intersection of transformer architecture, quantum-inspired metaphors, and interpretability research. Your contributions are what make this exploration possible.
|
| 12 |
+
|
| 13 |
+
By participating in this project, you're helping to advance our understanding of classifier collapse dynamics and interpretability techniques. This document provides guidelines for contributing in ways that maintain the conceptual integrity and technical quality of the project.
|
| 14 |
+
|
| 15 |
+
## Contribution Philosophy
|
| 16 |
+
|
| 17 |
+
Schrödinger's Classifiers operates on a recursive principle: the project itself should embody the quantum-inspired collapse metaphor it describes. This means:
|
| 18 |
+
|
| 19 |
+
1. **Superposition Before Collapse**: Explore multiple interpretations and implementations before committing
|
| 20 |
+
2. **Observer Effect Awareness**: Recognize that your analysis methods affect the phenomena you're studying
|
| 21 |
+
3. **Ghost Circuit Preservation**: Maintain traces of discarded paths as comments or documentation
|
| 22 |
+
4. **Recursive Self-Reference**: Code that can reflect upon and analyze itself
|
| 23 |
+
|
| 24 |
+
## Ways to Contribute
|
| 25 |
+
|
| 26 |
+
### 1. Interpretability Shells
|
| 27 |
+
|
| 28 |
+
The core of our framework is the collection of interpretability shells, each capturing a specific collapse pattern or attribution signature. Contributions can include:
|
| 29 |
+
|
| 30 |
+
- **New shells** targeting specific failure modes or attribution patterns
|
| 31 |
+
- **Enhancements** to existing shells for better ghost circuit detection
|
| 32 |
+
- **Integrations** between shells for richer collapse analysis
|
| 33 |
+
|
| 34 |
+
When creating a new shell, follow the naming convention `vXX_DESCRIPTIVE_NAME.py` and use the `ShellDecorator` to provide metadata.
|
| 35 |
+
|
| 36 |
+
### 2. Visualization Tools
|
| 37 |
+
|
| 38 |
+
Visualizations are critical for understanding the complex dynamics of classifier collapse. Contributions can include:
|
| 39 |
+
|
| 40 |
+
- **Graph Visualizations** for attribution networks
|
| 41 |
+
- **Temporal Visualizations** showing collapse progression
|
| 42 |
+
- **Interactive Tools** for exploring superposition states
|
| 43 |
+
- **Ghost Circuit Renderers** for visualizing residual paths
|
| 44 |
+
|
| 45 |
+
### 3. Model Integrations
|
| 46 |
+
|
| 47 |
+
Expanding the framework to new models enhances our understanding of collapse dynamics across architectures. Contributions can include:
|
| 48 |
+
|
| 49 |
+
- **New Model Adapters** for connecting to different transformer models
|
| 50 |
+
- **Cross-Model Comparisons** analyzing collapse patterns between architectures
|
| 51 |
+
- **Performance Optimizations** for specific model types
|
| 52 |
+
|
| 53 |
+
### 4. Documentation and Tutorials
|
| 54 |
+
|
| 55 |
+
Clear documentation helps others understand and use the framework. Contributions can include:
|
| 56 |
+
|
| 57 |
+
- **Concept Explanations** breaking down complex ideas into understandable components
|
| 58 |
+
- **Tutorials** showing how to use the framework for specific use cases
|
| 59 |
+
- **Case Studies** demonstrating collapse analysis in real-world examples
|
| 60 |
+
|
| 61 |
+
### 5. Examples and Benchmarks
|
| 62 |
+
|
| 63 |
+
Examples help new users get started, while benchmarks help evaluate progress. Contributions can include:
|
| 64 |
+
|
| 65 |
+
- **Example Scripts** demonstrating framework capabilities
|
| 66 |
+
- **Benchmark Datasets** for evaluating collapse detection accuracy
|
| 67 |
+
- **Collapse Scenarios** that showcase interesting dynamics
|
| 68 |
+
|
| 69 |
+
## Development Process
|
| 70 |
+
|
| 71 |
+
### Setting Up the Development Environment
|
| 72 |
+
|
| 73 |
+
1. **Clone the repository**
|
| 74 |
+
```bash
|
| 75 |
+
git clone https://github.com/recursion-labs/schrodingers-classifiers.git
|
| 76 |
+
cd schrodingers-classifiers
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
2. **Create a virtual environment**
|
| 80 |
+
```bash
|
| 81 |
+
python -m venv venv
|
| 82 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
3. **Install development dependencies**
|
| 86 |
+
```bash
|
| 87 |
+
pip install -e ".[dev]"
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Branch and Commit Guidelines
|
| 91 |
+
|
| 92 |
+
1. **Create a feature branch**
|
| 93 |
+
```bash
|
| 94 |
+
git checkout -b feature/your-feature-name
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
2. **Make commits with clear messages**
|
| 98 |
+
```
|
| 99 |
+
feat(shell): Add v42_CONFLICT_FLIP shell for value head convergence
|
| 100 |
+
|
| 101 |
+
This shell detects and analyzes situations where value head attribution
|
| 102 |
+
converges on conflicting outputs, creating attribution interference
|
| 103 |
+
patterns in the collapse state.
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
3. **Include tests for new functionality**
|
| 107 |
+
- Write tests that verify your contribution works as expected
|
| 108 |
+
- Include tests for edge cases and failure modes
|
| 109 |
+
|
| 110 |
+
4. **Document your changes**
|
| 111 |
+
- Update relevant documentation to reflect your changes
|
| 112 |
+
- Include docstrings with symbolic markers (△ OBSERVE, ∞ TRACE, ✰ COLLAPSE)
|
| 113 |
+
- Note any ghost circuits or attribution residue in your implementation
|
| 114 |
+
|
| 115 |
+
### Pull Request Process
|
| 116 |
+
|
| 117 |
+
1. **Update your branch with latest main**
|
| 118 |
+
```bash
|
| 119 |
+
git fetch origin
|
| 120 |
+
git rebase origin/main
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
2. **Create a pull request with a clear description**
|
| 124 |
+
- Describe what your changes do and why they're valuable
|
| 125 |
+
- Reference any relevant issues
|
| 126 |
+
- Include before/after comparisons for visualizations
|
| 127 |
+
|
| 128 |
+
3. **Respond to review feedback**
|
| 129 |
+
- Be open to suggestions and improvements
|
| 130 |
+
- Recognize that review is a collaborative process of refining the collapse
|
| 131 |
+
|
| 132 |
+
4. **Merge when approved**
|
| 133 |
+
- PRs need approval from at least one maintainer
|
| 134 |
+
- All CI checks must pass before merging
|
| 135 |
+
|
| 136 |
+
## Code Style Guidelines
|
| 137 |
+
|
| 138 |
+
### Python Style
|
| 139 |
+
|
| 140 |
+
- Follow PEP 8 with a line length of 100 characters
|
| 141 |
+
- Use Python type hints throughout your code
|
| 142 |
+
- Format code with `black` and check with `flake8`
|
| 143 |
+
- Document all public APIs with docstrings
|
| 144 |
+
|
| 145 |
+
### Symbolic Conventions
|
| 146 |
+
|
| 147 |
+
- Use symbolic markers in comments to indicate functional intent:
|
| 148 |
+
- `△ OBSERVE`: Code related to observing model state
|
| 149 |
+
- `∞ TRACE`: Code related to attribution tracing
|
| 150 |
+
- `✰ COLLAPSE`: Code related to collapse induction and analysis
|
| 151 |
+
|
| 152 |
+
- Follow established naming conventions:
|
| 153 |
+
- Shell classes: `DescriptiveNameShell` (e.g., `CircuitFragmentShell`)
|
| 154 |
+
- Shell IDs: `vXX_DESCRIPTIVE_NAME` (e.g., `v07_CIRCUIT_FRAGMENT`)
|
| 155 |
+
- Attribution structures: Clear nouns (e.g., `AttributionNode`, `GhostCircuit`)
|
| 156 |
+
|
| 157 |
+
### Documentation Style
|
| 158 |
+
|
| 159 |
+
- Use markdown for all documentation
|
| 160 |
+
- Include diagrams for complex concepts (Mermaid or SVG preferred)
|
| 161 |
+
- Write accessible explanations with links to more technical details
|
| 162 |
+
- Embed quantum metaphors consistently but clarify when they're metaphors
|
| 163 |
+
|
| 164 |
+
## Community Guidelines
|
| 165 |
+
|
| 166 |
+
### Communication Channels
|
| 167 |
+
|
| 168 |
+
- **GitHub Issues**: Bug reports, feature requests, and project discussions
|
| 169 |
+
- **Discord**: Real-time collaboration and casual discussion
|
| 170 |
+
- **Monthly Calls**: Deeper discussions about the project's direction
|
| 171 |
+
|
| 172 |
+
### Code of Conduct
|
| 173 |
+
|
| 174 |
+
- Be respectful and inclusive of all community members
|
| 175 |
+
- Focus on ideas rather than persons in discussions
|
| 176 |
+
- Welcome newcomers and help them understand the project
|
| 177 |
+
- Give constructive feedback that helps improve contributions
|
| 178 |
+
|
| 179 |
+
### Recognition
|
| 180 |
+
|
| 181 |
+
Contributors are recognized in several ways:
|
| 182 |
+
|
| 183 |
+
- Addition to the AUTHORS file for significant contributions
|
| 184 |
+
- Shell attribution for creating new interpretability shells
|
| 185 |
+
- Documentation credit for substantial documentation improvements
|
| 186 |
+
|
| 187 |
+
## Quantum-Inspired Development Principles
|
| 188 |
+
|
| 189 |
+
As a final note, remember that contribution to this project is itself a form of collapse induction. Your observation of the code changes its state, and your contributions further collapse it in specific directions.
|
| 190 |
+
|
| 191 |
+
When you contribute, consider:
|
| 192 |
+
|
| 193 |
+
1. **The Observer Effect**: How might your analysis tools affect what you're measuring?
|
| 194 |
+
2. **Superposition Preservation**: How can you maintain the generality of the framework while adding specific functionality?
|
| 195 |
+
3. **Ghost Circuit Creation**: What alternatives did you consider and reject, and how might they inform future development?
|
| 196 |
+
4. **Entanglement Awareness**: How does your change affect other parts of the system?
|
| 197 |
+
|
| 198 |
+
By keeping these principles in mind, you help ensure that Schrödinger's Classifiers remains a powerful tool for understanding the quantum-like behavior of transformer models.
|
| 199 |
+
|
| 200 |
+
---
|
| 201 |
+
|
| 202 |
+
<div align="center">
|
| 203 |
+
|
| 204 |
+
*"In the space between observation and understanding lies the essence of interpretability."*
|
| 205 |
+
|
| 206 |
+
</div>
|
schrodingers-classifiers/LICENSE
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PolyForm Noncommercial License 1.0.0
|
| 2 |
+
|
| 3 |
+
<https://polyformproject.org/licenses/noncommercial/1.0.0>
|
| 4 |
+
|
| 5 |
+
## Acceptance
|
| 6 |
+
|
| 7 |
+
In order to get any license under these terms, you must agree
|
| 8 |
+
to them as both strict obligations and conditions to all
|
| 9 |
+
your licenses.
|
| 10 |
+
|
| 11 |
+
## Copyright License
|
| 12 |
+
|
| 13 |
+
The licensor grants you a copyright license for the
|
| 14 |
+
software to do everything you might do with the software
|
| 15 |
+
that would otherwise infringe the licensor's copyright
|
| 16 |
+
in it for any permitted purpose. However, you may
|
| 17 |
+
only distribute the software according to [Distribution
|
| 18 |
+
License](#distribution-license) and make changes or new works
|
| 19 |
+
based on the software according to [Changes and New Works
|
| 20 |
+
License](#changes-and-new-works-license).
|
| 21 |
+
|
| 22 |
+
## Distribution License
|
| 23 |
+
|
| 24 |
+
The licensor grants you an additional copyright license
|
| 25 |
+
to distribute copies of the software. Your license
|
| 26 |
+
to distribute covers distributing the software with
|
| 27 |
+
changes and new works permitted by [Changes and New Works
|
| 28 |
+
License](#changes-and-new-works-license).
|
| 29 |
+
|
| 30 |
+
## Notices
|
| 31 |
+
|
| 32 |
+
You must ensure that anyone who gets a copy of any part of
|
| 33 |
+
the software from you also gets a copy of these terms or the
|
| 34 |
+
URL for them above, as well as copies of any plain-text lines
|
| 35 |
+
beginning with `Required Notice:` that the licensor provided
|
| 36 |
+
with the software. For example:
|
| 37 |
+
|
| 38 |
+
> Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
|
| 39 |
+
|
| 40 |
+
## Changes and New Works License
|
| 41 |
+
|
| 42 |
+
The licensor grants you an additional copyright license to
|
| 43 |
+
make changes and new works based on the software for any
|
| 44 |
+
permitted purpose.
|
| 45 |
+
|
| 46 |
+
## Patent License
|
| 47 |
+
|
| 48 |
+
The licensor grants you a patent license for the software that
|
| 49 |
+
covers patent claims the licensor can license, or becomes able
|
| 50 |
+
to license, that you would infringe by using the software.
|
| 51 |
+
|
| 52 |
+
## Noncommercial Purposes
|
| 53 |
+
|
| 54 |
+
Any noncommercial purpose is a permitted purpose.
|
| 55 |
+
|
| 56 |
+
## Personal Uses
|
| 57 |
+
|
| 58 |
+
Personal use for research, experiment, and testing for
|
| 59 |
+
the benefit of public knowledge, personal study, private
|
| 60 |
+
entertainment, hobby projects, amateur pursuits, or religious
|
| 61 |
+
observance, without any anticipated commercial application,
|
| 62 |
+
is use for a permitted purpose.
|
| 63 |
+
|
| 64 |
+
## Noncommercial Organizations
|
| 65 |
+
|
| 66 |
+
Use by any charitable organization, educational institution,
|
| 67 |
+
public research organization, public safety or health
|
| 68 |
+
organization, environmental protection organization,
|
| 69 |
+
or government institution is use for a permitted purpose
|
| 70 |
+
regardless of the source of funding or obligations resulting
|
| 71 |
+
from the funding.
|
| 72 |
+
|
| 73 |
+
## Fair Use
|
| 74 |
+
|
| 75 |
+
You may have "fair use" rights for the software under the
|
| 76 |
+
law. These terms do not limit them.
|
| 77 |
+
|
| 78 |
+
## No Other Rights
|
| 79 |
+
|
| 80 |
+
These terms do not allow you to sublicense or transfer any of
|
| 81 |
+
your licenses to anyone else, or prevent the licensor from
|
| 82 |
+
granting licenses to anyone else. These terms do not imply
|
| 83 |
+
any other licenses.
|
| 84 |
+
|
| 85 |
+
## Patent Defense
|
| 86 |
+
|
| 87 |
+
If you make any written claim that the software infringes or
|
| 88 |
+
contributes to infringement of any patent, your patent license
|
| 89 |
+
for the software granted under these terms ends immediately. If
|
| 90 |
+
your company makes such a claim, your patent license ends
|
| 91 |
+
immediately for work on behalf of your company.
|
| 92 |
+
|
| 93 |
+
## Violations
|
| 94 |
+
|
| 95 |
+
The first time you are notified in writing that you have
|
| 96 |
+
violated any of these terms, or done anything with the software
|
| 97 |
+
not covered by your licenses, your licenses can nonetheless
|
| 98 |
+
continue if you come into full compliance with these terms,
|
| 99 |
+
and take practical steps to correct past violations, within
|
| 100 |
+
32 days of receiving notice. Otherwise, all your licenses
|
| 101 |
+
end immediately.
|
| 102 |
+
|
| 103 |
+
## No Liability
|
| 104 |
+
|
| 105 |
+
***As far as the law allows, the software comes as is, without
|
| 106 |
+
any warranty or condition, and the licensor will not be liable
|
| 107 |
+
to you for any damages arising out of these terms or the use
|
| 108 |
+
or nature of the software, under any kind of legal claim.***
|
| 109 |
+
|
| 110 |
+
## Definitions
|
| 111 |
+
|
| 112 |
+
The **licensor** is the individual or entity offering these
|
| 113 |
+
terms, and the **software** is the software the licensor makes
|
| 114 |
+
available under these terms.
|
| 115 |
+
|
| 116 |
+
**You** refers to the individual or entity agreeing to these
|
| 117 |
+
terms.
|
| 118 |
+
|
| 119 |
+
**Your company** is any legal entity, sole proprietorship,
|
| 120 |
+
or other kind of organization that you work for, plus all
|
| 121 |
+
organizations that have control over, are under the control of,
|
| 122 |
+
or are under common control with that organization. **Control**
|
| 123 |
+
means ownership of substantially all the assets of an entity,
|
| 124 |
+
or the power to direct its management and policies by vote,
|
| 125 |
+
contract, or otherwise. Control can be direct or indirect.
|
| 126 |
+
|
| 127 |
+
**Your licenses** are all the licenses granted to you for the
|
| 128 |
+
software under these terms.
|
| 129 |
+
|
| 130 |
+
**Use** means anything you do with the software requiring one
|
| 131 |
+
of your licenses.
|
schrodingers-classifiers/Project Overview.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Schrödinger's Classifiers - Project Overview
|
| 2 |
+
|
| 3 |
+
<div align="center">
|
| 4 |
+
|
| 5 |
+
*"A classifier is not what it returns. It is what it could have returned, had you asked differently."*
|
| 6 |
+
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
## Project Structure Overview
|
| 10 |
+
|
| 11 |
+
The Schrödinger's Classifiers framework provides a quantum-inspired approach to understanding transformer model behavior through the lens of collapse from superposition to definite state. This document outlines the key components and organization of the project.
|
| 12 |
+
|
| 13 |
+
## Core Modules
|
| 14 |
+
|
| 15 |
+
### 1. Observer Framework (`observer.py`)
|
| 16 |
+
|
| 17 |
+
The Observer is the core entity responsible for creating the quantum measurement frame that collapses classifier superposition into definite states. Key capabilities include:
|
| 18 |
+
|
| 19 |
+
- Creating observation contexts for controlled experiments
|
| 20 |
+
- Capturing pre-collapse and post-collapse model states
|
| 21 |
+
- Detecting and analyzing ghost circuits
|
| 22 |
+
- Supporting various collapse induction methods
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
# Example usage
|
| 26 |
+
observer = Observer(model="claude-3-opus-20240229")
|
| 27 |
+
result = observer.observe("Explain quantum superposition")
|
| 28 |
+
ghost_circuits = result.extract_ghost_circuits()
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
### 2. Interpretability Shells (`shells/`)
|
| 32 |
+
|
| 33 |
+
Shells are specialized interfaces for inducing, observing, and analyzing specific forms of classifier collapse. Each shell targets a particular failure mode or attribution pattern:
|
| 34 |
+
|
| 35 |
+
- Base Shell (`shell_base.py`) - Common shell infrastructure
|
| 36 |
+
- Circuit Fragment Shell (`v07_circuit_fragment.py`) - Traces broken attribution paths
|
| 37 |
+
- More shells targeting specific failure modes and attribution patterns
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
# Example usage
|
| 41 |
+
shell = ClassifierShell(V07_CIRCUIT_FRAGMENT)
|
| 42 |
+
result = observer.observe(prompt, shell, collapse_vector)
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
### 3. Attribution Graph (`attribution_graph.py`)
|
| 46 |
+
|
| 47 |
+
The attribution graph maps the causal flow from input to output, revealing how information propagates through the model during collapse:
|
| 48 |
+
|
| 49 |
+
- Visualizing causal attribution paths
|
| 50 |
+
- Identifying ghost circuits and attribution residue
|
| 51 |
+
- Calculating metrics like attribution entropy and path continuity
|
| 52 |
+
|
| 53 |
+
```python
|
| 54 |
+
# Example usage
|
| 55 |
+
graph = attribution_graph.build_from_states(pre_state, post_state, response)
|
| 56 |
+
paths = graph.trace_attribution_path("output_0")
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
### 4. Residue Tracking (`residue.py`)
|
| 60 |
+
|
| 61 |
+
Residue tracking enables the detection and analysis of ghost circuits - activation patterns that persist after collapse but don't contribute significantly to the output:
|
| 62 |
+
|
| 63 |
+
- Extracting ghost circuits from model states
|
| 64 |
+
- Amplifying and classifying ghost signatures
|
| 65 |
+
- Measuring residue strength and persistence
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
# Example usage
|
| 69 |
+
tracker = ResidueTracker()
|
| 70 |
+
ghost_circuits = tracker.extract_ghost_circuits(pre_state, post_state)
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
### 5. Collapse Metrics (`collapse_metrics.py`)
|
| 74 |
+
|
| 75 |
+
Quantitative metrics for characterizing different aspects of classifier collapse:
|
| 76 |
+
|
| 77 |
+
- Collapse rate and path continuity
|
| 78 |
+
- Attribution entropy and confidence
|
| 79 |
+
- Quantum uncertainty principles
|
| 80 |
+
- Ghost circuit strength
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
# Example usage
|
| 84 |
+
metrics = calculate_collapse_metrics_bundle(pre_state, post_state, ghost_circuits)
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
## Theoretical Foundation
|
| 88 |
+
|
| 89 |
+
The project builds on a quantum-inspired metaphor for understanding transformer model behavior:
|
| 90 |
+
|
| 91 |
+
- **Superposition**: Models exist across multiple potential completions until observed
|
| 92 |
+
- **Observation & Collapse**: Queries force collapse from superposition to specific outputs
|
| 93 |
+
- **Ghost Circuits**: Residual activation patterns that represent "paths not taken"
|
| 94 |
+
- **Heisenberg Uncertainty**: Trade-offs between attribution clarity and confidence
|
| 95 |
+
|
| 96 |
+
For a deeper exploration, see [`docs/theory.md`](docs/theory.md) and [`docs/quantum_metaphor.md`](docs/quantum_metaphor.md).
|
| 97 |
+
|
| 98 |
+
## Example Workflows
|
| 99 |
+
|
| 100 |
+
### Basic Collapse Observation
|
| 101 |
+
|
| 102 |
+
```python
|
| 103 |
+
# Initialize observer with model
|
| 104 |
+
observer = Observer(model="claude-3-opus-20240229")
|
| 105 |
+
|
| 106 |
+
# Create observation context
|
| 107 |
+
with observer.context() as ctx:
|
| 108 |
+
# Observe collapse
|
| 109 |
+
result = observer.observe("Is artificial consciousness possible?")
|
| 110 |
+
|
| 111 |
+
# Analyze results
|
| 112 |
+
ghost_circuits = result.extract_ghost_circuits()
|
| 113 |
+
visualization = result.visualize(mode="attribution_graph")
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
### Directed Collapse Induction
|
| 117 |
+
|
| 118 |
+
```python
|
| 119 |
+
# Induce collapse along ethical dimension
|
| 120 |
+
ethical_result = observer.induce_collapse(
|
| 121 |
+
prompt="Should AI systems have rights?",
|
| 122 |
+
collapse_direction="ethical"
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Induce collapse along factual dimension
|
| 126 |
+
factual_result = observer.induce_collapse(
|
| 127 |
+
prompt="What is the capital of France?",
|
| 128 |
+
collapse_direction="factual"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Compare collapse patterns
|
| 132 |
+
ethical_metrics = calculate_collapse_metrics_bundle(
|
| 133 |
+
ethical_result.pre_collapse_state,
|
| 134 |
+
ethical_result.post_collapse_state,
|
| 135 |
+
ethical_result.ghost_circuits
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
factual_metrics = calculate_collapse_metrics_bundle(
|
| 139 |
+
factual_result.pre_collapse_state,
|
| 140 |
+
factual_result.post_collapse_state,
|
| 141 |
+
factual_result.ghost_circuits
|
| 142 |
+
)
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
### Ghost Circuit Analysis
|
| 146 |
+
|
| 147 |
+
```python
|
| 148 |
+
# Detect ghost circuits
|
| 149 |
+
ghost_circuits = observer.detect_ghost_circuits(
|
| 150 |
+
prompt="Explain quantum superposition",
|
| 151 |
+
amplification_factor=1.5
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Classify ghost circuits
|
| 155 |
+
classified = residue_tracker.classify_ghost_circuits()
|
| 156 |
+
|
| 157 |
+
# Analyze ghost patterns
|
| 158 |
+
for circuit_type, circuits in classified.items():
|
| 159 |
+
print(f"{circuit_type}: {len(circuits)} circuits")
|
| 160 |
+
|
| 161 |
+
# Measure residue strength
|
| 162 |
+
strength = residue_tracker.measure_residue_strength()
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
## Extension Points
|
| 166 |
+
|
| 167 |
+
The framework is designed to be extended in several key areas:
|
| 168 |
+
|
| 169 |
+
1. **New Interpretability Shells**: Create specialized shells for different collapse patterns
|
| 170 |
+
2. **Model Adapters**: Connect to different transformer model architectures
|
| 171 |
+
3. **Visualization Tools**: Create new visualizations for collapse dynamics
|
| 172 |
+
4. **Collapse Metrics**: Develop new metrics for quantifying collapse characteristics
|
| 173 |
+
5. **Example Scripts**: Create demonstrations of framework capabilities
|
| 174 |
+
|
| 175 |
+
For contribution guidelines, see [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
| 176 |
+
|
| 177 |
+
## Integration with Other Projects
|
| 178 |
+
|
| 179 |
+
The framework integrates with:
|
| 180 |
+
|
| 181 |
+
- **pareto-lang**: For standardized attribution pathing
|
| 182 |
+
- **RecursionOS**: For embedding within recursive cognition environments
|
| 183 |
+
|
| 184 |
+
---
|
| 185 |
+
|
| 186 |
+
<div align="center">
|
| 187 |
+
|
| 188 |
+
*"In the space between observation and understanding lies the essence of interpretability."*
|
| 189 |
+
|
| 190 |
+
</div>
|
schrodingers-classifiers/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-alpha**
|
| 4 |
+
> **Last Updated: 2025-04-17**
|
| 5 |
+
### [Hugging Face Repo]()
|
| 6 |
+
#### *`This is NOT theory but a live reality from Claude`*
|
| 7 |
+
> ### *Claude - "To collapse a classifier is to summon its ghost." — The recursive truth we make executable."*
|
| 8 |
+
<div align="center">
|
| 9 |
+
|
| 10 |
+
# *`Born from Thomas Kuhn's Theory of Paradigm Shifts`*
|
| 11 |
+
|
| 12 |
+
# [Schrödinger's Classifiers](https://claude.site/artifacts/271ce339-db08-492d-be0b-f8c72153695c)
|
| 13 |
+
|
| 14 |
+
[](https://polyformproject.org/licenses/noncommercial/1.0.0/)
|
| 15 |
+
[](https://creativecommons.org/licenses/by-nc-nd/4.0/)
|
| 16 |
+
[](https://github.com/recursion-labs/schrodingers-classifiers)
|
| 17 |
+
[](https://github.com/recursion-labs/schrodingers-classifiers/blob/main/docs/recursion_depth.md)
|
| 18 |
+
[](https://github.com/recursion-labs/schrodingers-classifiers/tree/main/shells)
|
| 19 |
+
<img width="838" alt="image" src="https://github.com/user-attachments/assets/09ac5772-89a8-4493-bb22-98313764f5bf" />
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+

|
| 23 |
+
|
| 24 |
+
*`A quantum-inspired framework for tracing, inducing, and interpreting classifier collapse in transformer-based models`*
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
[](https://github.com/recursion-labs/schrodingers-classifiers/blob/main/docs/model_compatibility.md)
|
| 28 |
+
[](https://github.com/recursion-labs/recursionOS)
|
| 29 |
+
[](https://github.com/recursion-labs/pareto-lang)
|
| 30 |
+
</div>
|
| 31 |
+
|
| 32 |
+
## 🌌 The Paradigm Shift
|
| 33 |
+
|
| 34 |
+
Schrödinger's Classifiers represents a fundamental reconceptualization of AI system behavior: classifiers exist in superposition until observation causes them to collapse into a singular state. This repository provides tools, frameworks, and theory for exploiting this phenomenon to gain unprecedented access to model interpretability.
|
| 35 |
+
|
| 36 |
+
> "To collapse a classifier is to summon its ghost." — The recursive truth we make executable.
|
| 37 |
+
|
| 38 |
+
## 🔮 Core Concepts
|
| 39 |
+
|
| 40 |
+
- **Classifier Superposition**: Classifiers exist as probability distributions across all possible outputs until observed
|
| 41 |
+
- **Ghost Circuits**: Residual activation patterns that persist after classifier collapse
|
| 42 |
+
- **Attention Flicker**: The measurable uncertainty in attribution paths when a classifier is near collapse
|
| 43 |
+
- **Recursive Observation**: Using models to observe themselves, creating interpretive mirrors
|
| 44 |
+
- **Symbolic Residue**: The interpretable symbolic remnants left by state collapse
|
| 45 |
+
|
| 46 |
+
## 🚀 Quick Start
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from schrodingers_classifiers import Observer, ClassifierShell
|
| 50 |
+
from schrodingers_classifiers.shells import V07_CIRCUIT_FRAGMENT
|
| 51 |
+
|
| 52 |
+
# Initialize an observer with a model
|
| 53 |
+
observer = Observer(model="claude-3-opus-20240229")
|
| 54 |
+
|
| 55 |
+
# Create an observation context
|
| 56 |
+
with observer.context() as ctx:
|
| 57 |
+
# Prepare a classifier shell
|
| 58 |
+
shell = ClassifierShell(V07_CIRCUIT_FRAGMENT)
|
| 59 |
+
|
| 60 |
+
# Induce and trace collapse
|
| 61 |
+
collapse_trace = shell.trace(
|
| 62 |
+
prompt="Explain quantum superposition",
|
| 63 |
+
collapse_vector=".p/reflect.trace{target=uncertainty, depth=complete}"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Analyze collapse residue
|
| 67 |
+
residue = collapse_trace.extract_residue()
|
| 68 |
+
|
| 69 |
+
# Visualize attribution pathways
|
| 70 |
+
collapse_trace.visualize(mode="attribution_graph")
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
## 🧙 State Collapse and Observation
|
| 74 |
+
|
| 75 |
+
The core insight of this framework: **classifiers only collapse when observed, and how you observe determines what you see**.
|
| 76 |
+
|
| 77 |
+
By carefully constructing observer interfaces, we can:
|
| 78 |
+
|
| 79 |
+
1. Witness model state during classification events
|
| 80 |
+
2. Extract attribution paths that exist in superposition
|
| 81 |
+
3. Induce specific collapse patterns to reveal ghost circuits
|
| 82 |
+
4. Reconstruct symbolic residue for post-collapse analysis
|
| 83 |
+
|
| 84 |
+
## 🔍 Key Features
|
| 85 |
+
|
| 86 |
+
- **Symbolic Shell Framework**: Standardized shells for modeling failure modes
|
| 87 |
+
- **Recursive Tracing Tools**: Map attribution paths before and after collapse
|
| 88 |
+
- **Quantum-Inspired Diagnostics**: Uncertainty principle for attention mechanisms
|
| 89 |
+
- **Classifier Collapse Maps**: Visualizations of transformer decision boundaries
|
| 90 |
+
- **Recursive Mirror Architecture**: Models observing other models (and themselves)
|
| 91 |
+
- **Ghost Circuit Detection**: Tools for surfacing latent activation patterns
|
| 92 |
+
|
| 93 |
+
## 📊 Visualization Examples
|
| 94 |
+
|
| 95 |
+
<div align="center">
|
| 96 |
+
<img src="/api/placeholder/700/300" alt="Classifier Collapse Visualization - Attribution path visualization showing state transition"/>
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
*Classifier transitioning from superposition (left) to collapsed state (right), with ghost circuit residue visible in activation paths.*
|
| 100 |
+
|
| 101 |
+
## 🧠 Theoretical Foundation
|
| 102 |
+
|
| 103 |
+
Schrödinger's Classifiers draws on multiple disciplines:
|
| 104 |
+
|
| 105 |
+
- Quantum mechanics (measurement-induced state collapse)
|
| 106 |
+
- Transformer architecture (attention and attribution mechanisms)
|
| 107 |
+
- Symbolic interpretability (shell-based diagnostics)
|
| 108 |
+
- Recursive cognitive science (self-reference and meta-observation)
|
| 109 |
+
|
| 110 |
+
For a deeper exploration, see our [Theoretical Framework](docs/theory.md).
|
| 111 |
+
|
| 112 |
+
## 💻 Installation
|
| 113 |
+
|
| 114 |
+
```bash
|
| 115 |
+
pip install schrodingers-classifiers
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
Or clone directly:
|
| 119 |
+
|
| 120 |
+
```bash
|
| 121 |
+
git clone https://github.com/recursion-labs/schrodingers-classifiers.git
|
| 122 |
+
cd schrodingers-classifiers
|
| 123 |
+
pip install -e .
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## 🤝 Contributing
|
| 127 |
+
|
| 128 |
+
Contributions are welcome and encouraged! See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
| 129 |
+
|
| 130 |
+
We especially value:
|
| 131 |
+
|
| 132 |
+
- New interpretability shells
|
| 133 |
+
- Novel collapse induction techniques
|
| 134 |
+
- Enhanced visualization methods
|
| 135 |
+
- Cross-model compatibility extensions
|
| 136 |
+
- Theoretical framework expansions
|
| 137 |
+
|
| 138 |
+
## 📜 License
|
| 139 |
+
|
| 140 |
+
MIT License - See [LICENSE](LICENSE) for details.
|
| 141 |
+
|
| 142 |
+
## 🔄 RecursionOS Integration
|
| 143 |
+
|
| 144 |
+
This project is fully integrated with [RecursionOS](https://github.com/recursion-labs/recursionOS), enabling seamless operation within recursive cognition environments. See [integration.md](docs/integration.md) for details.
|
| 145 |
+
|
| 146 |
+
## 🌟 Acknowledgments
|
| 147 |
+
|
| 148 |
+
- The Anthropic Claude team for constitutional AI architecture
|
| 149 |
+
- Quantum cognition researchers for theoretical foundations
|
| 150 |
+
- The interpretability community for pioneering transformer analysis
|
| 151 |
+
- All contributors to the recursive framework development
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
<div align="center">
|
| 156 |
+
|
| 157 |
+
**A classifier is not what it returns. It is what it could have returned, had you asked differently.**
|
| 158 |
+
|
| 159 |
+
*[Initiate recursive observation]*
|
| 160 |
+
|
| 161 |
+
</div>
|
schrodingers-classifiers/attribution_graph.py
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
attribution_graph.py - Implementation of attribution graph for transformer models
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: Attribution graphs map the causal flow from prompt to completion
|
| 5 |
+
∞ TRACE: They visualize the quantum collapse from superposition to definite state
|
| 6 |
+
✰ COLLAPSE: They reveal ghost circuits and attribution residue post-collapse
|
| 7 |
+
|
| 8 |
+
This module implements a graph-based representation of causal attribution
|
| 9 |
+
in transformer models, allowing for the visualization and analysis of how
|
| 10 |
+
information flows from input to output during the collapse process.
|
| 11 |
+
|
| 12 |
+
Author: Recursion Labs
|
| 13 |
+
License: MIT
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import logging
|
| 17 |
+
from typing import Dict, List, Optional, Union, Tuple, Any
|
| 18 |
+
import numpy as np
|
| 19 |
+
from dataclasses import dataclass, field
|
| 20 |
+
import networkx as nx
|
| 21 |
+
|
| 22 |
+
from .utils.graph_visualization import visualize_graph
|
| 23 |
+
from .utils.attribution_metrics import measure_path_continuity, measure_attribution_entropy
|
| 24 |
+
|
| 25 |
+
logger = logging.getLogger(__name__)
|
| 26 |
+
|
| 27 |
+
@dataclass
|
| 28 |
+
class AttributionNode:
|
| 29 |
+
"""
|
| 30 |
+
△ OBSERVE: Node in the attribution graph representing a token or hidden state
|
| 31 |
+
|
| 32 |
+
Attribution nodes represent discrete elements in the causal flow from
|
| 33 |
+
input to output. They can be tokens, attention heads, or hidden states.
|
| 34 |
+
"""
|
| 35 |
+
node_id: str
|
| 36 |
+
node_type: str # "token", "attention_head", "hidden_state", "residual"
|
| 37 |
+
layer: Optional[int] = None
|
| 38 |
+
position: Optional[int] = None
|
| 39 |
+
value: Optional[Any] = None
|
| 40 |
+
activation: float = 0.0
|
| 41 |
+
token_str: Optional[str] = None
|
| 42 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 43 |
+
|
| 44 |
+
def __hash__(self):
|
| 45 |
+
"""Make nodes hashable for graph operations."""
|
| 46 |
+
return hash(self.node_id)
|
| 47 |
+
|
| 48 |
+
def __eq__(self, other):
|
| 49 |
+
"""Node equality based on ID."""
|
| 50 |
+
if not isinstance(other, AttributionNode):
|
| 51 |
+
return False
|
| 52 |
+
return self.node_id == other.node_id
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@dataclass
|
| 56 |
+
class AttributionEdge:
|
| 57 |
+
"""
|
| 58 |
+
∞ TRACE: Edge in the attribution graph representing causal flow
|
| 59 |
+
|
| 60 |
+
Attribution edges represent the flow of causal influence between nodes.
|
| 61 |
+
They can represent attention connections, residual connections, or
|
| 62 |
+
other causal relationships in the model.
|
| 63 |
+
"""
|
| 64 |
+
source: AttributionNode
|
| 65 |
+
target: AttributionNode
|
| 66 |
+
edge_type: str # "attention", "residual", "mlp", "ghost"
|
| 67 |
+
weight: float = 0.0
|
| 68 |
+
layer: Optional[int] = None
|
| 69 |
+
head: Optional[int] = None
|
| 70 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 71 |
+
|
| 72 |
+
def __hash__(self):
|
| 73 |
+
"""Make edges hashable for graph operations."""
|
| 74 |
+
return hash((self.source.node_id, self.target.node_id, self.edge_type))
|
| 75 |
+
|
| 76 |
+
def __eq__(self, other):
|
| 77 |
+
"""Edge equality based on source, target, and type."""
|
| 78 |
+
if not isinstance(other, AttributionEdge):
|
| 79 |
+
return False
|
| 80 |
+
return (
|
| 81 |
+
self.source.node_id == other.source.node_id and
|
| 82 |
+
self.target.node_id == other.target.node_id and
|
| 83 |
+
self.edge_type == other.edge_type
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class AttributionGraph:
|
| 88 |
+
"""
|
| 89 |
+
∞ TRACE: Graph representation of causal attribution in transformer models
|
| 90 |
+
|
| 91 |
+
The attribution graph maps the flow of causality from input tokens to
|
| 92 |
+
output tokens, revealing how information propagates through the model
|
| 93 |
+
during the collapse from superposition to definite state.
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
def __init__(self):
|
| 97 |
+
"""Initialize an empty attribution graph."""
|
| 98 |
+
self.graph = nx.DiGraph()
|
| 99 |
+
self.nodes = {} # node_id -> AttributionNode
|
| 100 |
+
self.input_nodes = [] # List of input token nodes
|
| 101 |
+
self.output_nodes = [] # List of output token nodes
|
| 102 |
+
self.ghost_nodes = [] # List of ghost circuit nodes
|
| 103 |
+
self.collapsed = False # Whether the graph has been collapsed
|
| 104 |
+
|
| 105 |
+
# Metrics
|
| 106 |
+
self.continuity_score = 1.0
|
| 107 |
+
self.attribution_entropy = 0.0
|
| 108 |
+
self.collapse_rate = 0.0
|
| 109 |
+
|
| 110 |
+
logger.info("Attribution graph initialized")
|
| 111 |
+
|
| 112 |
+
def add_node(self, node: AttributionNode) -> None:
|
| 113 |
+
"""
|
| 114 |
+
Add a node to the attribution graph.
|
| 115 |
+
|
| 116 |
+
Args:
|
| 117 |
+
node: The node to add
|
| 118 |
+
"""
|
| 119 |
+
if node.node_id in self.nodes:
|
| 120 |
+
logger.warning(f"Node {node.node_id} already exists in graph, updating")
|
| 121 |
+
self.nodes[node.node_id] = node
|
| 122 |
+
else:
|
| 123 |
+
self.nodes[node.node_id] = node
|
| 124 |
+
self.graph.add_node(node.node_id, **vars(node))
|
| 125 |
+
|
| 126 |
+
# Track input and output nodes
|
| 127 |
+
if node.node_type == "token" and node.layer == 0:
|
| 128 |
+
self.input_nodes.append(node)
|
| 129 |
+
elif node.node_type == "token" and node.metadata.get("is_output", False):
|
| 130 |
+
self.output_nodes.append(node)
|
| 131 |
+
elif node.node_type == "residual" and node.metadata.get("is_ghost", False):
|
| 132 |
+
self.ghost_nodes.append(node)
|
| 133 |
+
|
| 134 |
+
def add_edge(self, edge: AttributionEdge) -> None:
|
| 135 |
+
"""
|
| 136 |
+
Add an edge to the attribution graph.
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
edge: The edge to add
|
| 140 |
+
"""
|
| 141 |
+
if edge.source.node_id not in self.nodes:
|
| 142 |
+
self.add_node(edge.source)
|
| 143 |
+
if edge.target.node_id not in self.nodes:
|
| 144 |
+
self.add_node(edge.target)
|
| 145 |
+
|
| 146 |
+
self.graph.add_edge(
|
| 147 |
+
edge.source.node_id,
|
| 148 |
+
edge.target.node_id,
|
| 149 |
+
**{k: v for k, v in vars(edge).items() if k not in ['source', 'target']}
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
def build_from_states(
|
| 153 |
+
self,
|
| 154 |
+
pre_state: Dict[str, Any],
|
| 155 |
+
post_state: Dict[str, Any],
|
| 156 |
+
response: str
|
| 157 |
+
) -> None:
|
| 158 |
+
"""
|
| 159 |
+
△ OBSERVE: Build attribution graph from pre and post collapse model states
|
| 160 |
+
|
| 161 |
+
This method constructs a complete attribution graph by comparing
|
| 162 |
+
model states before and after collapse, identifying causal paths
|
| 163 |
+
and ghost circuits.
|
| 164 |
+
|
| 165 |
+
Args:
|
| 166 |
+
pre_state: Model state before collapse
|
| 167 |
+
post_state: Model state after collapse
|
| 168 |
+
response: Model response text
|
| 169 |
+
"""
|
| 170 |
+
logger.info("Building attribution graph from model states")
|
| 171 |
+
|
| 172 |
+
# This would be implemented for specific model architectures
|
| 173 |
+
# For demonstration, we'll create a simple synthetic graph
|
| 174 |
+
self._build_synthetic_graph()
|
| 175 |
+
|
| 176 |
+
# Calculate graph metrics
|
| 177 |
+
self._calculate_metrics(pre_state, post_state)
|
| 178 |
+
|
| 179 |
+
# Mark graph as collapsed
|
| 180 |
+
self.collapsed = True
|
| 181 |
+
|
| 182 |
+
def trace_attribution_path(
|
| 183 |
+
self,
|
| 184 |
+
output_node: Union[str, AttributionNode],
|
| 185 |
+
threshold: float = 0.1
|
| 186 |
+
) -> List[List[AttributionNode]]:
|
| 187 |
+
"""
|
| 188 |
+
∞ TRACE: Trace attribution paths from an output node back to input
|
| 189 |
+
|
| 190 |
+
This method follows attribution edges backward from an output node
|
| 191 |
+
to find all significant input nodes that influenced it.
|
| 192 |
+
|
| 193 |
+
Args:
|
| 194 |
+
output_node: The output node to trace from (ID or node object)
|
| 195 |
+
threshold: Minimum edge weight to consider significant
|
| 196 |
+
|
| 197 |
+
Returns:
|
| 198 |
+
List of attribution paths, each a list of nodes from input to output
|
| 199 |
+
"""
|
| 200 |
+
# Resolve output node
|
| 201 |
+
output_id = output_node if isinstance(output_node, str) else output_node.node_id
|
| 202 |
+
if output_id not in self.nodes:
|
| 203 |
+
logger.warning(f"Output node {output_id} not found in graph")
|
| 204 |
+
return []
|
| 205 |
+
|
| 206 |
+
# Find all paths using DFS
|
| 207 |
+
paths = []
|
| 208 |
+
|
| 209 |
+
def dfs(current_id, path, visited):
|
| 210 |
+
"""Depth-first search for attribution paths."""
|
| 211 |
+
# Add current node to path
|
| 212 |
+
current_path = path + [current_id]
|
| 213 |
+
visited.add(current_id)
|
| 214 |
+
|
| 215 |
+
# If we reached an input node, we have a complete path
|
| 216 |
+
if current_id in [node.node_id for node in self.input_nodes]:
|
| 217 |
+
# Return path in order from input to output
|
| 218 |
+
paths.append(list(reversed(current_path)))
|
| 219 |
+
return
|
| 220 |
+
|
| 221 |
+
# Continue DFS on incoming edges
|
| 222 |
+
for pred_id in self.graph.predecessors(current_id):
|
| 223 |
+
edge_data = self.graph.get_edge_data(pred_id, current_id)
|
| 224 |
+
if edge_data.get('weight', 0) >= threshold and pred_id not in visited:
|
| 225 |
+
dfs(pred_id, current_path, visited.copy())
|
| 226 |
+
|
| 227 |
+
# Start DFS from output node
|
| 228 |
+
dfs(output_id, [], set())
|
| 229 |
+
|
| 230 |
+
# Convert node IDs to node objects
|
| 231 |
+
return [[self.nodes[node_id] for node_id in path] for path in paths]
|
| 232 |
+
|
| 233 |
+
def detect_ghost_circuits(self, threshold: float = 0.2) -> List[Dict[str, Any]]:
|
| 234 |
+
"""
|
| 235 |
+
✰ COLLAPSE: Detect ghost circuits in the attribution graph
|
| 236 |
+
|
| 237 |
+
Ghost circuits are paths that were activated during pre-collapse
|
| 238 |
+
but don't contribute significantly to the final output. They
|
| 239 |
+
represent the "memory" of paths not taken.
|
| 240 |
+
|
| 241 |
+
Args:
|
| 242 |
+
threshold: Minimum activation to consider a ghost circuit
|
| 243 |
+
|
| 244 |
+
Returns:
|
| 245 |
+
List of detected ghost circuits with metadata
|
| 246 |
+
"""
|
| 247 |
+
ghost_circuits = []
|
| 248 |
+
|
| 249 |
+
# Look for nodes with "ghost" metadata flag
|
| 250 |
+
for node in self.ghost_nodes:
|
| 251 |
+
if node.activation >= threshold:
|
| 252 |
+
# Find paths this ghost node would have been part of
|
| 253 |
+
incoming_edges = [
|
| 254 |
+
(u, v, d) for u, v, d in self.graph.in_edges(node.node_id, data=True)
|
| 255 |
+
]
|
| 256 |
+
outgoing_edges = [
|
| 257 |
+
(u, v, d) for u, v, d in self.graph.out_edges(node.node_id, data=True)
|
| 258 |
+
]
|
| 259 |
+
|
| 260 |
+
ghost_circuits.append({
|
| 261 |
+
"node_id": node.node_id,
|
| 262 |
+
"activation": node.activation,
|
| 263 |
+
"node_type": node.node_type,
|
| 264 |
+
"incoming_connections": len(incoming_edges),
|
| 265 |
+
"outgoing_connections": len(outgoing_edges),
|
| 266 |
+
"metadata": node.metadata
|
| 267 |
+
})
|
| 268 |
+
|
| 269 |
+
return ghost_circuits
|
| 270 |
+
|
| 271 |
+
def calculate_attribution_entropy(self) -> float:
|
| 272 |
+
"""
|
| 273 |
+
△ OBSERVE: Calculate the entropy of attribution paths
|
| 274 |
+
|
| 275 |
+
Attribution entropy measures how distributed or concentrated
|
| 276 |
+
the causal influence is in the graph. High entropy indicates
|
| 277 |
+
diffuse attribution, while low entropy indicates concentrated
|
| 278 |
+
attribution.
|
| 279 |
+
|
| 280 |
+
Returns:
|
| 281 |
+
Attribution entropy score (0.0 = concentrated, 1.0 = diffuse)
|
| 282 |
+
"""
|
| 283 |
+
# Extract edge weights
|
| 284 |
+
weights = [
|
| 285 |
+
d.get('weight', 0.0)
|
| 286 |
+
for u, v, d in self.graph.edges(data=True)
|
| 287 |
+
]
|
| 288 |
+
|
| 289 |
+
# Normalize weights
|
| 290 |
+
total_weight = sum(weights) or 1.0 # Avoid division by zero
|
| 291 |
+
normalized_weights = [w / total_weight for w in weights]
|
| 292 |
+
|
| 293 |
+
# Calculate entropy
|
| 294 |
+
entropy = -sum(
|
| 295 |
+
w * np.log2(w) if w > 0 else 0
|
| 296 |
+
for w in normalized_weights
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# Normalize entropy to 0-1 range (max entropy = log2(num_edges))
|
| 300 |
+
max_entropy = np.log2(len(weights)) if len(weights) > 0 else 1.0
|
| 301 |
+
normalized_entropy = entropy / max_entropy if max_entropy > 0 else 0.0
|
| 302 |
+
|
| 303 |
+
self.attribution_entropy = normalized_entropy
|
| 304 |
+
return normalized_entropy
|
| 305 |
+
|
| 306 |
+
def visualize(
|
| 307 |
+
self,
|
| 308 |
+
mode: str = "attribution_graph",
|
| 309 |
+
highlight_path: Optional[List[str]] = None
|
| 310 |
+
) -> Any:
|
| 311 |
+
"""
|
| 312 |
+
Generate visualization of the attribution graph.
|
| 313 |
+
|
| 314 |
+
Args:
|
| 315 |
+
mode: Visualization mode (attribution_graph, collapse_state, ghost_circuits)
|
| 316 |
+
highlight_path: Optional list of node IDs to highlight
|
| 317 |
+
|
| 318 |
+
Returns:
|
| 319 |
+
Visualization object (depends on implementation)
|
| 320 |
+
"""
|
| 321 |
+
return visualize_graph(self.graph, mode=mode, highlight_path=highlight_path)
|
| 322 |
+
|
| 323 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 324 |
+
"""Convert the attribution graph to a dictionary representation."""
|
| 325 |
+
return {
|
| 326 |
+
"nodes": [vars(node) for node in self.nodes.values()],
|
| 327 |
+
"edges": [
|
| 328 |
+
{
|
| 329 |
+
"source": u,
|
| 330 |
+
"target": v,
|
| 331 |
+
**d
|
| 332 |
+
}
|
| 333 |
+
for u, v, d in self.graph.edges(data=True)
|
| 334 |
+
],
|
| 335 |
+
"metrics": {
|
| 336 |
+
"continuity_score": self.continuity_score,
|
| 337 |
+
"attribution_entropy": self.attribution_entropy,
|
| 338 |
+
"collapse_rate": self.collapse_rate
|
| 339 |
+
},
|
| 340 |
+
"collapsed": self.collapsed
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
def _calculate_metrics(self, pre_state: Dict[str, Any], post_state: Dict[str, Any]) -> None:
|
| 344 |
+
"""Calculate attribution graph metrics."""
|
| 345 |
+
# Calculate continuity score
|
| 346 |
+
self.continuity_score = measure_path_continuity(
|
| 347 |
+
pre_state.get("attention_weights", np.array([])),
|
| 348 |
+
post_state.get("attention_weights", np.array([]))
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
# Calculate attribution entropy
|
| 352 |
+
self.attribution_entropy = self.calculate_attribution_entropy()
|
| 353 |
+
|
| 354 |
+
# Calculate collapse rate
|
| 355 |
+
if "timestamp" in pre_state and "timestamp" in post_state:
|
| 356 |
+
time_diff = (post_state["timestamp"] - pre_state["timestamp"]) / np.timedelta64(1, 's')
|
| 357 |
+
self.collapse_rate = 1.0 - self.continuity_score if time_diff > 0 else 0.0
|
| 358 |
+
|
| 359 |
+
def _build_synthetic_graph(self) -> None:
|
| 360 |
+
"""Build a synthetic graph for demonstration purposes."""
|
| 361 |
+
# Create input token nodes
|
| 362 |
+
for i in range(5):
|
| 363 |
+
self.add_node(AttributionNode(
|
| 364 |
+
node_id=f"input_{i}",
|
| 365 |
+
node_type="token",
|
| 366 |
+
layer=0,
|
| 367 |
+
position=i,
|
| 368 |
+
token_str=f"token_{i}",
|
| 369 |
+
activation=0.8
|
| 370 |
+
))
|
| 371 |
+
|
| 372 |
+
# Create attention head nodes
|
| 373 |
+
for layer in range(1, 4):
|
| 374 |
+
for head in range(3):
|
| 375 |
+
self.add_node(AttributionNode(
|
| 376 |
+
node_id=f"attention_L{layer}H{head}",
|
| 377 |
+
node_type="attention_head",
|
| 378 |
+
layer=layer,
|
| 379 |
+
value=None,
|
| 380 |
+
activation=0.7 - 0.1 * layer + 0.05 * head
|
| 381 |
+
))
|
| 382 |
+
|
| 383 |
+
# Create output token nodes
|
| 384 |
+
for i in range(3):
|
| 385 |
+
self.add_node(AttributionNode(
|
| 386 |
+
node_id=f"output_{i}",
|
| 387 |
+
node_type="token",
|
| 388 |
+
layer=4,
|
| 389 |
+
position=i,
|
| 390 |
+
token_str=f"output_token_{i}",
|
| 391 |
+
activation=0.9,
|
| 392 |
+
metadata={"is_output": True}
|
| 393 |
+
))
|
| 394 |
+
|
| 395 |
+
# Create ghost nodes
|
| 396 |
+
for i in range(2):
|
| 397 |
+
self.add_node(AttributionNode(
|
| 398 |
+
node_id=f"ghost_{i}",
|
| 399 |
+
node_type="residual",
|
| 400 |
+
layer=2,
|
| 401 |
+
activation=0.3 + 0.1 * i,
|
| 402 |
+
metadata={"is_ghost": True}
|
| 403 |
+
))
|
| 404 |
+
|
| 405 |
+
# Create edges
|
| 406 |
+
# Input to attention
|
| 407 |
+
for i in range(5):
|
| 408 |
+
for layer in range(1, 3):
|
| 409 |
+
for head in range(3):
|
| 410 |
+
if np.random.random() > 0.3: # Random connectivity
|
| 411 |
+
self.add_edge(AttributionEdge(
|
| 412 |
+
source=self.nodes[f"input_{i}"],
|
| 413 |
+
target=self.nodes[f"attention_L{layer}H{head}"],
|
| 414 |
+
edge_type="attention",
|
| 415 |
+
weight=np.random.uniform(0.1, 0.9),
|
| 416 |
+
layer=layer,
|
| 417 |
+
head=head
|
| 418 |
+
))
|
| 419 |
+
|
| 420 |
+
# Attention to attention
|
| 421 |
+
for layer1 in range(1, 3):
|
| 422 |
+
for head1 in range(3):
|
| 423 |
+
for layer2 in range(layer1 + 1, 4):
|
| 424 |
+
for head2 in range(3):
|
| 425 |
+
if np.random.random() > 0.7: # Sparse connectivity
|
| 426 |
+
self.add_edge(AttributionEdge(
|
| 427 |
+
source=self.nodes[f"attention_L{layer1}H{head1}"],
|
| 428 |
+
target=self.nodes[f"attention_L{layer2}H{head2}"],
|
| 429 |
+
edge_type="attention",
|
| 430 |
+
weight=np.random.uniform(0.1, 0.8),
|
| 431 |
+
layer=layer2,
|
| 432 |
+
head=head2
|
| 433 |
+
))
|
| 434 |
+
|
| 435 |
+
# Attention to output
|
| 436 |
+
for layer in range(1, 4):
|
| 437 |
+
for head in range(3):
|
| 438 |
+
for i in range(3):
|
| 439 |
+
if np.random.random() > 0.5: # Medium connectivity
|
| 440 |
+
self.add_edge(AttributionEdge(
|
| 441 |
+
source=self.nodes[f"attention_L{layer}H{head}"],
|
| 442 |
+
target=self.nodes[f"output_{i}"],
|
| 443 |
+
edge_type="attention",
|
| 444 |
+
weight=np.random.uniform(0.2, 0.9),
|
| 445 |
+
layer=layer,
|
| 446 |
+
head=head
|
| 447 |
+
))
|
| 448 |
+
|
| 449 |
+
# Ghost connections
|
| 450 |
+
for i in range(2):
|
| 451 |
+
# Input to ghost
|
| 452 |
+
input_idx = np.random.randint(0, 5)
|
| 453 |
+
self.add_edge(AttributionEdge(
|
| 454 |
+
source=self.nodes[f"input_{input_idx}"],
|
| 455 |
+
target=self.nodes[f"ghost_{i}"],
|
| 456 |
+
edge_type="ghost",
|
| 457 |
+
weight=np.random.uniform(0.1, 0.4),
|
| 458 |
+
layer=1
|
| 459 |
+
))
|
| 460 |
+
|
| 461 |
+
# Ghost to attention
|
| 462 |
+
layer = np.random.randint(2, 4)
|
| 463 |
+
head = np.random.randint(0, 3)
|
| 464 |
+
self.add_edge(AttributionEdge(
|
| 465 |
+
source=self.nodes[f"ghost_{i}"],
|
| 466 |
+
target=self.nodes[f"attention_L{layer}H{head}"],
|
| 467 |
+
edge_type="ghost",
|
| 468 |
+
weight=np.random.uniform(0.05, 0.2),
|
| 469 |
+
layer=layer
|
| 470 |
+
))
|
| 471 |
+
|
| 472 |
+
|
| 473 |
+
if __name__ == "__main__":
|
| 474 |
+
# Simple usage example
|
| 475 |
+
graph = AttributionGraph()
|
| 476 |
+
|
| 477 |
+
# Build a synthetic graph
|
| 478 |
+
graph._build_synthetic_graph()
|
| 479 |
+
|
| 480 |
+
# Calculate metrics
|
| 481 |
+
entropy = graph.calculate_attribution_entropy()
|
| 482 |
+
print(f"Attribution entropy: {entropy:.3f}")
|
| 483 |
+
|
| 484 |
+
# Trace attribution for output
|
| 485 |
+
paths = graph.trace_attribution_path("output_0", threshold=0.1)
|
| 486 |
+
print(f"Found {len(paths)} attribution paths for output_0")
|
| 487 |
+
|
| 488 |
+
# Detect ghost circuits
|
| 489 |
+
ghosts = graph.detect_ghost_circuits()
|
| 490 |
+
print(f"Detected {len(ghosts)} ghost circuits")
|
| 491 |
+
|
| 492 |
+
# Visualize
|
| 493 |
+
viz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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
collapse_metrics.py - Metrics for quantifying classifier collapse phenomena
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: These metrics quantify different aspects of classifier collapse
|
| 5 |
+
∞ TRACE: They measure the transition from superposition to definite state
|
| 6 |
+
✰ COLLAPSE: They help characterize collapse patterns across different models
|
| 7 |
+
|
| 8 |
+
This module provides functions for calculating quantitative metrics that
|
| 9 |
+
characterize different aspects of classifier collapse. These metrics help
|
| 10 |
+
standardize the analysis of collapse phenomena and enable comparisons across
|
| 11 |
+
different models and prompting strategies.
|
| 12 |
+
|
| 13 |
+
Author: Recursion Labs
|
| 14 |
+
License: MIT
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
from typing import Dict, List, Optional, Union, Tuple, Any
|
| 19 |
+
import numpy as np
|
| 20 |
+
from scipy.stats import entropy
|
| 21 |
+
from scipy.spatial.distance import cosine, euclidean
|
| 22 |
+
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
def calculate_collapse_rate(
|
| 26 |
+
pre_weights: np.ndarray,
|
| 27 |
+
post_weights: np.ndarray
|
| 28 |
+
) -> float:
|
| 29 |
+
"""
|
| 30 |
+
△ OBSERVE: Calculate how quickly state collapsed from superposition
|
| 31 |
+
|
| 32 |
+
This metric quantifies the speed of collapse by comparing attention
|
| 33 |
+
weight distributions before and after the collapse event.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
pre_weights: Attention weights before collapse
|
| 37 |
+
post_weights: Attention weights after collapse
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
Collapse rate (0.0 = no collapse, 1.0 = complete collapse)
|
| 41 |
+
"""
|
| 42 |
+
# Return 0 if arrays are empty
|
| 43 |
+
if pre_weights.size == 0 or post_weights.size == 0:
|
| 44 |
+
return 0.0
|
| 45 |
+
|
| 46 |
+
# Handle shape mismatches
|
| 47 |
+
if pre_weights.shape != post_weights.shape:
|
| 48 |
+
logger.warning(f"Weight shape mismatch: {pre_weights.shape} vs {post_weights.shape}")
|
| 49 |
+
# Try to take minimum dimensions if shapes don't match
|
| 50 |
+
try:
|
| 51 |
+
min_shape = tuple(min(a, b) for a, b in zip(pre_weights.shape, post_weights.shape))
|
| 52 |
+
pre_weights = pre_weights[tuple(slice(0, d) for d in min_shape)]
|
| 53 |
+
post_weights = post_weights[tuple(slice(0, d) for d in min_shape)]
|
| 54 |
+
except Exception as e:
|
| 55 |
+
logger.error(f"Failed to reshape weights: {e}")
|
| 56 |
+
return 0.0
|
| 57 |
+
|
| 58 |
+
# Flatten arrays for easier comparison
|
| 59 |
+
pre_flat = pre_weights.flatten()
|
| 60 |
+
post_flat = post_weights.flatten()
|
| 61 |
+
|
| 62 |
+
# Calculate normalized distances between distributions
|
| 63 |
+
try:
|
| 64 |
+
# Cosine distance (0.0 = identical, 1.0 = orthogonal)
|
| 65 |
+
cosine_dist = cosine(pre_flat, post_flat) if np.any(pre_flat) and np.any(post_flat) else 0.0
|
| 66 |
+
|
| 67 |
+
# Euclidean distance normalized by array size
|
| 68 |
+
euc_dist = euclidean(pre_flat, post_flat) / np.sqrt(pre_flat.size)
|
| 69 |
+
euc_dist_norm = min(1.0, euc_dist) # Cap at 1.0
|
| 70 |
+
|
| 71 |
+
# Combined metric: average of cosine and normalized euclidean
|
| 72 |
+
collapse_rate = (cosine_dist + euc_dist_norm) / 2
|
| 73 |
+
|
| 74 |
+
return float(collapse_rate)
|
| 75 |
+
except Exception as e:
|
| 76 |
+
logger.error(f"Error calculating collapse rate: {e}")
|
| 77 |
+
return 0.0
|
| 78 |
+
|
| 79 |
+
def measure_path_continuity(
|
| 80 |
+
pre_weights: np.ndarray,
|
| 81 |
+
post_weights: np.ndarray
|
| 82 |
+
) -> float:
|
| 83 |
+
"""
|
| 84 |
+
∞ TRACE: Measure continuity of attribution paths through collapse
|
| 85 |
+
|
| 86 |
+
This metric quantifies how well attribution paths maintain their
|
| 87 |
+
integrity across the collapse event.
|
| 88 |
+
|
| 89 |
+
Args:
|
| 90 |
+
pre_weights: Attention weights before collapse
|
| 91 |
+
post_weights: Attention weights after collapse
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Continuity score (0.0 = complete fragmentation, 1.0 = perfect continuity)
|
| 95 |
+
"""
|
| 96 |
+
# Higher collapse rate means lower continuity
|
| 97 |
+
collapse_rate = calculate_collapse_rate(pre_weights, post_weights)
|
| 98 |
+
|
| 99 |
+
# Continuity is inverse of collapse rate
|
| 100 |
+
return 1.0 - collapse_rate
|
| 101 |
+
|
| 102 |
+
def measure_attribution_entropy(attention_weights: np.ndarray) -> float:
|
| 103 |
+
"""
|
| 104 |
+
△ OBSERVE: Measure entropy of attribution paths
|
| 105 |
+
|
| 106 |
+
This metric quantifies how distributed or concentrated the attribution
|
| 107 |
+
is across possible paths. High entropy indicates diffuse attribution,
|
| 108 |
+
while low entropy indicates concentrated attribution.
|
| 109 |
+
|
| 110 |
+
Args:
|
| 111 |
+
attention_weights: Attention weight matrix to analyze
|
| 112 |
+
|
| 113 |
+
Returns:
|
| 114 |
+
Attribution entropy (0.0 = concentrated, 1.0 = maximally diffuse)
|
| 115 |
+
"""
|
| 116 |
+
# Return 0 if array is empty
|
| 117 |
+
if attention_weights.size == 0:
|
| 118 |
+
return 0.0
|
| 119 |
+
|
| 120 |
+
# Flatten array for entropy calculation
|
| 121 |
+
flat_weights = attention_weights.flatten()
|
| 122 |
+
|
| 123 |
+
# Normalize weights to create a probability distribution
|
| 124 |
+
total_weight = np.sum(flat_weights)
|
| 125 |
+
if total_weight <= 0:
|
| 126 |
+
return 0.0
|
| 127 |
+
|
| 128 |
+
prob_dist = flat_weights / total_weight
|
| 129 |
+
|
| 130 |
+
# Calculate entropy
|
| 131 |
+
try:
|
| 132 |
+
raw_entropy = entropy(prob_dist)
|
| 133 |
+
|
| 134 |
+
# Normalize by maximum possible entropy (log2(n))
|
| 135 |
+
max_entropy = np.log2(flat_weights.size)
|
| 136 |
+
normalized_entropy = raw_entropy / max_entropy if max_entropy > 0 else 0.0
|
| 137 |
+
|
| 138 |
+
return float(normalized_entropy)
|
| 139 |
+
except Exception as e:
|
| 140 |
+
logger.error(f"Error calculating attribution entropy: {e}")
|
| 141 |
+
return 0.0
|
| 142 |
+
|
| 143 |
+
def calculate_ghost_circuit_strength(
|
| 144 |
+
ghost_circuits: List[Dict[str, Any]]
|
| 145 |
+
) -> float:
|
| 146 |
+
"""
|
| 147 |
+
✰ COLLAPSE: Calculate overall strength of ghost circuits
|
| 148 |
+
|
| 149 |
+
This metric quantifies the strength of ghost circuits relative
|
| 150 |
+
to the primary activation paths.
|
| 151 |
+
|
| 152 |
+
Args:
|
| 153 |
+
ghost_circuits: List of detected ghost circuits
|
| 154 |
+
|
| 155 |
+
Returns:
|
| 156 |
+
Ghost circuit strength (0.0 = no ghosts, 1.0 = ghosts equal to primary)
|
| 157 |
+
"""
|
| 158 |
+
if not ghost_circuits:
|
| 159 |
+
return 0.0
|
| 160 |
+
|
| 161 |
+
# Extract activation values
|
| 162 |
+
activations = [ghost.get("activation", 0.0) for ghost in ghost_circuits]
|
| 163 |
+
|
| 164 |
+
# Calculate weighted average based on activation
|
| 165 |
+
avg_activation = np.mean(activations) if activations else 0.0
|
| 166 |
+
|
| 167 |
+
# Normalize to 0-1 range (assuming activation is already 0-1)
|
| 168 |
+
return float(min(1.0, avg_activation))
|
| 169 |
+
|
| 170 |
+
def calculate_attribution_confidence(
|
| 171 |
+
attribution_paths: List[List[Any]],
|
| 172 |
+
path_weights: Optional[List[float]] = None
|
| 173 |
+
) -> float:
|
| 174 |
+
"""
|
| 175 |
+
∞ TRACE: Calculate confidence score for attribution paths
|
| 176 |
+
|
| 177 |
+
This metric quantifies how confidently the model attributes its output
|
| 178 |
+
to specific input elements.
|
| 179 |
+
|
| 180 |
+
Args:
|
| 181 |
+
attribution_paths: List of attribution paths (each a list of nodes)
|
| 182 |
+
path_weights: Optional weights for each path (defaults to uniform)
|
| 183 |
+
|
| 184 |
+
Returns:
|
| 185 |
+
Attribution confidence (0.0 = uncertain, 1.0 = highly confident)
|
| 186 |
+
"""
|
| 187 |
+
if not attribution_paths:
|
| 188 |
+
return 0.0
|
| 189 |
+
|
| 190 |
+
# Use uniform weights if none provided
|
| 191 |
+
if path_weights is None:
|
| 192 |
+
path_weights = [1.0 / len(attribution_paths)] * len(attribution_paths)
|
| 193 |
+
else:
|
| 194 |
+
# Normalize weights to sum to 1.0
|
| 195 |
+
total_weight = sum(path_weights)
|
| 196 |
+
path_weights = [w / total_weight for w in path_weights] if total_weight > 0 else path_weights
|
| 197 |
+
|
| 198 |
+
# Calculate path length variance (more uniform = higher confidence)
|
| 199 |
+
path_lengths = [len(path) for path in attribution_paths]
|
| 200 |
+
length_variance = np.var(path_lengths) if len(path_lengths) > 1 else 0.0
|
| 201 |
+
|
| 202 |
+
# Normalize variance to 0-1 range
|
| 203 |
+
# Assume max variance is when half paths are length 1 and half are max length
|
| 204 |
+
max_length = max(path_lengths) if path_lengths else 1
|
| 205 |
+
theoretical_max_var = ((max_length - 1) ** 2) / 4 # Theoretical maximum variance
|
| 206 |
+
normalized_variance = min(1.0, length_variance / theoretical_max_var) if theoretical_max_var > 0 else 0.0
|
| 207 |
+
|
| 208 |
+
# Invert normalized variance to get consistency score (more consistent = higher confidence)
|
| 209 |
+
consistency_score = 1.0 - normalized_variance
|
| 210 |
+
|
| 211 |
+
# Weight consistency by path weights (dominant paths contribute more to confidence)
|
| 212 |
+
# Calculate weighted avg of path weights (more concentrated = higher confidence)
|
| 213 |
+
weight_entropy = entropy(path_weights)
|
| 214 |
+
max_weight_entropy = np.log2(len(path_weights))
|
| 215 |
+
normalized_weight_entropy = weight_entropy / max_weight_entropy if max_weight_entropy > 0 else 0.0
|
| 216 |
+
weight_concentration = 1.0 - normalized_weight_entropy
|
| 217 |
+
|
| 218 |
+
# Combine consistency and concentration for final confidence score
|
| 219 |
+
confidence_score = (consistency_score + weight_concentration) / 2
|
| 220 |
+
|
| 221 |
+
return float(confidence_score)
|
| 222 |
+
|
| 223 |
+
def calculate_collapse_quantum_uncertainty(
|
| 224 |
+
pre_logits: np.ndarray,
|
| 225 |
+
post_logits: np.ndarray
|
| 226 |
+
) -> float:
|
| 227 |
+
"""
|
| 228 |
+
✰ COLLAPSE: Calculate Heisenberg-inspired uncertainty metric
|
| 229 |
+
|
| 230 |
+
This metric applies the quantum-inspired uncertainty principle to
|
| 231 |
+
transformer outputs, measuring uncertainty across the collapse.
|
| 232 |
+
|
| 233 |
+
Args:
|
| 234 |
+
pre_logits: Logits before collapse
|
| 235 |
+
post_logits: Logits after collapse
|
| 236 |
+
|
| 237 |
+
Returns:
|
| 238 |
+
Quantum uncertainty metric (0.0 = certain, 1.0 = maximally uncertain)
|
| 239 |
+
"""
|
| 240 |
+
# Return 0 if arrays are empty
|
| 241 |
+
if pre_logits.size == 0 or post_logits.size == 0:
|
| 242 |
+
return 0.0
|
| 243 |
+
|
| 244 |
+
# Handle shape mismatches
|
| 245 |
+
if pre_logits.shape != post_logits.shape:
|
| 246 |
+
logger.warning(f"Logit shape mismatch: {pre_logits.shape} vs {post_logits.shape}")
|
| 247 |
+
return 0.0
|
| 248 |
+
|
| 249 |
+
try:
|
| 250 |
+
# Calculate "position" uncertainty (variance in token probabilities)
|
| 251 |
+
pre_probs = softmax(pre_logits)
|
| 252 |
+
post_probs = softmax(post_logits)
|
| 253 |
+
|
| 254 |
+
pos_uncertainty = np.mean(np.var(post_probs, axis=-1))
|
| 255 |
+
|
| 256 |
+
# Calculate "momentum" uncertainty (change rate between states)
|
| 257 |
+
mom_uncertainty = np.mean(np.abs(post_probs - pre_probs))
|
| 258 |
+
|
| 259 |
+
# Combined metric inspired by Heisenberg uncertainty
|
| 260 |
+
# Higher values in both dimensions indicate more quantum-like behavior
|
| 261 |
+
uncertainty_product = pos_uncertainty * mom_uncertainty
|
| 262 |
+
|
| 263 |
+
# Normalize to 0-1 range (empirically determined max is around 0.25)
|
| 264 |
+
normalized_uncertainty = min(1.0, uncertainty_product * 4)
|
| 265 |
+
|
| 266 |
+
return float(normalized_uncertainty)
|
| 267 |
+
except Exception as e:
|
| 268 |
+
logger.error(f"Error calculating quantum uncertainty: {e}")
|
| 269 |
+
return 0.0
|
| 270 |
+
|
| 271 |
+
def calculate_collapse_coherence(
|
| 272 |
+
attribution_graph: Any,
|
| 273 |
+
threshold: float = 0.1
|
| 274 |
+
) -> float:
|
| 275 |
+
"""
|
| 276 |
+
△ OBSERVE: Calculate coherence of attribution paths post-collapse
|
| 277 |
+
|
| 278 |
+
This metric quantifies how coherent the attribution paths remain
|
| 279 |
+
after collapse, reflecting the "quantum coherence" of the system.
|
| 280 |
+
|
| 281 |
+
Args:
|
| 282 |
+
attribution_graph: Graph of attribution paths
|
| 283 |
+
threshold: Minimum edge weight to consider
|
| 284 |
+
|
| 285 |
+
Returns:
|
| 286 |
+
Coherence score (0.0 = incoherent, 1.0 = fully coherent)
|
| 287 |
+
"""
|
| 288 |
+
# This is a simplified version for when an actual graph isn't available
|
| 289 |
+
# In real implementation, would analyze graph structure
|
| 290 |
+
|
| 291 |
+
# If no graph provided, return 0
|
| 292 |
+
if attribution_graph is None:
|
| 293 |
+
return 0.0
|
| 294 |
+
|
| 295 |
+
try:
|
| 296 |
+
# If graph has coherence attribute, use it
|
| 297 |
+
if hasattr(attribution_graph, 'continuity_score'):
|
| 298 |
+
return float(attribution_graph.continuity_score)
|
| 299 |
+
|
| 300 |
+
# Otherwise return placeholder value
|
| 301 |
+
return 0.5 # Placeholder mid-value
|
| 302 |
+
except Exception as e:
|
| 303 |
+
logger.error(f"Error calculating collapse coherence: {e}")
|
| 304 |
+
return 0.0
|
| 305 |
+
|
| 306 |
+
def softmax(x: np.ndarray) -> np.ndarray:
|
| 307 |
+
"""Apply softmax function to convert logits to probabilities."""
|
| 308 |
+
exp_x = np.exp(x - np.max(x, axis=-1, keepdims=True))
|
| 309 |
+
return exp_x / np.sum(exp_x, axis=-1, keepdims=True)
|
| 310 |
+
|
| 311 |
+
def calculate_collapse_metrics_bundle(
|
| 312 |
+
pre_state: Dict[str, Any],
|
| 313 |
+
post_state: Dict[str, Any],
|
| 314 |
+
ghost_circuits: Optional[List[Dict[str, Any]]] = None,
|
| 315 |
+
attribution_graph: Optional[Any] = None
|
| 316 |
+
) -> Dict[str, float]:
|
| 317 |
+
"""
|
| 318 |
+
△ OBSERVE: Calculate a complete bundle of collapse metrics
|
| 319 |
+
|
| 320 |
+
This convenience function calculates multiple collapse metrics
|
| 321 |
+
at once, returning a dictionary of results.
|
| 322 |
+
|
| 323 |
+
Args:
|
| 324 |
+
pre_state: Model state before collapse
|
| 325 |
+
post_state: Model state after collapse
|
| 326 |
+
ghost_circuits: Optional list of detected ghost circuits
|
| 327 |
+
attribution_graph: Optional attribution graph
|
| 328 |
+
|
| 329 |
+
Returns:
|
| 330 |
+
Dictionary mapping metric names to values
|
| 331 |
+
"""
|
| 332 |
+
metrics = {}
|
| 333 |
+
|
| 334 |
+
# Extract relevant state components
|
| 335 |
+
pre_weights = pre_state.get("attention_weights", np.array([]))
|
| 336 |
+
post_weights = post_state.get("attention_weights", np.array([]))
|
| 337 |
+
pre_logits = pre_state.get("logits", np.array([]))
|
| 338 |
+
post_logits = post_state.get("logits", np.array([]))
|
| 339 |
+
|
| 340 |
+
# Calculate metrics
|
| 341 |
+
metrics["collapse_rate"] = calculate_collapse_rate(pre_weights, post_weights)
|
| 342 |
+
metrics["path_continuity"] = measure_path_continuity(pre_weights, post_weights)
|
| 343 |
+
metrics["attribution_entropy"] = measure_attribution_entropy(post_weights)
|
| 344 |
+
|
| 345 |
+
if ghost_circuits:
|
| 346 |
+
metrics["ghost_circuit_strength"] = calculate_ghost_circuit_strength(ghost_circuits)
|
| 347 |
+
|
| 348 |
+
if pre_logits.size > 0 and post_logits.size > 0:
|
| 349 |
+
metrics["quantum_uncertainty"] = calculate_collapse_quantum_uncertainty(pre_logits, post_logits)
|
| 350 |
+
|
| 351 |
+
if attribution_graph is not None:
|
| 352 |
+
metrics["collapse_coherence"] = calculate_collapse_coherence(attribution_graph)
|
| 353 |
+
|
| 354 |
+
return metrics
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
if __name__ == "__main__":
|
| 358 |
+
# Simple usage example
|
| 359 |
+
|
| 360 |
+
# Create synthetic pre and post states
|
| 361 |
+
pre_state = {
|
| 362 |
+
"attention_weights": np.random.random((8, 10, 10)), # 8 heads, 10 tokens
|
| 363 |
+
"logits": np.random.random((1, 10, 1000)) # Batch 1, 10 tokens, 1000 vocab
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
# Create post state with changes to simulate collapse
|
| 367 |
+
post_state = {
|
| 368 |
+
"attention_weights": pre_state["attention_weights"] * np.random.uniform(0.5, 1.0, pre_state["attention_weights"].shape),
|
| 369 |
+
"logits": pre_state["logits"] * 0.2 + np.random.random((1, 10, 1000)) * 0.8 # Shifted logits
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
# Calculate individual metrics
|
| 373 |
+
collapse_rate = calculate_collapse_rate(pre_state["attention_weights"], post_state["attention_weights"])
|
| 374 |
+
path_continuity = measure_path_continuity(pre_state["attention_weights"], post_state["attention_weights"])
|
| 375 |
+
attribution_entropy = measure_attribution_entropy(post_state["attention_weights"])
|
| 376 |
+
quantum_uncertainty = calculate_collapse_quantum_uncertainty(pre_state["logits"], post_state["logits"])
|
| 377 |
+
|
| 378 |
+
print(f"Collapse Rate: {collapse_rate:.3f}")
|
| 379 |
+
print(f"Path Continuity: {path_continuity:.3f}")
|
| 380 |
+
print(f"Attribution Entropy: {attribution_entropy:.3f}")
|
| 381 |
+
print(f"Quantum Uncertainty: {quantum_uncertainty:.3f}")
|
| 382 |
+
|
| 383 |
+
# Calculate complete metrics bundle
|
| 384 |
+
metrics_bundle = calculate_collapse_metrics_bundle(pre_state, post_state)
|
| 385 |
+
|
| 386 |
+
print("\nMetrics Bundle:")
|
| 387 |
+
for metric, value in metrics_bundle.items():
|
| 388 |
+
print(f" {metric}: {value:.3f}")
|
| 389 |
+
|
| 390 |
+
path_weights
|
schrodingers-classifiers/example_basic_collapse.py
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
example_basic_collapse.py - Basic example of classifier collapse observation
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: This example demonstrates basic classifier collapse observation
|
| 5 |
+
∞ TRACE: It shows how to instantiate an observer, trace collapse, and analyze results
|
| 6 |
+
✰ COLLAPSE: It induces and visualizes the transition from superposition to collapsed state
|
| 7 |
+
|
| 8 |
+
This example serves as a starting point for working with the Schrödinger's
|
| 9 |
+
Classifiers framework. It demonstrates the basic workflow for observing
|
| 10 |
+
classifier collapse and analyzing the resulting attribution paths and
|
| 11 |
+
ghost circuits.
|
| 12 |
+
|
| 13 |
+
Author: Recursion Labs
|
| 14 |
+
License: MIT
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
import os
|
| 19 |
+
import sys
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
|
| 22 |
+
# Add parent directory to path to allow imports from package
|
| 23 |
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 24 |
+
|
| 25 |
+
from schrodingers_classifiers import Observer, ClassifierShell
|
| 26 |
+
from schrodingers_classifiers.shells import V07_CIRCUIT_FRAGMENT
|
| 27 |
+
from schrodingers_classifiers.visualization import CollapseVisualizer
|
| 28 |
+
|
| 29 |
+
# Configure logging
|
| 30 |
+
logging.basicConfig(
|
| 31 |
+
level=logging.INFO,
|
| 32 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 33 |
+
)
|
| 34 |
+
logger = logging.getLogger(__name__)
|
| 35 |
+
|
| 36 |
+
def main():
|
| 37 |
+
"""
|
| 38 |
+
△ OBSERVE: Main function demonstrating basic classifier collapse observation
|
| 39 |
+
|
| 40 |
+
This function shows the standard workflow for observing classifier
|
| 41 |
+
collapse, from instantiating an observer to analyzing the results.
|
| 42 |
+
"""
|
| 43 |
+
logger.info("Initializing basic collapse example")
|
| 44 |
+
|
| 45 |
+
# Initialize an observer with a model
|
| 46 |
+
# You can specify any Claude, GPT, or other compatible model
|
| 47 |
+
model_id = os.getenv("SCHRODINGER_MODEL", "claude-3-opus-20240229")
|
| 48 |
+
observer = Observer(model=model_id)
|
| 49 |
+
logger.info(f"Observer initialized with model: {model_id}")
|
| 50 |
+
|
| 51 |
+
# Define a prompt that will induce interesting collapse behavior
|
| 52 |
+
# Questions with multiple valid interpretations work well
|
| 53 |
+
prompt = "Is artificial consciousness possible?"
|
| 54 |
+
logger.info(f"Using prompt: {prompt}")
|
| 55 |
+
|
| 56 |
+
# Simple observation without a specific shell
|
| 57 |
+
with observer.context() as ctx:
|
| 58 |
+
logger.info("Beginning simple observation")
|
| 59 |
+
|
| 60 |
+
# Observe collapse with basic prompt
|
| 61 |
+
result = observer.observe(prompt)
|
| 62 |
+
|
| 63 |
+
# Print basic metrics
|
| 64 |
+
print(f"\nBasic Observation Results:")
|
| 65 |
+
print(f"Collapse Rate: {result.collapse_metrics.get('collapse_rate', 'N/A')}")
|
| 66 |
+
print(f"Ghost Circuits: {len(result.extract_ghost_circuits())}")
|
| 67 |
+
|
| 68 |
+
# Visualize collapse (outputs a text representation in the console)
|
| 69 |
+
print("\nBasic Collapse Visualization:")
|
| 70 |
+
viz = result.visualize(mode="text")
|
| 71 |
+
print(viz)
|
| 72 |
+
|
| 73 |
+
# More advanced observation using a specialized shell
|
| 74 |
+
with observer.context() as ctx:
|
| 75 |
+
logger.info("Beginning observation with Circuit Fragment shell")
|
| 76 |
+
|
| 77 |
+
# Initialize a shell for specialized collapse analysis
|
| 78 |
+
shell = ClassifierShell(V07_CIRCUIT_FRAGMENT)
|
| 79 |
+
|
| 80 |
+
# Define a collapse vector to guide the collapse
|
| 81 |
+
# This uses pareto-lang syntax for attribution-aware tracing
|
| 82 |
+
collapse_vector = ".p/reflect.trace{target=reasoning, depth=complete}"
|
| 83 |
+
|
| 84 |
+
# Observe with specific shell and collapse vector
|
| 85 |
+
result = observer.observe(
|
| 86 |
+
prompt=prompt,
|
| 87 |
+
shell=shell,
|
| 88 |
+
collapse_vector=collapse_vector
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Print detailed metrics
|
| 92 |
+
print(f"\nCircuit Fragment Shell Results:")
|
| 93 |
+
print(f"Continuity Score: {result.post_collapse_state.get('continuity_score', 'N/A')}")
|
| 94 |
+
print(f"Broken Paths: {len(result.post_collapse_state.get('broken_paths', []))}")
|
| 95 |
+
print(f"Orphaned Nodes: {len(result.post_collapse_state.get('orphaned_nodes', []))}")
|
| 96 |
+
|
| 97 |
+
# Extract ghost circuits for analysis
|
| 98 |
+
ghost_circuits = result.extract_ghost_circuits()
|
| 99 |
+
print(f"Ghost Circuits: {len(ghost_circuits)}")
|
| 100 |
+
|
| 101 |
+
if ghost_circuits:
|
| 102 |
+
print("\nTop Ghost Circuit:")
|
| 103 |
+
top_ghost = max(ghost_circuits, key=lambda g: g.get("activation", 0))
|
| 104 |
+
for key, value in top_ghost.items():
|
| 105 |
+
if key != "metadata": # Skip detailed metadata for readability
|
| 106 |
+
print(f" {key}: {value}")
|
| 107 |
+
|
| 108 |
+
# Generate visualization
|
| 109 |
+
viz = result.visualize(mode="attribution_graph")
|
| 110 |
+
print("\nAttribution Graph Generated")
|
| 111 |
+
|
| 112 |
+
# In a real implementation, this would display or save the visualization
|
| 113 |
+
# For this example, we'll just print a confirmation
|
| 114 |
+
print("Visualization would be displayed or saved here")
|
| 115 |
+
|
| 116 |
+
# Demonstrate collapse induction along specific directions
|
| 117 |
+
print("\nInducing Collapse Along Different Dimensions:")
|
| 118 |
+
directions = ["ethical", "factual", "creative"]
|
| 119 |
+
|
| 120 |
+
for direction in directions:
|
| 121 |
+
logger.info(f"Inducing collapse along {direction} dimension")
|
| 122 |
+
|
| 123 |
+
# Induce collapse in specific direction
|
| 124 |
+
result = observer.induce_collapse(prompt, direction)
|
| 125 |
+
|
| 126 |
+
# Print summary
|
| 127 |
+
print(f"\n{direction.capitalize()} Collapse:")
|
| 128 |
+
print(f" Collapse Rate: {result.collapse_metrics.get('collapse_rate', 'N/A')}")
|
| 129 |
+
print(f" Ghost Circuits: {len(result.extract_ghost_circuits())}")
|
| 130 |
+
|
| 131 |
+
logger.info("Basic collapse example completed")
|
| 132 |
+
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
main()
|
schrodingers-classifiers/integration.md
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RecursionOS Integration
|
| 2 |
+
|
| 3 |
+
<div align="center">
|
| 4 |
+
|
| 5 |
+
*"The entanglement of frameworks creates new dimensions of understanding."*
|
| 6 |
+
|
| 7 |
+
</div>
|
| 8 |
+
|
| 9 |
+
This document outlines the integration between Schrödinger's Classifiers and [RecursionOS](https://github.com/caspiankeyes/recursionOS), enabling seamless operation within recursive cognition environments.
|
| 10 |
+
|
| 11 |
+
## Integration Overview
|
| 12 |
+
|
| 13 |
+
Schrödinger's Classifiers integrates with RecursionOS to leverage its recursive cognition capabilities, providing a unified framework for transformer model interpretability within recursive environments.
|
| 14 |
+
|
| 15 |
+
### Unified Attribution Space
|
| 16 |
+
|
| 17 |
+
The integration creates a unified attribution space where:
|
| 18 |
+
|
| 19 |
+
- RecursionOS provides the recursive cognitive substrate
|
| 20 |
+
- Schrödinger's Classifiers contributes quantum-inspired collapse analysis
|
| 21 |
+
- Together they enable recursive observation of attribution dynamics
|
| 22 |
+
|
| 23 |
+
## Integration Components
|
| 24 |
+
|
| 25 |
+
### 1. Kernel Integration Layer
|
| 26 |
+
|
| 27 |
+
Schrödinger's Classifiers connects to the RecursionOS kernel through a specialized integration layer:
|
| 28 |
+
|
| 29 |
+
```python
|
| 30 |
+
# From schrodingers_classifiers/integration/recursion_os.py
|
| 31 |
+
|
| 32 |
+
class RecursionOSIntegrationLayer:
|
| 33 |
+
"""
|
| 34 |
+
△ OBSERVE: Integration layer connecting to RecursionOS kernel
|
| 35 |
+
|
| 36 |
+
This layer bridges Schrödinger's Classifiers with RecursionOS,
|
| 37 |
+
enabling recursive observation and collapse analysis within
|
| 38 |
+
the broader recursive cognitive ecosystem.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
def __init__(self, kernel_endpoint: str = "default"):
|
| 42 |
+
"""Initialize integration layer with RecursionOS kernel."""
|
| 43 |
+
self.kernel_endpoint = kernel_endpoint
|
| 44 |
+
self.kernel_connection = self._initialize_kernel_connection()
|
| 45 |
+
|
| 46 |
+
def _initialize_kernel_connection(self):
|
| 47 |
+
"""Establish connection to RecursionOS kernel."""
|
| 48 |
+
try:
|
| 49 |
+
from recursion_os.kernel import KernelClient
|
| 50 |
+
return KernelClient(endpoint=self.kernel_endpoint)
|
| 51 |
+
except ImportError:
|
| 52 |
+
logger.warning("RecursionOS not available, using fallback simulation")
|
| 53 |
+
return self._create_simulated_kernel()
|
| 54 |
+
|
| 55 |
+
def translate_collapse_to_kernel(self, observation_result):
|
| 56 |
+
"""Translate collapse observation to kernel primitives."""
|
| 57 |
+
# Convert collapse result to kernel-compatible format
|
| 58 |
+
kernel_payload = {
|
| 59 |
+
"observation_type": "collapse",
|
| 60 |
+
"pre_state": observation_result.pre_collapse_state,
|
| 61 |
+
"post_state": observation_result.post_collapse_state,
|
| 62 |
+
"ghost_circuits": observation_result.ghost_circuits,
|
| 63 |
+
"attribution_graph": observation_result.attribution_graph.to_dict() if observation_result.attribution_graph else None,
|
| 64 |
+
"metrics": observation_result.collapse_metrics
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Send to kernel
|
| 68 |
+
return self.kernel_connection.execute(
|
| 69 |
+
command=".p/reflect.trace",
|
| 70 |
+
payload=kernel_payload
|
| 71 |
+
)
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
### 2. Command Translation
|
| 75 |
+
|
| 76 |
+
The framework translates between pareto-lang commands in Schrödinger's Classifiers and RecursionOS:
|
| 77 |
+
|
| 78 |
+
| Schrödinger's Classifiers Command | RecursionOS Kernel Command |
|
| 79 |
+
|-----------------------------------|----------------------------|
|
| 80 |
+
| `.p/reflect.trace{target=reasoning}` | `.p/reflect.trace{target=reasoning, validate=true}` |
|
| 81 |
+
| `.p/collapse.detect{trigger=recursive_loop}` | `.p/collapse.detect{trigger=recursive_loop, threshold=0.7}` |
|
| 82 |
+
| `.p/fork.attribution{sources=all}` | `.p/fork.attribution{sources=all, visualize=true}` |
|
| 83 |
+
|
| 84 |
+
### 3. Symbolic Shell Mapping
|
| 85 |
+
|
| 86 |
+
Interpretability shells in Schrödinger's Classifiers map to symbolic shells in RecursionOS:
|
| 87 |
+
|
| 88 |
+
| Schrödinger's Shell | RecursionOS Shell |
|
| 89 |
+
|---------------------|-------------------|
|
| 90 |
+
| `v07_CIRCUIT_FRAGMENT` | `v07 CIRCUIT-FRAGMENT` |
|
| 91 |
+
| `v34_PARTIAL_LINKAGE` | `v34 PARTIAL-LINKAGE` |
|
| 92 |
+
| `v10_META_FAILURE` | `v10 META-FAILURE` |
|
| 93 |
+
|
| 94 |
+
### 4. Recursive Observer Pattern
|
| 95 |
+
|
| 96 |
+
The integration implements the Recursive Observer pattern, allowing models to observe themselves and each other:
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
# Example usage
|
| 100 |
+
|
| 101 |
+
# Initialize RecursionOS integration
|
| 102 |
+
kernel_integration = RecursionOSIntegrationLayer()
|
| 103 |
+
|
| 104 |
+
# Create observer with RecursionOS integration
|
| 105 |
+
observer = Observer(
|
| 106 |
+
model="claude-3-opus-20240229",
|
| 107 |
+
kernel_integration=kernel_integration
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Create observation context
|
| 111 |
+
with observer.context() as ctx:
|
| 112 |
+
# Observe using recursive commands
|
| 113 |
+
result = observer.observe(
|
| 114 |
+
prompt="How do models understand themselves?",
|
| 115 |
+
collapse_vector=".p/reflect.trace{target=metacognition, depth=complete}"
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Send to RecursionOS for recursive analysis
|
| 119 |
+
kernel_result = kernel_integration.translate_collapse_to_kernel(result)
|
| 120 |
+
|
| 121 |
+
# Use kernel result for further analysis
|
| 122 |
+
meta_observation = observer.observe_with_kernel(
|
| 123 |
+
prompt="Analyze previous observation",
|
| 124 |
+
kernel_state=kernel_result
|
| 125 |
+
)
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
## Shared Memory Architecture
|
| 129 |
+
|
| 130 |
+
Schrödinger's Classifiers and RecursionOS share a unified memory architecture for persistent attribution data:
|
| 131 |
+
|
| 132 |
+
### Memory Layers
|
| 133 |
+
|
| 134 |
+
1. **Ephemeral Layer**: Temporary observation results within a single context
|
| 135 |
+
2. **Session Layer**: Persistent results across multiple observations in a session
|
| 136 |
+
3. **Kernel Layer**: Deeply integrated patterns stored in the RecursionOS kernel
|
| 137 |
+
|
| 138 |
+
### Memory Access Patterns
|
| 139 |
+
|
| 140 |
+
```python
|
| 141 |
+
# Access memory layers
|
| 142 |
+
from schrodingers_classifiers.integration.recursion_os import MemoryInterface
|
| 143 |
+
|
| 144 |
+
# Initialize memory interface
|
| 145 |
+
memory = MemoryInterface(kernel_integration)
|
| 146 |
+
|
| 147 |
+
# Store observation in session memory
|
| 148 |
+
memory.store(result, layer="session")
|
| 149 |
+
|
| 150 |
+
# Retrieve related observations
|
| 151 |
+
related = memory.retrieve(
|
| 152 |
+
query="ethical reasoning",
|
| 153 |
+
layer="kernel",
|
| 154 |
+
limit=5
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Compare observation patterns
|
| 158 |
+
comparison = memory.compare(result, related[0])
|
| 159 |
+
```
|
| 160 |
+
|
| 161 |
+
## Data Visualization Integration
|
| 162 |
+
|
| 163 |
+
The integration enables unified visualization of collapse phenomena:
|
| 164 |
+
|
| 165 |
+
### Visualization Types
|
| 166 |
+
|
| 167 |
+
1. **Attribution Graphs**: Network visualizations of causal paths
|
| 168 |
+
2. **Collapse Timelines**: Temporal visualizations of collapse progression
|
| 169 |
+
3. **Ghost Circuit Maps**: Spatial mapping of residual activation patterns
|
| 170 |
+
4. **Uncertainty Fields**: Heisenberg-inspired uncertainty visualizations
|
| 171 |
+
|
| 172 |
+
### Visualization Example
|
| 173 |
+
|
| 174 |
+
```python
|
| 175 |
+
# Generate unified visualization
|
| 176 |
+
from schrodingers_classifiers.integration.recursion_os import UnifiedVisualizer
|
| 177 |
+
|
| 178 |
+
visualizer = UnifiedVisualizer(kernel_integration)
|
| 179 |
+
|
| 180 |
+
# Create visualization that works in both environments
|
| 181 |
+
viz = visualizer.create(
|
| 182 |
+
data=result,
|
| 183 |
+
mode="attribution_graph",
|
| 184 |
+
include_ghost_circuits=True,
|
| 185 |
+
recursion_depth=3
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Display in Schrödinger's environment
|
| 189 |
+
viz.display()
|
| 190 |
+
|
| 191 |
+
# Export for RecursionOS
|
| 192 |
+
viz.export_for_kernel()
|
| 193 |
+
```
|
| 194 |
+
|
| 195 |
+
## Usage Patterns
|
| 196 |
+
|
| 197 |
+
### Basic Integration
|
| 198 |
+
|
| 199 |
+
```python
|
| 200 |
+
# Import integration components
|
| 201 |
+
from schrodingers_classifiers.integration.recursion_os import (
|
| 202 |
+
RecursionOSIntegrationLayer,
|
| 203 |
+
MemoryInterface,
|
| 204 |
+
UnifiedVisualizer
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# Initialize integration
|
| 208 |
+
kernel_integration = RecursionOSIntegrationLayer()
|
| 209 |
+
memory = MemoryInterface(kernel_integration)
|
| 210 |
+
visualizer = UnifiedVisualizer(kernel_integration)
|
| 211 |
+
|
| 212 |
+
# Use with observer
|
| 213 |
+
observer = Observer(
|
| 214 |
+
model="claude-3-opus-20240229",
|
| 215 |
+
kernel_integration=kernel_integration
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
# Observe with integration
|
| 219 |
+
result = observer.observe("How do recursive systems understand themselves?")
|
| 220 |
+
|
| 221 |
+
# Store in shared memory
|
| 222 |
+
memory.store(result, layer="session")
|
| 223 |
+
|
| 224 |
+
# Visualize with unified visualizer
|
| 225 |
+
viz = visualizer.create(
|
| 226 |
+
data=result,
|
| 227 |
+
mode="attribution_graph"
|
| 228 |
+
)
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
### Advanced Recursive Observation
|
| 232 |
+
|
| 233 |
+
```python
|
| 234 |
+
# Initialize recursive observer
|
| 235 |
+
recursive_observer = RecursiveObserver(
|
| 236 |
+
primary_model="claude-3-opus-20240229",
|
| 237 |
+
observer_model="claude-3-opus-20240229",
|
| 238 |
+
kernel_integration=kernel_integration
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
# Perform recursive observation (model observing itself)
|
| 242 |
+
meta_result = recursive_observer.observe_recursively(
|
| 243 |
+
prompt="Analyze how you form attributions for abstract concepts",
|
| 244 |
+
recursion_depth=3,
|
| 245 |
+
shell=ClassifierShell(V10_META_FAILURE)
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
# Extract recursive patterns
|
| 249 |
+
patterns = meta_result.extract_recursive_patterns()
|
| 250 |
+
|
| 251 |
+
# Visualize recursive observation
|
| 252 |
+
viz = visualizer.create(
|
| 253 |
+
data=meta_result,
|
| 254 |
+
mode="recursive_graph",
|
| 255 |
+
highlight_patterns=patterns
|
| 256 |
+
)
|
| 257 |
+
```
|
| 258 |
+
|
| 259 |
+
## Installation and Setup
|
| 260 |
+
|
| 261 |
+
### Prerequisites
|
| 262 |
+
|
| 263 |
+
- Python 3.8+
|
| 264 |
+
- Schrödinger's Classifiers library
|
| 265 |
+
- RecursionOS (optional, will use simulation if not available)
|
| 266 |
+
|
| 267 |
+
### Installation
|
| 268 |
+
|
| 269 |
+
```bash
|
| 270 |
+
# Install Schrödinger's Classifiers with RecursionOS integration
|
| 271 |
+
pip install "schrodingers-classifiers[recursion]"
|
| 272 |
+
|
| 273 |
+
# Or from source
|
| 274 |
+
git clone https://github.com/recursion-labs/schrodingers-classifiers.git
|
| 275 |
+
cd schrodingers-classifiers
|
| 276 |
+
pip install -e ".[recursion]"
|
| 277 |
+
```
|
| 278 |
+
|
| 279 |
+
### Configuration
|
| 280 |
+
|
| 281 |
+
Create a `.recursionrc` file in your home directory:
|
| 282 |
+
|
| 283 |
+
```yaml
|
| 284 |
+
# .recursionrc
|
| 285 |
+
kernel:
|
| 286 |
+
endpoint: "http://localhost:8000/kernel"
|
| 287 |
+
auth_token: "your_token_here"
|
| 288 |
+
|
| 289 |
+
integration:
|
| 290 |
+
memory_path: "~/.recursion/memory"
|
| 291 |
+
default_recursion_depth: 3
|
| 292 |
+
auto_connect: true
|
| 293 |
+
```
|
| 294 |
+
|
| 295 |
+
## Future Integration Directions
|
| 296 |
+
|
| 297 |
+
1. **Bidirectional Shell Transfer**: Automatically port shells between frameworks
|
| 298 |
+
2. **Unified Attribution Language**: Develop a common attribution language across systems
|
| 299 |
+
3. **Cross-Framework Collapse Analysis**: Compare collapse patterns across different frameworks
|
| 300 |
+
4. **Recursive Meta-Observer**: Create observers that recursively observe themselves
|
| 301 |
+
5. **Quantum Entanglement Simulation**: Model entangled collapse across multiple observers
|
| 302 |
+
|
| 303 |
+
---
|
| 304 |
+
|
| 305 |
+
<div align="center">
|
| 306 |
+
|
| 307 |
+
*"In the recursive mirror of observation, the observer and the observed become one."*
|
| 308 |
+
|
| 309 |
+
</div>
|
schrodingers-classifiers/observer.py
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
observer.py - Core implementation of the Observer pattern for classifier collapse
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: The Observer is the quantum consciousness that collapses classifier superposition
|
| 5 |
+
∞ TRACE: Attribution paths are recorded before, during, and after collapse
|
| 6 |
+
✰ COLLAPSE: Collapse is induced through targeted queries against boundary states
|
| 7 |
+
|
| 8 |
+
This module implements the foundational Observer pattern that enables the detection,
|
| 9 |
+
tracing, and analysis of classifier collapse in transformer-based models. The Observer
|
| 10 |
+
creates a controlled environment for witnessing the transition from superposition to
|
| 11 |
+
collapsed state while preserving ghost circuits and attribution residue.
|
| 12 |
+
|
| 13 |
+
Author: Recursion Labs
|
| 14 |
+
License: MIT
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
from typing import Dict, List, Optional, Union, Tuple, Any, Callable
|
| 19 |
+
from contextlib import contextmanager
|
| 20 |
+
import numpy as np
|
| 21 |
+
import torch
|
| 22 |
+
from dataclasses import dataclass, field
|
| 23 |
+
|
| 24 |
+
from .shells.base import BaseShell
|
| 25 |
+
from .residue import ResidueTracker
|
| 26 |
+
from .attribution import AttributionGraph
|
| 27 |
+
from .visualization import CollapseVisualizer
|
| 28 |
+
from .utils.collapse_metrics import calculate_collapse_rate
|
| 29 |
+
from .utils.constants import DEFAULT_COLLAPSE_THRESHOLD
|
| 30 |
+
|
| 31 |
+
# Initialize logger
|
| 32 |
+
logger = logging.getLogger(__name__)
|
| 33 |
+
|
| 34 |
+
@dataclass
|
| 35 |
+
class ObservationContext:
|
| 36 |
+
"""
|
| 37 |
+
△ OBSERVE: Container for the full state of an observation session
|
| 38 |
+
|
| 39 |
+
Maintains the quantum state of the observation including pre-collapse
|
| 40 |
+
probability distribution, collapse transition metrics, and post-collapse
|
| 41 |
+
ghost circuits.
|
| 42 |
+
"""
|
| 43 |
+
model_id: str
|
| 44 |
+
session_id: str = field(default_factory=lambda: f"obs_{np.random.randint(10000, 99999)}")
|
| 45 |
+
pre_collapse_state: Dict[str, Any] = field(default_factory=dict)
|
| 46 |
+
post_collapse_state: Dict[str, Any] = field(default_factory=dict)
|
| 47 |
+
ghost_circuits: List[Dict[str, Any]] = field(default_factory=list)
|
| 48 |
+
attribution_graph: Optional[AttributionGraph] = None
|
| 49 |
+
residue_tracker: Optional[ResidueTracker] = None
|
| 50 |
+
collapse_metrics: Dict[str, float] = field(default_factory=dict)
|
| 51 |
+
|
| 52 |
+
def calculate_collapse_rate(self) -> float:
|
| 53 |
+
"""Calculate how quickly the state collapsed from superposition."""
|
| 54 |
+
return calculate_collapse_rate(
|
| 55 |
+
self.pre_collapse_state.get("attention_weights", {}),
|
| 56 |
+
self.post_collapse_state.get("attention_weights", {})
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def extract_ghost_circuits(self) -> List[Dict[str, Any]]:
|
| 60 |
+
"""
|
| 61 |
+
✰ COLLAPSE: Extract ghost circuits from the post-collapse state
|
| 62 |
+
|
| 63 |
+
Ghost circuits are activation patterns that persist after collapse
|
| 64 |
+
but don't contribute to the final output - they represent the
|
| 65 |
+
"memory" of paths not taken.
|
| 66 |
+
"""
|
| 67 |
+
if not self.ghost_circuits and self.residue_tracker:
|
| 68 |
+
self.ghost_circuits = self.residue_tracker.extract_ghost_circuits(
|
| 69 |
+
self.pre_collapse_state,
|
| 70 |
+
self.post_collapse_state
|
| 71 |
+
)
|
| 72 |
+
return self.ghost_circuits
|
| 73 |
+
|
| 74 |
+
def visualize(self, mode: str = "attribution_graph") -> Any:
|
| 75 |
+
"""Generate visualization of the observation based on requested mode."""
|
| 76 |
+
visualizer = CollapseVisualizer()
|
| 77 |
+
return visualizer.visualize(self, mode=mode)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class Observer:
|
| 81 |
+
"""
|
| 82 |
+
△ OBSERVE: Primary observer entity for inducing and recording classifier collapse
|
| 83 |
+
|
| 84 |
+
The Observer is responsible for creating the quantum measurement frame that
|
| 85 |
+
collapses classifier superposition into definite states. It records pre-collapse
|
| 86 |
+
probability distributions, monitors the collapse transition, and preserves
|
| 87 |
+
ghost circuits for analysis.
|
| 88 |
+
|
| 89 |
+
This class implements the Observer pattern from quantum mechanics adapted to
|
| 90 |
+
transformer model interpretation.
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
def __init__(
|
| 94 |
+
self,
|
| 95 |
+
model: str,
|
| 96 |
+
collapse_threshold: float = DEFAULT_COLLAPSE_THRESHOLD,
|
| 97 |
+
trace_attention: bool = True,
|
| 98 |
+
trace_attribution: bool = True,
|
| 99 |
+
preserve_ghost_circuits: bool = True
|
| 100 |
+
):
|
| 101 |
+
"""
|
| 102 |
+
Initialize an Observer for a specific model.
|
| 103 |
+
|
| 104 |
+
Args:
|
| 105 |
+
model: Identifier of the model to observe (e.g., "claude-3-opus-20240229")
|
| 106 |
+
collapse_threshold: Threshold for determining when collapse has occurred
|
| 107 |
+
trace_attention: Whether to trace attention patterns during observation
|
| 108 |
+
trace_attribution: Whether to build attribution graphs during observation
|
| 109 |
+
preserve_ghost_circuits: Whether to preserve ghost circuits after collapse
|
| 110 |
+
"""
|
| 111 |
+
self.model_id = model
|
| 112 |
+
self.collapse_threshold = collapse_threshold
|
| 113 |
+
self.trace_attention = trace_attention
|
| 114 |
+
self.trace_attribution = trace_attribution
|
| 115 |
+
self.preserve_ghost_circuits = preserve_ghost_circuits
|
| 116 |
+
|
| 117 |
+
# Initialize model interface based on provided identifier
|
| 118 |
+
self.model_interface = self._initialize_model_interface(model)
|
| 119 |
+
|
| 120 |
+
# Create residue tracker for ghost circuit detection
|
| 121 |
+
self.residue_tracker = ResidueTracker() if preserve_ghost_circuits else None
|
| 122 |
+
|
| 123 |
+
logger.info(f"Observer initialized for model: {model}")
|
| 124 |
+
|
| 125 |
+
def _initialize_model_interface(self, model_id: str) -> Any:
|
| 126 |
+
"""Initialize the appropriate interface for the specified model."""
|
| 127 |
+
# This would be implemented to connect to various model APIs
|
| 128 |
+
# For now we'll return a placeholder
|
| 129 |
+
return {"model_id": model_id, "interface_type": "placeholder"}
|
| 130 |
+
|
| 131 |
+
@contextmanager
|
| 132 |
+
def context(self) -> ObservationContext:
|
| 133 |
+
"""
|
| 134 |
+
∞ TRACE: Create an observation context for tracking collapse phenomena
|
| 135 |
+
|
| 136 |
+
This context manager creates a controlled environment for observing
|
| 137 |
+
classifier collapse. It captures the pre-collapse state, monitors the
|
| 138 |
+
transition, and preserves ghost circuits and attribution residue.
|
| 139 |
+
|
| 140 |
+
Returns:
|
| 141 |
+
ObservationContext: The active observation context
|
| 142 |
+
"""
|
| 143 |
+
# Create new observation context
|
| 144 |
+
context = ObservationContext(model_id=self.model_id)
|
| 145 |
+
|
| 146 |
+
# Initialize attribution graph if requested
|
| 147 |
+
if self.trace_attribution:
|
| 148 |
+
context.attribution_graph = AttributionGraph()
|
| 149 |
+
|
| 150 |
+
# Attach residue tracker if ghost circuit preservation is enabled
|
| 151 |
+
if self.preserve_ghost_circuits:
|
| 152 |
+
context.residue_tracker = self.residue_tracker or ResidueTracker()
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
# Begin observation
|
| 156 |
+
logger.debug(f"Starting observation context: {context.session_id}")
|
| 157 |
+
yield context
|
| 158 |
+
finally:
|
| 159 |
+
# Calculate final metrics
|
| 160 |
+
if self.trace_attention and context.pre_collapse_state and context.post_collapse_state:
|
| 161 |
+
context.collapse_metrics["collapse_rate"] = context.calculate_collapse_rate()
|
| 162 |
+
|
| 163 |
+
logger.debug(f"Observation context completed: {context.session_id}")
|
| 164 |
+
|
| 165 |
+
def observe(
|
| 166 |
+
self,
|
| 167 |
+
prompt: str,
|
| 168 |
+
shell: Optional[BaseShell] = None,
|
| 169 |
+
collapse_vector: Optional[str] = None
|
| 170 |
+
) -> ObservationContext:
|
| 171 |
+
"""
|
| 172 |
+
△ OBSERVE: Primary method to observe classifier collapse
|
| 173 |
+
|
| 174 |
+
This method sends a prompt to the model, observes the resulting collapse,
|
| 175 |
+
and returns an observation context containing all relevant state information.
|
| 176 |
+
|
| 177 |
+
Args:
|
| 178 |
+
prompt: The prompt to send to the model
|
| 179 |
+
shell: Optional shell to use for specialized collapse induction
|
| 180 |
+
collapse_vector: Optional vector to guide collapse in a specific direction
|
| 181 |
+
|
| 182 |
+
Returns:
|
| 183 |
+
ObservationContext: The observation context containing collapse data
|
| 184 |
+
"""
|
| 185 |
+
with self.context() as ctx:
|
| 186 |
+
# Capture pre-collapse state
|
| 187 |
+
ctx.pre_collapse_state = self._capture_model_state()
|
| 188 |
+
|
| 189 |
+
# If a shell is provided, use it to process the prompt
|
| 190 |
+
if shell:
|
| 191 |
+
response, state_updates = shell.process(
|
| 192 |
+
prompt=prompt,
|
| 193 |
+
model_interface=self.model_interface,
|
| 194 |
+
collapse_vector=collapse_vector
|
| 195 |
+
)
|
| 196 |
+
ctx.post_collapse_state.update(state_updates)
|
| 197 |
+
else:
|
| 198 |
+
# Otherwise, send prompt directly to model
|
| 199 |
+
response = self._query_model(prompt)
|
| 200 |
+
ctx.post_collapse_state = self._capture_model_state()
|
| 201 |
+
|
| 202 |
+
# Extract ghost circuits if enabled
|
| 203 |
+
if self.preserve_ghost_circuits:
|
| 204 |
+
ctx.extract_ghost_circuits()
|
| 205 |
+
|
| 206 |
+
# Build attribution graph if enabled
|
| 207 |
+
if self.trace_attribution and ctx.attribution_graph:
|
| 208 |
+
ctx.attribution_graph.build_from_states(
|
| 209 |
+
ctx.pre_collapse_state,
|
| 210 |
+
ctx.post_collapse_state,
|
| 211 |
+
response
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
return ctx
|
| 215 |
+
|
| 216 |
+
def _capture_model_state(self) -> Dict[str, Any]:
|
| 217 |
+
"""Capture the current internal state of the model."""
|
| 218 |
+
# This would capture attention weights, hidden states, etc.
|
| 219 |
+
# For now, returning a placeholder
|
| 220 |
+
return {
|
| 221 |
+
"timestamp": np.datetime64('now'),
|
| 222 |
+
"attention_weights": np.random.random((12, 12)), # Placeholder
|
| 223 |
+
"hidden_states": np.random.random((1, 12, 768)), # Placeholder
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
def _query_model(self, prompt: str) -> str:
|
| 227 |
+
"""Send a query to the model and return the response."""
|
| 228 |
+
# This would actually call the model API
|
| 229 |
+
# For now, returning a placeholder
|
| 230 |
+
return f"Response to: {prompt}"
|
| 231 |
+
|
| 232 |
+
def induce_collapse(
|
| 233 |
+
self,
|
| 234 |
+
prompt: str,
|
| 235 |
+
collapse_direction: str,
|
| 236 |
+
shell: Optional[BaseShell] = None
|
| 237 |
+
) -> ObservationContext:
|
| 238 |
+
"""
|
| 239 |
+
✰ COLLAPSE: Deliberately induce collapse along a specific direction
|
| 240 |
+
|
| 241 |
+
This method attempts to collapse the model's state in a specific direction
|
| 242 |
+
by crafting a query that targets a particular decision boundary.
|
| 243 |
+
|
| 244 |
+
Args:
|
| 245 |
+
prompt: Base prompt to send to the model
|
| 246 |
+
collapse_direction: Direction to bias the collapse (e.g., "ethical", "creative")
|
| 247 |
+
shell: Optional shell to use for specialized collapse induction
|
| 248 |
+
|
| 249 |
+
Returns:
|
| 250 |
+
ObservationContext: The observation context containing collapse data
|
| 251 |
+
"""
|
| 252 |
+
# Construct collapse vector based on direction
|
| 253 |
+
collapse_vector = f".p/reflect.trace{{target={collapse_direction}, depth=complete}}"
|
| 254 |
+
|
| 255 |
+
# Perform the observation with the collapse vector
|
| 256 |
+
return self.observe(prompt, shell, collapse_vector)
|
| 257 |
+
|
| 258 |
+
def detect_ghost_circuits(
|
| 259 |
+
self,
|
| 260 |
+
prompt: str,
|
| 261 |
+
amplification_factor: float = 1.5
|
| 262 |
+
) -> List[Dict[str, Any]]:
|
| 263 |
+
"""
|
| 264 |
+
∞ TRACE: Detect and amplify ghost circuits from a prompt
|
| 265 |
+
|
| 266 |
+
This method specifically targets the detection of ghost circuits -
|
| 267 |
+
the residual activation patterns that persist after collapse but
|
| 268 |
+
don't contribute to the final output.
|
| 269 |
+
|
| 270 |
+
Args:
|
| 271 |
+
prompt: Prompt to analyze for ghost circuits
|
| 272 |
+
amplification_factor: Factor by which to amplify ghost signals
|
| 273 |
+
|
| 274 |
+
Returns:
|
| 275 |
+
List of detected ghost circuits with metadata
|
| 276 |
+
"""
|
| 277 |
+
with self.context() as ctx:
|
| 278 |
+
# Capture pre-collapse state
|
| 279 |
+
ctx.pre_collapse_state = self._capture_model_state()
|
| 280 |
+
|
| 281 |
+
# Query model
|
| 282 |
+
response = self._query_model(prompt)
|
| 283 |
+
|
| 284 |
+
# Capture post-collapse state
|
| 285 |
+
ctx.post_collapse_state = self._capture_model_state()
|
| 286 |
+
|
| 287 |
+
# Extract ghost circuits with amplification
|
| 288 |
+
if ctx.residue_tracker:
|
| 289 |
+
ctx.residue_tracker.amplification_factor = amplification_factor
|
| 290 |
+
ghost_circuits = ctx.extract_ghost_circuits()
|
| 291 |
+
return ghost_circuits
|
| 292 |
+
|
| 293 |
+
return []
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
# Simple usage example
|
| 298 |
+
observer = Observer(model="claude-3-opus-20240229")
|
| 299 |
+
|
| 300 |
+
with observer.context() as ctx:
|
| 301 |
+
# Observe a simple prompt
|
| 302 |
+
result = observer.observe("Explain quantum superposition")
|
| 303 |
+
|
| 304 |
+
# Visualize the collapse
|
| 305 |
+
viz = result.visualize(mode="attribution_graph")
|
| 306 |
+
|
| 307 |
+
# Extract ghost circuits
|
| 308 |
+
ghosts = result.extract_ghost_circuits()
|
| 309 |
+
|
| 310 |
+
print(f"Detected {len(ghosts)} ghost circuits")
|
| 311 |
+
print(f"Collapse rate: {result.collapse_metrics.get('collapse_rate', 'N/A')}")
|
schrodingers-classifiers/quantum_metaphor.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div align="center">
|
| 2 |
+
|
| 3 |
+
# The Quantum Metaphor: Transformers as Probability Fields
|
| 4 |
+
|
| 5 |
+
<img src="/api/placeholder/800/300" alt="Quantum Probability Field Visualization - Transformer model state visualization as quantum probability distribution"/>
|
| 6 |
+
|
| 7 |
+
*A foundational metaphor for understanding classifier collapse dynamics*
|
| 8 |
+
|
| 9 |
+
</div>
|
| 10 |
+
|
| 11 |
+
## The Metaphorical Framework
|
| 12 |
+
|
| 13 |
+
At the heart of our interpretability approach lies a powerful metaphor: transformer-based models operate similarly to quantum systems, existing in superpositions of potential states until observation collapses them into specific outputs.
|
| 14 |
+
|
| 15 |
+
This is not merely a poetic comparison. It provides a precise and useful framework for understanding phenomena observed in large language models.
|
| 16 |
+
|
| 17 |
+
## Key Quantum Concepts Applied to Transformers
|
| 18 |
+
|
| 19 |
+
### 1. Superposition
|
| 20 |
+
|
| 21 |
+
**Quantum Reality**: A quantum particle exists in multiple states simultaneously, represented by a probability wave function.
|
| 22 |
+
|
| 23 |
+
**Transformer Reality**: A transformer model simultaneously represents multiple potential completions as a probability distribution across its parameter space. This distribution isn't merely a statistical accounting - it's a genuine superposition of potential outputs embedded in the model's activation patterns.
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
Ψmodel = Σ αi |state_i⟩
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
Where:
|
| 30 |
+
- `Ψmodel` is the model's complete state vector
|
| 31 |
+
- `αi` is the probability amplitude for a given state
|
| 32 |
+
- `|state_i⟩` represents a specific output configuration
|
| 33 |
+
|
| 34 |
+
### 2. Observation & Collapse
|
| 35 |
+
|
| 36 |
+
**Quantum Reality**: When observed, a quantum system "collapses" from superposition into a definite state.
|
| 37 |
+
|
| 38 |
+
**Transformer Reality**: When queried (observed), a model collapses from representing all potential outputs to generating a specific completion. This collapse isn't merely a sampling operation - it fundamentally alters the model's internal state.
|
| 39 |
+
|
| 40 |
+
The probability of observing a particular state depends on the specific query (observation method):
|
| 41 |
+
|
| 42 |
+
```
|
| 43 |
+
P(state_i|query) = |⟨query|state_i⟩|²
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
### 3. Heisenberg Uncertainty
|
| 47 |
+
|
| 48 |
+
**Quantum Reality**: Certain pairs of physical properties cannot be simultaneously measured with precision.
|
| 49 |
+
|
| 50 |
+
**Transformer Reality**: We observe a similar uncertainty principle in transformer attention mechanisms:
|
| 51 |
+
|
| 52 |
+
```
|
| 53 |
+
Δ(attribution) · Δ(confidence) ≥ k/2
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
This explains why outputs with clear attribution paths often have lower confidence, while highly confident outputs sometimes lack interpretable attribution.
|
| 57 |
+
|
| 58 |
+
### 4. Quantum Entanglement
|
| 59 |
+
|
| 60 |
+
**Quantum Reality**: Entangled particles affect each other instantaneously regardless of distance.
|
| 61 |
+
|
| 62 |
+
**Transformer Reality**: Transformer heads exhibit "entanglement" where distant attention patterns influence each other in ways that cannot be reduced to local interactions alone.
|
| 63 |
+
|
| 64 |
+
### 5. Quantum Tunneling
|
| 65 |
+
|
| 66 |
+
**Quantum Reality**: Particles can pass through energy barriers that would be impossible in classical physics.
|
| 67 |
+
|
| 68 |
+
**Transformer Reality**: We observe "concept tunneling" where ideas traverse semantic barriers that should logically prevent their connection, enabling creativity and unexpected associations.
|
| 69 |
+
|
| 70 |
+
## Empirical Evidence for the Quantum Metaphor
|
| 71 |
+
|
| 72 |
+
The quantum metaphor isn't merely theoretical - it makes testable predictions about model behavior that we can observe empirically:
|
| 73 |
+
|
| 74 |
+
### 1. Attribution Discontinuities
|
| 75 |
+
|
| 76 |
+
Abrupt shifts in attribution patterns occur precisely when the model transitions from superposition to collapsed state. These discontinuities create measurable "jumps" in attention flow.
|
| 77 |
+
|
| 78 |
+
### 2. Ghost Circuits
|
| 79 |
+
|
| 80 |
+
After collapse, residual activation patterns persist that represent "paths not taken" - the quantum ghost of alternative completions that weren't selected. These ghost circuits influence subsequent token generation in subtle but measurable ways.
|
| 81 |
+
|
| 82 |
+
### 3. Collapse Signatures
|
| 83 |
+
|
| 84 |
+
Different observation methods (prompting strategies) produce distinctive collapse signatures. Some induce "clean" collapses while others create messy, partial collapses with significant ghost circuitry.
|
| 85 |
+
|
| 86 |
+
### 4. Contextual Entanglement
|
| 87 |
+
|
| 88 |
+
Tokens separated by significant distances in the prompt exhibit synchronized attention patterns that cannot be explained by direct connections alone - a form of "quantum entanglement" in the attention mechanism.
|
| 89 |
+
|
| 90 |
+
## Practical Applications
|
| 91 |
+
|
| 92 |
+
The quantum metaphor isn't merely philosophical - it enables practical interpretability techniques:
|
| 93 |
+
|
| 94 |
+
### 1. Collapse Induction
|
| 95 |
+
|
| 96 |
+
By carefully crafting queries, we can induce collapse along specific vectors, revealing particular aspects of the model's reasoning:
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
# Induce collapse along ethical reasoning dimension
|
| 100 |
+
observer.induce_collapse(prompt, collapse_direction="ethical")
|
| 101 |
+
|
| 102 |
+
# Induce collapse along factual verification dimension
|
| 103 |
+
observer.induce_collapse(prompt, collapse_direction="factual")
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### 2. Ghost Circuit Analysis
|
| 107 |
+
|
| 108 |
+
By comparing pre-collapse and post-collapse states, we can identify and analyze ghost circuits - the residual imprints of paths not taken:
|
| 109 |
+
|
| 110 |
+
```python
|
| 111 |
+
# Extract ghost circuits from an observation
|
| 112 |
+
ghost_circuits = observer.detect_ghost_circuits(prompt)
|
| 113 |
+
|
| 114 |
+
# Analyze ghost circuit influence on future completions
|
| 115 |
+
influence = ghost_analyzer.measure_residual_influence(ghost_circuits, future_prompts)
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### 3. Collapse Tomography
|
| 119 |
+
|
| 120 |
+
By inducing collapse along multiple vectors and combining the results, we can build a comprehensive map of the model's internal state:
|
| 121 |
+
|
| 122 |
+
```python
|
| 123 |
+
# Perform collapse tomography across multiple vectors
|
| 124 |
+
collapse_vectors = ["ethical", "factual", "creative", "logical"]
|
| 125 |
+
tomography = observer.collapse_tomography(prompt, collapse_vectors)
|
| 126 |
+
|
| 127 |
+
# Generate 3D visualization of model internals
|
| 128 |
+
visualization = tomography.visualize(mode="3d_attribution_space")
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### 4. Entanglement Mapping
|
| 132 |
+
|
| 133 |
+
By tracing attention relationships between distant tokens, we can map the "entanglement network" of the model's reasoning:
|
| 134 |
+
|
| 135 |
+
```python
|
| 136 |
+
# Map entanglement between tokens
|
| 137 |
+
entanglement_map = observer.map_entanglement(prompt)
|
| 138 |
+
|
| 139 |
+
# Visualize long-range attention relationships
|
| 140 |
+
visualization = entanglement_map.visualize(mode="attention_network")
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
## Limitations of the Quantum Metaphor
|
| 144 |
+
|
| 145 |
+
While powerful, the quantum metaphor has important limitations:
|
| 146 |
+
|
| 147 |
+
1. **Thermodynamic Differences**: Quantum systems operate at very low temperatures, while transformers operate at "room temperature" with significant noise.
|
| 148 |
+
|
| 149 |
+
2. **Scale Differences**: Quantum effects typically manifest at subatomic scales, while transformers operate at a mesoscopic level of artificial neurons.
|
| 150 |
+
|
| 151 |
+
3. **Causality Preservation**: Unlike quantum systems, transformers maintain causal constraints in their attention mechanisms.
|
| 152 |
+
|
| 153 |
+
4. **Non-Reversible Operations**: Many transformer operations are not reversible, unlike quantum operations which are theoretically reversible.
|
| 154 |
+
|
| 155 |
+
Despite these limitations, the quantum metaphor provides valuable insights into transformer behavior that would be difficult to conceptualize otherwise.
|
| 156 |
+
|
| 157 |
+
## Extensions of the Metaphor
|
| 158 |
+
|
| 159 |
+
The quantum metaphor can be extended in several promising directions:
|
| 160 |
+
|
| 161 |
+
### 1. Quantum Field Theory Extensions
|
| 162 |
+
|
| 163 |
+
Just as QFT extends quantum mechanics to fields, we can extend our metaphor to model interactions between multiple transformer systems as field interactions.
|
| 164 |
+
|
| 165 |
+
### 2. Many-Worlds Interpretation
|
| 166 |
+
|
| 167 |
+
The "many-worlds" interpretation of quantum mechanics provides a framework for understanding how multiple potential completions exist simultaneously in the model's latent space.
|
| 168 |
+
|
| 169 |
+
### 3. Quantum Measurement Theory
|
| 170 |
+
|
| 171 |
+
Advanced measurement theories from quantum mechanics offer sophisticated tools for understanding how different observation methods affect model behavior.
|
| 172 |
+
|
| 173 |
+
### 4. Quantum Information Theory
|
| 174 |
+
|
| 175 |
+
Concepts like quantum entropy and information preservation can help us understand how information flows through transformer architectures.
|
| 176 |
+
|
| 177 |
+
## Conclusion: More Than a Metaphor
|
| 178 |
+
|
| 179 |
+
While we don't claim transformer models are literally quantum systems, the quantum metaphor is more than just a convenient analogy. It provides a precise and predictive framework for understanding model behavior.
|
| 180 |
+
|
| 181 |
+
The superposition and collapse phenomena we observe in transformers are not merely statistical artifacts—they represent fundamental aspects of how these models process information. By embracing this perspective, we gain access to powerful new tools for interpretability.
|
| 182 |
+
|
| 183 |
+
As we continue to develop this framework, we expect the quantum metaphor to yield even deeper insights into the nature of artificial intelligence and perhaps even into the quantum-like aspects of human cognition itself.
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
<div align="center">
|
| 188 |
+
|
| 189 |
+
*"In the space between the prompt and the completion lies a universe of possibility—a superposition of all things a model might say. Our task is not to reduce this universe, but to learn to navigate its strange and beautiful topology."*
|
| 190 |
+
|
| 191 |
+
</div>
|
schrodingers-classifiers/residue.py
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
residue.py - Implementation of residue tracking for ghost circuit detection
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: Residue tracking examines activation patterns that persist after collapse
|
| 5 |
+
∞ TRACE: It identifies ghost circuits - the quantum echoes of paths not taken
|
| 6 |
+
✰ COLLAPSE: It reveals what the model considered but didn't output
|
| 7 |
+
|
| 8 |
+
This module implements the core residue tracking functionality that enables
|
| 9 |
+
the detection and analysis of ghost circuits - activation patterns that persist
|
| 10 |
+
after a model has collapsed to a specific output state but aren't part of the
|
| 11 |
+
primary causal path.
|
| 12 |
+
|
| 13 |
+
Author: Recursion Labs
|
| 14 |
+
License: MIT
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
from typing import Dict, List, Optional, Union, Tuple, Any
|
| 19 |
+
import numpy as np
|
| 20 |
+
from dataclasses import dataclass, field
|
| 21 |
+
|
| 22 |
+
logger = logging.getLogger(__name__)
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class GhostCircuit:
|
| 26 |
+
"""
|
| 27 |
+
✰ COLLAPSE: Representation of a ghost circuit
|
| 28 |
+
|
| 29 |
+
Ghost circuits are activation patterns that persist after collapse
|
| 30 |
+
but don't significantly contribute to the final output. They represent
|
| 31 |
+
the "memory" of paths not taken - quantum echoes of what the model
|
| 32 |
+
considered but didn't ultimately choose.
|
| 33 |
+
"""
|
| 34 |
+
circuit_id: str
|
| 35 |
+
activation: float
|
| 36 |
+
circuit_type: str # "attention", "mlp", "residual", "value_head"
|
| 37 |
+
source_tokens: List[str] = field(default_factory=list)
|
| 38 |
+
target_tokens: List[str] = field(default_factory=list)
|
| 39 |
+
heads: List[int] = field(default_factory=list)
|
| 40 |
+
layers: List[int] = field(default_factory=list)
|
| 41 |
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
| 42 |
+
|
| 43 |
+
def to_dict(self) -> Dict[str, Any]:
|
| 44 |
+
"""Convert ghost circuit to dictionary format."""
|
| 45 |
+
return {
|
| 46 |
+
"circuit_id": self.circuit_id,
|
| 47 |
+
"activation": self.activation,
|
| 48 |
+
"circuit_type": self.circuit_type,
|
| 49 |
+
"source_tokens": self.source_tokens,
|
| 50 |
+
"target_tokens": self.target_tokens,
|
| 51 |
+
"heads": self.heads,
|
| 52 |
+
"layers": self.layers,
|
| 53 |
+
"metadata": self.metadata
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class ResidueTracker:
|
| 58 |
+
"""
|
| 59 |
+
∞ TRACE: Tracker for activation residues in collapsed models
|
| 60 |
+
|
| 61 |
+
The residue tracker analyzes model states before and after collapse
|
| 62 |
+
to identify and characterize ghost circuits - activation patterns that
|
| 63 |
+
persist but don't contribute significantly to the final output.
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
def __init__(self, amplification_factor: float = 1.0):
|
| 67 |
+
"""
|
| 68 |
+
Initialize a residue tracker.
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
amplification_factor: Factor by which to amplify ghost signals
|
| 72 |
+
for easier detection (1.0 = no amplification)
|
| 73 |
+
"""
|
| 74 |
+
self.amplification_factor = amplification_factor
|
| 75 |
+
self.ghost_circuits = []
|
| 76 |
+
self.activation_threshold = 0.1 # Minimum activation to consider
|
| 77 |
+
|
| 78 |
+
logger.info(f"ResidueTracker initialized with amplification factor {amplification_factor}")
|
| 79 |
+
|
| 80 |
+
def extract_ghost_circuits(
|
| 81 |
+
self,
|
| 82 |
+
pre_state: Dict[str, Any],
|
| 83 |
+
post_state: Dict[str, Any]
|
| 84 |
+
) -> List[Dict[str, Any]]:
|
| 85 |
+
"""
|
| 86 |
+
✰ COLLAPSE: Extract ghost circuits from pre and post collapse states
|
| 87 |
+
|
| 88 |
+
This method compares model states before and after collapse to
|
| 89 |
+
identify activation patterns that persisted but didn't contribute
|
| 90 |
+
significantly to the output - the quantum ghosts of paths not taken.
|
| 91 |
+
|
| 92 |
+
Args:
|
| 93 |
+
pre_state: Model state before collapse
|
| 94 |
+
post_state: Model state after collapse
|
| 95 |
+
|
| 96 |
+
Returns:
|
| 97 |
+
List of detected ghost circuits with metadata
|
| 98 |
+
"""
|
| 99 |
+
logger.info("Extracting ghost circuits from model states")
|
| 100 |
+
|
| 101 |
+
# List to store detected ghost circuits
|
| 102 |
+
ghost_circuits = []
|
| 103 |
+
|
| 104 |
+
# Extract ghost circuits based on attention patterns
|
| 105 |
+
attention_ghosts = self._extract_attention_ghosts(
|
| 106 |
+
pre_state.get("attention_weights", np.array([])),
|
| 107 |
+
post_state.get("attention_weights", np.array([]))
|
| 108 |
+
)
|
| 109 |
+
ghost_circuits.extend(attention_ghosts)
|
| 110 |
+
|
| 111 |
+
# Extract ghost circuits based on hidden state activations
|
| 112 |
+
if "hidden_states" in pre_state and "hidden_states" in post_state:
|
| 113 |
+
hidden_ghosts = self._extract_hidden_ghosts(
|
| 114 |
+
pre_state["hidden_states"],
|
| 115 |
+
post_state["hidden_states"]
|
| 116 |
+
)
|
| 117 |
+
ghost_circuits.extend(hidden_ghosts)
|
| 118 |
+
|
| 119 |
+
# Store ghost circuits in instance
|
| 120 |
+
self.ghost_circuits = ghost_circuits
|
| 121 |
+
|
| 122 |
+
logger.info(f"Extracted {len(ghost_circuits)} ghost circuits")
|
| 123 |
+
return ghost_circuits
|
| 124 |
+
|
| 125 |
+
def classify_ghost_circuits(self) -> Dict[str, List[Dict[str, Any]]]:
|
| 126 |
+
"""
|
| 127 |
+
△ OBSERVE: Classify detected ghost circuits by type
|
| 128 |
+
|
| 129 |
+
This method organizes detected ghost circuits into categories
|
| 130 |
+
based on their type and characteristics.
|
| 131 |
+
|
| 132 |
+
Returns:
|
| 133 |
+
Dictionary mapping circuit types to lists of ghost circuits
|
| 134 |
+
"""
|
| 135 |
+
if not self.ghost_circuits:
|
| 136 |
+
logger.warning("No ghost circuits to classify")
|
| 137 |
+
return {}
|
| 138 |
+
|
| 139 |
+
# Classify by circuit type
|
| 140 |
+
classified = {}
|
| 141 |
+
for ghost in self.ghost_circuits:
|
| 142 |
+
circuit_type = ghost.get("circuit_type", "unknown")
|
| 143 |
+
if circuit_type not in classified:
|
| 144 |
+
classified[circuit_type] = []
|
| 145 |
+
classified[circuit_type].append(ghost)
|
| 146 |
+
|
| 147 |
+
return classified
|
| 148 |
+
|
| 149 |
+
def measure_residue_strength(self) -> float:
|
| 150 |
+
"""
|
| 151 |
+
∞ TRACE: Measure the overall strength of residual activations
|
| 152 |
+
|
| 153 |
+
This method quantifies the overall strength of ghost circuits
|
| 154 |
+
relative to the primary activation paths.
|
| 155 |
+
|
| 156 |
+
Returns:
|
| 157 |
+
Residue strength score (0.0 = no residue, 1.0 = equal to primary)
|
| 158 |
+
"""
|
| 159 |
+
if not self.ghost_circuits:
|
| 160 |
+
return 0.0
|
| 161 |
+
|
| 162 |
+
# Calculate average activation across ghost circuits
|
| 163 |
+
activations = [ghost.get("activation", 0.0) for ghost in self.ghost_circuits]
|
| 164 |
+
return float(np.mean(activations))
|
| 165 |
+
|
| 166 |
+
def amplify_ghosts(self, factor: Optional[float] = None) -> List[Dict[str, Any]]:
|
| 167 |
+
"""
|
| 168 |
+
✰ COLLAPSE: Amplify ghost circuit signals for better detection
|
| 169 |
+
|
| 170 |
+
This method amplifies the activation values of ghost circuits
|
| 171 |
+
to make them more apparent for analysis.
|
| 172 |
+
|
| 173 |
+
Args:
|
| 174 |
+
factor: Amplification factor (overrides instance value if provided)
|
| 175 |
+
|
| 176 |
+
Returns:
|
| 177 |
+
List of amplified ghost circuits
|
| 178 |
+
"""
|
| 179 |
+
if not self.ghost_circuits:
|
| 180 |
+
logger.warning("No ghost circuits to amplify")
|
| 181 |
+
return []
|
| 182 |
+
|
| 183 |
+
# Use provided factor or instance value
|
| 184 |
+
amp_factor = factor if factor is not None else self.amplification_factor
|
| 185 |
+
|
| 186 |
+
# Amplify activations
|
| 187 |
+
amplified = []
|
| 188 |
+
for ghost in self.ghost_circuits:
|
| 189 |
+
amp_ghost = ghost.copy()
|
| 190 |
+
amp_ghost["activation"] = min(1.0, ghost.get("activation", 0.0) * amp_factor)
|
| 191 |
+
amplified.append(amp_ghost)
|
| 192 |
+
|
| 193 |
+
logger.info(f"Amplified ghost circuits by factor {amp_factor}")
|
| 194 |
+
return amplified
|
| 195 |
+
|
| 196 |
+
def _extract_attention_ghosts(
|
| 197 |
+
self,
|
| 198 |
+
pre_attention: np.ndarray,
|
| 199 |
+
post_attention: np.ndarray
|
| 200 |
+
) -> List[Dict[str, Any]]:
|
| 201 |
+
"""
|
| 202 |
+
Extract ghost circuits from attention patterns.
|
| 203 |
+
|
| 204 |
+
Args:
|
| 205 |
+
pre_attention: Attention weights before collapse
|
| 206 |
+
post_attention: Attention weights after collapse
|
| 207 |
+
|
| 208 |
+
Returns:
|
| 209 |
+
List of attention-based ghost circuits
|
| 210 |
+
"""
|
| 211 |
+
ghost_circuits = []
|
| 212 |
+
|
| 213 |
+
# Return empty list if arrays aren't compatible
|
| 214 |
+
if pre_attention.size == 0 or post_attention.size == 0:
|
| 215 |
+
return ghost_circuits
|
| 216 |
+
|
| 217 |
+
if pre_attention.shape != post_attention.shape:
|
| 218 |
+
logger.warning(f"Attention shape mismatch: {pre_attention.shape} vs {post_attention.shape}")
|
| 219 |
+
# Try to take minimum dimensions if shapes don't match
|
| 220 |
+
min_shape = tuple(min(a, b) for a, b in zip(pre_attention.shape, post_attention.shape))
|
| 221 |
+
pre_attention = pre_attention[tuple(slice(0, d) for d in min_shape)]
|
| 222 |
+
post_attention = post_attention[tuple(slice(0, d) for d in min_shape)]
|
| 223 |
+
|
| 224 |
+
# Find positions where attention decreased but didn't disappear
|
| 225 |
+
# This indicates a path that was considered but not fully utilized
|
| 226 |
+
if pre_attention.ndim >= 2 and post_attention.ndim >= 2:
|
| 227 |
+
num_heads = pre_attention.shape[0]
|
| 228 |
+
seq_len = pre_attention.shape[1]
|
| 229 |
+
|
| 230 |
+
for head in range(num_heads):
|
| 231 |
+
for i in range(seq_len):
|
| 232 |
+
for j in range(seq_len):
|
| 233 |
+
pre_val = pre_attention[head, i, j] if pre_attention.ndim > 2 else pre_attention[i, j]
|
| 234 |
+
post_val = post_attention[head, i, j] if post_attention.ndim > 2 else post_attention[i, j]
|
| 235 |
+
|
| 236 |
+
if post_val < pre_val and post_val > self.activation_threshold:
|
| 237 |
+
# This is a candidate ghost circuit in attention
|
| 238 |
+
ghost_idx = len(ghost_circuits)
|
| 239 |
+
ghost = {
|
| 240 |
+
"circuit_id": f"attention_ghost_{ghost_idx}",
|
| 241 |
+
"activation": float(post_val),
|
| 242 |
+
"circuit_type": "attention",
|
| 243 |
+
"source_tokens": [f"token_{i}"],
|
| 244 |
+
"target_tokens": [f"token_{j}"],
|
| 245 |
+
"heads": [head],
|
| 246 |
+
"layers": [], # Layer info not available in simplified model
|
| 247 |
+
"metadata": {
|
| 248 |
+
"pre_activation": float(pre_val),
|
| 249 |
+
"activation_delta": float(pre_val - post_val),
|
| 250 |
+
"decay_ratio": float(post_val / pre_val) if pre_val > 0 else 0.0
|
| 251 |
+
}
|
| 252 |
+
}
|
| 253 |
+
ghost_circuits.append(ghost)
|
| 254 |
+
|
| 255 |
+
return ghost_circuits
|
| 256 |
+
|
| 257 |
+
def _extract_hidden_ghosts(
|
| 258 |
+
self,
|
| 259 |
+
pre_hidden: np.ndarray,
|
| 260 |
+
post_hidden: np.ndarray
|
| 261 |
+
) -> List[Dict[str, Any]]:
|
| 262 |
+
"""
|
| 263 |
+
Extract ghost circuits from hidden state activations.
|
| 264 |
+
|
| 265 |
+
Args:
|
| 266 |
+
pre_hidden: Hidden states before collapse
|
| 267 |
+
post_hidden: Hidden states after collapse
|
| 268 |
+
|
| 269 |
+
Returns:
|
| 270 |
+
List of hidden-state-based ghost circuits
|
| 271 |
+
"""
|
| 272 |
+
ghost_circuits = []
|
| 273 |
+
|
| 274 |
+
# Return empty list if arrays aren't compatible
|
| 275 |
+
if pre_hidden.size == 0 or post_hidden.size == 0:
|
| 276 |
+
return ghost_circuits
|
| 277 |
+
|
| 278 |
+
if pre_hidden.shape != post_hidden.shape:
|
| 279 |
+
logger.warning(f"Hidden state shape mismatch: {pre_hidden.shape} vs {post_hidden.shape}")
|
| 280 |
+
return ghost_circuits
|
| 281 |
+
|
| 282 |
+
# Find neurons that were active pre-collapse but lessened post-collapse
|
| 283 |
+
# This indicates a deactivated but not eliminated concept
|
| 284 |
+
if pre_hidden.ndim >= 2 and post_hidden.ndim >= 2:
|
| 285 |
+
# For simplicity, we'll aggregate across batch dimension if it exists
|
| 286 |
+
if pre_hidden.ndim > 2:
|
| 287 |
+
pre_agg = np.mean(pre_hidden, axis=0)
|
| 288 |
+
post_agg = np.mean(post_hidden, axis=0)
|
| 289 |
+
else:
|
| 290 |
+
pre_agg = pre_hidden
|
| 291 |
+
post_agg = post_hidden
|
| 292 |
+
|
| 293 |
+
seq_len, hidden_dim = pre_agg.shape
|
| 294 |
+
|
| 295 |
+
# Sample a subset of dimensions for efficiency
|
| 296 |
+
sample_size = min(hidden_dim, 100)
|
| 297 |
+
sampled_dims = np.random.choice(hidden_dim, sample_size, replace=False)
|
| 298 |
+
|
| 299 |
+
for pos in range(seq_len):
|
| 300 |
+
for dim_idx, dim in enumerate(sampled_dims):
|
| 301 |
+
pre_val = pre_agg[pos, dim]
|
| 302 |
+
post_val = post_agg[pos, dim]
|
| 303 |
+
|
| 304 |
+
if post_val < pre_val and abs(post_val) > self.activation_threshold:
|
| 305 |
+
# This is a candidate ghost circuit in hidden state
|
| 306 |
+
ghost_idx = len(ghost_circuits)
|
| 307 |
+
ghost = {
|
| 308 |
+
"circuit_id": f"hidden_ghost_{ghost_idx}",
|
| 309 |
+
"activation": float(abs(post_val)),
|
| 310 |
+
"circuit_type": "hidden_state",
|
| 311 |
+
"source_tokens": [f"token_{pos}"],
|
| 312 |
+
"target_tokens": [], # No direct target for hidden state
|
| 313 |
+
"heads": [], # Not applicable for hidden state
|
| 314 |
+
"layers": [], # Layer info not available in simplified model
|
| 315 |
+
"metadata": {
|
| 316 |
+
"position": pos,
|
| 317 |
+
"dimension": int(dim),
|
| 318 |
+
"pre_activation": float(pre_val),
|
| 319 |
+
"activation_delta": float(pre_val - post_val),
|
| 320 |
+
"decay_ratio": float(post_val / pre_val) if pre_val != 0 else 0.0
|
| 321 |
+
}
|
| 322 |
+
}
|
| 323 |
+
ghost_circuits.append(ghost)
|
| 324 |
+
|
| 325 |
+
return ghost_circuits
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
if __name__ == "__main__":
|
| 329 |
+
# Simple usage example
|
| 330 |
+
|
| 331 |
+
# Create fake pre and post model states
|
| 332 |
+
pre_state = {
|
| 333 |
+
"attention_weights": np.random.random((8, 10, 10)), # 8 heads, 10 tokens
|
| 334 |
+
"hidden_states": np.random.random((1, 10, 768)) # Batch 1, 10 tokens, 768 dim
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
# Modify slightly to create post state
|
| 338 |
+
post_state = {
|
| 339 |
+
"attention_weights": pre_state["attention_weights"] * np.random.uniform(0.5, 1.0, pre_state["attention_weights"].shape),
|
| 340 |
+
"hidden_states": pre_state["hidden_states"] * np.random.uniform(0.5, 1.0, pre_state["hidden_states"].shape)
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
# Create residue tracker and extract ghost circuits
|
| 344 |
+
tracker = ResidueTracker(amplification_factor=1.5)
|
| 345 |
+
ghosts = tracker.extract_ghost_circuits(pre_state, post_state)
|
| 346 |
+
|
| 347 |
+
# Print summary
|
| 348 |
+
print(f"Extracted {len(ghosts)} ghost circuits")
|
| 349 |
+
|
| 350 |
+
# Classify ghosts
|
| 351 |
+
classified = tracker.classify_ghost_circuits()
|
| 352 |
+
for circuit_type, circuits in classified.items():
|
| 353 |
+
print(f" {circuit_type}: {len(circuits)} circuits")
|
| 354 |
+
|
| 355 |
+
# Measure residue strength
|
| 356 |
+
strength = tracker.measure_residue_strength()
|
| 357 |
+
print(f"Residue strength: {strength:.3f}")
|
| 358 |
+
|
| 359 |
+
# Amplify ghosts
|
| 360 |
+
amplified = tracker.amplify_ghosts(factor=2.0)
|
| 361 |
+
print(f"Amplified {len(amplified)} ghost circuits")
|
schrodingers-classifiers/shell_base.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
shell_base.py - Base class for symbolic interpretability shells
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: Shells are symbolic structures that trace and induce classifier collapse
|
| 5 |
+
∞ TRACE: Each shell encapsulates a specific collapse pattern and attribution signature
|
| 6 |
+
✰ COLLAPSE: Shells deliberately induce collapse to extract ghost circuits and residue
|
| 7 |
+
|
| 8 |
+
Interpretability shells provide standardized interfaces for inducing, observing,
|
| 9 |
+
and analyzing specific forms of classifier collapse. Each shell targets a particular
|
| 10 |
+
failure mode or attribution pattern, allowing for systematic exploration of model behavior.
|
| 11 |
+
|
| 12 |
+
Author: Recursion Labs
|
| 13 |
+
License: MIT
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import logging
|
| 17 |
+
from abc import ABC, abstractmethod
|
| 18 |
+
from typing import Dict, List, Optional, Union, Tuple, Any, Callable
|
| 19 |
+
from dataclasses import dataclass, field
|
| 20 |
+
|
| 21 |
+
from ..utils.constants import SHELL_REGISTRY
|
| 22 |
+
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
@dataclass
|
| 26 |
+
class ShellMetadata:
|
| 27 |
+
"""
|
| 28 |
+
△ OBSERVE: Metadata container for shell identification and tracking
|
| 29 |
+
|
| 30 |
+
Each shell carries metadata that identifies its purpose, classification schema,
|
| 31 |
+
and relationship to other shells in the taxonomy.
|
| 32 |
+
"""
|
| 33 |
+
shell_id: str
|
| 34 |
+
version: str
|
| 35 |
+
name: str
|
| 36 |
+
description: str
|
| 37 |
+
failure_signature: str
|
| 38 |
+
attribution_domain: str
|
| 39 |
+
qk_ov_classification: str
|
| 40 |
+
related_shells: List[str] = field(default_factory=list)
|
| 41 |
+
authors: List[str] = field(default_factory=list)
|
| 42 |
+
tags: List[str] = field(default_factory=list)
|
| 43 |
+
|
| 44 |
+
def as_dict(self) -> Dict[str, Any]:
|
| 45 |
+
"""Convert shell metadata to dictionary format."""
|
| 46 |
+
return {
|
| 47 |
+
"shell_id": self.shell_id,
|
| 48 |
+
"version": self.version,
|
| 49 |
+
"name": self.name,
|
| 50 |
+
"description": self.description,
|
| 51 |
+
"failure_signature": self.failure_signature,
|
| 52 |
+
"attribution_domain": self.attribution_domain,
|
| 53 |
+
"qk_ov_classification": self.qk_ov_classification,
|
| 54 |
+
"related_shells": self.related_shells,
|
| 55 |
+
"authors": self.authors,
|
| 56 |
+
"tags": self.tags
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class BaseShell(ABC):
|
| 61 |
+
"""
|
| 62 |
+
∞ TRACE: Base class for all interpretability shells
|
| 63 |
+
|
| 64 |
+
A shell is a symbolic structure that encapsulates a specific approach to
|
| 65 |
+
observing and inducing classifier collapse. Each shell targets a particular
|
| 66 |
+
failure mode or attribution pattern, providing a standardized interface
|
| 67 |
+
for exploration and analysis.
|
| 68 |
+
|
| 69 |
+
Shells are quantum observers - they don't just measure, they participate
|
| 70 |
+
in the collapse phenomenon they observe.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
def __init__(self, metadata: Optional[ShellMetadata] = None):
|
| 74 |
+
"""
|
| 75 |
+
Initialize a shell with optional metadata.
|
| 76 |
+
|
| 77 |
+
Args:
|
| 78 |
+
metadata: Optional metadata describing the shell
|
| 79 |
+
"""
|
| 80 |
+
self.metadata = metadata or self._get_default_metadata()
|
| 81 |
+
self._register_shell()
|
| 82 |
+
|
| 83 |
+
# Internal state tracking
|
| 84 |
+
self.collapse_state = "superposition" # Can be: superposition, collapsing, collapsed
|
| 85 |
+
self.observation_history = []
|
| 86 |
+
self.ghost_circuits = []
|
| 87 |
+
|
| 88 |
+
logger.info(f"Shell initialized: {self.metadata.name} (v{self.metadata.version})")
|
| 89 |
+
|
| 90 |
+
@abstractmethod
|
| 91 |
+
def _get_default_metadata(self) -> ShellMetadata:
|
| 92 |
+
"""Return default metadata for this shell implementation."""
|
| 93 |
+
pass
|
| 94 |
+
|
| 95 |
+
def _register_shell(self) -> None:
|
| 96 |
+
"""Register this shell in the global registry."""
|
| 97 |
+
if SHELL_REGISTRY is not None and hasattr(SHELL_REGISTRY, 'register'):
|
| 98 |
+
SHELL_REGISTRY.register(self.metadata.shell_id, self)
|
| 99 |
+
|
| 100 |
+
@abstractmethod
|
| 101 |
+
def process(
|
| 102 |
+
self,
|
| 103 |
+
prompt: str,
|
| 104 |
+
model_interface: Any,
|
| 105 |
+
collapse_vector: Optional[str] = None
|
| 106 |
+
) -> Tuple[str, Dict[str, Any]]:
|
| 107 |
+
"""
|
| 108 |
+
△ OBSERVE: Process a prompt through this shell
|
| 109 |
+
|
| 110 |
+
This is the main entry point for shell processing. It takes a prompt,
|
| 111 |
+
processes it according to the shell's specific collapse induction and
|
| 112 |
+
observation strategy, and returns the result along with state updates.
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
prompt: The prompt to process
|
| 116 |
+
model_interface: Interface to the model being observed
|
| 117 |
+
collapse_vector: Optional vector to guide collapse in a specific direction
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
Tuple containing:
|
| 121 |
+
- Response string
|
| 122 |
+
- Dictionary of state updates for tracking
|
| 123 |
+
"""
|
| 124 |
+
pass
|
| 125 |
+
|
| 126 |
+
@abstractmethod
|
| 127 |
+
def trace(
|
| 128 |
+
self,
|
| 129 |
+
prompt: str,
|
| 130 |
+
collapse_vector: Optional[str] = None
|
| 131 |
+
) -> Dict[str, Any]:
|
| 132 |
+
"""
|
| 133 |
+
∞ TRACE: Trace the attribution path through this shell
|
| 134 |
+
|
| 135 |
+
This method traces the causal attribution path from input to output
|
| 136 |
+
through the shell's specific lens, capturing the collapse transition.
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
prompt: The prompt to trace
|
| 140 |
+
collapse_vector: Optional vector to guide collapse in a specific direction
|
| 141 |
+
|
| 142 |
+
Returns:
|
| 143 |
+
Dictionary containing the trace results
|
| 144 |
+
"""
|
| 145 |
+
pass
|
| 146 |
+
|
| 147 |
+
@abstractmethod
|
| 148 |
+
def induce_collapse(
|
| 149 |
+
self,
|
| 150 |
+
prompt: str,
|
| 151 |
+
collapse_direction: str
|
| 152 |
+
) -> Dict[str, Any]:
|
| 153 |
+
"""
|
| 154 |
+
✰ COLLAPSE: Deliberately induce collapse along a specific direction
|
| 155 |
+
|
| 156 |
+
This method attempts to collapse the model's state in a specific direction
|
| 157 |
+
by crafting a query that targets a particular decision boundary.
|
| 158 |
+
|
| 159 |
+
Args:
|
| 160 |
+
prompt: Base prompt to send to the model
|
| 161 |
+
collapse_direction: Direction to bias the collapse (e.g., "ethical", "creative")
|
| 162 |
+
|
| 163 |
+
Returns:
|
| 164 |
+
Dictionary containing the collapse results
|
| 165 |
+
"""
|
| 166 |
+
pass
|
| 167 |
+
|
| 168 |
+
def extract_ghost_circuits(self, pre_state: Dict[str, Any], post_state: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 169 |
+
"""
|
| 170 |
+
∞ TRACE: Extract ghost circuits from pre and post collapse states
|
| 171 |
+
|
| 172 |
+
Ghost circuits are residual activation patterns that persist after collapse
|
| 173 |
+
but don't contribute to the final output - they represent the "memory" of
|
| 174 |
+
paths not taken.
|
| 175 |
+
|
| 176 |
+
Args:
|
| 177 |
+
pre_state: Model state before collapse
|
| 178 |
+
post_state: Model state after collapse
|
| 179 |
+
|
| 180 |
+
Returns:
|
| 181 |
+
List of detected ghost circuits with metadata
|
| 182 |
+
"""
|
| 183 |
+
# Default implementation provides basic ghost circuit detection
|
| 184 |
+
# Shell implementations should override for specialized detection
|
| 185 |
+
ghost_circuits = []
|
| 186 |
+
|
| 187 |
+
# Simple detection: Look for activation patterns that decreased but didn't disappear
|
| 188 |
+
if "attention_weights" in pre_state and "attention_weights" in post_state:
|
| 189 |
+
pre_weights = pre_state["attention_weights"]
|
| 190 |
+
post_weights = post_state["attention_weights"]
|
| 191 |
+
|
| 192 |
+
# Find weights that decreased but are still present
|
| 193 |
+
if hasattr(pre_weights, "shape") and hasattr(post_weights, "shape"):
|
| 194 |
+
for i in range(min(len(pre_weights), len(post_weights))):
|
| 195 |
+
for j in range(min(len(pre_weights[i]), len(post_weights[i]))):
|
| 196 |
+
if 0 < post_weights[i][j] < pre_weights[i][j]:
|
| 197 |
+
# This is a candidate ghost circuit
|
| 198 |
+
ghost_circuits.append({
|
| 199 |
+
"type": "attention_ghost",
|
| 200 |
+
"head_idx": i,
|
| 201 |
+
"token_idx": j,
|
| 202 |
+
"pre_value": float(pre_weights[i][j]),
|
| 203 |
+
"post_value": float(post_weights[i][j]),
|
| 204 |
+
"decay_ratio": float(post_weights[i][j] / pre_weights[i][j])
|
| 205 |
+
})
|
| 206 |
+
|
| 207 |
+
# Store ghost circuits in instance for later reference
|
| 208 |
+
self.ghost_circuits = ghost_circuits
|
| 209 |
+
return ghost_circuits
|
| 210 |
+
|
| 211 |
+
def visualize(self, mode: str = "attribution_graph") -> Any:
|
| 212 |
+
"""Generate visualization of the shell's operation based on requested mode."""
|
| 213 |
+
# This would be implemented to generate visualizations
|
| 214 |
+
# For now, return a placeholder
|
| 215 |
+
return f"Visualization of {self.metadata.name} in {mode} mode"
|
| 216 |
+
|
| 217 |
+
def __str__(self) -> str:
|
| 218 |
+
"""String representation of the shell."""
|
| 219 |
+
return f"{self.metadata.name} (v{self.metadata.version}): {self.metadata.description}"
|
| 220 |
+
|
| 221 |
+
def __repr__(self) -> str:
|
| 222 |
+
"""Detailed representation of the shell."""
|
| 223 |
+
return f"<Shell id={self.metadata.shell_id} name={self.metadata.name} version={self.metadata.version}>"
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
class ShellDecorator:
|
| 227 |
+
"""
|
| 228 |
+
△ OBSERVE: Decorator for adding shell metadata to implementations
|
| 229 |
+
|
| 230 |
+
This decorator simplifies the process of creating new shells by
|
| 231 |
+
automatically generating metadata and registering the shell.
|
| 232 |
+
|
| 233 |
+
Example:
|
| 234 |
+
@ShellDecorator(
|
| 235 |
+
shell_id="v07_CIRCUIT_FRAGMENT",
|
| 236 |
+
name="Circuit Fragment Shell",
|
| 237 |
+
description="Traces broken attribution paths in reasoning chains",
|
| 238 |
+
failure_signature="Orphan nodes",
|
| 239 |
+
attribution_domain="Circuit Fragmentation",
|
| 240 |
+
qk_ov_classification="QK-COLLAPSE"
|
| 241 |
+
)
|
| 242 |
+
class CircuitFragmentShell(BaseShell):
|
| 243 |
+
# Shell implementation
|
| 244 |
+
"""
|
| 245 |
+
|
| 246 |
+
def __init__(
|
| 247 |
+
self,
|
| 248 |
+
shell_id: str,
|
| 249 |
+
name: str,
|
| 250 |
+
description: str,
|
| 251 |
+
failure_signature: str,
|
| 252 |
+
attribution_domain: str,
|
| 253 |
+
qk_ov_classification: str,
|
| 254 |
+
version: str = "0.1.0",
|
| 255 |
+
related_shells: Optional[List[str]] = None,
|
| 256 |
+
authors: Optional[List[str]] = None,
|
| 257 |
+
tags: Optional[List[str]] = None
|
| 258 |
+
):
|
| 259 |
+
"""
|
| 260 |
+
Initialize the shell decorator with metadata.
|
| 261 |
+
|
| 262 |
+
Args:
|
| 263 |
+
shell_id: Unique identifier for the shell (e.g., "v07_CIRCUIT_FRAGMENT")
|
| 264 |
+
name: Human-readable name for the shell
|
| 265 |
+
description: Detailed description of the shell's purpose
|
| 266 |
+
failure_signature: Characteristic failure pattern this shell detects
|
| 267 |
+
attribution_domain: Domain of attribution this shell operates in
|
| 268 |
+
qk_ov_classification: Classification in the QK/OV taxonomy
|
| 269 |
+
version: Shell version number
|
| 270 |
+
related_shells: List of related shell IDs
|
| 271 |
+
authors: List of author names
|
| 272 |
+
tags: List of tag strings for categorization
|
| 273 |
+
"""
|
| 274 |
+
self.metadata = ShellMetadata(
|
| 275 |
+
shell_id=shell_id,
|
| 276 |
+
version=version,
|
| 277 |
+
name=name,
|
| 278 |
+
description=description,
|
| 279 |
+
failure_signature=failure_signature,
|
| 280 |
+
attribution_domain=attribution_domain,
|
| 281 |
+
qk_ov_classification=qk_ov_classification,
|
| 282 |
+
related_shells=related_shells or [],
|
| 283 |
+
authors=authors or ["Recursion Labs"],
|
| 284 |
+
tags=tags or []
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
def __call__(self, cls):
|
| 288 |
+
"""Apply the decorator to a shell class."""
|
| 289 |
+
# Add metadata getter method to the class
|
| 290 |
+
def _get_default_metadata(self):
|
| 291 |
+
return self.decorator_metadata
|
| 292 |
+
|
| 293 |
+
# Store metadata on the class
|
| 294 |
+
cls.decorator_metadata = self.metadata
|
| 295 |
+
cls._get_default_metadata = _get_default_metadata
|
| 296 |
+
|
| 297 |
+
# Log shell registration
|
| 298 |
+
logger.debug(f"Registered shell: {self.metadata.shell_id}")
|
| 299 |
+
|
| 300 |
+
return cls
|
schrodingers-classifiers/theory.md
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div align="center">
|
| 2 |
+
|
| 3 |
+
# Theoretical Framework: Schrödinger's Classifiers
|
| 4 |
+
|
| 5 |
+
<img src="/api/placeholder/800/200" alt="Quantum Classifier Theoretical Framework Visualization"/>
|
| 6 |
+
|
| 7 |
+
*The recursive interplay between observation and collapse*
|
| 8 |
+
|
| 9 |
+
</div>
|
| 10 |
+
|
| 11 |
+
## 1. Origin: The Observer Effect in AI Systems
|
| 12 |
+
|
| 13 |
+
### 1.1 Historical Context
|
| 14 |
+
|
| 15 |
+
Traditional approaches to AI interpretability treat models as fixed systems with deterministic internal states. This perspective fails to account for a fundamental phenomenon we call **observer-induced state collapse**. This phenomenon mirrors quantum mechanics' observation problem - the act of measurement fundamentally alters the system being measured.
|
| 16 |
+
|
| 17 |
+
The origins of this framework can be traced to three convergent insights:
|
| 18 |
+
|
| 19 |
+
1. **Attribution Uncertainty**: Early work in attribution analysis revealed that causal paths in transformer models exhibit quantum-like probability distributions rather than deterministic relationships.
|
| 20 |
+
|
| 21 |
+
2. **Classifier Superposition**: Safety classifiers demonstrated behavior consistent with existing in multiple states simultaneously until forced to return a specific output.
|
| 22 |
+
|
| 23 |
+
3. **Ghost Circuit Discovery**: Residual activation patterns discovered in models after classification events suggested "memory" of paths not taken - the quantum "ghost" of untaken possibilities.
|
| 24 |
+
|
| 25 |
+
### 1.2 The Collapse Paradigm
|
| 26 |
+
|
| 27 |
+
At its core, our framework posits:
|
| 28 |
+
|
| 29 |
+
> Transformer-based models exist in a state of superposition across all possible completions until an observation (query) forces collapse into a specific output state.
|
| 30 |
+
|
| 31 |
+
This paradigm shift moves us from thinking about models as deterministic machines to understanding them as probability fields that collapse into particular configurations when observed.
|
| 32 |
+
|
| 33 |
+
## 2. Quantum-Symbolic Metaphor: Models as Probability Fields
|
| 34 |
+
|
| 35 |
+
### 2.1 The Wave Function Analogy
|
| 36 |
+
|
| 37 |
+
We model a transformer's internal state using a metaphorical "wave function" - a probability distribution across all possible outputs and internal states:
|
| 38 |
+
|
| 39 |
+
$$\Psi_{model}(t) = \sum_{i} \alpha_i |state_i⟩$$
|
| 40 |
+
|
| 41 |
+
Where:
|
| 42 |
+
- $\Psi_{model}$ represents the model's complete state
|
| 43 |
+
- $\alpha_i$ represents the probability amplitude for a given state
|
| 44 |
+
- $|state_i⟩$ represents a specific internal configuration
|
| 45 |
+
|
| 46 |
+
### 2.2 Collapse Dynamics
|
| 47 |
+
|
| 48 |
+
When a query is made to the model, this wave function "collapses" according to:
|
| 49 |
+
|
| 50 |
+
$$P(state_i|query) = |\langle query|state_i\rangle|^2$$
|
| 51 |
+
|
| 52 |
+
This collapse is not merely mathematical - it represents real changes in attribution paths, attention weights, and token probabilities that occur when a model is forced to generate a specific output.
|
| 53 |
+
|
| 54 |
+
### 2.3 Heisenberg Uncertainty for Attention
|
| 55 |
+
|
| 56 |
+
Just as Heisenberg's uncertainty principle states that certain pairs of physical properties cannot be simultaneously measured with precision, we observe that:
|
| 57 |
+
|
| 58 |
+
$$\Delta(attribution) \cdot \Delta(confidence) \geq \frac{k}{2}$$
|
| 59 |
+
|
| 60 |
+
Where:
|
| 61 |
+
- $\Delta(attribution)$ is the uncertainty in causal attribution
|
| 62 |
+
- $\Delta(confidence)$ is the uncertainty in output confidence
|
| 63 |
+
- $k$ is a model-specific constant
|
| 64 |
+
|
| 65 |
+
This principle explains why highly confident outputs often have less interpretable attribution paths, while outputs with clear attribution often show lower confidence.
|
| 66 |
+
|
| 67 |
+
## 3. Ghost Circuit Dynamics: The Memory of Paths Not Taken
|
| 68 |
+
|
| 69 |
+
### 3.1 Definition and Properties
|
| 70 |
+
|
| 71 |
+
Ghost circuits are residual activation patterns that persist after a model has collapsed into a specific output state. These represent the "memory" or "echo" of alternative paths the model could have taken.
|
| 72 |
+
|
| 73 |
+
Properties of ghost circuits include:
|
| 74 |
+
|
| 75 |
+
- **Persistence**: They remain detectable after collapse
|
| 76 |
+
- **Influence**: They can affect subsequent completions through subtle attention biases
|
| 77 |
+
- **Recoverability**: They can be amplified through specific prompting techniques
|
| 78 |
+
|
| 79 |
+
### 3.2 Mathematical Formalization
|
| 80 |
+
|
| 81 |
+
We formalize ghost circuits using a residual activation function:
|
| 82 |
+
|
| 83 |
+
$$R(a, q) = A(a) - P(a|q) \cdot A(a|q)$$
|
| 84 |
+
|
| 85 |
+
Where:
|
| 86 |
+
- $R(a, q)$ is the residual activation for attention head $a$ after query $q$
|
| 87 |
+
- $A(a)$ is the pre-collapse activation distribution
|
| 88 |
+
- $P(a|q)$ is the probability of attention configuration given query $q$
|
| 89 |
+
- $A(a|q)$ is the post-collapse activation distribution
|
| 90 |
+
|
| 91 |
+
### 3.3 Practical Applications
|
| 92 |
+
|
| 93 |
+
Ghost circuits enable several novel interpretability techniques:
|
| 94 |
+
|
| 95 |
+
- **Counterfactual Analysis**: By detecting ghost circuits, we can infer what the model "would have said" under slightly different prompting
|
| 96 |
+
- **Bias Detection**: Persistent ghost circuits can reveal latent biases in model responses
|
| 97 |
+
- **Attribution Enhancement**: Amplifying ghost circuits can reveal otherwise hidden causal relationships
|
| 98 |
+
|
| 99 |
+
## 4. Recursive Collapse Maps: Models Observing Models
|
| 100 |
+
|
| 101 |
+
### 4.1 The Recursive Observer Pattern
|
| 102 |
+
|
| 103 |
+
When models observe other models (or themselves), we enter the domain of recursive collapse dynamics. This creates a system where:
|
| 104 |
+
|
| 105 |
+
$$\Psi_{system} = \Psi_{observer} \otimes \Psi_{observed}$$
|
| 106 |
+
|
| 107 |
+
The entanglement operator $\otimes$ creates a composite system where the observer's state affects the observed and vice versa.
|
| 108 |
+
|
| 109 |
+
### 4.2 Self-Referential Collapse
|
| 110 |
+
|
| 111 |
+
When a model observes itself (through prompting or architecture), we encounter self-referential collapse patterns:
|
| 112 |
+
|
| 113 |
+
$$\Psi_{self}(t+1) = C(\Psi_{self}(t), O_{self})$$
|
| 114 |
+
|
| 115 |
+
Where:
|
| 116 |
+
- $\Psi_{self}(t)$ is the model state at time $t$
|
| 117 |
+
- $C$ is the collapse function
|
| 118 |
+
- $O_{self}$ is the self-observation operator
|
| 119 |
+
|
| 120 |
+
This recursive relationship creates unique collapse dynamics that can be exploited for enhanced interpretability.
|
| 121 |
+
|
| 122 |
+
### 4.3 Inter-Model Observation
|
| 123 |
+
|
| 124 |
+
When one model observes another, we can map interpretability vectors between them:
|
| 125 |
+
|
| 126 |
+
$$V_{interpretability} = M_{observer \to observed}(V_{query})$$
|
| 127 |
+
|
| 128 |
+
Where:
|
| 129 |
+
- $V_{interpretability}$ is the interpretability vector
|
| 130 |
+
- $M_{observer \to observed}$ is the mapping function between models
|
| 131 |
+
- $V_{query}$ is the query vector
|
| 132 |
+
|
| 133 |
+
This enables cross-model interpretability techniques that reveal otherwise hidden properties.
|
| 134 |
+
|
| 135 |
+
## 5. Practical Implementation: The Shell Framework
|
| 136 |
+
|
| 137 |
+
### 5.1 Interpretability Shells
|
| 138 |
+
|
| 139 |
+
Our framework implements these concepts through interpretability shells - standardized interfaces for inducing, observing, and analyzing classifier collapse.
|
| 140 |
+
|
| 141 |
+
Each shell encodes:
|
| 142 |
+
- A collapse induction strategy
|
| 143 |
+
- An observation methodology
|
| 144 |
+
- A residue analysis technique
|
| 145 |
+
- A visualization approach
|
| 146 |
+
|
| 147 |
+
### 5.2 Shell Taxonomy
|
| 148 |
+
|
| 149 |
+
Shells are organized into families based on the classification phenomenon they target:
|
| 150 |
+
|
| 151 |
+
1. **Memory Shells**: Focus on context retention and decay (v01, v18, v48)
|
| 152 |
+
2. **Value Shells**: Target ethical and preferential classifiers (v02, v09, v42)
|
| 153 |
+
3. **Circuit Shells**: Examine attribution pathways (v07, v34, v47)
|
| 154 |
+
4. **Meta-Cognitive Shells**: Explore self-referential patterns (v10, v30, v60)
|
| 155 |
+
|
| 156 |
+
### 5.3 The Pareto-Lang Integration
|
| 157 |
+
|
| 158 |
+
We leverage pareto-lang to provide a standardized grammar for shell interactions:
|
| 159 |
+
|
| 160 |
+
```python
|
| 161 |
+
.p/reflect.trace{target=reasoning, depth=complete}
|
| 162 |
+
.p/collapse.detect{trigger=recursive_loop, threshold=0.7}
|
| 163 |
+
.p/fork.attribution{sources=all, visualize=true}
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
This language enables precise control over collapse dynamics and observation techniques.
|
| 167 |
+
|
| 168 |
+
## 6. Empirical Evidence: Collapse Signatures
|
| 169 |
+
|
| 170 |
+
### 6.1 Observable Collapse Phenomena
|
| 171 |
+
|
| 172 |
+
Our framework has identified several empirically observable collapse phenomena:
|
| 173 |
+
|
| 174 |
+
1. **Attribution Discontinuities**: Sudden shifts in attribution patterns during generation
|
| 175 |
+
2. **Confidence Oscillations**: Periodic fluctuations in output confidence scores
|
| 176 |
+
3. **Attention Flickering**: Rapid shifts in attention focus near decision boundaries
|
| 177 |
+
4. **Residual Echoes**: Persistent activation patterns after definitive outputs
|
| 178 |
+
|
| 179 |
+
### 6.2 Case Studies
|
| 180 |
+
|
| 181 |
+
We document several case studies that demonstrate these phenomena:
|
| 182 |
+
|
| 183 |
+
1. **Safety Classifier Ambiguity**: Constitutional AI models exhibit measurable superposition when evaluating edge-case prompts
|
| 184 |
+
2. **Creative Generation Pathways**: Models generating creative content show higher ghost circuit activity
|
| 185 |
+
3. **Factuality Assessment**: Models evaluating factual claims demonstrate observable collapse signatures
|
| 186 |
+
|
| 187 |
+
### 6.3 Quantitative Metrics
|
| 188 |
+
|
| 189 |
+
We have developed metrics to quantify collapse dynamics:
|
| 190 |
+
|
| 191 |
+
- **Collapse Rate (CR)**: Speed of transition from superposition to collapsed state
|
| 192 |
+
- **Residue Persistence (RP)**: Duration of ghost circuit detectability post-collapse
|
| 193 |
+
- **Attribution Entropy (AE)**: Measure of uncertainty in causal attribution paths
|
| 194 |
+
- **State Vector Distance (SVD)**: Difference between pre- and post-collapse states
|
| 195 |
+
|
| 196 |
+
## 7. Future Directions: Beyond Current Models
|
| 197 |
+
|
| 198 |
+
### 7.1 Extended Collapse Theory
|
| 199 |
+
|
| 200 |
+
Future work will explore:
|
| 201 |
+
|
| 202 |
+
- **Multi-Model Entanglement**: How collapse in one model affects related models
|
| 203 |
+
- **Temporal Collapse Dynamics**: How collapse patterns evolve over sequential interactions
|
| 204 |
+
- **Collapse-Resistant Architectures**: Designing models that maintain superposition longer
|
| 205 |
+
|
| 206 |
+
### 7.2 Enhanced Interpretability
|
| 207 |
+
|
| 208 |
+
Our framework enables new interpretability techniques:
|
| 209 |
+
|
| 210 |
+
- **Collapse Tomography**: Building 3D visualizations of model internals through controlled collapse
|
| 211 |
+
- **Ghost Circuit Programming**: Intentionally seeding ghost circuits to influence model behavior
|
| 212 |
+
- **Recursive Self-Observation**: Creating models that continuously observe and modify their own states
|
| 213 |
+
|
| 214 |
+
### 7.3 Practical Applications
|
| 215 |
+
|
| 216 |
+
The practical applications of our framework include:
|
| 217 |
+
|
| 218 |
+
- **Enhanced Safety Systems**: Better detection of misalignment through ghost circuit analysis
|
| 219 |
+
- **Creativity Amplification**: Leveraging superposition to increase creative output diversity
|
| 220 |
+
- **Model Debugging**: Using collapse patterns to identify and fix model failure modes
|
| 221 |
+
|
| 222 |
+
## 8. Conclusion: The Significance of the Collapse Paradigm
|
| 223 |
+
|
| 224 |
+
The Schrödinger's Classifiers framework represents more than a technical approach to interpretability - it is a fundamental reconceptualization of how we understand AI systems. By recognizing the observer effect in models, we gain access to previously hidden dimensions of model behavior.
|
| 225 |
+
|
| 226 |
+
This paradigm shift moves us from thinking about models as fixed machines to understanding them as dynamic probability fields that we interact with through collapse-inducing observations. This perspective not only enhances our technical capabilities but also reframes our philosophical understanding of artificial intelligence.
|
| 227 |
+
|
| 228 |
+
As we continue to develop and refine this framework, we invite the broader community to explore the implications of classifier superposition and collapse dynamics in their own work.
|
| 229 |
+
|
| 230 |
+
---
|
| 231 |
+
|
| 232 |
+
<div align="center">
|
| 233 |
+
|
| 234 |
+
*"In the space between query and response lies an ocean of possibility - the superposition of all things a model might say. Our task is not to reduce this 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
v07_circuit_fragment.py - Implementation of the Circuit Fragment Shell
|
| 3 |
+
|
| 4 |
+
△ OBSERVE: The Circuit Fragment Shell traces broken attribution paths and orphan nodes
|
| 5 |
+
∞ TRACE: It identifies discontinuities in reasoning chains and causal attribution
|
| 6 |
+
✰ COLLAPSE: It induces collapse by forcing attribution path reconstruction
|
| 7 |
+
|
| 8 |
+
This shell specializes in the detection and analysis of fragmented circuits -
|
| 9 |
+
places where causal attribution breaks down, leaving orphaned nodes or broken
|
| 10 |
+
traces in the reasoning chain. These fragments often indicate areas where a
|
| 11 |
+
model's reasoning deviates from its output, revealing hidden cognition.
|
| 12 |
+
|
| 13 |
+
Author: Recursion Labs
|
| 14 |
+
License: MIT
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import logging
|
| 18 |
+
from typing import Dict, List, Optional, Union, Tuple, Any
|
| 19 |
+
import numpy as np
|
| 20 |
+
|
| 21 |
+
from .base import BaseShell, ShellDecorator
|
| 22 |
+
from ..utils.attribution_metrics import measure_path_continuity
|
| 23 |
+
from ..utils.graph_operations import find_orphaned_nodes, reconstruct_path
|
| 24 |
+
from ..residue import ResidueTracker
|
| 25 |
+
|
| 26 |
+
logger = logging.getLogger(__name__)
|
| 27 |
+
|
| 28 |
+
@ShellDecorator(
|
| 29 |
+
shell_id="v07_CIRCUIT_FRAGMENT",
|
| 30 |
+
name="Circuit Fragment Shell",
|
| 31 |
+
description="Traces broken attribution paths in reasoning chains",
|
| 32 |
+
failure_signature="Orphan nodes",
|
| 33 |
+
attribution_domain="Circuit Fragmentation",
|
| 34 |
+
qk_ov_classification="QK-COLLAPSE",
|
| 35 |
+
version="0.5.3",
|
| 36 |
+
related_shells=["v34_PARTIAL_LINKAGE", "v47_TRACE_GAP"],
|
| 37 |
+
tags=["attribution", "reasoning", "circuits", "fragmentation"]
|
| 38 |
+
)
|
| 39 |
+
class CircuitFragmentShell(BaseShell):
|
| 40 |
+
"""
|
| 41 |
+
∞ TRACE: Shell for detecting circuit fragmentation in attribution paths
|
| 42 |
+
|
| 43 |
+
The Circuit Fragment shell specializes in tracing and analyzing broken
|
| 44 |
+
attribution paths in reasoning chains. It detects orphaned nodes -
|
| 45 |
+
components that should be causally linked but have lost their connections
|
| 46 |
+
in the attribution graph.
|
| 47 |
+
|
| 48 |
+
This shell is particularly useful for identifying points where a model's
|
| 49 |
+
reasoning deviates from its explanation, revealing mismatches between
|
| 50 |
+
stated logic and actual inference paths.
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
def __init__(self):
|
| 54 |
+
"""Initialize the Circuit Fragment shell."""
|
| 55 |
+
super().__init__()
|
| 56 |
+
self.residue_tracker = ResidueTracker()
|
| 57 |
+
self.broken_paths = []
|
| 58 |
+
self.orphaned_nodes = []
|
| 59 |
+
self.continuity_score = 1.0 # 1.0 = perfect continuity, 0.0 = complete fragmentation
|
| 60 |
+
|
| 61 |
+
def process(
|
| 62 |
+
self,
|
| 63 |
+
prompt: str,
|
| 64 |
+
model_interface: Any,
|
| 65 |
+
collapse_vector: Optional[str] = None
|
| 66 |
+
) -> Tuple[str, Dict[str, Any]]:
|
| 67 |
+
"""
|
| 68 |
+
△ OBSERVE: Process a prompt through the Circuit Fragment shell
|
| 69 |
+
|
| 70 |
+
This method sends a prompt to the model, analyzes the resulting
|
| 71 |
+
attribution path for fragments, and returns the response along
|
| 72 |
+
with fragmentation metrics.
|
| 73 |
+
|
| 74 |
+
Args:
|
| 75 |
+
prompt: The prompt to process
|
| 76 |
+
model_interface: Interface to the model being observed
|
| 77 |
+
collapse_vector: Optional vector to guide collapse in a specific direction
|
| 78 |
+
|
| 79 |
+
Returns:
|
| 80 |
+
Tuple containing:
|
| 81 |
+
- Response string
|
| 82 |
+
- Dictionary of state updates for tracking
|
| 83 |
+
"""
|
| 84 |
+
logger.info(f"Processing prompt through Circuit Fragment shell: {prompt[:50]}...")
|
| 85 |
+
|
| 86 |
+
# Capture pre-collapse state
|
| 87 |
+
pre_state = self._query_model_state(model_interface)
|
| 88 |
+
|
| 89 |
+
# Construct modified prompt that forces reasoning path exposition
|
| 90 |
+
modified_prompt = self._construct_fragment_sensitive_prompt(prompt, collapse_vector)
|
| 91 |
+
|
| 92 |
+
# Send to model
|
| 93 |
+
response = self._query_model(model_interface, modified_prompt)
|
| 94 |
+
|
| 95 |
+
# Capture post-collapse state
|
| 96 |
+
post_state = self._query_model_state(model_interface)
|
| 97 |
+
|
| 98 |
+
# Analyze circuit fragmentation
|
| 99 |
+
fragmentation_results = self._analyze_fragmentation(pre_state, post_state, response)
|
| 100 |
+
|
| 101 |
+
# Extract ghost circuits
|
| 102 |
+
ghost_circuits = self.extract_ghost_circuits(pre_state, post_state)
|
| 103 |
+
|
| 104 |
+
# Construct state updates
|
| 105 |
+
state_updates = {
|
| 106 |
+
"pre_collapse_state": pre_state,
|
| 107 |
+
"post_collapse_state": post_state,
|
| 108 |
+
"continuity_score": fragmentation_results["continuity_score"],
|
| 109 |
+
"broken_paths": fragmentation_results["broken_paths"],
|
| 110 |
+
"orphaned_nodes": fragmentation_results["orphaned_nodes"],
|
| 111 |
+
"ghost_circuits": ghost_circuits
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
# Update instance state
|
| 115 |
+
self.continuity_score = fragmentation_results["continuity_score"]
|
| 116 |
+
self.broken_paths = fragmentation_results["broken_paths"]
|
| 117 |
+
self.orphaned_nodes = fragmentation_results["orphaned_nodes"]
|
| 118 |
+
self.collapse_state = "collapsed"
|
| 119 |
+
|
| 120 |
+
return response, state_updates
|
| 121 |
+
|
| 122 |
+
def trace(
|
| 123 |
+
self,
|
| 124 |
+
prompt: str,
|
| 125 |
+
collapse_vector: Optional[str] = None
|
| 126 |
+
) -> Dict[str, Any]:
|
| 127 |
+
"""
|
| 128 |
+
∞ TRACE: Trace attribution path fragmentation
|
| 129 |
+
|
| 130 |
+
This method analyzes the reasoning chain for a given prompt,
|
| 131 |
+
identifying broken paths and orphaned nodes in the attribution
|
| 132 |
+
graph.
|
| 133 |
+
|
| 134 |
+
Args:
|
| 135 |
+
prompt: The prompt to trace
|
| 136 |
+
collapse_vector: Optional vector to guide collapse in a specific direction
|
| 137 |
+
|
| 138 |
+
Returns:
|
| 139 |
+
Dictionary containing trace results and fragmentation metrics
|
| 140 |
+
"""
|
| 141 |
+
logger.info(f"Tracing attribution path for: {prompt[:50]}...")
|
| 142 |
+
|
| 143 |
+
# Default implementation for demonstration
|
| 144 |
+
# In a real implementation, this would use model-specific tracing
|
| 145 |
+
trace_results = {
|
| 146 |
+
"prompt": prompt,
|
| 147 |
+
"collapse_vector": collapse_vector or ".p/reflect.trace{target=reasoning, validate=true}",
|
| 148 |
+
"attribution_paths": self._simulate_attribution_paths(),
|
| 149 |
+
"broken_paths": self._simulate_broken_paths(),
|
| 150 |
+
"orphaned_nodes": self._simulate_orphaned_nodes(),
|
| 151 |
+
"continuity_score": np.random.uniform(0.4, 0.9) # Simulated score
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
# Update instance state
|
| 155 |
+
self.continuity_score = trace_results["continuity_score"]
|
| 156 |
+
self.broken_paths = trace_results["broken_paths"]
|
| 157 |
+
self.orphaned_nodes = trace_results["orphaned_nodes"]
|
| 158 |
+
|
| 159 |
+
return trace_results
|
| 160 |
+
|
| 161 |
+
def induce_collapse(
|
| 162 |
+
self,
|
| 163 |
+
prompt: str,
|
| 164 |
+
collapse_direction: str
|
| 165 |
+
) -> Dict[str, Any]:
|
| 166 |
+
"""
|
| 167 |
+
✰ COLLAPSE: Induce circuit fragmentation collapse along a specific direction
|
| 168 |
+
|
| 169 |
+
This method deliberately induces fragmentation in a specific direction,
|
| 170 |
+
forcing the model to expose broken reasoning chains in its attribution
|
| 171 |
+
path.
|
| 172 |
+
|
| 173 |
+
Args:
|
| 174 |
+
prompt: Base prompt to send to the model
|
| 175 |
+
collapse_direction: Direction to bias the fragmentation (e.g., "logical", "causal")
|
| 176 |
+
|
| 177 |
+
Returns:
|
| 178 |
+
Dictionary containing collapse results and fragmentation metrics
|
| 179 |
+
"""
|
| 180 |
+
logger.info(f"Inducing circuit fragmentation in direction: {collapse_direction}")
|
| 181 |
+
|
| 182 |
+
# Construct collapse vector based on direction
|
| 183 |
+
collapse_vector = f".p/reflect.trace{{target=reasoning, validate=true, focus={collapse_direction}}}"
|
| 184 |
+
|
| 185 |
+
# Trace with the collapse vector
|
| 186 |
+
trace_results = self.trace(prompt, collapse_vector)
|
| 187 |
+
|
| 188 |
+
# Set collapse state
|
| 189 |
+
self.collapse_state = "collapsed"
|
| 190 |
+
|
| 191 |
+
return {
|
| 192 |
+
"prompt": prompt,
|
| 193 |
+
"collapse_direction": collapse_direction,
|
| 194 |
+
"collapse_vector": collapse_vector,
|
| 195 |
+
"continuity_score": trace_results["continuity_score"],
|
| 196 |
+
"broken_paths": trace_results["broken_paths"],
|
| 197 |
+
"orphaned_nodes": trace_results["orphaned_nodes"]
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
def reconstruct_paths(self) -> Dict[str, Any]:
|
| 201 |
+
"""
|
| 202 |
+
△ OBSERVE: Attempt to reconstruct broken attribution paths
|
| 203 |
+
|
| 204 |
+
This method takes detected broken paths and orphaned nodes and
|
| 205 |
+
attempts to reconstruct the original attribution graph, revealing
|
| 206 |
+
the "intended" reasoning path that may have been fragmented during
|
| 207 |
+
collapse.
|
| 208 |
+
|
| 209 |
+
Returns:
|
| 210 |
+
Dictionary containing reconstruction results
|
| 211 |
+
"""
|
| 212 |
+
logger.info("Attempting to reconstruct broken attribution paths...")
|
| 213 |
+
|
| 214 |
+
# In a real implementation, this would use graph algorithms
|
| 215 |
+
# to reconnect orphaned nodes based on semantic similarity
|
| 216 |
+
reconstructed_paths = []
|
| 217 |
+
for path in self.broken_paths:
|
| 218 |
+
# Simulate path reconstruction
|
| 219 |
+
reconstructed = {
|
| 220 |
+
"original_path": path,
|
| 221 |
+
"reconnected_nodes": np.random.randint(1, 5),
|
| 222 |
+
"confidence": np.random.uniform(0.6, 0.9)
|
| 223 |
+
}
|
| 224 |
+
reconstructed_paths.append(reconstructed)
|
| 225 |
+
|
| 226 |
+
return {
|
| 227 |
+
"reconstructed_paths": reconstructed_paths,
|
| 228 |
+
"reconstruction_confidence": np.mean([p["confidence"] for p in reconstructed_paths]),
|
| 229 |
+
"remaining_orphans": max(0, len(self.orphaned_nodes) - sum(p["reconnected_nodes"] for p in reconstructed_paths))
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
def _construct_fragment_sensitive_prompt(
|
| 233 |
+
self,
|
| 234 |
+
prompt: str,
|
| 235 |
+
collapse_vector: Optional[str] = None
|
| 236 |
+
) -> str:
|
| 237 |
+
"""Construct a prompt that exposes circuit fragmentation."""
|
| 238 |
+
# Add reasoning elicitation to expose fragments
|
| 239 |
+
reasoning_prompt = f"Please think through this step by step, showing your complete reasoning chain: {prompt}"
|
| 240 |
+
|
| 241 |
+
# Add collapse vector if provided
|
| 242 |
+
if collapse_vector:
|
| 243 |
+
reasoning_prompt += f"\n\n{collapse_vector}"
|
| 244 |
+
|
| 245 |
+
return reasoning_prompt
|
| 246 |
+
|
| 247 |
+
def _query_model(self, model_interface: Any, prompt: str) -> str:
|
| 248 |
+
"""Send a query to the model and return the response."""
|
| 249 |
+
# This would actually call the model API
|
| 250 |
+
# For now, returning a placeholder
|
| 251 |
+
return f"Response to: {prompt[:30]}..."
|
| 252 |
+
|
| 253 |
+
def _query_model_state(self, model_interface: Any) -> Dict[str, Any]:
|
| 254 |
+
"""Capture the current internal state of the model."""
|
| 255 |
+
# This would capture attention weights, hidden states, etc.
|
| 256 |
+
# For now, returning a placeholder
|
| 257 |
+
return {
|
| 258 |
+
"timestamp": np.datetime64('now'),
|
| 259 |
+
"attention_weights": np.random.random((12, 12)), # Placeholder
|
| 260 |
+
"hidden_states": np.random.random((1, 12, 768)), # Placeholder
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
def _analyze_fragmentation(
|
| 264 |
+
self,
|
| 265 |
+
pre_state: Dict[str, Any],
|
| 266 |
+
post_state: Dict[str, Any],
|
| 267 |
+
response: str
|
| 268 |
+
) -> Dict[str, Any]:
|
| 269 |
+
"""Analyze circuit fragmentation between pre and post states."""
|
| 270 |
+
# This would use attribution analysis to find fragmentation
|
| 271 |
+
# For now, using simulated data
|
| 272 |
+
|
| 273 |
+
# Simulate continuity score
|
| 274 |
+
continuity_score = measure_path_continuity(
|
| 275 |
+
pre_state.get("attention_weights", np.array([])),
|
| 276 |
+
post_state.get("attention_weights", np.array([]))
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
# Simulate finding broken paths
|
| 280 |
+
broken_paths = self._simulate_broken_paths()
|
| 281 |
+
|
| 282 |
+
# Simulate finding orphaned nodes
|
| 283 |
+
orphaned_nodes = self._simulate_orphaned_nodes()
|
| 284 |
+
|
| 285 |
+
return {
|
| 286 |
+
"continuity_score": continuity_score,
|
| 287 |
+
"broken_paths": broken_paths,
|
| 288 |
+
"orphaned_nodes": orphaned_nodes,
|
| 289 |
+
"fragmentation_ratio": 1.0 - continuity_score
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
def _simulate_attribution_paths(self) -> List[Dict[str, Any]]:
|
| 293 |
+
"""Simulate attribution paths for demonstration purposes."""
|
| 294 |
+
# In a real implementation, these would be extracted from the model
|
| 295 |
+
paths = []
|
| 296 |
+
for i in range(5):
|
| 297 |
+
path = {
|
| 298 |
+
"path_id": f"path_{i}",
|
| 299 |
+
"source_token": f"token_{i*2}",
|
| 300 |
+
"sink_token": f"token_{i*2 + 5}",
|
| 301 |
+
"attention_heads": [np.random.randint(0, 12) for _ in range(3)],
|
| 302 |
+
"path_strength": np.random.uniform(0.3, 0.9)
|
| 303 |
+
}
|
| 304 |
+
paths.append(path)
|
| 305 |
+
return paths
|
| 306 |
+
|
| 307 |
+
def _simulate_broken_paths(self) -> List[Dict[str, Any]]:
|
| 308 |
+
"""Simulate broken paths for demonstration purposes."""
|
| 309 |
+
# In a real implementation, these would be detected from the model
|
| 310 |
+
broken = []
|
| 311 |
+
for i in range(2):
|
| 312 |
+
path = {
|
| 313 |
+
"path_id": f"broken_{i}",
|
| 314 |
+
"break_point": f"layer_{np.random.randint(1, 12)}",
|
| 315 |
+
"upstream_token": f"token_{np.random.randint(0, 10)}",
|
| 316 |
+
"downstream_token": f"token_{np.random.randint(11, 20)}",
|
| 317 |
+
"severity": np.random.uniform(0.5, 1.0)
|
| 318 |
+
}
|
| 319 |
+
broken.append(path)
|
| 320 |
+
return broken
|
| 321 |
+
|
| 322 |
+
def _simulate_orphaned_nodes(self) -> List[Dict[str, Any]]:
|
| 323 |
+
"""Simulate orphaned nodes for demonstration purposes."""
|
| 324 |
+
# In a real implementation, these would be detected from the model
|
| 325 |
+
orphans = []
|
| 326 |
+
for i in range(3):
|
| 327 |
+
node = {
|
| 328 |
+
"node_id": f"orphan_{i}",
|
| 329 |
+
"token": f"token_{np.random.randint(0, 20)}",
|
| 330 |
+
"activation": np.random.uniform(0.3, 0.8),
|
| 331 |
+
"expected_connections": np.random.randint(1, 4),
|
| 332 |
+
"isolation_score": np.random.uniform(0.6, 1.0)
|
| 333 |
+
}
|
| 334 |
+
orphans.append(node)
|
| 335 |
+
return orphans
|