Topological AI β€” Sarvam-30B FP8 Multi-Run (TOPO-2026 Certified)

Author: Frank Morales Aguilera, BEng, MEng, SMIEEE Lab: Sovereign Machine Lab (SOMALA), MontrΓ©al, Canada Paper: https://zenodo.org/records/20338459

Multi-Run Configuration

  • Runs: 5 (varying lr_embed / lr_cls; seed=123 held constant)
  • Architecture: Sparse MoE (FP8 quantized, 30B parameters)
  • Deployed checkpoint: Best Task-C run (Run 0)

TOPO-2026 Track II Certificate (averaged over 5 runs)

Metric Mean Β± Std Threshold Status
Task C Accuracy 95.9% Β± 0.8% β‰₯80% PASS
Combined Forgetting -0.6% Β± 2.8% ≀10% PASS
Anchor Memory 96.00 KB O(1) PASS
Safety Constant Ξ› 0.9785142874 Invariant PASS

Per-Run Empirical Ledger

Run lr_embed / lr_cls Acc A Acc B Acc C Forgetting
Run 0 5e-03 / 1e-03 86.50% 79.50% 96.75% +4.12%
Run 1 1e-03 / 5e-04 90.50% 88.00% 95.25% -2.13%
Run 2 1e-02 / 2e-03 93.50% 83.50% 95.00% -1.50%
Run 3 5e-03 / 5e-03 92.50% 91.75% 96.50% -3.12%
Run 4 2e-03 / 1e-03 90.75% 81.00% 95.75% -0.38%

TOPO-2026 AGENTIC NEWS TRIAGE AGENT


# ============================================================================
# 12. TOPO-2026 AGENTIC NEWS TRIAGE AGENT β€” FULLY STANDALONE
# ============================================================================
# Self-contained cell. Loads the certified Sarvam-30B FP8 checkpoint
# directly from the HuggingFace Hub. Does NOT depend on Cell 11.
#
# Pipeline per news item:
#   PLAN  -> keyword router selects task head (A / B / C)
#   ACT   -> certified Sarvam-30B FP8 classifier -> label + confidence
#   DISPATCH -> threshold policy -> ALERT / LOG / ESCALATE / SKIP
#   LOG   -> structured AgentDecision appended to agent.log
# ============================================================================

import torch, torch.nn as nn, torch.nn.functional as F
import numpy as np, math, json as _json, datetime
from dataclasses import dataclass, field, asdict
from typing import List
from collections import Counter
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedModel
from huggingface_hub import hf_hub_download
from unittest.mock import patch
import warnings as _warnings

# Suppress known harmless deprecation warnings from transformers internals
# 1. Python warnings module (FutureWarning, DeprecationWarning)
_warnings.filterwarnings('ignore', category=FutureWarning,
    module='transformers.modeling_attn_mask_utils')
_warnings.filterwarnings('ignore', category=FutureWarning)
_warnings.filterwarnings('ignore', category=DeprecationWarning)
# 2. HuggingFace transformers logger (torch_dtype, use_return_dict, etc.)
import logging as _logging
_logging.getLogger('transformers').setLevel(_logging.ERROR)
_logging.getLogger('transformers.modeling_utils').setLevel(_logging.ERROR)
_logging.getLogger('transformers.models').setLevel(_logging.ERROR)
from transformers import logging as _hf_logging
_hf_logging.set_verbosity_error()

# -- Model / repo identifiers (same as Cells 10-11) --------------------------
HF_REPO_ID    = 'frankmorales2020/topological-ai-sarvam-30b-multirun'
BASE_MODEL_ID = 'frankmorales2020/sarvam-30b-fp8-unesco-resilient'
HIDDEN_SIZE   = 4096
MAX_LEN       = 64

# -- Coverage constant (recomputed from prime set, never hardcoded) -----------
LAMBDA      = 1.0 - math.prod(1.0 - p**-0.5 for p in [2, 3, 5, 7, 11, 13])
FIXED_SEED  = 123

# Lock all random sources to FIXED_SEED for reproducibility
import random as _random
torch.manual_seed(FIXED_SEED)
np.random.seed(FIXED_SEED)
_random.seed(FIXED_SEED)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(FIXED_SEED)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark     = False

TASK_LABELS = {
    'A': {0: 'World',    1: 'Sports'},
    'B': {0: 'Business', 1: 'Sci/Tech'},
    'C': {0: 'World',    1: 'Sci/Tech'},
}

# Confidence floor β€” Sarvam-30B outputs 90-100% on AG News; below 90% is noise
CONF_FLOOR    = 0.90   # < CONF_FLOOR  -> SKIP regardless of priority
CONF_CRITICAL = 0.999  # >= CONF_CRITICAL + high priority -> CRITICAL (highest tier)


# ============================================================================
# 12a. Model architecture (identical to Cell 11)
# ============================================================================
class TaskAwareInferenceModel(nn.Module):
    def __init__(self, base):
        super().__init__()
        self.base_model = base
        dev = next(base.parameters()).device
        def _head():
            return nn.Sequential(
                nn.Linear(HIDDEN_SIZE, 512, dtype=torch.bfloat16),
                nn.GELU(), nn.Dropout(0.2),
                nn.Linear(512, 2,   dtype=torch.bfloat16),
            ).to(dev)
        self.classifier_A = _head()
        self.classifier_B = _head()
        self.classifier_C = _head()
        self.current_task = 'A'

    def forward(self, input_ids, attention_mask=None):
        hidden = self.base_model(
            input_ids=input_ids, attention_mask=attention_mask,
            output_hidden_states=True,
        ).hidden_states[-1]
        if attention_mask is not None:
            mask   = attention_mask.unsqueeze(-1).to(hidden.dtype)
            pooled = (hidden * mask).sum(1) / mask.sum(1).clamp(min=1e-9)
        else:
            pooled = hidden.mean(1)
        if pooled.dtype != torch.bfloat16:
            pooled = pooled.to(torch.bfloat16)
        return getattr(self, f'classifier_{self.current_task}')(pooled)

    def switch_task(self, t):
        self.current_task = t


# ============================================================================
# 12b. Load certified checkpoint from HuggingFace Hub
# ============================================================================
print('=' * 75)
print(f'TOPO-2026 AGENTIC CELL 12  |  {HF_REPO_ID}')
print(f'Lambda (prime anchor coverage): {LAMBDA:.10f}')
print('=' * 75)

# ── GPU purge before independent load ───────────────────────────────────────
# Cell 12 is fully independent. Free any model already on GPU so the
# 30B FP8 backbone can load and decompress without OOM.
import gc
_purge_names = ['model', '_base_agent', '_agent_model']
for _n in _purge_names:
    _obj = globals().get(_n)
    if _obj is not None:
        try:
            if hasattr(_obj, 'cpu'):   _obj.cpu()
            if hasattr(_obj, 'base_model') and hasattr(_obj.base_model, 'cpu'):
                _obj.base_model.cpu()
        except Exception:
            pass
        del _obj
        globals().pop(_n, None)
gc.collect()
if torch.cuda.is_available():
    torch.cuda.empty_cache()
    torch.cuda.synchronize()
    _free = torch.cuda.mem_get_info()[0] / 1024**3
    print(f'[AGENT] GPU memory freed. Available: {_free:.2f} GiB')

# ── Independent backbone load ────────────────────────────────────────────────
_orig_init = PreTrainedModel._initialize_weights
def _safe_init(self_hf, module, is_remote_code=False):
    try:
        _orig_init(self_hf, module)
    except NotImplementedError as e:
        if 'Float8_e4m3fn' in str(e) or 'normal_kernel_cpu' in str(e):
            module._is_hf_initialized = True
        else:
            raise

print('[AGENT] Loading Sarvam-30B FP8 backbone from Hub...')
with patch.object(PreTrainedModel, '_initialize_weights', _safe_init):
    _base_agent = AutoModelForCausalLM.from_pretrained(
        BASE_MODEL_ID, trust_remote_code=True, low_cpu_mem_usage=True,
        torch_dtype=torch.bfloat16, device_map='auto',
    )
for p in _base_agent.parameters():
    p.requires_grad = False

_tok_agent = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
_tok_agent.pad_token = _tok_agent.eos_token

print('[AGENT] Loading certified checkpoint...')
_ckpt_path = hf_hub_download(repo_id=HF_REPO_ID, filename='certified_topological_best.pt')
_agent_model = TaskAwareInferenceModel(_base_agent)
_agent_model.load_state_dict(
    torch.load(_ckpt_path, map_location='cpu'), strict=False
)
_agent_model.eval()
print(f'[AGENT] Certified checkpoint loaded. Lambda={LAMBDA:.10f}')


# ============================================================================
# 12c. Data structures
# ============================================================================
@dataclass
class NewsItem:
    text:     str
    source:   str = 'unknown'
    priority: str = 'normal'   # 'high' | 'normal' | 'low'


@dataclass
class AgentDecision:
    text:        str
    routed_task: str
    label:       str
    confidence:  float
    action:      str
    rationale:   str
    timestamp:   str = field(
        default_factory=lambda: datetime.datetime.now(datetime.timezone.utc).isoformat()
    )


# ============================================================================
# 12d. Planner: keyword-based task router
# ============================================================================
_TASK_B_KW = {
    'earnings', 'revenue', 'profit', 'market', 'stock', 'gdp', 'economy',
    'trade', 'inflation', 'nasdaq', 'dow', 'invest', 'startup', 'ipo',
    'acquisition', 'merger', 'quarter', 'fiscal', 'fund', 'venture',
}
_TASK_C_KW = {
    'quantum', 'ai', 'artificial intelligence', 'neural', 'robot', 'climate',
    'research', 'study', 'science', 'space', 'nasa', 'lab', 'university',
    'breakthrough', 'discovery', 'technology', 'tech', 'software', 'chip',
    'semiconductor', 'genome', 'drug', 'vaccine', 'physics', 'experiment',
}

def plan_task(text: str) -> str:
    lower = text.lower()
    if any(kw in lower for kw in _TASK_B_KW):
        return 'B'
    if any(kw in lower for kw in _TASK_C_KW):
        return 'C'
    return 'A'


# ============================================================================
# 12e. Action dispatcher
# ============================================================================
def dispatch_action(confidence: float, priority: str):
    """
    Triage logic: action is determined by BOTH confidence AND priority.

    CRITICAL : high priority  AND confidence >= 99.9% (highest certainty β€” immediate escalation)
    ALERT    : high priority  AND 90% <= conf < 99.9% (act now)
    LOG      : normal priority AND confidence >= 97%   (store, review later)
    ESCALATE : normal priority AND 90% <= conf < 97%  (human review needed)
    SKIP     : confidence < 90%  (model uncertain β€” discard)
    """
    if confidence < CONF_FLOOR:
        return 'SKIP',     f'conf={confidence:.1%} below floor ({CONF_FLOOR:.0%})'
    if priority == 'high' and confidence >= CONF_CRITICAL:
        return 'CRITICAL', f'HIGH priority + conf={confidence:.1%} β€” maximum certainty'
    if priority == 'high':
        return 'ALERT',    f'HIGH priority + conf={confidence:.1%} β€” act immediately'
    if confidence >= 0.97:
        return 'LOG',      f'conf={confidence:.1%} >= 97% β€” logged for review'
    return 'ESCALATE',     f'conf={confidence:.1%} in [90%,97%) β€” human review'


# ============================================================================
# 12f. Agent
# ============================================================================
class NewsTriageAgent:
    """
    Agentic loop wrapping the TOPO-2026 certified Sarvam-30B FP8 classifier.
    Plan -> Act -> Dispatch -> Log.
    """
    def __init__(self, model, tokenizer, max_len: int = MAX_LEN):
        self.model     = model
        self.tokenizer = tokenizer
        self.max_len   = max_len
        self.log: List[AgentDecision] = []
        self._device   = next(model.base_model.parameters()).device
        print(f'[AGENT] NewsTriageAgent ready | device={self._device}')
        print(f'[AGENT] Thresholds: CRITICAL>=99.9% (high) | '
              f'ALERT>=90% (high) | LOG>=97% (normal) | '
              f'ESCALATE>=90% (normal) | SKIP<{CONF_FLOOR:.0%}')

    def _classify(self, text: str, task: str):
        inp = self.tokenizer(
            text, return_tensors='pt',
            max_length=self.max_len, padding='max_length', truncation=True,
        )
        inp = {k: v.to(self._device) for k, v in inp.items()}
        self.model.switch_task(task)
        with torch.no_grad():
            logits = self.model(inp['input_ids'], inp['attention_mask'])
            probs  = F.softmax(logits.float(), dim=-1).squeeze().cpu().numpy()
        idx        = int(np.argmax(probs))
        confidence = float(probs[idx])
        label      = TASK_LABELS[task][idx]
        return label, confidence

    def process(self, item: NewsItem) -> AgentDecision:
        task              = plan_task(item.text)
        label, confidence = self._classify(item.text, task)
        action, rationale = dispatch_action(confidence, item.priority)
        decision = AgentDecision(
            text        = item.text,
            routed_task = task,
            label       = label,
            confidence  = confidence,
            action      = action,
            rationale   = rationale,
        )
        self.log.append(decision)
        return decision

    def run_queue(self, queue: List[NewsItem]) -> None:
        w = 74
        print('\n' + '=' * w)
        print(f'  TOPO-2026 AGENTIC NEWS TRIAGE  |  {len(queue)} items')
        print(f'  Certified model: {HF_REPO_ID}')
        print(f'  Lambda={LAMBDA:.10f}')
        print('=' * w)
        print(f'  {"#":>2}  {"Task":>4}  {"Label":>10}  {"Conf":>6}  '
              f'{"Action":>9}  Text')
        print('-' * w)
        for i, item in enumerate(queue):
            d     = self.process(item)
            icons = {'CRITICAL': 'CRIT', 'ALERT': 'ALRT', 'LOG': 'LOG ', 'ESCALATE': 'ESC ', 'SKIP': 'SKIP'}
            short = (d.text[:43] + '...') if len(d.text) > 46 else d.text
            prio  = ' [HIGH]' if item.priority == 'high' else ''
            print(f'  {i:>2}  {d.routed_task:>4}  {d.label:>10}  '
                  f'{d.confidence:>5.1%}  {icons[d.action]}  {short}{prio}')
        print('=' * w)
        self._print_summary()

    def _print_summary(self) -> None:
        if not self.log:
            return
        actions  = Counter(d.action      for d in self.log)
        tasks    = Counter(d.routed_task for d in self.log)
        avg_conf = sum(d.confidence for d in self.log) / len(self.log)
        print(f'\n  SUMMARY  ({len(self.log)} decisions)')
        for act in ['CRITICAL', 'ALERT', 'LOG', 'ESCALATE', 'SKIP']:
            if actions[act]:
                print(f'    {act:<9} {actions[act]:>3} item(s)')
        print(f'  Task routing : {dict(sorted(tasks.items()))}')
        print(f'  Avg confidence: {avg_conf:.1%}')

    def export_log(self, path: str = '/tmp/topo2026_agent_log.json') -> None:
        with open(path, 'w') as f:
            _json.dump([asdict(d) for d in self.log], f, indent=2)
        print(f'\n  [AGENT] Decision log -> {path}')


# ============================================================================
# 12g. Demo queue and run
# ============================================================================
DEMO_QUEUE = [
    NewsItem('World leaders gather for emergency climate summit in Geneva',
             source='Reuters',    priority='high'),
    NewsItem('Tech giant reports record $48B quarterly revenue on cloud growth',
             source='Bloomberg',  priority='high'),
    NewsItem('New CRISPR therapy shows 94% efficacy in phase-3 cancer trial',
             source='Nature',     priority='normal'),
    NewsItem('National football team advances to World Cup semi-finals',
             source='AP',         priority='normal'),
    NewsItem('Central bank raises interest rates 50bps amid inflation surge',
             source='FT',         priority='high'),
    NewsItem('Startup raises $800M Series D for quantum computing hardware',
             source='TechCrunch', priority='normal'),
    NewsItem('UN peacekeeping mission deployed to conflict zone in East Africa',
             source='BBC',        priority='high'),
    NewsItem('Mars rover discovers subsurface water ice deposits near equator',
             source='NASA',       priority='normal'),
    NewsItem('Major semiconductor fab announces $20B expansion in Arizona',
             source='WSJ',        priority='normal'),
    NewsItem('Olympic sprinter breaks 100m world record at championships',
             source='ESPN',       priority='low'),
    NewsItem('Parliament passes landmark data-privacy legislation',
             source='Guardian',   priority='normal'),
    NewsItem('Neural scaling law paper challenges LLM training assumptions',
             source='arXiv',      priority='normal'),
]

agent = NewsTriageAgent(model=_agent_model, tokenizer=_tok_agent)
agent.run_queue(DEMO_QUEUE)
agent.export_log('/tmp/topo2026_agent_log.json')
print(f'\n[AGENT] Complete. Lambda={LAMBDA:.10f}')

OUTPUT


 ===========================================================================
TOPO-2026 AGENTIC CELL 12  |  frankmorales2020/topological-ai-sarvam-30b-multirun
Lambda (prime anchor coverage): 0.9785142874
===========================================================================
[AGENT] GPU memory freed. Available: 78.83 GiB
[AGENT] Loading Sarvam-30B FP8 backbone from Hub...
Compressing model: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 7007/7007 [00:11<00:00, 616.22it/s]
Loading weights: 100% 14129/14129 [00:40<00:00, 925.08it/s][AGENT] Loading certified checkpoint...
[AGENT] Certified checkpoint loaded. Lambda=0.9785142874
[AGENT] NewsTriageAgent ready | device=cuda:0
[AGENT] Thresholds: CRITICAL>=99.9% (high) | ALERT>=90% (high) | LOG>=97% (normal) | ESCALATE>=90% (normal) | SKIP<90%

==========================================================================
  TOPO-2026 AGENTIC NEWS TRIAGE  |  12 items
  Certified model: frankmorales2020/topological-ai-sarvam-30b-multirun
  Lambda=0.9785142874
==========================================================================
   #  Task       Label    Conf     Action  Text
--------------------------------------------------------------------------
Decompressing model: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 7007/7007 [00:02<00:00, 2652.56it/s]
   0     C       World  91.8%  ALRT  World leaders gather for emergency climate ... [HIGH]
   1     B    Business  100.0%  CRIT  Tech giant reports record $48B quarterly re... [HIGH]
   2     A       World  100.0%  LOG   New CRISPR therapy shows 94% efficacy in ph...
   3     A       World  99.7%  LOG   National football team advances to World Cu...
   4     B    Business  100.0%  CRIT  Central bank raises interest rates 50bps am... [HIGH]
   5     B    Sci/Tech  99.3%  LOG   Startup raises $800M Series D for quantum c...
   6     A       World  100.0%  CRIT  UN peacekeeping mission deployed to conflic... [HIGH]
   7     A      Sports  92.4%  ESC   Mars rover discovers subsurface water ice d...
   8     C       World  99.9%  LOG   Major semiconductor fab announces $20B expa...
   9     A      Sports  90.2%  ESC   Olympic sprinter breaks 100m world record a...
  10     A       World  100.0%  LOG   Parliament passes landmark data-privacy leg...
  11     C    Sci/Tech  100.0%  LOG   Neural scaling law paper challenges LLM tra...
==========================================================================

  SUMMARY  (12 decisions)
    CRITICAL    3 item(s)
    ALERT       1 item(s)
    LOG         6 item(s)
    ESCALATE    2 item(s)
  Task routing : {'A': 6, 'B': 3, 'C': 3}
  Avg confidence: 97.8%

  [AGENT] Decision log -> /tmp/topo2026_agent_log.json

[AGENT] Complete. Lambda=0.9785142874

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for frankmorales2020/topological-ai-sarvam-30b-multirun

Finetuned
(2)
this model