Skip to main content

English | Bahasa Indonesia | 简体中文 | 日本語 | 한국어 | Español | Français | Deutsch | Русский | العربية

Dual-Loop Cognitive Controller

Hardware-Aligned Latent Deliberation, Context Directional Routing & Memory Architecture for Any Transformer

PyPI version Python Versions PyTorch Hugging Face GitHub License


Overview

Dual-Loop Cognitive Controller is a universal framework that equips standard autoregressive Transformers with dual-process System 1 (fast, intuitive) and System 2 (deliberative) cognitive capabilities.

Instead of generating hundreds or thousands of expensive Chain-of-Thought (CoT) text tokens, Dual-Loop deliberates recursively in continuous latent vector space ($D=2048\dots 10240$) inside GPU SRAM/L2 cache:

  • Zero Output Token Waste: Millisecond latent deliberation without KV-cache explosion or context bloat (0 extra text tokens).
  • Context Directional Bipolar Router: Projects tasks into a directional manifold ($\rho_{\text{direction}}$): Scientific inquiry routes upwards to deep System 2 deliberation, while everyday reality routes downwards to common-sense grounding.
  • Compact Common-Sense Reservoir ($f \circ g$): Stores foundational physical reality axioms in a micro-prototype matrix ($< 50\text{ KB}$ in RAM), eliminating associative overthinking.
  • Probabilistic Soft Belief Revision & 2x-Think Gating: Replaces brittle hard-locks with soft penalties, enabling adaptive belief updates upon overwhelming deliberative evidence ($76.00%$ Macro Accuracy on standard N=75 suite).
  • Zero Negative Drift: Directional Safety Projection ensures confident intuitive answers are never degraded.
  • Universal Compatibility: Attaches to any causal Transformer (LLaMA, Mistral, Qwen, Gemma, DeepSeek, Phi) and scales from 1B to 120B+ models with multi-GPU sharding and 4-bit quantization.

📖 Full Documentation, Empirical Scoreboards & Architectural Comparisons:
For the complete benchmark report (75-item standard benchmark suite, token overload analysis, and system comparison graphs), please visit our GitHub Repository.


Installation

# Core package
pip install dual-loop-controller

# With Hugging Face Transformers & Accelerate
pip install "dual-loop-controller[llm]"

Quickstart

1. Universal Model Attachment in 3 Lines

Attach the controller to any standard Hugging Face model (Llama, Mistral, Qwen, Gemma, etc.):

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from dual_loop import attach_dual_loop

# 1. Load your model
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"  # or "Qwen/Qwen2.5-7B", "mistralai/Mistral-7B-v0.3"
tokenizer = AutoTokenizer.from_pretrained(model_id)
base_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")

# 2. Attach Dual-Loop Controller (automatically attaches to optimal middle layer)
model = attach_dual_loop(base_model, k_steps=2)

# 3. Deliberative inference in latent space (Zero Extra Text Tokens)
prompt = "Question: In inverted buoyancy physics, denser objects float. Does lead or cork float?\nAnswer:"
inputs = tokenizer(prompt, return_tensors="pt").to(base_model.device)
output = model.generate(**inputs, max_new_tokens=64)
print(tokenizer.decode(output[0], skip_special_tokens=True))

2. Directional Router & Probabilistic Cognitive Judge

from dual_loop import ProbabilisticCognitiveJudge

# Initialize Cognitive Judge with Directional Manifold & Common-Sense Reservoir (f o g)
judge = ProbabilisticCognitiveJudge(
    cs_margin_threshold=0.35,
    base_lambda=0.85,
    intuitive_lambda=0.20,
    soft_penalty_weight=4.5,
    allow_belief_revision=True,
    use_directional_reservoir=True
)

prompt = "Which requires energy to move?"
choices = ["weasel", "willow", "mango", "poison ivy"]
labels = ["A", "B", "C", "D"]

scores_base = [-8.40759, -8.40907, -14.929, -5.713]
scores_delib = [-7.5420, -5.9615, -13.826, -5.317]

# Evaluates candidates with directional routing and soft belief revision
decision = judge.judge_and_fuse(
    scores_base=scores_base,
    scores_delib=scores_delib,
    labels=labels,
    banned_labels=["D"],  # Previously logged wrong choice
    prompt=prompt,
    choices=choices
)

print("Predicted Choice :", decision["pred_label"])   # -> 'A' (weasel - CORRECT)
print("Manifold Vector  :", decision["direction"])    # -> 'DOWN_COMMONSENSE'
print("Grounding Delta  :", decision["cs_deltas"])   # -> [+2.2, -0.8, -0.8, -0.8]

3. Large Models (27B, 70B, 120B+) with 4-Bit Quantization

Scale to massive models without 30–60 second CoT latency or VRAM exhaustion:

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from dual_loop import attach_dual_loop

# 4-bit NF4 quantization for large parameters
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

model_id = "Qwen/Qwen2.5-27B-Instruct"  # or "meta-llama/Meta-Llama-3-70B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
base_model = AutoModelForCausalLM.from_pretrained(
    model_id,
    quantization_config=bnb_config,
    device_map="auto"  # Shards across available GPUs
)

# Automatically matches quantized layer device & precision
model = attach_dual_loop(base_model, k_steps=2)

inputs = tokenizer("Analyze Byzantine fault tolerance in decentralized state machines:\nAnswer:", return_tensors="pt").to(base_model.device)
output = model.generate(**inputs, max_new_tokens=128)
print(tokenizer.decode(output[0], skip_special_tokens=True))

Supported Architectures

Family Architectures Scales
Meta LLaMA LLaMA-2, LLaMA-3, LLaMA-3.1, LLaMA-3.2 1B, 3B, 8B, 70B+
Mistral AI Mistral-7B, Mixtral-8x7B, Mixtral-8x22B, Mistral Large 7B to 8x22B
Qwen Qwen-1.5, Qwen-2, Qwen-2.5, Qwen-3.5 0.5B, 7B, 27B, 72B
Google Gemma Gemma, Gemma-2 2B, 9B, 27B
DeepSeek DeepSeek-V2, DeepSeek-V3, DeepSeek-R1-Distill 1.5B to 70B
Microsoft Phi Phi-2, Phi-3, Phi-3.5 3.8B to 14B
Generic Any causal Hugging Face PreTrainedModel Up to 120B+

License

MIT License. See LICENSE for details.

Download files

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

Source Distribution

dual_loop_controller-2.3.0.tar.gz (941.7 kB view details)

Uploaded Source

Built Distribution

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

dual_loop_controller-2.3.0-py3-none-any.whl (923.4 kB view details)

Uploaded Python 3

File details

Details for the file dual_loop_controller-2.3.0.tar.gz.

File metadata

  • Download URL: dual_loop_controller-2.3.0.tar.gz
  • Upload date:
  • Size: 941.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for dual_loop_controller-2.3.0.tar.gz
Algorithm Hash digest
SHA256 a0b88152a2a3db39de06ea1a77eba2455befba2ba82baeda277bee776e3c2082
MD5 96d113a7eee1d9bae0c2abc03347889a
BLAKE2b-256 d08df1a3e27b02cfac96b18da15092e7bcc74e10f75f3d7a459abe5d229de126

See more details on using hashes here.

File details

Details for the file dual_loop_controller-2.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dual_loop_controller-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 015bdeef4424ec3bb03be8552fd134a60b19e9a0413f2ebdbffb0a2c3bd153b1
MD5 699585b59f9345365a191d6e2e73078b
BLAKE2b-256 4bb4fdfe43a50b420093099a4032e89463a5c8049f341ec2d39abb4ed6e1c90d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.3.0 This release

2 files

2.2.3

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page