Skip to content

DivyamTalwar/OUROBOROS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

OUROBOROS Banner

🐍 OUROBOROS

Optimal Unified Residual Operations with Bounded Orthogonal Reflection and Spectral Control

Teaching Neural Networks the Art of Forgetting

Python 3.8+ PyTorch License: CC BY 4.0 HuggingFace

Overview β€’ Architecture β€’ Installation β€’ Quick Start β€’ Theory β€’ Insights


"The serpent that devours itself to be reborn β€” features consumed, transformed, and emerged anew."


πŸŒ€ Overview

The Problem

Standard residual networks can only add information. They lack the ability to erase, forget, or reflect β€” leading to residual accumulation where noisy features persist indefinitely.

Our Solution

A geometric residual connection that learns:

  • 🧠 When to remember β€” preserve critical features
  • πŸ—‘οΈ When to forget β€” erase noise and outdated info
  • πŸ”„ When to transform β€” flip representations
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Traditional ResNet    β”‚
β”‚                         β”‚
β”‚  X_{l+1} = X_l + F(X)   β”‚
β”‚                         β”‚
β”‚    ❌ Can only ADD      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      OUROBOROS          β”‚
β”‚                         β”‚
β”‚  X_{l+1} = AΒ·X + Ξ²Β·kΒ·vα΅€ β”‚
β”‚                         β”‚
β”‚ βœ… ADD, ERASE, REFLECT  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

OUROBOROS enables neural networks to:

Capability Description
✨ Selective Forgetting Surgically erase outdated or noisy information
πŸ”„ Feature Reflection Model oscillatory and oppositional dynamics
🎯 Spectral Control Shape layer-wise transitions with precision
⚑ Gradient Stability Maintain gradient flow with gated identity

πŸ—οΈ Architecture

The Delta Operator

At the heart of OUROBOROS lies the Delta Operator β€” a generalized Householder transformation:

A(X) = I βˆ’ Ξ²(X) Β· k(X) Β· k(X)α΅€

Symbol Name Description Range
k(X) Reflection Direction Unit vector defining transformation axis β€–kβ€– = 1
Ξ²(X) Scalar Gate Controls transformation intensity [0, 2]
v(X) Value Vector New information to inject β„α΅ˆα΅›

⚑ The Magic of Ξ² β€” One Scalar, Three Transformations

Beta Spectrum

A single learnable scalar dynamically interpolates between three geometric operations:

Ξ² Value Transformation Eigenvalue Effect
Ξ² β†’ 0 Identity Ξ» = 1 Pass through unchanged
Ξ² β†’ 1 Projection Ξ» = 0 Erase component along k
Ξ² β†’ 2 Reflection Ξ» = -1 Flip direction along k

πŸ”„ Geometric Visualization

Geometric Transformations

Vector v transformed by Delta Operator. P(v) = projection (Ξ²=1), R(v) = reflection (Ξ²=2). Vector k = hyperplane normal.


🌊 Data Flow

Data Flow

The input X splits into three learnable branches that compute k, Ξ², and v, which combine through the Delta operation with a skip connection.


πŸ›οΈ Full Model Architecture

Model Architecture

Each OuroborosBlock contains:

  • RMSNorm β†’ Attention β†’ Ouroboros Residual
  • RMSNorm β†’ MLP β†’ Ouroboros Residual

🧬 The Ouroboros Residual Block

Ouroboros Block Architecture

Core Update Rule β€” The Delta Rule

X_{l+1} = X_l + Ξ² Β· k Β· (vα΅€ βˆ’ kα΅€ Β· X_l)
                        ↑         ↑
                     TARGET    CURRENT
               (what to write) (what exists)

This unifies three operations with a single gate:

Operation Formula Effect
Erasure βˆ’Ξ² Β· k Β· (kα΅€ Β· X) Removes component along k
Writing +Ξ² Β· k Β· vα΅€ Injects new information
Sync Same Ξ² Both scale together

πŸ“ Mathematical Foundations

Spectral Decomposition Theorem

Theorem: For A = I βˆ’ Ξ²Β·kΒ·kα΅€ where β€–kβ€– = 1:

Οƒ(A) = { 1, 1, ..., 1, (1βˆ’Ξ²) }
         β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         (dβˆ’1) times
Property Formula Notes
Eigenvalue along k Ξ»_k = 1 βˆ’ Ξ² Controlled by gate
Eigenvalues in kβŠ₯ Ξ» = 1 Multiplicity: dβˆ’1
Determinant det(A) = 1 βˆ’ Ξ² Zero at Ξ²=1
Orthogonality Aα΅€A = I When Ξ² ∈ {0, 2}
Involution AΒ² = I When Ξ² = 2

Why Standard ResNets Are Limited

Property ResNet OUROBOROS
Eigenvalues β‰ˆ 1 + Ξ΅ ∈ [-1, 1]
Negative Ξ» ❌ No βœ… Yes
Singular ❌ No βœ… Yes (Ξ²=1)
Data-dependent ❌ Fixed βœ… Learnable

πŸ“Š Feature Coupling

Feature Coupling

Key Insight: The geometric coherence term k_i Β· k_j enables learned feature interactions without explicit cross-attention.


πŸš€ Installation

# Clone the repository
git clone https://github.com/DivyamTalwar/OUROBOROS.git
cd ouroboros

# Install dependencies
pip install torch>=2.0 transformers einops

# Install in development mode
pip install -e .

Requirements

Package Version Purpose
Python β‰₯ 3.8 Runtime
PyTorch β‰₯ 2.0 Deep learning
Transformers β‰₯ 4.30 HuggingFace
einops β‰₯ 0.6 Tensor ops

⚑ Quick Start

Basic Usage

from model.ouroboros import OuroborosModel, OuroborosConfig
import torch

# Configure the model
config = OuroborosConfig(
    vocab_size=50304,
    hidden_size=768,
    num_hidden_layers=12,
    num_attention_heads=6,
    head_dim=128,
)

# Initialize model
model = OuroborosModel(config)
print(f"Parameters: {model.get_num_params():,}")

# Forward pass
input_ids = torch.randint(0, 50304, (2, 512))
labels = input_ids.clone()

logits, loss = model(input_ids, targets=labels)
print(f"Loss: {loss.item():.4f}")

Training Loop

from torch.optim import AdamW

model = OuroborosModel(config).cuda()
optimizer = AdamW(model.parameters(), lr=3e-4, weight_decay=0.1)

for batch in dataloader:
    input_ids, labels = batch['input_ids'].cuda(), batch['labels'].cuda()
    
    logits, loss = model(input_ids, targets=labels)
    
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    
    print(f"Loss: {loss.item():.4f}")

πŸ”§ Configuration Reference

Model Parameters

Parameter Type Default Description
vocab_size int 50304 Vocabulary size
hidden_size int 768 Model dimension
num_hidden_layers int 12 Number of blocks
num_attention_heads int 6 Attention heads
head_dim int 128 Dimension per head
block_size int 1024 Max sequence length

Ouroboros Parameters

Parameter Type Default Description
ouroboros_k_eps float 1e-6 k normalization Ξ΅
ouroboros_beta_init float 1.0 Initial Ξ² [0, 2]
ouroboros_v_sigmoid bool True Sigmoid on v
ouroboros_v_sigmoid_scale float 4.0 v scale factor

πŸ“ Project Structure

OUROBOROS/
β”œβ”€β”€ πŸ“„ README.md              # Documentation
β”œβ”€β”€  assets/                # Images
β”‚   β”œβ”€β”€ banner.png
β”‚   β”œβ”€β”€ architecture.png
β”‚   β”œβ”€β”€ beta_spectrum.png
β”‚   β”œβ”€β”€ geometric_transform.png
β”‚   β”œβ”€β”€ dataflow.png
β”‚   β”œβ”€β”€ model_architecture.png
β”‚   └── feature_coupling.png
└── πŸ“ model/
    └── ouroboros.py          # Core implementation

πŸ’‘ Key Insights

Why OUROBOROS Works

Challenge ResNet OUROBOROS
Noisy features accumulate ❌ Can only add βœ… Can erase
Oscillatory patterns ❌ No negative Ξ» βœ… Ξ» ∈ [-1, 1]
Feature interference ❌ No filter βœ… Projection
Gradient stability βœ… Identity βœ… Gated identity

Depth-Wise Delta Rule

OUROBOROS is the depth-wise dual of time-wise recurrence:

Time (DeltaNet):     S_t     = A Β· S_{t-1}  + Ξ² Β· k Β· vα΅€
Depth (OUROBOROS):   X_{l+1} = A Β· X_l      + Ξ² Β· k Β· vα΅€

πŸ”¬ Advanced Topics

Invertibility

When Ξ² β‰  1, the Delta Operator is invertible:

A⁻¹ = I + (Ξ² / (1βˆ’Ξ²)) Β· k Β· kα΅€

At β = 2: A = A⁻¹ (orthogonal involution).


πŸ“ Citation

@software{ouroboros2025,
  title   = {OUROBOROS: Optimal Unified Residual Operations with 
             Bounded Orthogonal Reflection and Spectral Control},
  year    = {2025},
  url     = {https://github.com/DivyamTalwar/OUROBOROS}
}

🀝 Contributing

  1. Fork the repository
  2. Create feature branch: git checkout -b feature/amazing
  3. Commit changes: git commit -m 'Add feature'
  4. Push: git push origin feature/amazing
  5. Open a Pull Request

πŸ“œ License

Creative Commons Attribution 4.0 International (CC-BY-4.0)


🐍 OUROBOROS

The ancient serpent eating its own tail β€” a symbol of cyclical transformation.

Features are consumed, transformed, and reborn through each layer.


Built with πŸ’œ for the ML community

⬆️ Back to Top

About

ResNet + learnable geometric transformation that lets the network forget/reflect features, not just add to them.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages