Skip to main content

RILA: Recursive Indexed Language Architecture — a production-ready PyTorch implementation

Project description

pyrila

RILA: Recursive Indexed Language Architecture

A production-ready PyTorch implementation of the RILA architecture — a novel language model that combines contextual indexing, adaptive retrieval, recursive internal reasoning, and autonomous result verification.

Key Features

  • Contextual Indexing — Organizes input into Context Cells with a dynamic affinity graph for fast retrieval
  • Adaptive Retrieval — Learns what, when, and how much context to retrieve without specialized datasets
  • Recursive Reasoning — Internal reasoning loop with budget control, no intermediate text generation
  • Self-Verification — Multi-dimensional confidence evaluation before committing to output
  • Scale-Invariant — Same architecture from 2M to 1.3B+ parameters

How RILA Differs from Traditional Transformers

Traditional transformers apply dense attention over all tokens at every layer, scaling quadratically with context length. RILA replaces this with structured context cells and a learned affinity graph — only relevant cells are retrieved and processed deeply, enabling sub-quadratic scaling. The recursive reasoning loop allows the model to iteratively refine its understanding with autonomous budget control, something fixed-depth transformers cannot do.

Architecture

┌────────────────────────────────────────────────────────────────────┐
│                     RILA PIPELINE                                   │
│                                                                    │
│  Input Tokens                                                      │
│       │                                                            │
│       ▼                                                            │
│  [Tokenizer] → [Cell Builder] → [Cell Encoder]                    │
│                                       │                            │
│                                       ▼                            │
│                              [Context Index]  (affinity graph)     │
│                                       │                            │
│                                       ▼                            │
│                         [Recursive Context Explorer]               │
│                                       │                            │
│                                       ▼                            │
│                             [Working Context]                      │
│                                       │                            │
│                                       ▼                            │
│                        [Core Language Processor]                   │
│                           (iterative GRU + convergence)            │
│                                       │                            │
│                                       ▼                            │
│                         [Pre-Output Generator]                     │
│                                       │                            │
│                                       ▼                            │
│                   [Recursive Verification Engine]                  │
│                          confidence >= τ ?                         │
│                         /              \                           │
│                       Yes               No                        │
│                        │                 │                         │
│                        │      [Reasoning Loop]                    │
│                        │      (budget-limited)                    │
│                        │           │                              │
│                        ▼           ▼                              │
│                     [Final Decoder]                                │
│                          │                                        │
│                          ▼                                        │
│                    Output Tokens                                   │
└────────────────────────────────────────────────────────────────────┘

See docs/ARCHITECTURE.md for the full architecture explanation with mathematical formulas.

Installation

pip install pyrila

For development:

git clone https://github.com/bueormnew/pyrila.git
cd pyrila
pip install -e ".[dev]"

Quick Start

import torch
from pyrila import RILA, RILAConfig, rila_small

# Create a small model for testing
config = rila_small()
model = RILA(config)

# Forward pass with teacher forcing
input_ids = torch.randint(0, config.vocab_size, (1, 1024))
target_ids = torch.randint(0, config.vocab_size, (1, 64))
outputs = model(input_ids, target_ids=target_ids)

print(f"Logits shape: {outputs['logits'].shape}")
print(f"Confidence: {outputs['confidence'].mean().item():.4f}")
print(f"Budget used: {outputs['budget_used']} cycles")

Preset Configurations

Preset Parameters Use Case
rila_small() ~2M Testing and prototyping
rila_base() ~125M Research experiments
rila_large() ~350M Production workloads
rila_xl() ~1.3B Maximum capability

Training

from pyrila import RILA, RILATrainer, rila_small

config = rila_small()
model = RILA(config)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
trainer = RILATrainer(model, optimizer, use_uncertainty_weighting=True)

batch = {
    "input_ids": torch.randint(0, config.vocab_size, (2, 1024)),
    "target_ids": torch.randint(0, config.vocab_size, (2, 64)),
}
result = trainer.train_step(batch)
print(f"Loss: {result['loss']:.4f}")

Save & Load

# Save model (config + weights)
model.save("./my_model")

# Load model
from pyrila import RILA
model = RILA.load("./my_model", device="cuda")

Tokenizer-Agnostic

RILA accepts any tokenizer. Configure special token IDs in the config:

from pyrila import RILA, RILAConfig

config = RILAConfig(
    vocab_size=32000,       # Match your tokenizer's vocab
    pad_token_id=0,         # Your tokenizer's PAD
    bos_token_id=1,         # Your tokenizer's BOS
    eos_token_id=2,         # Your tokenizer's EOS
    # ... other params
)
model = RILA(config)

# Use any tokenizer (HuggingFace, SentencePiece, tiktoken, etc.)
input_ids = your_tokenizer.encode("Hello world")
output = model(torch.tensor([input_ids]))

Error Handling

pyrila provides a hierarchy of custom exceptions for clear, actionable error messages:

from pyrila import PyRILAError, ConfigurationError, SequenceTooLongError

# All pyrila exceptions inherit from PyRILAError
try:
    output = model(input_ids)
except PyRILAError as e:
    print(f"pyrila error: {e}")

# Specific exceptions provide context
try:
    config = RILAConfig(hidden_dim=100, num_heads=12)
except ConfigurationError as e:
    print(e.param_name, e.value)  # "hidden_dim", 100

Available exceptions: ConfigurationError, IndexNotBuiltError, DivergenceError, GradientError, TeacherForcingError, SequenceTooLongError, BudgetExhaustedError, CheckpointError.

Documentation

Contributing

Contributions are welcome! To get started:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Install development dependencies: pip install -e ".[dev]"
  4. Run tests: pytest tests/ -v
  5. Submit a pull request

Please ensure all tests pass and follow the existing code style.

Citation

@software{pyrila2024,
  title={pyrila: Recursive Indexed Language Architecture},
  author={RILA Team},
  year={2024},
  url={https://github.com/bueormnew/pyrila},
  version={0.3.0}
}

License

MIT — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrila-0.3.0.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrila-0.3.0-py3-none-any.whl (48.1 kB view details)

Uploaded Python 3

File details

Details for the file pyrila-0.3.0.tar.gz.

File metadata

  • Download URL: pyrila-0.3.0.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyrila-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d1189e876c7e8cf0be2595421d452773e8b06334588d0b9b76ca714df9791b3f
MD5 7518549037baa1652c7bc8d6fe7762d4
BLAKE2b-256 99c6f61ef45c9c2574288d3caab7dce4a431cffec31dd08bdfba58f24379d055

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrila-0.3.0.tar.gz:

Publisher: publish.yml on bueormnew/pyrila

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyrila-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyrila-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 48.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyrila-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b36bf52cd1c160a566481d39a5193b3cbffcbbf9914385ddb5e55536adf4f064
MD5 5f257db7705a4f6de0754a339c5e1850
BLAKE2b-256 0120ba2d2594e51864bc9e44ef8cbe2ee754ddd44da2a325182c1a9879eaa28c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyrila-0.3.0-py3-none-any.whl:

Publisher: publish.yml on bueormnew/pyrila

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page