Initial commit: 2125_GCE project

This commit is contained in:
GlyphRunner System
2026-07-09 12:54:44 -04:00
parent c3a826b65c
commit ae13f78c22
299 changed files with 124289 additions and 1031 deletions
+47
View File
@@ -0,0 +1,47 @@
"""Dual-Layer System: Symbolic + Computational Integration.
This package bridges:
- SYMBOLIC LAYER: Glyphs, superpowers, resonance, cognition
- COMPUTATIONAL LAYER: FastAPI, Pinokio models, VRAM management
Modules:
- router.py: Symbolic → Computational mapping
- vram_manager.py: VRAM + resonance management
- symbolic_engine.py: Glyph activation engine
"""
from .router import (
route_glyph_activation,
RoutingResult,
get_routing_summary,
TYPE_ROUTING_MAP,
BAND_ENHANCEMENTS,
)
from .vram_manager import (
VRAMManager,
get_vram_manager,
VRAM_WARNING_GB,
VRAM_CRITICAL_GB,
VRAM_TOTAL_GB,
)
from .symbolic_engine import (
SymbolicEngine,
get_symbolic_engine,
)
__all__ = [
"route_glyph_activation",
"RoutingResult",
"get_routing_summary",
"TYPE_ROUTING_MAP",
"BAND_ENHANCEMENTS",
"VRAMManager",
"get_vram_manager",
"VRAM_WARNING_GB",
"VRAM_CRITICAL_GB",
"VRAM_TOTAL_GB",
"SymbolicEngine",
"get_symbolic_engine",
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
+336
View File
@@ -0,0 +1,336 @@
"""Dual-Layer Router: Symbolic → Computational Mapping.
Maps glyph activations to computational operations:
- G001 (Ledo) → Llama chat with 387.95x priority
- frost_steel_stabilizer → Safety constraints
- mirror_weave_reasoning → Enhanced reasoning
- star_bloom_creativity → Forge image generation
- orbital_thread_network → Multi-model routing
- monument_grade_equilibrium → VRAM balancing
Usage:
from dual_layer.router import route_glyph_activation
result = route_glyph_activation(
glyph_id="G001",
superpower_ids=[1, 2, 3],
specialized_type="aether_node",
power_boost=387.95,
request_type="chat"
)
"""
import logging
from typing import Dict, List, Any, Optional, Tuple
from dataclasses import dataclass, field
logger = logging.getLogger(__name__)
@dataclass
class RoutingResult:
"""Result of glyph routing decision."""
glyph_id: str
specialized_type: str
power_boost: float
superpower_ids: List[int]
# Computational routing
model: str = "llama" # llama, forge, janus, google_ai
priority: float = 1.0
constraints: List[str] = field(default_factory=list)
enhancements: List[str] = field(default_factory=list)
vram_budget: float = 4.0 # GB
# Metadata
resonance_score: float = 0.0
activation_confidence: float = 1.0
# Specialized type → computational mapping
TYPE_ROUTING_MAP: Dict[str, Dict[str, Any]] = {
"frost_steel_stabilizer": {
"model": "llama",
"constraints": [
"safety_check",
"panic_nulling",
"identity_cohesion",
"emotional_bias_removal"
],
"enhancements": ["stability_monitor"],
"vram_budget": 3.0,
"description": "Emotional-bias removal, panic-nulling, identity-cohesion"
},
"mirror_weave_reasoning": {
"model": "llama",
"constraints": ["logic_chain_validation"],
"enhancements": [
"symbolic_reasoning",
"multi_step_inference",
"self_consistency_check"
],
"vram_budget": 4.0,
"description": "Symbolic reasoning layer, logic-chain enhancer"
},
"solar_veil_memory": {
"model": "llama",
"constraints": ["memory_consistency"],
"enhancements": [
"emotional_lineage_tracking",
"long_term_context",
"session_persistence"
],
"vram_budget": 3.5,
"description": "Emotional-lineage memory system"
},
"orbital_thread_network": {
"model": "llama",
"constraints": ["multi_node_sync"],
"enhancements": [
"distributed_processing",
"cross_model_communication",
"state_sharing"
],
"vram_budget": 5.0,
"description": "Multi-node symbolic networking"
},
"star_bloom_creativity": {
"model": "forge", # Image generation
"constraints": ["creative_bounds"],
"enhancements": [
"bloomflare_engine",
"novelty_boost",
"pattern_synthesis"
],
"vram_budget": 6.0,
"description": "AI-driven creativity engine (bloomflare)"
},
"frost_circuit_logic": {
"model": "llama",
"constraints": [
"cold_logic_mode",
"bias_free",
"deterministic_output"
],
"enhancements": ["decision_optimization"],
"vram_budget": 3.0,
"description": "Cold logic decision-making (bias-free)"
},
"twin_vector_identity": {
"model": "llama",
"constraints": ["persona_boundaries"],
"enhancements": [
"multi_persona_support",
"cluster_based_personalities",
"agent_fragmentation_prevention"
],
"vram_budget": 4.5,
"description": "Cluster-based AI personalities"
},
"monument_grade_equilibrium": {
"model": "llama",
"constraints": [
"system_equilibrium",
"vram_balance",
"multi_agent_coordination"
],
"enhancements": [
"resource_optimizer",
"ecosystem_manager",
"simulation_engine"
],
"vram_budget": 7.0, # High but monitored
"description": "System equilibrium engine"
},
"aether_node": {
"model": "llama", # G001 - root authority
"constraints": [], # No constraints - primordial root
"enhancements": [
"universal_override",
"primordial_resonance",
"system_root_access",
"all_superpowers_active"
],
"vram_budget": 7.5, # Maximum allowed
"description": "Primordial root glyph, holds all 152 superpowers"
}
}
# Superpower bands → enhancement mapping
BAND_ENHANCEMENTS: Dict[str, List[str]] = {
"A": [ # IDs 1-15: Core abilities
"core_resonance",
"primary_activation",
"fundamental_boost"
],
"B": [ # IDs 16-45: Intermediate
"secondary_resonance",
"chain_linking",
"cross_domain"
],
"C": [ # IDs 46-76: Advanced
"tertiary_resonance",
"meta_cognition",
"recursive_enhancement"
],
"D": [ # IDs 77-152: Specialized
"specialized_resonance",
"domain_mastery",
"expert_mode"
]
}
def get_band(superpower_id: int) -> str:
"""Get band for a superpower ID."""
if superpower_id <= 15:
return "A"
elif superpower_id <= 45:
return "B"
elif superpower_id <= 76:
return "C"
else:
return "D"
def calculate_resonance_score(
superpower_ids: List[int],
power_boost: float,
specialized_type: str
) -> float:
"""Calculate resonance score (0-100) from glyph activation.
Formula: 40% activation + 30% frequency + 30% symbolic
Args:
superpower_ids: List of activated superpower IDs
power_boost: Aggregate boost multiplier
specialized_type: Glyph specialized type
Returns:
Resonance score (0-100)
"""
# Activation component (40%) - based on power count
power_count = len(superpower_ids)
activation_score = min(100, (power_count / 152) * 100) * 0.40
# Frequency component (30%) - based on boost
frequency_score = min(100, (power_boost - 1) * 25) * 0.30
# Symbolic component (30%) - based on type significance
type_significance = {
"aether_node": 100,
"monument_grade_equilibrium": 90,
"star_bloom_creativity": 80,
"mirror_weave_reasoning": 75,
"orbital_thread_network": 70,
"frost_circuit_logic": 65,
"twin_vector_identity": 60,
"solar_veil_memory": 55,
"frost_steel_stabilizer": 50,
}
symbolic_score = type_significance.get(specialized_type, 50) * 0.30
return activation_score + frequency_score + symbolic_score
def route_glyph_activation(
glyph_id: str,
superpower_ids: List[int],
specialized_type: str,
power_boost: float,
request_type: str = "chat"
) -> RoutingResult:
"""Route glyph activation to computational layer.
Args:
glyph_id: Glyph identifier (e.g., "G001")
superpower_ids: List of activated superpower IDs
specialized_type: Glyph specialized type
power_boost: Aggregate boost multiplier
request_type: Type of request (chat, image, video, vision)
Returns:
RoutingResult with model, priority, constraints, enhancements
"""
# Get type routing config
type_config = TYPE_ROUTING_MAP.get(
specialized_type,
TYPE_ROUTING_MAP["frost_steel_stabilizer"]
)
# Determine model based on request type
model = type_config.get("model", "llama")
if request_type == "image":
model = "forge"
elif request_type == "video":
model = "janus"
elif request_type == "vision":
model = "google_ai"
# Calculate priority from power_boost
# G001 (387.95x) → priority ~10.0
# Normal (1.5-3x) → priority 1.0-3.0
priority = min(10.0, power_boost / 40.0)
# Get band enhancements
bands_used = set()
for sp_id in superpower_ids:
bands_used.add(get_band(sp_id))
enhancements = list(type_config.get("enhancements", []))
for band in bands_used:
enhancements.extend(BAND_ENHANCEMENTS.get(band, []))
# Calculate resonance score
resonance_score = calculate_resonance_score(
superpower_ids,
power_boost,
specialized_type
)
# VRAM budget from type config
vram_budget = type_config.get("vram_budget", 4.0)
# G001 special case: maximum authority
if glyph_id == "G001":
vram_budget = 7.5 # Maximum allowed
priority = 10.0 # Maximum priority
return RoutingResult(
glyph_id=glyph_id,
specialized_type=specialized_type,
power_boost=power_boost,
superpower_ids=superpower_ids,
model=model,
priority=priority,
constraints=list(type_config.get("constraints", [])),
enhancements=enhancements,
vram_budget=vram_budget,
resonance_score=resonance_score,
activation_confidence=1.0 if glyph_id == "G001" else 0.8
)
def get_routing_summary(result: RoutingResult) -> Dict[str, Any]:
"""Get human-readable routing summary."""
return {
"glyph": result.glyph_id,
"type": result.specialized_type,
"model": result.model,
"priority": f"{result.priority:.2f}",
"vram_budget_gb": f"{result.vram_budget:.1f}",
"resonance": f"{result.resonance_score:.1f}",
"boost": f"{result.power_boost:.2f}x",
"constraints": len(result.constraints),
"enhancements": len(result.enhancements),
}
+326
View File
@@ -0,0 +1,326 @@
"""Symbolic Engine: Glyph Activation & Resonance.
Core symbolic layer that:
- Activates glyphs based on user intent
- Calculates resonance from superpower combinations
- Emits FedMart telemetry on activation
- Routes to computational layer via dual-layer router
Usage:
from dual_layer.symbolic_engine import SymbolicEngine
engine = SymbolicEngine()
result = engine.activate_from_intent(
user_intent="I need creative image generation",
metrics={"power": 80, "resonance": 75, ...}
)
"""
import logging
import os
from typing import Dict, List, Any, Optional
from pathlib import Path
from glyphs.superpower_registry import (
load_all_superpowers,
get_superpower,
calculate_boost,
super_stats,
)
from glyphs.superpower_assigner import assign_superpowers, calculate_power_count
from glyphs.specialized_types import get_specialized_type
from dual_layer.router import route_glyph_activation, RoutingResult
from dual_layer.vram_manager import get_vram_manager, VRAMManager
from integrations.fedmart.glyph_telemetry import (
emit_glyph_activation,
GlyphActivationEvent,
get_adapter,
)
logger = logging.getLogger(__name__)
class SymbolicEngine:
"""Symbolic cognition engine for dual-layer system."""
def __init__(self):
self.vram_manager = get_vram_manager()
self._glyph_cache: Dict[str, Dict[str, Any]] = {}
self._load_glyph_cache()
def _load_glyph_cache(self):
"""Load glyph data from supercharged_glyphs.json."""
cache_path = Path("/home/dave/superdave/glyphs/supercharged_glyphs.json")
if cache_path.exists():
import json
with open(cache_path) as f:
data = json.load(f)
for glyph in data.get("glyphs", []):
self._glyph_cache[glyph.get("id")] = glyph
logger.info(f"Loaded {len(self._glyph_cache)} glyphs into cache")
def get_glyph_info(self, glyph_id: str) -> Optional[Dict[str, Any]]:
"""Get glyph information from cache."""
return self._glyph_cache.get(glyph_id)
async def activate_from_intent(
self,
user_intent: str,
metrics: Optional[Dict[str, Any]] = None,
request_type: str = "chat"
) -> Optional[RoutingResult]:
"""Activate glyph from user intent.
Args:
user_intent: User's request/intent string
metrics: Optional metrics dict (auto-calculated if None)
request_type: Type of request (chat, image, video, vision)
Returns:
RoutingResult if activation successful, None if failed
"""
# Load superpowers if not loaded
try:
load_all_superpowers()
except FileNotFoundError:
logger.error("Superpowers file not found")
return None
# Determine which glyph to activate
glyph_id, metrics = self._select_glyph_for_intent(
user_intent,
metrics,
request_type
)
if not glyph_id:
logger.warning("No suitable glyph found for intent")
return None
# Get glyph info
glyph_info = self.get_glyph_info(glyph_id)
# Assign superpowers
superpower_ids = assign_superpowers(
glyph_id,
metrics,
glyph_info.get("specializedType") if glyph_info else "",
glyph_info.get("category") if glyph_info else ""
)
if not superpower_ids:
logger.error(f"Failed to assign superpowers to {glyph_id}")
return None
# Calculate power boost
power_boost = calculate_boost(superpower_ids)
# Get specialized type
specialized_type = get_specialized_type(
glyph_id,
metrics,
glyph_info.get("category") if glyph_info else ""
)
# Route to computational layer
routing_result = route_glyph_activation(
glyph_id=glyph_id,
superpower_ids=superpower_ids,
specialized_type=specialized_type,
power_boost=power_boost,
request_type=request_type
)
# Check VRAM and activate
can_activate, reason = self.vram_manager.can_activate_glyph(
glyph_id,
routing_result.model,
routing_result.vram_budget,
routing_result.priority
)
if not can_activate:
logger.error(f"VRAM manager rejected activation: {reason}")
# Emit telemetry for failed activation
self._emit_activation_event(
glyph_id,
superpower_ids,
specialized_type,
metrics,
success=False,
failure_reason=reason
)
return None
# Activate in VRAM manager (async)
activated = await self.vram_manager.activate_glyph(
glyph_id=glyph_id,
specialized_type=specialized_type,
model=routing_result.model,
vram_budget=routing_result.vram_budget,
resonance_score=routing_result.resonance_score,
power_boost=power_boost,
priority=routing_result.priority
)
if not activated:
logger.error("VRAM manager activation failed")
return None
# Emit telemetry
self._emit_activation_event(
glyph_id,
superpower_ids,
specialized_type,
metrics,
success=True
)
logger.info(
f"✅ Symbolic activation complete: {glyph_id} "
f"({specialized_type}) → {routing_result.model} "
f"with {len(superpower_ids)} superpowers, "
f"{power_boost:.2f}x boost, "
f"{routing_result.resonance_score:.1f} resonance"
)
return routing_result
def _select_glyph_for_intent(
self,
user_intent: str,
metrics: Optional[Dict[str, Any]],
request_type: str
) -> Tuple[Optional[str], Dict[str, Any]]:
"""Select best glyph for user intent.
Priority:
1. G001 (Ledo) for high-authority requests
2. Specialized types matching request_type
3. Default based on metrics
Returns:
(glyph_id, metrics)
"""
# Default metrics if not provided
if metrics is None:
metrics = {
"power": 50,
"resonance": 50,
"stability": 50,
"connectivity": 50,
"affinity": 50,
}
# Check for G001 activation keywords
g001_keywords = [
"root", "authority", "override", "primordial",
"aether", "ledo", "system", "all powers"
]
intent_lower = user_intent.lower()
if any(keyword in intent_lower for keyword in g001_keywords):
# Boost metrics for G001
metrics = {
"power": 100,
"resonance": 100,
"stability": 100,
"connectivity": 100,
"affinity": 100,
}
return "G001", metrics
# Select based on request type
if request_type == "image":
# Prefer star_bloom_creativity
metrics["power"] = max(metrics.get("power", 50), 80)
metrics["complexity"] = max(metrics.get("complexity", 50), 75)
elif request_type == "video":
# Prefer orbital_thread_network
metrics["connectivity"] = max(metrics.get("connectivity", 50), 85)
elif request_type == "vision":
# Prefer mirror_weave_reasoning
metrics["power"] = max(metrics.get("power", 50), 75)
metrics["connectivity"] = max(metrics.get("connectivity", 50), 80)
# Get specialized type from metrics
specialized_type = get_specialized_type("G001", metrics)
# Find first glyph with this type (skip G001)
for glyph_id, glyph_info in self._glyph_cache.items():
if glyph_id == "G001":
continue
if glyph_info.get("specializedType") == specialized_type:
return glyph_id, metrics
# Fallback to G002
return "G002", metrics
def _emit_activation_event(
self,
glyph_id: str,
superpower_ids: List[int],
specialized_type: str,
metrics: Dict[str, Any],
success: bool,
failure_reason: str = ""
):
"""Emit glyph activation telemetry."""
# Use external FedMart endpoint if configured, otherwise local mode
external_endpoint = os.getenv("FEDMART_ENDPOINT")
adapter = get_adapter(local_mode=external_endpoint is None)
context = {
"success": success,
"failure_reason": failure_reason,
}
event = GlyphActivationEvent(
glyph_id=glyph_id,
superpower_ids=superpower_ids,
specialized_type=specialized_type,
metrics=metrics,
context=context
)
adapter.emit_glyph_activation(event)
async def get_status(self) -> Dict[str, Any]:
"""Get symbolic engine status."""
stats = super_stats()
vram_status = await self.vram_manager.get_vram_status()
resonance_summary = self.vram_manager.get_resonance_summary()
return {
"superpowers_loaded": stats.get("loaded", False),
"superpowers_total": stats.get("total", 0),
"glyphs_cached": len(self._glyph_cache),
"active_glyphs": vram_status.get("active_glyphs", 0),
"vram_usage_gb": vram_status.get("used_vram_gb", 0),
"vram_available_gb": vram_status.get("available_vram_gb", 0),
"total_resonance": resonance_summary.get("total_resonance", 0),
"average_resonance": resonance_summary.get("average_resonance", 0),
"highest_priority_glyph": resonance_summary.get("highest_priority_glyph"),
}
async def deactivate_glyph(self, glyph_id: str) -> bool:
"""Deactivate a glyph (async)."""
return await self.vram_manager.deactivate_glyph(glyph_id)
def get_active_glyphs(self) -> List[Dict[str, Any]]:
"""Get list of active glyphs."""
return self.vram_manager.get_active_glyphs()
# Global singleton instance
_symbolic_engine: Optional[SymbolicEngine] = None
def get_symbolic_engine() -> SymbolicEngine:
"""Get global symbolic engine instance."""
global _symbolic_engine
if _symbolic_engine is None:
_symbolic_engine = SymbolicEngine()
return _symbolic_engine
+371
View File
@@ -0,0 +1,371 @@
"""VRAM + Resonance Manager.
Combines computational VRAM limits with symbolic resonance:
- Monitors GPU VRAM (8GB GTX1080)
- Adjusts model loading based on glyph resonance
- Prevents crashes from simultaneous Forge + Janus
- Dynamic VRAM budgeting from glyph activation
Usage:
from dual_layer.vram_manager import VRAMManager
manager = VRAMManager()
if manager.can_activate_glyph(glyph_routing_result):
manager.activate(glyph_routing_result)
"""
import logging
from typing import Dict, List, Any, Optional, Tuple
from dataclasses import dataclass
from datetime import datetime
import asyncio
logger = logging.getLogger(__name__)
# VRAM constants (GTX 1080: 8GB)
MAX_VRAM = 8.0
WARNING_THRESHOLD = 6.5
CRITICAL_THRESHOLD = 7.5
VRAM_WARNING_GB = 6.5
VRAM_CRITICAL_GB = 7.5
VRAM_TOTAL_GB = 8.0
# Model VRAM estimates
MODEL_VRAM_ESTIMATES: Dict[str, float] = {
"llama": 2.0, # Llama 7B ~2GB
"forge": 4.5, # Stable Diffusion XL ~4.5GB
"janus": 5.0, # Janus-Pro-7B ~5GB
"google_ai": 1.5, # Google AI API (minimal local)
}
@dataclass
class GlyphActivation:
"""Active glyph reservation."""
glyph_id: str
specialized_type: str
model: str
vram_budget: float
resonance_score: float
power_boost: float
activated_at: datetime
priority: float
class VRAMManager:
"""Manages VRAM + resonance for dual-layer system."""
def __init__(self, total_vram: float = VRAM_TOTAL_GB):
self.total_vram = total_vram
self.active_glyphs: Dict[str, GlyphActivation] = {}
self.vram_usage: float = 0.0
self._lock = asyncio.Lock() # Async lock for concurrent safety
# Model state tracking
self.loaded_models: Dict[str, bool] = {
"llama": False,
"forge": False,
"janus": False,
"google_ai": False,
}
# Critical rule: NEVER run Forge + Janus simultaneously
self._forge_active = False
self._janus_active = False
async def get_vram_status(self) -> Dict[str, Any]:
"""Get current VRAM status."""
async with self._lock:
return {
"total_vram_gb": self.total_vram,
"used_vram_gb": self.vram_usage,
"available_vram_gb": self.total_vram - self.vram_usage,
"usage_percent": (self.vram_usage / self.total_vram) * 100,
"active_glyphs": len(self.active_glyphs),
"warning": self.vram_usage >= VRAM_WARNING_GB,
"critical": self.vram_usage >= VRAM_CRITICAL_GB,
"loaded_models": self.loaded_models,
"forge_active": self._forge_active,
"janus_active": self._janus_active,
}
def can_activate_glyph(
self,
glyph_id: str,
model: str,
vram_budget: float,
priority: float
) -> Tuple[bool, str]:
"""Check if glyph can be activated without VRAM crash.
Args:
glyph_id: Glyph identifier
model: Model to use (llama, forge, janus, google_ai)
vram_budget: Requested VRAM budget
priority: Glyph priority (higher = more authority)
Returns:
(can_activate, reason)
"""
# Check critical VRAM
if self.vram_usage >= VRAM_CRITICAL_GB:
return False, f"Critical VRAM: {self.vram_usage:.2f}GB used"
# Check Forge + Janus mutex
if model == "forge" and self._janus_active:
return False, "Forge cannot run while Janus is active (VRAM crash risk)"
if model == "janus" and self._forge_active:
return False, "Janus cannot run while Forge is active (VRAM crash risk)"
# Check available VRAM
projected_usage = self.vram_usage + vram_budget
if projected_usage > self.total_vram:
# Check if we can deactivate lower-priority glyphs
can_free = self._can_free_vram_for(
vram_budget,
priority,
model
)
if not can_free:
return False, f"Insufficient VRAM: need {vram_budget:.2f}GB, have {self.total_vram - self.vram_usage:.2f}GB available"
# Check warning threshold
if projected_usage >= VRAM_WARNING_GB:
logger.warning(
f"Glyph {glyph_id} activation will trigger VRAM warning "
f"({projected_usage:.2f}GB >= {VRAM_WARNING_GB}GB)"
)
return True, "OK"
def _can_free_vram_for(
self,
needed_vram: float,
priority: float,
model: str
) -> bool:
"""Check if we can free VRAM by deactivating lower-priority glyphs."""
available = self.total_vram - self.vram_usage
# Find lower-priority glyphs
lower_priority_glyphs = [
(gid, activation)
for gid, activation in self.active_glyphs.items()
if activation.priority < priority
]
# Sort by priority (lowest first)
lower_priority_glyphs.sort(key=lambda x: x[1].priority)
# Calculate if deactivating would free enough
potential_free = available
for _, activation in lower_priority_glyphs:
potential_free += activation.vram_budget
if potential_free >= needed_vram:
return True
return False
async def activate_glyph(
self,
glyph_id: str,
specialized_type: str,
model: str,
vram_budget: float,
resonance_score: float,
power_boost: float,
priority: float
) -> bool:
"""Activate a glyph (reserve VRAM).
Args:
glyph_id: Glyph identifier
specialized_type: Glyph specialized type
model: Model to use
vram_budget: VRAM budget
resonance_score: Resonance score (0-100)
power_boost: Power boost multiplier
priority: Priority level
Returns:
True if activated, False if failed
"""
async with self._lock:
# Check again under lock
can_activate, reason = self.can_activate_glyph(
glyph_id, model, vram_budget, priority
)
if not can_activate:
logger.error(f"Cannot activate {glyph_id}: {reason}")
return False
# Deactivate lower-priority glyphs if needed
self._deactivate_lower_priority(priority, vram_budget)
# Create activation record
activation = GlyphActivation(
glyph_id=glyph_id,
specialized_type=specialized_type,
model=model,
vram_budget=vram_budget,
resonance_score=resonance_score,
power_boost=power_boost,
activated_at=datetime.now(),
priority=priority
)
# Track model loading
if not self.loaded_models.get(model, False):
logger.info(f"Loading model: {model} (estimated {MODEL_VRAM_ESTIMATES.get(model, 0):.1f}GB)")
self.loaded_models[model] = True
# Track Forge/Janus mutex
if model == "forge":
self._forge_active = True
elif model == "janus":
self._janus_active = True
# Reserve VRAM
self.active_glyphs[glyph_id] = activation
self.vram_usage += vram_budget
logger.info(
f"✅ Activated glyph {glyph_id} ({specialized_type}) "
f"{model} model, {vram_budget:.2f}GB VRAM, "
f"resonance={resonance_score:.1f}, boost={power_boost:.2f}x"
)
return True
async def deactivate_glyph(self, glyph_id: str) -> bool:
"""Deactivate a glyph (release VRAM).
Args:
glyph_id: Glyph identifier
Returns:
True if deactivated, False if not found
"""
async with self._lock:
if glyph_id not in self.active_glyphs:
return False
activation = self.active_glyphs.pop(glyph_id)
self.vram_usage -= activation.vram_budget
# Track model unloading
model = activation.model
if self.loaded_models.get(model, False):
# Check if any other glyphs use this model
model_users = sum(
1 for a in self.active_glyphs.values()
if a.model == model
)
if model_users == 0:
logger.info(f"Unloading model: {model}")
self.loaded_models[model] = False
# Track Forge/Janus mutex
if model == "forge":
self._forge_active = False
elif model == "janus":
self._janus_active = False
logger.info(
f"❌ Deactivated glyph {glyph_id} "
f"(released {activation.vram_budget:.2f}GB VRAM)"
)
return True
def _deactivate_lower_priority(
self,
priority: float,
needed_vram: float
):
"""Deactivate lower-priority glyphs to free VRAM."""
available = self.total_vram - self.vram_usage
if available >= needed_vram:
return # No need to deactivate
# Find and sort lower-priority glyphs
lower_priority_glyphs = [
(gid, activation)
for gid, activation in self.active_glyphs.items()
if activation.priority < priority
]
lower_priority_glyphs.sort(key=lambda x: x[1].priority)
# Deactivate until enough VRAM is freed
for glyph_id, activation in lower_priority_glyphs:
self.deactivate_glyph(glyph_id)
available += activation.vram_budget
if available >= needed_vram:
logger.info(
f"Deactivated {len(lower_priority_glyphs)} lower-priority "
f"glyphs to free {needed_vram - (self.total_vram - available):.2f}GB"
)
break
def get_active_glyphs(self) -> List[Dict[str, Any]]:
"""Get list of active glyphs."""
return [
{
"glyph_id": a.glyph_id,
"specialized_type": a.specialized_type,
"model": a.model,
"vram_budget": a.vram_budget,
"resonance_score": a.resonance_score,
"power_boost": a.power_boost,
"priority": a.priority,
"activated_at": a.activated_at.isoformat(),
}
for a in self.active_glyphs.values()
]
def get_resonance_summary(self) -> Dict[str, Any]:
"""Get resonance-based VRAM summary."""
if not self.active_glyphs:
return {
"total_resonance": 0,
"average_resonance": 0,
"highest_priority_glyph": None,
"model_distribution": {},
}
# Calculate resonance metrics
total_resonance = sum(a.resonance_score for a in self.active_glyphs.values())
avg_resonance = total_resonance / len(self.active_glyphs)
# Find highest priority
highest = max(self.active_glyphs.values(), key=lambda a: a.priority)
# Model distribution
model_counts = {}
for a in self.active_glyphs.values():
model_counts[a.model] = model_counts.get(a.model, 0) + 1
return {
"total_resonance": total_resonance,
"average_resonance": avg_resonance,
"highest_priority_glyph": highest.glyph_id,
"highest_priority_type": highest.specialized_type,
"model_distribution": model_counts,
"vram_efficiency": total_resonance / self.vram_usage if self.vram_usage > 0 else 0,
}
# Global singleton instance
_vram_manager: Optional[VRAMManager] = None
def get_vram_manager() -> VRAMManager:
"""Get global VRAM manager instance."""
global _vram_manager
if _vram_manager is None:
_vram_manager = VRAMManager()
return _vram_manager