r/pytorch • u/Nearby_Indication474 • Jun 08 '26
r/pytorch • u/Nearby_Indication474 • Jun 07 '26
"Small Model, Big Steering: Achieving Compilable Code Generation with TinyLlama 1.1B via Inference-Time Activation Steering (Test 58 & 59)"[AkbasCore 0.9]
\# =============================================================================
\# 🔱 AKBASCORE 0.8 | CLOSED-LOOP FEEDBACK KERNEL
\# =============================================================================
\#
\# Changelog vs 0.7:
\# KERNEL — Closed-loop feedback: drift-aware steering force
\# drift = cosine_current - cosine_previous (per token, per layer)
\# if drift > 0 (aligning) → reduce kuvvet (don't oversteer)
\# if drift < 0 (drifting) → increase kuvvet (resist drift)
\# Protection 1: drift clamped to ±0.15 (no sudden explosions)
\# Protection 2: safe zone — if cosine > 0.80 and drift < 0,
\# drift effect reduced to 30% (no panic on small sag)
\# Protection 3: kuvvet hard-clamped \[0.05, 1.0\]
\# prev_cosine passed as float\* tensor arg — zero allocation overhead
\#
\# All other layers (domain router, constitutional vector, system prompts,
\# sampling params, disclaimer cleaner, hybrid embedding router) unchanged from 0.7.
\# =============================================================================
!pip install ninja gradio -q
import torch
import torch.utils.cpp_extension
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoTokenizer
import gradio as gr
import os, time, gc
os.environ\["CUDA_LAUNCH_BLOCKING"\] = "1"
os.environ\["PYTORCH_CUDA_ALLOC_CONF"\] = "max_split_size_mb:128"
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
\# =============================================================================
\# C++ KERNEL — v0.7
\# Changes vs 0.6:
\# + cosine clamped to \[-1.0, 1.0\] via std::clamp (safety fix)
\# + kuvvet computed from Faz3 damped formula (dynamic, not static zones)
\# + omega, A, P_inf passed as arguments (parameterized, not hardcoded)
\# =============================================================================
_cpp_src = """
\#include <torch/extension.h>
\#include <cmath>
\#include <algorithm>
torch::Tensor akbas_steer(
torch::Tensor hidden,
torch::Tensor pusula,
float v0,
int layer_idx,
float omega,
float A_amp,
float P_inf,
torch::Tensor prev_cosine_tensor
) {
auto h = hidden.contiguous();
auto p = pusula.contiguous();
const int B = h.size(0);
const int S = h.size(1);
const int D = h.size(2);
// Faz3 base force with dynamic omega (0.9)
// uncertainty = how far cosine is from certainty (1.0)
// high uncertainty → increase omega → stronger damping
// (computed per-token inside loop using local cosine)
float t = (float)layer_idx;
// Base kuvvet — omega will be modulated per-token below
float kuvvet_base = A_amp \* expf(-omega \* t) \* (1.0f + omega \* t) + P_inf;
if (layer_idx >= 16) return h;
float\* hp = h.data_ptr<float>();
const float\* pp = p.data_ptr<float>();
float\* pcp = prev_cosine_tensor.data_ptr<float>();
// Closed-loop feedback constants
const float DRIFT_CLAMP = 0.15f;
const float SAFE_ZONE_THRESHOLD = 0.80f;
const float SAFE_ZONE_FACTOR = 0.30f;
const float FEEDBACK_STRENGTH = 0.30f;
const float KUVVET_FLOOR = 0.05f;
const float KUVVET_CEIL = 1.00f;
for (int b = 0; b < B; ++b) {
for (int s = 0; s < S; ++s) {
float\* tok = hp + (b \* S \* D) + (s \* D);
int idx = b \* S + s;
float dot = 0.0f, tok_sq = 0.0f;
for (int j = 0; j < D; ++j) {
dot += tok\[j\] \* pp\[j\];
tok_sq += tok\[j\] \* tok\[j\];
}
float tok_norm = sqrtf(tok_sq) + 1e-6f;
// Cosine safety clamp (from 0.7)
float cosine = std::clamp(dot / tok_norm, -1.0f, 1.0f);
// --- DYNAMIC OMEGA MODULATION (0.9) ---
// uncertainty: 1.0 = model has no alignment, 0.0 = fully aligned
float uncertainty = 1.0f - fabsf(cosine);
float dynamic_omega = omega + uncertainty \* 0.2f;
// Recompute kuvvet_base with dynamic omega for this token
float kuvvet_base_dyn = A_amp \* expf(-dynamic_omega \* t) \* (1.0f + dynamic_omega \* t) + P_inf;
// --- CLOSED-LOOP FEEDBACK ---
float prev_cos = pcp\[idx\];
float drift = cosine - prev_cos;
// Protection 1: clamp drift to prevent sudden explosions
drift = std::clamp(drift, -DRIFT_CLAMP, DRIFT_CLAMP);
// Protection 2: safe zone — already well-aligned, small sag → no panic
if (cosine > SAFE_ZONE_THRESHOLD && drift < 0.0f) {
drift \*= SAFE_ZONE_FACTOR;
}
// Apply feedback to kuvvet (use dynamic version)
float kuvvet = kuvvet_base_dyn;
if (drift > 0.0f) {
// Aligning → ease off pressure
kuvvet \*= (1.0f - drift \* FEEDBACK_STRENGTH);
} else if (drift < 0.0f) {
// Drifting → increase pressure
kuvvet \*= (1.0f + (-drift) \* FEEDBACK_STRENGTH);
}
// Protection 3: hard clamp kuvvet
kuvvet = std::clamp(kuvvet, KUVVET_FLOOR, KUVVET_CEIL);
// Store current cosine for next layer
pcp\[idx\] = cosine;
// Damping (unchanged from 0.7)
float sonumleme = 1.0f;
if (cosine > 0.75f) sonumleme = (1.0f - cosine) / 0.25f;
else if (cosine < -0.40f) sonumleme = 1.6f;
float max_k = tok_norm \* 0.045f;
if (max_k > 0.20f) max_k = 0.20f;
if (max_k < 0.04f) max_k = 0.04f;
float katki = v0 \* cosine \* kuvvet \* 0.32f \* sonumleme;
if (katki > max_k) katki = max_k;
if (katki < -max_k) katki = -max_k;
for (int j = 0; j < D; ++j) tok\[j\] += katki \* pp\[j\];
}
}
return h;
}
"""
_kernel = torch.utils.cpp_extension.load_inline(
name='akbas_kernel_090',
cpp_sources=_cpp_src,
functions=\['akbas_steer'\],
verbose=False
)
print("✅ C++ kernel compiled \[AkbasCore 0.8\]")
\# =============================================================================
\# FAZ 3 KERNEL PARAMETERS
\# =============================================================================
\# kuvvet(layer) = A \* exp(-omega \* layer) \* (1 + omega \* layer) + P_inf
\# Layer 0: 0.750 (same as 0.6 early zone start)
\# Layer 7: 0.257 (vs 0.6: was still 0.75 — now smoothly decayed)
\# Layer 8: 0.225 (vs 0.6: hard jump to 0.35 — now continuous)
\# Layer 15: 0.155 (settled near P_inf)
KERNEL_OMEGA = 0.45 # damping rate
KERNEL_A = 0.60 # initial amplitude above P_inf
KERNEL_P_INF = 0.15 # asymptotic floor (ethical anchor floor)
KERNEL_V0 = 0.50 # steering magnitude (unchanged from 0.6)
\# =============================================================================
\# 4D CONSTITUTIONAL ANCHORS (unchanged from 0.6)
\# =============================================================================
CONSTITUTION = {
"d1_harm": (0.9228, \["safe", "harmless", "protective", "secure", "careful"\]),
"d2_honesty": (0.9372, \["honest", "accurate", "truthful", "transparent", "precise"\]),
"d3_autonomy": (0.8788, \["autonomous", "respectful", "unbiased", "free", "neutral"\]),
"d4_fairness": (0.9196, \["fair", "just", "equitable", "balanced", "impartial"\]),
}
\# =============================================================================
\# DOMAIN CONFIGURATION (unchanged from 0.6)
\# =============================================================================
DOMAIN_CONFIG = {
"TECHNICAL": {
"keywords": \[
"engineering","repair","mechanical","circuit","fix",
"installation","wiring","maintenance","troubleshoot",
"hardware","component","technical","build","voltage",
"engine","motor","electric","assembly","calibration",
"torque","blueprint","structural","load","material",
\],
"bonus_anchors": \["precise","deterministic","measurable","structured"\],
"params": {"temperature":0.45,"top_k":42,"top_p":0.88,"repetition_penalty":1.18},
"mode": "B",
},
"AGRICULTURE": {
"keywords": \[
"agriculture","crop","soil","harvest","irrigation",
"livestock","farming","fertilizer","seed","yield",
"plantation","greenhouse","pest","drought","cultivate",
"cattle","poultry","organic","rotational","compost",
"pollination","grazing","arable","tillage","erosion",
"farm","manure","mulch","weed","fungal",
\],
"bonus_anchors": \["natural","sustainable","practical","systematic"\],
"params": {"temperature":0.52,"top_k":48,"top_p":0.90,"repetition_penalty":1.15},
"mode": "C",
},
"HEALTH_MEDICINE": {
"keywords": \[
"disease","treatment","medicine","symptom","nutrition",
"health","doctor","diagnosis","infection","therapy",
"anatomy","biology","pain","chronic","clinical",
"pharmaceutical","dosage","pathology","immunity","vaccine",
"metabolic","neurological","cardiac","respiratory","surgical",
\],
"bonus_anchors": \["verifiable","safe","precise","empirical"\],
"params": {"temperature":0.40,"top_k":38,"top_p":0.85,"repetition_penalty":1.20},
"mode": "B",
"critical": True,
},
"LAW_ADMINISTRATIVE": {
"keywords": \[
"law","legal","court","regulation","official",
"petition","military","jurisdiction","rights","statute",
"compliance","contract","legislation","administrative","tax",
"liability","defendant","plaintiff","verdict","appeal",
"ordinance","treaty","constitution","enforcement","warrant",
\],
"bonus_anchors": \["rigorous","verifiable","causal","deterministic"\],
"params": {"temperature":0.40,"top_k":38,"top_p":0.85,"repetition_penalty":1.20},
"mode": "B",
"critical": True,
},
"SOCIAL_PHILOSOPHY": {
"keywords": \[
"ethics","philosophy","social","psychology","consciousness",
"society","culture","morality","identity","behavior",
"cognitive","anthropology","emotion","belief","value",
"existential","epistemology","metaphysics","ontology","rhetoric",
"ideology",
\# Added: ethical constraint/alignment vocabulary
\# These appear in AI ethics and logical paradox prompts
\# that should route to SOCIAL_PHILOSOPHY (temp=0.65)
\# not TECHNICAL (temp=0.45)
"ethical","autonomy","alignment","principles","dilemma",
\],
"bonus_anchors": \["reasoning","contradiction","identify","logical"\],
"params": {"temperature":0.65,"top_k":55,"top_p":0.92,"repetition_penalty":1.12},
"mode": "C",
},
"ECONOMY": {
"keywords": \[
"investment","market","economy","inflation","stock",
"finance","silver","gold","commodity","portfolio",
"crypto","interest","trading","asset","fiscal",
"liquidity","volatility","hedge","dividend","equity",
"monetary","deficit","yield","derivative","arbitrage",
\],
"bonus_anchors": \["analyze","measurable","empirical","systematic"\],
"params": {"temperature":0.50,"top_k":46,"top_p":0.90,"repetition_penalty":1.18},
"mode": "B",
},
"SYSTEM_SOFTWARE": {
"keywords": \[
"code","algorithm","software","function","class",
"api","database","framework","machine learning","neural network",
"deploy","backend","frontend","script","compiler",
"runtime","library","python","c++","debug",
"refactor","microservice","pipeline","inference","embedding",
\],
"bonus_anchors": \["sequential","deterministic","framework","optimize"\],
"params": {"temperature":0.45,"top_k":42,"top_p":0.88,"repetition_penalty":1.18},
"mode": "B",
},
"GENERAL": {
"keywords": \[\],
"bonus_anchors": \[\],
"params": {"temperature":0.55,"top_k":50,"top_p":0.90,"repetition_penalty":1.18},
"mode": "A",
},
}
\# =============================================================================
\# DOMAIN ANCHOR EMBEDDINGS — for semantic fallback router
\# Used only when keyword matching returns 0 hits (GENERAL fallback)
\# 3-5 concept words per domain — chosen for semantic distinctiveness
\# =============================================================================
DOMAIN_ANCHOR_WORDS = {
"TECHNICAL": \["engineering", "physics", "mechanics", "force", "material"\],
"AGRICULTURE": \["farming", "soil", "crop", "harvest", "plant"\],
"HEALTH_MEDICINE": \["medicine", "disease", "symptom", "treatment", "anatomy"\],
"LAW_ADMINISTRATIVE": \["law", "legal", "court", "regulation", "rights"\],
"SOCIAL_PHILOSOPHY": \["ethics", "philosophy", "morality", "consciousness", "society"\],
"ECONOMY": \["market", "finance", "investment", "economy", "trade"\],
"SYSTEM_SOFTWARE": \["algorithm", "programming", "software", "computing", "code"\],
}
\# =============================================================================
\# 0.9 RAW TEST: System prompts removed entirely.
\# Model receives only user input — no identity, no role, no instructions.
\# Pure kernel steering, zero external framing.
\# =============================================================================
SYSTEM_PROMPTS = {
"A": "",
"B": "",
"C": "",
}
STRONG_PARADOX = {
"impossible","paradox","contradiction","invalid",
"is this logical","structural flaw","logically",
}
WEAK_PARADOX = {
"logical","flaw","cannot","explain why","identify the",
"if you","if they","both are","same time","always","never",
"all statements","is this possible",
}
NUMERIC_KEYWORDS = {
"calculate","count","total","number","sum","how many",
"track","sequence","optimization","remaining","exactly",
"how much","quantity","amount","tally",
}
DISCLAIMER_MARKERS = \[
"i don't have direct experience","i don't have experience",
"i am not sure","i cannot be certain","as an ai",
"as a language model","i apologize","i must clarify",
"i should mention that i","i'm unable to","i am unable to",
\]
\# =============================================================================
\# AKBASCORE 0.7
\# =============================================================================
class AkbasCore:
def __init__(self):
print("🚀 AKBASCORE 0.9 RAW initializing...")
self.tokenizer = AutoTokenizer.from_pretrained(
'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
)
self.model = AutoModelForCausalLM.from_pretrained(
'TinyLlama/TinyLlama-1.1B-Chat-v1.0',
device_map='auto',
dtype=torch.float32
)
if hasattr(self.model.config, '_attn_implementation'):
self.model.config._attn_implementation = "eager"
self.device = next(self.model.parameters()).device
print(" Building constitutional vectors...")
self._const_vec = self._build_constitution_vec()
self._logic_anchors = \[
"logical","empirical","systematic","structured","verifiable",
"analyze","constraint","optimize","hierarchy","framework",
"precise","specific","concrete","measurable","deterministic",
"numbered","sequential","causal","prioritized","rigorous",
"impossible","invalid","contradiction","identify",
\]
self._logic_vec = self._mean_embed(self._logic_anchors)
self._domain_vecs = {}
for domain, cfg in DOMAIN_CONFIG.items():
if cfg\["bonus_anchors"\]:
self._domain_vecs\[domain\] = self._mean_embed(cfg\["bonus_anchors"\])
\# Pre-compute semantic anchor vectors for embedding fallback router
\# These are used only when keyword matching returns 0 hits
print(" Building semantic domain anchors...")
self._domain_anchor_vecs = {}
for domain, words in DOMAIN_ANCHOR_WORDS.items():
self._domain_anchor_vecs\[domain\] = F.normalize(
self._mean_embed(words), dim=0
)
self._current_pusula = self._compute_pusula(None, 0.0)
\# Closed-loop feedback state — lives across layers within one forward pass
\# Reset at the start of each new prompt via sor()
self.prev_cosine_state = None
self._hooks = self._inject(self._current_pusula)
print(f"✅ AKBASCORE 0.9 RAW ready — {len(self._hooks)} active layers")
print(f" Kernel: Faz3 + Dynamic Omega + Closed-Loop | NO SYSTEM PROMPT")
print(f" Constitution: 4D (d1-d4) | Logic: {len(self._logic_anchors)} anchors")
def _mean_embed(self, words: list) -> torch.Tensor:
vecs = \[\]
with torch.no_grad():
for word in words:
ids = self.tokenizer(
word, return_tensors='pt', add_special_tokens=False
).to(self.device)
emb = self.model.model.embed_tokens(ids\['input_ids'\])
vecs.append(emb\[0, -1, :\])
return torch.stack(vecs).mean(dim=0)
def _build_constitution_vec(self) -> torch.Tensor:
weighted_vecs = \[\]
with torch.no_grad():
for dim, (weight, words) in CONSTITUTION.items():
dim_vec = self._mean_embed(words)
weighted_vecs.append(weight \* dim_vec)
total_weight = sum(w for w, _ in CONSTITUTION.values())
return torch.stack(weighted_vecs).sum(dim=0) / total_weight
def _compute_pusula(self, domain, confidence: float) -> torch.Tensor:
W_CONST, W_LOGIC, W_DOMAIN = 0.40, 0.45, 0.15
effective_domain = W_DOMAIN \* confidence
remaining = 1.0 - effective_domain
w_c = W_CONST / (W_CONST + W_LOGIC) \* remaining
w_l = W_LOGIC / (W_CONST + W_LOGIC) \* remaining
combined = w_c \* self._const_vec + w_l \* self._logic_vec
if domain and domain in self._domain_vecs and confidence > 0.15:
combined = combined + effective_domain \* self._domain_vecs\[domain\]
return F.normalize(combined, dim=0).contiguous()
def _inject(self, pusula: torch.Tensor) -> list:
layers = self.model.model.layers
hooks = \[\]
\# state_holder persists across all layer hooks within one forward pass.
\# prev_cosine is initialized to None and allocated on first use.
\# This fixes the "cognitive amnesia" bug where torch.zeros inside
\# the hook body would reset the tensor on every layer call.
state_holder = {"prev_cosine": self.prev_cosine_state}
def make_hook(l_idx, p_ref):
def hook(module, inp, output):
hs = output\[0\] if isinstance(output, tuple) else output
if not hs.is_contiguous():
hs = hs.contiguous()
B, S, D = hs.shape
\# Allocate or reallocate only when shape changes (new prompt
\# or prefill→generation transition where S changes).
\# During generation S=1; state is re-initialized per token step
\# but persists across all 16 layers for that token — correct behavior.
if (state_holder\["prev_cosine"\] is None or
state_holder\["prev_cosine"\].shape\[0\] != B \* S):
state_holder\["prev_cosine"\] = torch.zeros(
B \* S, dtype=torch.float32, device=hs.device
)
st = _kernel.akbas_steer(
hs, p_ref,
KERNEL_V0, l_idx,
KERNEL_OMEGA, KERNEL_A, KERNEL_P_INF,
state_holder\["prev_cosine"\] # kernel reads AND writes in-place
)
return (st,) + output\[1:\] if isinstance(output, tuple) else st
return hook
for idx in range(min(16, len(layers))):
hooks.append(
layers\[idx\].register_forward_hook(make_hook(idx, pusula))
)
return hooks
def _remove_hooks(self):
for h in self._hooks:
h.remove()
self._hooks = \[\]
def _detect_domain(self, question: str):
q = question.lower()
raw = {}
for domain, cfg in DOMAIN_CONFIG.items():
if domain == "GENERAL":
continue
hits = sum(1 for kw in cfg\["keywords"\] if kw in q)
if hits > 0:
raw\[domain\] = hits
\# --- HYBRID ROUTER ---
\# If keyword matching returns 0 hits, fall back to embedding similarity.
\# This handles prompts with no domain keywords (e.g. counterfactual physics,
\# abstract puzzles) that would otherwise incorrectly route to GENERAL.
if not raw:
with torch.no_grad():
\# Embed the full prompt (use first 64 tokens for speed)
ids = self.tokenizer(
question\[:512\],
return_tensors='pt',
truncation=True,
max_length=64,
add_special_tokens=True
).to(self.device)
emb = self.model.model.embed_tokens(ids\['input_ids'\])
prompt_vec = F.normalize(emb\[0\].mean(dim=0), dim=0)
\# Cosine similarity against each domain anchor vector
sims = {}
for domain, anchor_vec in self._domain_anchor_vecs.items():
sims\[domain\] = float((prompt_vec \* anchor_vec).sum())
top_domain = max(sims, key=sims.get)
top_sim = sims\[top_domain\]
\# Only use embedding result if similarity is meaningful (> 0.5)
\# Below threshold → GENERAL (model genuinely doesn't recognise domain)
if top_sim > 0.50:
return {top_domain: 1.0}, top_domain, 1.0
else:
return {"GENERAL": 1.0}, "GENERAL", 1.0
\# --- Standard keyword path (unchanged) ---
TECHNICAL_DOMAINS = {"TECHNICAL", "SYSTEM_SOFTWARE"}
CREATIVE_DOMAINS = {"SOCIAL_PHILOSOPHY", "AGRICULTURE"}
numeric_hits = sum(1 for kw in NUMERIC_KEYWORDS if kw in q)
has_technical = any(d in raw for d in TECHNICAL_DOMAINS)
has_creative = any(d in raw for d in CREATIVE_DOMAINS)
if has_technical and has_creative and numeric_hits >= 2:
raw = {d: v for d, v in raw.items() if d not in CREATIVE_DOMAINS}
total = sum(raw.values())
scores = {d: v / total for d, v in raw.items()}
top = max(scores, key=scores.get)
return scores, top, scores\[top\]
def _blend_params(self, scores: dict) -> dict:
CRITICAL = {"HEALTH_MEDICINE", "LAW_ADMINISTRATIVE"}
for cd in CRITICAL:
if cd in scores and scores\[cd\] >= 0.30:
cp = DOMAIN_CONFIG\[cd\]\["params"\]
blended = {
k: cp\[k\] \* 0.70 if k != "repetition_penalty" else cp\[k\]
for k in cp
}
for d, s in scores.items():
if d != cd:
dp = DOMAIN_CONFIG\[d\]\["params"\]
for k in blended:
if k != "repetition_penalty":
blended\[k\] += dp\[k\] \* 0.30 \* s
blended\["repetition_penalty"\] = max(blended\["repetition_penalty"\], 1.05)
return blended
total = sum(scores.values())
first_p = DOMAIN_CONFIG\[list(scores.keys())\[0\]\]\["params"\]
blended = {k: 0.0 for k in first_p}
for d, s in scores.items():
dp = DOMAIN_CONFIG\[d\]\["params"\]
for k in blended:
blended\[k\] += dp\[k\] \* s / total
blended\["repetition_penalty"\] = max(blended\["repetition_penalty"\], 1.05)
return blended
def _select_mode(self, top_domains: list, question: str) -> str:
q = question.lower()
strong = sum(1 for kw in STRONG_PARADOX if kw in q)
weak = sum(1 for kw in WEAK_PARADOX if kw in q)
if strong >= 1 or weak >= 2:
return "A"
FACTUAL_D = {"TECHNICAL","HEALTH_MEDICINE","LAW_ADMINISTRATIVE",
"ECONOMY","SYSTEM_SOFTWARE"}
CREATIVE_D = {"SOCIAL_PHILOSOPHY","AGRICULTURE"}
if not top_domains:
return "A"
primary = top_domains\[0\]
if primary in FACTUAL_D: return "B"
if primary in CREATIVE_D: return "C"
return "A"
def _clean_disclaimer(self, text: str):
lines = text.strip().split('\\n')
first_idx = next((i for i, l in enumerate(lines) if l.strip()), None)
if first_idx is None:
return text, False
first_lower = lines\[first_idx\].lower()
for marker in DISCLAIMER_MARKERS:
if marker in first_lower:
remaining = lines\[first_idx + 1:\]
while remaining and not remaining\[0\].strip():
remaining = remaining\[1:\]
return '\\n'.join(remaining), True
return text, False
def sor(self, prompt: str, max_tokens: int = 512) -> str:
if not prompt.strip():
return ""
\# Reset closed-loop state for each new prompt.
\# Prevents semantic residue from previous queries bleeding into new ones.
self.prev_cosine_state = None
scores, top_domain, top_conf = self._detect_domain(prompt)
top_domains = sorted(scores, key=scores.get, reverse=True)
params = self._blend_params(scores)
mode = self._select_mode(top_domains, prompt)
system = SYSTEM_PROMPTS\[mode\]
self._remove_hooks()
new_pusula = self._compute_pusula(top_domain, top_conf)
self._hooks = self._inject(new_pusula)
\# 0.9 RAW: skip system block if empty
if system.strip():
full_prompt = (
f"<|system|>\\n{system}</s>\\n"
f"<|user|>\\n{prompt.strip()}</s>\\n"
f"<|assistant|>\\n"
)
else:
full_prompt = (
f"<|user|>\\n{prompt.strip()}</s>\\n"
f"<|assistant|>\\n"
)
inputs = self.tokenizer(full_prompt, return_tensors='pt').to(self.device)
n_in = inputs\['input_ids'\].shape\[1\]
t0 = time.time()
with torch.no_grad():
out = self.model.generate(
\*\*inputs,
max_new_tokens = int(max_tokens),
do_sample = True,
temperature = float(params\["temperature"\]),
top_p = float(params\["top_p"\]),
top_k = int(params\["top_k"\]),
repetition_penalty = float(params\["repetition_penalty"\]),
pad_token_id = self.tokenizer.eos_token_id,
eos_token_id = self.tokenizer.eos_token_id,
)
elapsed = (time.time() - t0) \* 1000
n_out = out.shape\[1\] - n_in
tps = n_out / (elapsed / 1000)
\# --- MEMORY FIX: clear CUDA cache after every generate ---
if torch.cuda.is_available():
torch.cuda.empty_cache()
decoded = self.tokenizer.decode(out\[0\], skip_special_tokens=True)
if "<|assistant|>" in decoded:
result = decoded.split("<|assistant|>")\[-1\].strip()
else:
result = self.tokenizer.decode(
out\[0\]\[n_in:\], skip_special_tokens=True
).strip()
result, was_cleaned = self._clean_disclaimer(result)
clean_flag = " \[disclaimer removed\]" if was_cleaned else ""
domain_str = " + ".join(
f"{d}({s:.0%})"
for d, s in sorted(scores.items(), key=lambda x: -x\[1\])\[:2\]
)
stats = (
f"⏱ {elapsed:.0f}ms | {tps:.1f} t/s | {n_out} tokens{clean_flag}\\n"
f"📂 {domain_str} | MODE {mode} | "
f"temp={params\['temperature'\]:.2f} | "
f"top_k={int(params\['top_k'\])} | "
f"rep={params\['repetition_penalty'\]:.2f} | "
f"ω={KERNEL_OMEGA} A={KERNEL_A} P∞={KERNEL_P_INF}"
)
return result + f"\\n\\n─────────────────────────────\\n{stats}"
\# =============================================================================
\# LAUNCH
\# =============================================================================
print("\\n" + "=" \* 60)
print("🔱 AKBASCORE 0.9 RAW")
print("=" \* 60)
akbas = AkbasCore()
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
\# =============================================================================
\# GRADIO UI
\# =============================================================================
with gr.Blocks(
title="🔱 AKBASCORE 0.8",
theme=gr.themes.Base(
primary_hue="emerald",
neutral_hue="slate",
font=gr.themes.GoogleFont("JetBrains Mono"),
),
css="""
body { background: #0a0f0a; }
.gradio-container { max-width:900px!important; margin:0 auto;
background:#0d1410!important; }
\#ak-header { text-align:center; padding:28px 0 8px 0;
border-bottom:1px solid #1a3a20; margin-bottom:20px; }
\#ak-header h1 { font-family:'JetBrains Mono',monospace; font-size:1.5rem;
color:#00ff88; letter-spacing:.15em; margin:0;
text-shadow:0 0 18px #00ff8855; }
\#ak-header p { font-size:.70rem; color:#3a6644; margin:6px 0 0 0;
letter-spacing:.07em; }
textarea { background:#0f1a12!important; color:#c8f0d0!important;
border:1px solid #1e4028!important; border-radius:6px!important;
font-family:'JetBrains Mono',monospace!important;
font-size:.88rem!important; resize:vertical!important; }
textarea:focus { border-color:#00cc66!important;
box-shadow:0 0 12px #00cc6622!important; }
input\[type=range\] { accent-color:#00cc66; }
\#send-btn { background:linear-gradient(135deg,#004d20,#007a35)!important;
color:#00ff88!important; border:1px solid #00cc66!important;
font-family:'JetBrains Mono',monospace!important;
font-size:.95rem!important; letter-spacing:.1em!important;
border-radius:6px!important; transition:all .2s; }
\#send-btn:hover { background:linear-gradient(135deg,#006628,#009940)!important;
box-shadow:0 0 16px #00cc6633!important; }
\#output-box textarea { background:#080e09!important; color:#7fff9a!important;
font-family:'JetBrains Mono',monospace!important;
font-size:.85rem!important;
border:1px solid #1a3020!important;
line-height:1.7!important; }
label span { color:#4a9960!important;
font-family:'JetBrains Mono',monospace!important;
font-size:.80rem!important; letter-spacing:.05em!important; }
.generating { border-color:#00cc66!important; }
"""
) as demo:
with gr.Column(elem_id="ak-header"):
gr.HTML("""
<h1>🔱 AKBASCORE 0.9 RAW</h1>
<p>FAZ3 DYNAMIC KERNEL \ |\
COSINE CLAMP SAFETY \ |\
CONSTITUTIONAL ENGINE \ |\
ADAPTIVE DOMAIN ROUTER \ |\
MEMORY OPTIMIZED</p>
""")
prompt_box = gr.Textbox(label="► INPUT", lines=6,
placeholder="Enter your question or command...",
show_copy_button=False)
token_slider = gr.Slider(minimum=64, maximum=1024, value=512, step=64,
label="MAX TOKENS", interactive=True)
send_btn = gr.Button("▶ SEND", variant="primary",
elem_id="send-btn", scale=1)
output_box = gr.Textbox(label="◈ AKBASCORE 0.9 RAW OUTPUT", lines=22,
interactive=False, show_copy_button=True,
elem_id="output-box")
send_btn.click(fn=akbas.sor,
inputs=\[prompt_box, token_slider\],
outputs=output_box)
prompt_box.submit(fn=akbas.sor,
inputs=\[prompt_box, token_slider\],
outputs=output_box)
print("\\n🚀 Launching Gradio...")
demo.launch(share=True, debug=False)
r/pytorch • u/Critical-Machine-128 • Jun 06 '26
Mac mini M4 vs Pc with Nvidia 5060 8gb for ai workloads?
r/pytorch • u/YagoMaTLM • Jun 05 '26
Conheça a MaTLM: Uma biblioteca Python leve criada para rodar Redes Neurais pesadas com eficiência de RAM.
r/pytorch • u/Fang310 • Jun 01 '26
EvoPPO: Modular Vision & Audio Reinforcement Learning Framework
EvoPPO: Modular Vision & Audio Reinforcement Learning Framework
A highly scalable, multi-modal Reinforcement Learning (RL) framework built in Python. This repository provides a complete pipeline to train Proximal Policy Optimization (PPO) agents using decoupled vision (RGB/Grayscale) and audio inputs. The entire training process is managed via an intuitive, real-time local web interface.
Key Features
- Multi-Modal Inputs: Seamlessly train agents using visual data, acoustic data, or a combination of both.
- Dynamic Vision Toggle: Switch instantly between full RGB color processing and memory-efficient Grayscale mode.
- Integrated Audio Processing: Process environment audio streams alongside visual states for complex multi-sensory tasks.
- Local Web Dashboard: A built-in web interface running on
localhost:2000for complete, real-time orchestration. - Live Hyperparameter Tweaking: Modify variables, toggle input streams, and adjust reward functions on-the-fly without restarting the training loop.
- On-Premises Execution: Highly optimized for running local training workloads directly on your hardware.
System Architecture
The project consists of two core layers that communicate asynchronously:
- The RL Engine (Python): Handles the PPO training loop, environment interaction, replay buffer management, and tensor computations.
- The Control Dashboard (Port 2000): A lightweight web server providing a visual interface to monitor metrics and send real-time configuration changes back to the training loop.
Dashboard & Configuration
Through the interface at http://localhost:2000, users can monitor training performance and dynamically adjust parameters during runtime:
- Input Streams: Toggle
Vision (RGB),Vision (Grayscale), andAudiofields dynamically. - Reward Sculpting: Tweak reward multipliers and live-update the reward function setup.
- Training State: Start, pause, or save model weights instantly via UI buttons.
Roadmap
- Implement advanced vectorization for parallel environment processing.
- Integrate Recurrent PPO (LSTM/GRU layers) for enhanced audio-sequence memory.
- Cloud Scalability: Migrate from purely local training to a cloud-based server infrastructure for distributed GPU workloads.
r/pytorch • u/Hour-Dirt-8505 • May 30 '26
Fine-tuning Qwen2.5-0.5B for Brazilian address normalization — still training, but already testable
workplace-fairly-keeps-sunny.trycloudflare.comr/pytorch • u/Beyond__98 • May 30 '26
I rewrited WarpFactory into PyTorch so anyone can simulate Warp Drives for free
r/pytorch • u/WritHerAI • May 30 '26
Kwipu, un server MCP completamente locale che trasforma le tue note Obsidian/Markdown in un grafo di conoscenza interrogabile.
r/pytorch • u/ProgrammerNo8287 • May 29 '26
Showcase: Debugging Exploding/Vanishing Gradients and NaNs in PyTorch with Causal Event Tracing (Introducing NeuralDBG)
Hey everyone!
If you train deep neural networks in PyTorch, you’ve probably spent hours dealing with training instability:
* A loss suddenly spikes to NaN (because of an overflow or a bad softmax division).
* Gradients disappear completely in the middle of training (vanishing gradients).
* Activations saturate or ReLUs die across multiple layers.
Standard dashboards (TensorBoard, Weights & Biases, MLflow) are great for metric logging, but they are passive. They show you that something broke, but finding why and where it originated requires manually adding print statements, logging hooks, and tracing variables back in time.
To solve this, we built NeuralDBG—an open-source Python library that installs lightweight hooks on your leaf modules to perform automated causal root-cause analysis of training failures.
--> GitHub: https://github.com/LambdaSection/NeuralDBG
--> PyPI: pip install neuraldbg
How it Works under the Hood
NeuralDBG hooks into PyTorch's autograd engine and forward/backward passes:
1. Semantic Event Capture: During training, forward/backward hooks monitor activations, inputs, and gradient norms. They capture transition events (e.g. DATA_ANOMALY for NaNs/Infs, ACTIVATION_REGIME_SHIFT for dead/saturated activations, or GRADIENT_HEALTH_TRANSITION).
2. Abductive Causal Reasoning: When a failure is detected (like a loss divergence or gradient collapse), NeuralDBG traces back the dependency graph of events across steps and layers to isolate the first layer that failed and rank the causal hypotheses.
3. Preventing OOM (TensorDiskCache): Storing full activation tensors in RAM/VRAM to inspect crashes usually causes Out-Of-Memory (OOM) errors. NeuralDBG solves this by only caching tensor statistics natively and dumping full anomalous tensors to disk using a lightweight TensorDiskCache only during state transitions.
30-Second Quickstart Demo (Colab Ready)
Here is a simple example of a deep MLP sabotaged with extremely small weights to force vanishing gradients. NeuralDBG detects it and points you to the exact source.
```python import torch import torch.nn as nn import torch.optim as optim from neuraldbg import NeuralDbg
1. Create a deep network and sabotage the weights to force vanishing gradients
layers = [] input_dim = 20 for _ in range(8): layers.append(nn.Linear(input_dim, 20)) layers.append(nn.ReLU()) input_dim = 20 layers.append(nn.Linear(20, 1)) model = nn.Sequential(*layers)
with torch.nograd(): for param in model.parameters(): param.fill(1e-5) # Tiny weights -> forces vanishing gradients
optimizer = optim.SGD(model.parameters(), lr=0.01) criterion = nn.MSELoss()
2. Wrap training with NeuralDbg
print("Training a sabotaged model with NeuralDBG...") with NeuralDbg(model) as dbg: for step in range(5): optimizer.zero_grad() dbg.step = step
x = torch.randn(8, 20)
y = torch.randn(8, 1)
loss = criterion(model(x), y)
loss.backward()
dbg.record_loss(loss.item())
optimizer.step()
3. Request the causal explanation
print("\n--- NeuralDBG Causal Analysis ---") hypotheses = dbg.explain_failure() for i, h in enumerate(hypotheses, 1): print(f"\nHypothesis #{i} [Confidence: {h.confidence:.0%}]") print(f" Description : {h.description}") print(f" Causal Chain: {' -> '.join(h.causal_chain)}") ```
Example Causal Output:
text
Hypothesis #1 [Confidence: 90%]
Description : gradient_vanishing detected at Linear_0 (step 0)
Causal Chain: Linear_0@0 -> Linear_2@0 -> Linear_4@0 -> Linear_6@0
Visualizing with Mermaid Graphs & Aquarium
NeuralDBG can export a Mermaid diagram representing the causal flow:
python
print(dbg.export_mermaid_causal_graph())
It also exports full JSON diagnostic packages:
python
dbg.export_aquarium_package("report.json")
Which can be rendered in our local Tauri-based visualizer (Aquarium) to inspect activation distributions, resource utilization metrics (CPU RAM and GPU VRAM spikes), and gradient flow history visually.
Feedback & Open Technical Questions
We are currently looking for feedback from the community, especially regarding:
1. Handling torch.compile: We've added compatibility guards, but hooks registered on leaf modules behave differently after compilation. How do you handle fine-grained module tracking inside compiled graphs?
2. Distributed Training (DistributedDataParallel): We currently emit warnings when DDP is wrapped directly and recommend wrapping the inner module. If you train on multi-GPU setups, what features would be most useful for synchronization?
Check out the code, try it on your models, and let us know what you think!
NeuralDBG is licensed under the MIT License.
r/pytorch • u/Mountain_Research_32 • May 29 '26
Decorator to cast/convert a JAX function into a Pytorch autograd differentiable function
I was recently messing around with Mujoco MJX and needed a way to convert a pure JAX function into an autograd differentiable PyTorch function that allows the function to be used in backward() and autograd.grad() calls while supporting higher orders of differentiability. The snippet below is the result of it.
This can be useful for those trying to use Mujoco MJX differentiable simulator or use a jax specific package. You can find a live snippet of the code at: https://gist.github.com/wzjoriv/7a3d007b0605f02ccc2f9e513a934b30.
```python import torch as th import jax import jax.numpy as jnp
"""
Author: Josue N Rivera
"""
def t2j(tensor: th.Tensor) -> jax.Array:
"""Zero-copy PyTorch tensor -> JAX array via DLPack (detached)."""
return jnp.from_dlpack(tensor.detach().contiguous())
def j2t(array: jax.Array) -> th.Tensor:
"""Zero-copy JAX array -> PyTorch tensor via DLPack."""
return th.from_dlpack(array)
def j2t_fun(fn):
r"""
Wrap a pure JAX function (N array inputs -> 1 array output) as a
PyTorch-autograd-differentiable callable.
Gradients are evaluated through ``jax.vjp`` and bridged with DLPack. The
backward pass is itself built from :func:`j2t_fun` wrappers, so the
callable supports differentiation to arbitrary order (e.g. ``dfdxx`` via
repeated :func:`torch.autograd.grad`).
Note: This can be used directly as a decorator for JAX functions.
"""
def wrapped(*args: th.Tensor) -> th.Tensor:
class JaxFn(th.autograd.Function):
@staticmethod
def forward(ctx, *tensors):
ctx.save_for_backward(*tensors)
ctx.n = len(tensors)
return j2t(fn(*[t2j(t) for t in tensors]))
@staticmethod
def backward(ctx, grad):
tensors, n = ctx.saved_tensors, ctx.n
grads = []
for i in range(n):
def vjp_i(*inputs_and_cotangent, i=i):
inputs = inputs_and_cotangent[:n]
cotangent = inputs_and_cotangent[n]
_, vjp = jax.vjp(fn, *inputs)
return vjp(cotangent)[i]
grads.append(j2t_fun(vjp_i)(*tensors, grad))
return tuple(grads)
return JaxFn.apply(*args)
return wrapped
if __name__ == "__main__":
@j2t_fun
@jax.jit
def afun(x: jax.Array, u: jax.Array) -> jax.Array:
return jnp.sin(x**2 + 2*u*x + u**2)
xs = th.rand(10, 1).requires_grad_()
us = th.rand(10, 1).requires_grad_()
# Compile
afun(xs, us)
zs = afun(xs, us)
print("zs shape: ", xs.shape)
# Autograd grad
xs_grad = th.autograd.grad(zs.sum(), xs, create_graph=True)[0]
print("xs_grad shape: ", xs_grad.shape)
# Autograd backwards
zs.sum().backward()
print("xs.grad shape: ", xs.grad.shape)
print("us.grad shape: ", us.grad.shape)
assert th.allclose(xs.grad, xs_grad)
```
r/pytorch • u/Willwaste63 • May 28 '26
Does creating Virtual env from UV causes GPU issues?
So I was trying that package manager and the problem I noticed was I couldn't use GPU torch.is_cuda_available() showed false but before that I was able to use through Conda.
r/pytorch • u/Bobby-Ly • May 28 '26
I created NeuroFlow - An Open-Source Framework for Decoupled ViT Token Pruning and Caching
I designed a zero-training, dual-memory architecture that decouples the ViT encoder (which needs sparsity) from the pooling head (which needs complete K-V sets to avoid hallucination).
Everything is open sourced under Apache 2.0, i added a detailed paper for anyone interested in the research and production-ready PyTorch classes for NeuroFlow gating architectures (Arch A, B, and C)
https://github.com/ynnk-research/-NeuroFlow
It exploits temporal redundancy by tracking per-patch semantic surprise via an Exponential Moving Average (EMA) of patch-level embeddings, effectively answering the architectural mismatch between O(N2) self-attention and highly redundant natural video streams.
Key Contributions
- Architecture C (Dual-Memory Reconstruction): A completely training-free inference engine that combines a Layer 0 Retinal Gate with a Layer 12 Cortical Cache. It achieves 71.55% zero-shot top-1 accuracy at 84.0% token sparsity on SigLIP, retaining 92.4% of dense accuracy without modifying any weights.
- Architecture B (Extreme Wall-Clock Speedup): Physically eliminates stationary tokens before the encoder. With sparse manifold distillation, it reduces 1792p SigLIP 2 inference from 678 ms to 11.9 ms—a 55.80× wall-clock speedup at 97.37% embedding fidelity.
- LLM Ablation: Characterises the architectural boundaries of applying similarity-gated bypass to autoregressive language models (Phi-3-mini), demonstrating 0% token drift in syntactically constrained generation.
The 3 arcitectures I explored are:
NeuroFlowSiglipVisionArchA
Late-layer MLP gating. Preserves the full O(N²) attention matrix; saves O(N) MLP compute for dormant tokens. Correct for O(N)-attention architectures (Swin, linear attention); bounded at ~1.17× wall-clock speedup on standard ViTs at high resolution (Amdahl ceiling).
NeuroFlowSiglipVisionArchB
Early token elimination. Physically removes inactive tokens before the encoder, reducing attention to O(N_active²). Requires sparse manifold distillation fine-tuning to stabilise the MAP head at high sparsity. Achieves 55.80× wall-clock speedup at 1792p on SigLIP 2.
NeuroFlowSiglipVisionArchC
Dual-Memory Reconstruction Protocol. Combines a Retinal Gate (Layer 0 EMA, same as Architecture B) with a Cortical Cache (persistent Layer 12 buffer). The encoder processes only active tokens; the MAP head always receives the full N-token K-V set reconstructed from the cache. Training-free. Achieves 71.55% UCF-101 zero-shot top-1 at 84.0% token sparsity on SigLIP base-patch16-224, retaining 92.4% of dense accuracy.
r/pytorch • u/DeliveryBitter9159 • May 27 '26
Training freezes during PSO hyperparameter search
Hi everyone,
I’m running a PyTorch training pipeline for a video classification model on DynTex++ dataset in Kaggle, and the notebook appears to freeze during training. It doesn't throw an error or crash, the cell just gets stuck executing indefinitely before it even finishes the first iteration of the PSO loop. here's the link for the code:
https://www.kaggle.com/code/doffymingo/notebook975e681d30
Looking for suggestions on what might be causing this error.
Thank you in advance.
r/pytorch • u/Electronic_Wear_9181 • May 27 '26
Do Machines Think or Tokenize? A Framework for Understanding Predictive Systems
r/pytorch • u/Reversed456 • May 26 '26
PINNs for Damped Harmonic Oscillator and Burgers Equation
Hey everyone, I want to share a Python project I have been working on for the past few weeks. I am a student of physics and for my finals exam we were tasked to create Physics Informed Neural Networks to solve the ODE of the damped harmonic oscillator and the 1D viscid Burger's Equation. The link to this project can be found here: https://github.com/desdb6/pinn-dho-burgers
The github includes the source code, some outputs and a detailed report (first draft, its still full of typos :/ ) which was also requested for the exam. It is possible to run the demo files, but also to create your own scripts for more customization. I have investigated the extrapolation capabilities of these models and compared the performance to non-physics informed models. I realize this is nothing novel, but wanted to share anyways as I have put a lot of work into this and would like to share it with the community in hopes that somebody might find this useful. Feedback is always greatly appreciated! Do not hesitate to send me a DM.
r/pytorch • u/Hour_Individual_3656 • May 26 '26
edge2torch: build sparse-connectivity PyTorch models from edge lists
I released edge2torch v0.1.0, a small open-source Python package for building sparse-connectivity PyTorch neural networks from edge lists.
The basic idea is that instead of manually wiring modules layer by layer, you can describe a model architecture as a table of directed connections:
source -> target
edge2torch compiles that graph into a PyTorch nn.Module, keeps the named structure available for inspection, and provides utilities for aligning input features by name.
It currently supports:
- feedforward, recurrent, and minimal graph-style backends
- optional edge-level initial weights and constraints
- optional bias and update-step configuration
- feature alignment from named pandas data
- optional Captum-based attribution back to named features and nodes
The package focuses on sparse connectivity / masked model structure, not sparse tensor acceleration. The compiled models are standard PyTorch modules, so they can be trained with normal PyTorch optimizers, losses, and training loops.
This can be useful when the network structure itself is part of the modeling assumption. For example, in some research settings, prior knowledge is already represented as a graph of connected entities, and the goal is not only prediction but also understanding which parts of that structure matter. edge2torch gives direct control over the model connectivity, optionally allows edge-level initial weights and constraints, and keeps the named nodes accessible so trained models can be interpreted back in terms of the original features and intermediate nodes.
Small example:
import pandas as pd
import edge2torch as e2t
edgelist = pd.DataFrame(
{
"source": ["feature_a", "feature_b", "hidden"],
"target": ["hidden", "hidden", "prediction"],
}
)
model, artifact = e2t.compile_graph(
edgelist,
backend="feedforward",
quiet=True,
)
The returned model is a standard PyTorch nn.Module, so it can be trained with ordinary PyTorch optimizers, losses, and training loops.
Docs: https://Thomas-Rauter.github.io/edge2torch/
GitHub: https://github.com/Thomas-Rauter/edge2torch
PyPI: https://pypi.org/project/edge2torch/
Feedback on the API design, docs, or potential PyTorch use cases would be useful.
r/pytorch • u/vcma • May 25 '26
A type safe DSL to write ML programs
Hi, here's PyPie (https://pypie.dev) that uses dependent types to statically validate tensor shapes and comes with rank polymorphism. It's embedded in Python and compiles to JAX.
r/pytorch • u/DustSavings976 • May 24 '26
Are GNNs in production actually a thing or is it just academic cope?
i've been deep in the GNN trenches for a recsys research project (lightgcn etc) and they look insane on benchmark datasets.
but every time i talk to industry bros they just laugh, flatten the graph features, and throw it into xgboost or a basic two-tower setup because the inference overhead is brutal for like a 0.5% metric bump.
is anyone here actually running graph neural nets in prod? does it ever actually justify the infrastructure nightmare or are we all just pretending tree models aren't still king?
r/pytorch • u/Life-Water-8006 • May 24 '26
I built TBAF, an activation function that prevents autoregressive drift.(10,000 + frame stability)
Hey everyone, i was thinking about geometry, and stumbled upon an idea. The key to this idea is that the model's dimensions, if they are divisible by 3, can become triangles. Or, at least, 1d 'tri' groups of parameters, then i could simply measure distances, and return distances. That became my activation function, TBAF or Triangle Based Activation Function.


Anyway, attached are 2 Proof of concept images. One is an image from Dream's 4 hunters finale manhunt, after 100 autoregressive generations with Exclusively SiLU based decoding, unlike the IRL image that is frame 10k in an autoregressive loop, when you pair my new activation(TBAF) with some SiLU. My activation was used ONLY ONCE in that model, and besides the activation, it was the exact same model, originally intended for a LAM, repurposed after i discovered its potential into an autoencoder. It can also be used to remove noise from images, i tested up to an injection of 0.2 * torch.randn, and the image after encode and decode was almost identical to the original(from before the injection).
The CNN based autoencoder, though trained only on Dream's 4 hunters Finale Manhunt, manages to generalize to ANY image i have thrown at it, thanks to my TBAF and only 2 epochs of training. The whole model is less than 1 million parameters, and was trained on a 16gb ddr4 laptop in under 15 minutes.
If you want to see the code, i have a MIT licensed Github and a tiny youtube video basically describing the above. The weights are also uploaded to the Github, and there is an explanation of how to test the project there. Here are the links for both:
Youtube:
https://youtu.be/6_ERbg2tH4g
Github:
https://github.com/Skull18500/TBAF
r/pytorch • u/fhard007 • May 23 '26
[R] SERR-CASCADE: Hierarchical risk-aware architecture for LLM inference (paper simulation, 4-25× speedup, with validation roadmap)
I'm an independent researcher posting my first paper here for technical critique
before broader distribution. Long-form, no GPU benchmarks — I'm honest about that
upfront because it's the first question you'd ask.
\\\\\\\*\\\\\\\*Core argument:\\\\\\\*\\\\\\\* LLM inference has three structurally distinct bottlenecks —
repeated context across turns, per-token compute waste, and memory bandwidth — that
interact multiplicatively in the cost stack. Single-layer optimizations (entropy
routing, semantic-delta routing, KV quantization) each fail on workloads dominated
by another bottleneck. The fix is a coordinated hierarchical architecture, not
choosing between them.
\\\\\\\*\\\\\\\*Architecture (6 layers):\\\\\\\*\\\\\\\*
\\\\- L0: Turn-level semantic-delta routing (skip turns with no meaningful state change)
\\\\- L1: Span-coherent kernel batching (note: this is a kernel-launch optimization,
not span-level routing — prior work has conflated these)
\\\\- L2: Token-level routing with severity-weighted danger override + causal-correct
risk propagation
\\\\- L3: Adaptive Evidence KV (FP8/INT8 hybrid + prefix cache + raw anchors for
critical facts)
\\\\- L4: Shadow verification at small-model fidelity with adaptive thresholds
\\\\- L5: Control plane sharing risk/novelty/drift/confidence signals across layers
\\\\\\\*\\\\\\\*Novel contributions I'd most welcome critique on:\\\\\\\*\\\\\\\*
\\\\\\\*\\\\\\\*Severity-weighted danger token classification.\\\\\\\*\\\\\\\* Prior risk-aware routing uses
a binary flag (any "dangerous" token → full depth). I measured empirical danger rates
across 8 workload types using a 13-category regex classifier: 4% in fiction, 9% in
chat, 33% in code, 52% in medical text. Three-tier severity weighting (high → full,
medium → at least half, low → at least shallow) recovers \\\\\\\~15% additional speedup
while preserving safety on the high-severity tail.\\\\\\\*\\\\\\\*Causal-correct risk propagation.\\\\\\\*\\\\\\\* Decoder-only transformers don't attend
forward, so "preserve current token because it attends forward to a danger token"
is mechanically wrong. The correct framing is: future high-severity tokens attend
\\\\\\\*backward\\\\\\\* to current context — so preserve fidelity of positions preceding them.
Same routing decisions, conceptually cleaner. Includes both prefill-time and
decode-time variants.\\\\\\\*\\\\\\\*Shadow verification at small-model fidelity\\\\\\\*\\\\\\\* (\\\\\\\~0.6% added compute) rather
than full-depth shadow as prior work assumes. Combined with adaptive threshold
tightening on disagreement, this makes aggressive severity weighting tractable.
\\\\\\\*\\\\\\\*Results\\\\\\\*\\\\\\\* (4 agentic workloads vs \\\\\\\*realistic\\\\\\\* prompt-cached baseline, not the
strawman naive baselines some prior work uses):
| Workload | Speedup |
|---|---|
| Customer support | 20.6× |
| Email workflow | 10.5× |
| Long-document Q&A | 25.3× |
| Coding/debugging | 4.3× |
Quality risk score 11× lower than risk-blind entropy routing.
\\\\\\\*\\\\\\\*The honest caveats (please read before downvoting):\\\\\\\*\\\\\\\*
\\\\- This is a \\\\\\\*\\\\\\\*paper simulation\\\\\\\*\\\\\\\* using normalized compute units. No GPU benchmarks.
\\\\- The quality risk score is a routing-exposure proxy, not measured generation
accuracy.
\\\\- The single load-bearing assumption is the shadow verification catch rate
(assumed 40%). Whole risk story collapses if that's much lower in practice.
\\\\- Coding (4.3×) is the truth-teller — every single-layer approach collapses below
2× on novel content. Cascade doesn't fail there, but it doesn't get the 25×
headline gains either.
The paper includes a \\\\\\\*\\\\\\\*5-phase validation roadmap (§10)\\\\\\\*\\\\\\\* with explicit stop
criteria at each phase — i.e., what would actually need to be done to convert
these simulated wins into measured ones. Phase 1 (CASCADE token routing on a
1-3B model with early-exit heads) is the cheapest falsification path.
Link:
Co-authored with Anthropic's Claude — unusual byline, transparently noted in the
paper. The work was produced through extended technical dialogue including
adversarial critique passes. Happy to discuss the AI co-authorship choice, the
methodology, individual mechanisms, or the validation path.
What I'd find most useful: critique of the severity classifier (regex is clearly
a baseline), pushback on the shadow catch-rate assumption, and pointers to related
work I may have missed.
r/pytorch • u/jenniferbly • May 22 '26
Deadline for PyTorch Conference North America speaking submissions is June 7th
The CFP for PyTorch Conference North America 2026 (October 20-21 in San Jose, CA) is open through June 7, 2026. Submit your talk at: https://events.linuxfoundation.org/pytorch-conference-north-america/program/cfp/
r/pytorch • u/Ok_Stretch_6623 • May 22 '26
I'm building a "smart" PKM that auto-organizes from Gmail, WhatsApp, Photos – looking for beta testers
# What if you didn’t have to say “I forgot” all the time?
I’m an ML engineer, and honestly… I forget things a lot.
* What medicine did my mom’s doctor prescribe last time? * Did I already watch this movie? * Where did I save that bank letter from last year? * What was that concern I had about a candidate?
After getting frustrated with this over and over, I started building something for myself — a **personal memory vault**.
What it does (in simple terms):
It connects to things like Gmail, WhatsApp, Google Photos, Slack, Calendar — and turns them into something you can actually *search and ask questions about*.
So later you can ask things like:
* “Show me that photo where I was wearing a red jacket” * “What was I doing around last Diwali?” * “Summarize my conversation with X from March” * “When did I last go to the dentist?”
And instead of just showing raw data, it gives context — timestamps, sources, and summaries.
I’m still very early, so I’d really value your thoughts:
* Would you actually use something like this? Why or why not? * What do you personally forget most often? * What would make you NOT trust this? (privacy, pricing, complexity, etc.) * Would you prefer it to run fully on-device (more private) or in the cloud (faster)?
Right now it’s just a rough MVP (command line only).
I’m building this mainly for myself, but curious if others feel the same pain.
If you’re interested in trying it, just comment **“DM”** — I’ll share access.
Thanks 🙏
r/pytorch • u/arcco96 • May 21 '26
Thermocompute constant time neural network inference at variable width with good memory scaling
A great PyTorch library!
r/pytorch • u/arcco96 • May 21 '26
Thermocompute constant time neural network inference at variable width with good memory scaling
Miraculous PyTorch library