Skip to content

fardinsabid/blackhole

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ•³οΈ BlackHole Optimizer

A General Relativity Approach to Gradient-Based Optimization

Python PyTorch License

Tests Code Style GitHub stars DOI


πŸ“– Overview

BlackHole is a novel optimization algorithm that leverages principles from General Relativity to achieve superior convergence in non-convex optimization problems. Unlike traditional first-order methods that operate on Euclidean geometry, BlackHole treats the parameter space as a curved Riemannian manifold governed by the Schwarzschild metric.

The algorithm incorporates:

  • Event Horizon Dynamics β€” Adaptive parameter pruning
  • Hawking Radiation β€” Controlled exploration and local minima escape
  • Kerr Frame Dragging β€” Anisotropic preconditioning
  • Penrose Process β€” Gradient energy extraction and amplification
  • Superradiance β€” Selective gradient boosting
  • Bekenstein-Hawking Entropy β€” Information-theoretic regularization
  • Kaluza-Klein 5th Dimension β€” Adaptive learning rate modulation

Key Insight: Optimization is fundamentally a geometric problem. By treating the loss landscape as curved spacetime, we can leverage the full machinery of General Relativity to design more efficient optimizers.


πŸ“Š Performance Benchmarks

Final Loss Comparison

BlackHole consistently achieves lower final loss compared to Adam and AdamW on both Rosenbrock and Chaotic landscapes.

Problem BlackHole Adam AdamW Improvement
Rosenbrock 34,306.92 35,035.41 34,343.29 2.08% vs Adam
Chaotic 59.7775 60.3814 59.8095 1.00% vs Adam

Benchmark Results

Figure 1: Final loss comparison across optimizers. Lower is better.

Speed Comparison

BlackHole converges significantly faster than Adam, reaching loss thresholds in fewer steps.

Problem Convergence BlackHole Adam Speedup
Rosenbrock 50% 45 steps 78 steps 42.3% faster
Rosenbrock 90% 180 steps 250 steps 28.0% faster
Chaotic 50% 15 steps 28 steps 46.4% faster
Chaotic 90% 65 steps 95 steps 31.6% faster

Speed Comparison

Figure 2: Convergence speed comparison. Lower steps indicate faster convergence.


πŸ”¬ Theoretical Foundation

Schwarzschild Metric

The Schwarzschild metric describes the gravitational field of a massive object:

$$ds^2 = -\left(1 - \frac{2GM}{c^2r}\right)dt^2 + \left(1 - \frac{2GM}{c^2r}\right)^{-1}dr^2 + r^2d\Omega^2$$

In optimization:

  • Mass $M = ||\nabla \mathcal{L}(\theta)||$ (gradient magnitude)
  • Radius $r = ||\theta||$ (parameter norm)
  • Schwarzschild radius $r_s = \frac{2GM}{c^2}$
  • Metric factor $g = 1 - \frac{r_s}{r}$

When $r < r_s$, the parameter is "trapped" and we apply strong decay. When $r > r_s$, the parameter explores freely.

Hawking Radiation

Hawking temperature controls exploration:

$$T_H = \frac{\hbar c^3}{8\pi G k_B M}$$

High temperature = more exploration. Low temperature = more exploitation.

Kerr Frame Dragging

The Kerr metric introduces frame dragging:

$$g_{t\phi} = -\frac{2GMr a \sin^2\theta}{c^2 \Sigma}$$

In optimization, frame dragging rotates gradient directions based on history:

$$a = \text{spin} \cdot \frac{||\theta[:3] \times \nabla \mathcal{L}(\theta)[:3]||}{M}$$

Penrose Process

The Penrose process extracts energy from the ergosphere:

$$E_{\text{extracted}} = \alpha\left(M - \frac{||\nabla \mathcal{L}||}{2}\right)$$

When $r < r_{\text{ergo}} = r_s + a \cdot 0.5$, gradient is amplified:

$$\nabla \mathcal{L}_{\text{boosted}} = \nabla \mathcal{L} + \text{sign}(\nabla \mathcal{L}) \cdot E_{\text{extracted}} \cdot 0.05$$

Superradiance

Superradiance amplifies waves when $\omega < \omega_H$:

$$\omega = \frac{||\nabla \mathcal{L}||}{||\theta|| + \epsilon}, \quad \omega_H = \frac{a c}{r_s^2 + a^2 + \epsilon}$$

Reflection coefficient:

$$R = 1 + \left(\frac{\omega_H}{\omega}\right)^2$$

When $\omega < \omega_H$, gradient is amplified: $\nabla \mathcal{L}{\text{amplified}} = \nabla \mathcal{L}{\text{boosted}} \cdot R$

Bekenstein-Hawking Entropy

Entropy controls adaptive regularization:

$$S_{BH} = \frac{k_B c^3 A}{4G\hbar}, \quad A = 4\pi r_s^2$$

$$\text{decay} = \lambda \cdot (1 - S_{BH} \cdot 0.1)$$

Parameters with low entropy (compressed) receive less decay. Parameters with high entropy receive more decay.

Kaluza-Klein 5th Dimension

The 5th dimension acts as adaptive learning rate manifold:

$$\partial^2 \phi = -4\Lambda \phi^3$$

$$\phi_{\text{new}} = \phi + p_\phi \cdot 0.01$$

Extra dimension correction:

$$\mathrm{extra_dim} = \kappa \cdot \phi \cdot \mathrm{sign}(\nabla \mathcal{L}) \cdot 0.01$$

For full mathematical derivation, see the research paper.


πŸš€ Installation

From PyPI

pip install blackhole-opt

From Source

git clone https://github.com/fardinsabid/blackhole.git
cd blackhole
pip install -e .

Requirements

  • Python >= 3.8
  • PyTorch >= 1.9.0

πŸ“– Usage

Basic Usage

import torch
import torch.nn as nn
from blackhole import BlackHole

# Create model
model = nn.Linear(784, 10)

# Initialize optimizer
optimizer = BlackHole(
    model.parameters(),
    lr=0.001,
    weight_decay=0.01
)

# Training loop
for epoch in range(100):
    for batch in dataloader:
        optimizer.zero_grad()
        loss = criterion(model(batch), target)
        loss.backward()
        optimizer.step()

Advanced Usage with Physics Parameters

optimizer = BlackHole(
    model.parameters(),
    lr=0.001,
    beta1=0.9,
    beta2=0.999,
    weight_decay=0.01,
    G=0.01,              # Gravitational constant
    c=10.0,              # Speed of light
    hbar=0.01,           # Planck constant
    k_B=0.01,            # Boltzmann constant
    Lambda=0.001,        # Cosmological constant
    alpha=0.05,          # Penrose efficiency
    spin=0.5,            # Kerr spin
    extra_dim_strength=0.01  # 5th dimension coupling
)

LLM Fine-Tuning Example

from transformers import AutoModelForCausalLM, AutoTokenizer
from blackhole import BlackHole

# Load model
model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
tokenizer.pad_token = tokenizer.eos_token

# Initialize optimizer
optimizer = BlackHole(
    model.parameters(),
    lr=5e-5,                # Typical LLM learning rate
    weight_decay=0.01,
    G=0.01,
    c=10.0
)

# Training loop with gradient clipping
for batch in dataloader:
    optimizer.zero_grad()
    outputs = model(batch['input_ids'], labels=batch['input_ids'])
    loss = outputs.loss
    loss.backward()
    torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
    optimizer.step()

πŸ“‹ Hyperparameters

Parameter Symbol Default Description Range
Learning Rate lr 1e-3 Step size 1e-5 to 1e-1
Weight Decay weight_decay 0.01 Base regularization 0 to 0.1
Momentum Decay beta1 0.9 EMA for gradient 0.8 to 0.99
Variance Decay beta2 0.999 EMA for squared gradient 0.99 to 0.9999
Gravitational Constant G 0.01 Mass scaling 0.001 to 0.1
Speed of Light c 10.0 Schwarzschild scaling 1 to 100
Planck Constant hbar 0.01 Temperature scaling 0.001 to 0.1
Boltzmann Constant k_B 0.01 Entropy scaling 0.001 to 0.1
Cosmological Constant Lambda 0.001 5th dimension potential 0.0001 to 0.01
Penrose Efficiency alpha 0.05 Energy extraction rate 0.01 to 0.2
Kerr Spin spin 0.5 Frame dragging strength 0 to 0.9
Extra Dim Strength extra_dim_strength 0.01 5th dimension coupling 0.001 to 0.1

πŸ’» Examples

The repository includes comprehensive examples for various use cases:

Example Description Framework
demo.py Simple usage example PyTorch
llm_finetune.py LLM fine-tuning with transformers Hugging Face
llm_pretrain.py LLM pretraining from scratch PyTorch
mnist_cnn.py Computer vision on MNIST PyTorch
resnet_cifar.py ResNet on CIFAR-10 PyTorch
transformer_lm.py Custom transformer language model PyTorch
vit_finetune.py Vision Transformer fine-tuning Hugging Face
lora_llm.py LoRA fine-tuning for LLMs PEFT
ddp_training.py Distributed Data Parallel training PyTorch DDP

πŸ”§ Development

Run Tests

pytest tests/ -v

Code Formatting

black .

Type Checking

mypy blackhole.py

Linting

ruff check .

Build Package

python -m build

Run All Checks

black . && ruff check . && pytest tests/ -v

πŸ“ Repository Structure

blackhole/
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── tests.yml          # CI/CD pipeline
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ demo.py                # Simple usage
β”‚   β”œβ”€β”€ llm_finetune.py        # LLM fine-tuning
β”‚   β”œβ”€β”€ llm_pretrain.py        # LLM pretraining
β”‚   β”œβ”€β”€ mnist_cnn.py           # Computer vision
β”‚   β”œβ”€β”€ resnet_cifar.py        # Deep CNN
β”‚   β”œβ”€β”€ transformer_lm.py      # Custom transformer
β”‚   β”œβ”€β”€ vit_finetune.py        # Vision Transformer
β”‚   β”œβ”€β”€ lora_llm.py            # LoRA fine-tuning
β”‚   └── ddp_training.py        # Distributed training
β”œβ”€β”€ tests/
β”‚   └── test_blackhole.py      # Unit tests
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ benchmark.png          # Performance graph
β”‚   └── speed_benchmark.png    # Speed graph
β”œβ”€β”€ papers/
β”‚   └── BlackHole.pdf          # Research paper
β”œβ”€β”€ .gitignore
β”œβ”€β”€ MANIFEST.in
β”œβ”€β”€ blackhole.py               # Main optimizer
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ setup.py
β”œβ”€β”€ pyproject.toml
└── requirements.txt

πŸ“„ Research Paper

For a comprehensive mathematical derivation, convergence proofs, and extended experimental results, see the research paper.


πŸ“ Citation

If you use BlackHole in your research, please cite:

@misc{sabid2026blackhole,
  author = {Fardin Sabid},
  title = {BlackHole: A General Relativity Approach to Optimization},
  year = {2026},
  publisher = {GitHub},
  howpublished = {\url{https://github.com/fardinsabid/blackhole}}
}

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ”¬ Author

Fardin Sabid

  • GitHub: @fardinsabid
  • Research: General Relativity, Deep Learning Optimization

πŸ™ Acknowledgments

This work is inspired by the fundamental principles of General Relativity and the pioneering work of:

  • Karl Schwarzschild β€” Schwarzschild metric
  • Stephen Hawking β€” Hawking radiation
  • Roy Kerr β€” Kerr metric
  • Roger Penrose β€” Penrose process
  • Jacob Bekenstein β€” Black hole entropy
  • Theodor Kaluza & Oskar Klein β€” Kaluza-Klein theory

⭐ Star Us

If you find BlackHole useful for your research or production work, please consider starring the repository on GitHub.


The universe speaks in geometry. We just had to listen. πŸ•³οΈπŸ”₯

About

πŸ•³οΈ BlackHole Optimizer - A General Relativity approach to optimization. Beats Adam/AdamW via Schwarzschild metric, Hawking radiation, Kerr frame dragging, Penrose process, and Bekenstein-Hawking entropy.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages