Skip to main content

Zarvan: A Hybrid MoE Architecture for Advanced Sequence Modeling.

Project description

Zarvan: A Hybrid MoE Architecture for Advanced Sequence Modeling

PyPI Version Build Status License: Apache 2.0 Python 3.8+

Zarvan is an advanced neural network architecture designed to overcome the fundamental limitations of Transformers and RNNs. By unifying the strengths of parallel processing and stateful reasoning, Zarvan provides a powerful, scalable solution for the next generation of sequence modeling challenges.

This library is built on pure PyTorch, offering a lightweight, independent, and high-performance implementation of the Zarvan architecture.


🚀 Key Features

  • Hybrid Mixture-of-Experts (MoE) Architecture: Employs an intelligent MoE system that dynamically chooses between three "experts" to process a sequence: two for global pattern recognition and a dedicated state machine for step-by-step reasoning.
  • Linear Time Complexity ($O(S)$): By replacing the quadratic ($O(S^2)$) self-attention mechanism, Zarvan is significantly more efficient and ideal for processing ultra-long sequences.
  • 🧠 Stateful Sequential Reasoning: Features the Sequential Extractor, a deterministic state machine that maintains a perfect, non-decaying memory of the sequence history, enabling it to solve complex, path-dependent tasks where Transformers fail.
  • ⚡ Lightweight & Independent: Built on pure PyTorch with zero external dependencies beyond torch, ensuring easy integration, maximum flexibility, and no version conflicts.

🏛️ Architecture Overview

The core of Zarvan is a stack of identical blocks. Each block is a Mixture-of-Experts model that dynamically combines the outputs of three specialist modules via a learned gating network.

  1. Holistic Extractor: Captures the "gist" or overall summary of the sequence.
  2. Associative Extractor: Acts as a "focused memory" retriever for salient, sparse information.
  3. Sequential Extractor (The State Machine): Functions as a parallelized state machine that tracks the sequence history losslessly using gated accumulation and phase representation.

An Expert Gate then learns to weigh the outputs of these three modules for each token, allowing the model to adapt its strategy based on the input.


🚀 Installation

Install the package directly from PyPI:

pip install zarvan

Or after cloning the repository locally:

git clone [https://github.com/systbs/zarvan-torch.git](https://github.com/systbs/zarvan-torch.git)
cd zarvan-torch
pip install .

✨ Quick Start

Using the independent zarvan library is clean and simple.

import torch
from zarvan import Config, Zarvan

# --- Step 1: Create and Save Each Component Independently ---

# 1a. The Backbone
backbone_config = Config.Backbone(
    embed_dim=256,
    hidden_dim=1024,
    num_layers=6,
    num_heads=4,
)
backbone = Zarvan.Backbone(backbone_config)
backbone_save_dir = "./saved-zarvan-backbone"
backbone.save(backbone_save_dir)
print(f"Backbone saved to '{backbone_save_dir}'.")

# 1b. The Text Modality Head
text_modality_config = Config.Text(
    embed_dim=256, # Must match backbone's embed_dim
    vocab_size=30522,
    max_len=128
)
text_modality_head = Zarvan.Modality.Text(text_modality_config)
text_head_save_dir = "./saved-zarvan-text"
text_modality_head.save(text_head_save_dir)
print(f"Text Modality Head saved to '{text_head_save_dir}'.")

# 1c. The Classification Task Head (Finisher)
classification_config = Config.Classification(
    embed_dim=256, # Must match backbone's embed_dim
    num_classes=10
)
classification_task_head = Zarvan.Task.Classification(classification_config)
task_head_save_dir = "./saved-zarvan-classification"
classification_task_head.save(task_head_save_dir)
print(f"Classification Task Head saved to '{task_head_save_dir}'.")
print("-" * 50)


# --- Step 2: Load and Combine Components for Inference ---

# Load each independent component
loaded_backbone = Zarvan.Backbone.load(backbone_save_dir)
loaded_text_head = Zarvan.Modality.Text.load(text_head_save_dir)
loaded_task_head = Zarvan.Task.Classification.load(task_head_save_dir)

# Set all components to evaluation mode
loaded_backbone.eval()
loaded_text_head.eval()
loaded_task_head.eval()

print("All components loaded successfully.")
print("-" * 50)


# --- Step 3: Perform a Forward Pass ---

# Create dummy input data
input_ids = torch.randint(0, text_modality_config.vocab_size, (2, 50)) # (Batch, SeqLen)

# The forward pass is a chain of the three components
with torch.no_grad():
    # 1. Modality head converts tokens to embeddings
    embeddings = loaded_text_head(input_ids)
    # 2. Backbone processes the embeddings
    hidden_states = loaded_backbone(embeddings)
    # 3. Task head produces the final output (logits)
    logits = loaded_task_head(hidden_states, task='classification')

print("--- I/O Shapes ---")
print("Input IDs Shape:     ", input_ids.shape)
print("Embeddings Shape:    ", embeddings.shape)
print("Hidden States Shape: ", hidden_states.shape)
print("Logits Shape:        ", logits.shape)
# Expected Logits shape: torch.Size([2, 10])
print("-" * 50)

print("Model composition and forward pass successful. ✅")

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

zarvan-0.2.0.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

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

zarvan-0.2.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file zarvan-0.2.0.tar.gz.

File metadata

  • Download URL: zarvan-0.2.0.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for zarvan-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ac2fe4d058536c08f8f8b1daab0db6672e6c3073abc1d4a4be053de959a6795d
MD5 b4c312ff26d3962146d8ace1e27c0fe3
BLAKE2b-256 40966be441a34c1613bc26fe1dd5aa6fc034cdac33409106cd343c4821775918

See more details on using hashes here.

File details

Details for the file zarvan-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: zarvan-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for zarvan-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5a508f6c0700cff51b7ef2a441eb9e00108b04f6aa3883b3feed97741a13e1c
MD5 0fe6aea903d4ed2e1adc027b1f8e1c35
BLAKE2b-256 a3cae2ec3472a480ba6c5b5b0ca4f3963e6fef0b1fdf925b5adac65c1dbbab0f

See more details on using hashes here.

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