Skip to main content

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

Dual-Loop Cognitive Controller

Hardware-Aligned Latent Deliberation & Cognitive Reasoning Framework 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.
  • Cognitive Matrix Helper (EBA): Automatically prunes 40%–57% distractor choices (wrong logs) and rescues tough multi-choice errors (+33.3% to +40.0% net accuracy gain).
  • 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 (20 datasets, historical version evolution graphs, and deep CoT comparisons), 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
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. 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))

3. Cognitive Matrix Helper (Eliminating Distractors)

import numpy as np
from dual_loop import CognitiveMatrixHelper

# Initialize helper
matrix_helper = CognitiveMatrixHelper(elimination_threshold=0.12, min_survivors=2)

# Bench 1: Raw candidate scores from base model
scores_bench1 = [-9.1488, -9.2891, -9.5007, -11.0977, -10.9492]
labels = ["D", "E", "F", "A", "B"]

# Step 1: Populate matrix and eliminate superficial distractors
matrix = matrix_helper.build_evidence_matrix(scores_bench1, labels=labels)
# matrix["eliminated_labels"] -> ['A', 'B'] (Filtered out)
# matrix["survivor_labels"]   -> ['D', 'E', 'F'] (Contenders)

# Bench 2: Focused System 2 deliberation on surviving candidates
scores_delib_survivors = [-6.9465, -5.8747, -4.4858]

final_scores = matrix_helper.fuse_scores(
    scores_base=scores_bench1,
    scores_delib_survivors=scores_delib_survivors,
    survivor_indices=matrix["survivors"],
    lambda_delib=0.85
)

best_idx = np.argmax(final_scores)
print("Rescued Decision:", labels[best_idx])  # -> 'F' (Correct!)

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.2.3.tar.gz (934.0 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.2.3-py3-none-any.whl (916.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dual_loop_controller-2.2.3.tar.gz
  • Upload date:
  • Size: 934.0 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.2.3.tar.gz
Algorithm Hash digest
SHA256 c32700bf601527e9078c2de9ad2af0a83f89d33afb61a35185830bf5b3d82d41
MD5 0fbf42757519fe5b535df7083c5668c8
BLAKE2b-256 aa7093e98b05508cec67cbc7749a7c7ae102505d1bef84b871f2f5e529181f25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dual_loop_controller-2.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cbc5b1e475bd44a87365b0097da8cd679d80edcf0d46a78a1eeed72b7dfe183f
MD5 534d549e1d555cd3a08fadc14c4484a4
BLAKE2b-256 03a45c42e75b3a95a66e8d9c98d3b596163dcab608d952219beb2ac35b833dea

See more details on using hashes here.

Release history Release notifications | RSS feed

2.3.0

2 files

This release

2.2.3 This release

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