V6+: add scripts/train_v6_progressive.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056"""train_v6_progressive.py — Treino V6 com esquema progressivo de samples.
═══════════════════════════════════════════════════════════════════════════════ESQUEMA PROGRESSIVO (conforme solicitado pelo usuário)═══════════════════════════════════════════════════════════════════════════════
Estágios: PONTUAÇÃO (start here): 100 → 200 → 300 → 400 → 500 samples (limite 500) TEXTO: 100 → 200 → 300 → 400 → 500 samples (limite 500) PESADOS: 10 → 20 → 30 → 40 → 50 → 60 → 70 → 80 → 90 → 100 samples (limite 100)
A cada estágio: * Treina 1 epoch (samples do estágio + cumulativos anteriores opcionais) * Captura 12/12 métricas (igual V5/V6) * Salva evolução (loss, perplexity, eval_acc, RSS, TFLOPS, etc.) * DELETA binários do treino anterior (model checkpoints) * Mantém apenas o estado em memória (continuidade do modelo)
Bug hunting V6 (continuação): * Bug 6 (NOVO, esta rodada): EWC consolidate com torch.enable_grad() no compute_fisher_diagonal — fix aplicado. * Bug 7 (NOVO, esta rodada): mtp_alphas não se movem porque MTP não tem gradient flowing via softmax com teacher forcing simples — adicionar entropia regularizer nos alphas para forçar movimento. * Bug 8 (conhecido): RLHF/TrustNeuralAgent só se move com reward feedback — sem humano no loop, fica inerte (esperado).
Arquitetura: inovadora — avaliação humana necessária para resultados baixos."""from __future__ import annotations
import osimport sysimport timeimport jsonimport gcimport mathimport randomimport psutilimport warningsimport shutilfrom pathlib import Pathfrom typing import Optional, List, Dict, Any
# ==============================================================================# CONFIGURAÇÃO XEON (ANTES de import torch)# ==============================================================================# Inline configuration (in case xeon_runtime.py is unavailable)os.environ.setdefault("OMP_NUM_THREADS", "2")os.environ.setdefault("MKL_NUM_THREADS", "2")os.environ.setdefault("OMP_DYNAMIC", "FALSE")os.environ.setdefault("MKL_DYNAMIC", "FALSE")os.environ.setdefault("MKL_ENABLE_INSTRUCTIONS", "AVX512")os.environ.setdefault("ONEDNN_MAX_CPU_ISA", "AMX_INT8")os.environ.setdefault("DNNL_PRIMITIVE_CACHE_CAPACITY", "1024")os.environ.setdefault("TOKENIZERS_PARALLELISM", "true")os.environ.setdefault("PYTHONUNBUFFERED", "1")os.environ.setdefault("KMP_AFFINITY", "granularity=fine,compact,1,0")os.environ.setdefault("KMP_BLOCKTIME", "1")
warnings.filterwarnings("ignore", category=UserWarning)warnings.filterwarnings("ignore", category=FutureWarning)warnings.filterwarnings("ignore", category=DeprecationWarning)
# Path setupSCRIPT_DIR = Path(__file__).resolve().parentPROJECT_DIR = SCRIPT_DIR.parentSRC_DIR = PROJECT_DIR / "BiGRU_T_version" / "src"sys.path.insert(0, str(SRC_DIR))sys.path.insert(0, str(SCRIPT_DIR))
import torchimport torch.nn as nnimport torch.nn.functional as Fimport numpy as np
# Try to apply IPEX optimizationtry: import intel_extension_for_pytorch as ipex HAS_IPEX = True IPEX_VERSION = ipex.__version__ torch._C._jit_set_profiling_mode(False) torch._C._jit_set_profiling_executor(False)except ImportError: HAS_IPEX = False IPEX_VERSION = None
torch.set_num_threads(2)torch.set_num_interop_threads(1)
# Imports do modelofrom bigru_t.model import UnifiedModel, UnifiedModelConfigfrom bigru_t.model.orq_cell import OrqCell, KohonenSOM4Dfrom bigru_t.model.window_context import WindowContextfrom bigru_t.model.mtp import MTPModule, MTPAdaptiveAttention, DynamicRepetitionPenaltyfrom bigru_t.training.elastic_weight_consolidation import ElasticWeightConsolidationfrom bigru_t.training.rlhf_possibility_mapping import LearnablePossibilityMappingfrom bigru_t.reasoning.reasoning_system import ReasoningEnginefrom bigru_t.tools import ToolCache, ToolMutationEngine, ToolAgentCoordinatorfrom bigru_t.inference.optimized_pipeline import OptimizedPipelinefrom bigru_t.inference.langchain_adapter import LangchainAdapterfrom bigru_t.data.data_augmentation import augment_batch, mixup, embedding_noisefrom bigru_t.training.meta_configurator import MetaConfiguratorfrom bigru_t.training.triplet_prototype_loss import TripletPrototypeLossfrom bigru_t.tokenizer.bbpe_tokenizer import BBPETokenizer, PAD_ID
# ==============================================================================# CONFIG# ==============================================================================EPOCHS = 1 # progressive: 1 epoch per stage (was 2)BATCH_SIZE = 8MAX_SEQ_LEN = 24 # slightly larger to fit punctuation + textLR = 3e-4NUM_LAYERS_HYP = 8TRIPLET_WEIGHT = 0.05META_INTERVAL = 5 # less frequent meta (samples are larger now)
# Progressive stagesPUNCTUATION_STAGES = [100, 200, 300, 400, 500]TEXT_STAGES = [100, 200, 300, 400, 500]HEAVY_STAGES = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
VOCAB_SIZE = None # setado após carregar tokenizer
# Checkpoint dir to delete between stagesCHECKPOINT_DIR = PROJECT_DIR / "BiGRU_T_version" / "_temp_checkpoints"CHECKPOINT_DIR.mkdir(parents=True, exist_ok=True)
# ==============================================================================# MONITOR DE MÉTRICAS (12/12 — CPU-adaptado, com evolução)# ==============================================================================class MetricsMonitor: def __init__(self): self.train_losses: List[float] = [] self.val_losses: List[float] = [] self.perplexities: List[float] = [] self.tokens_per_sec: List[float] = [] self.tflops: List[float] = [] self.time_per_step_ms: List[float] = [] self.grad_norms: List[float] = [] self.cpu_pcts: List[float] = [] self.rss_mbs: List[float] = [] self.alerts: List[tuple] = [] self.eval_accs: List[float] = [] self.overfitting_gaps: List[float] = [] self.mtp_alphas: List[float] = [] self.som_quant_losses: List[float] = [] self.som_neighborhood_entropies: List[float] = [] self.som_sigmas: List[float] = [] self.ewc_lambdas: List[float] = [] self.rlhf_rewards: List[float] = [] self.reasoning_steps: List[int] = []
def record(self, **kwargs): if "train_loss" in kwargs: self.train_losses.append(float(kwargs["train_loss"])) if "val_loss" in kwargs: self.val_losses.append(float(kwargs["val_loss"])) self.perplexities.append(math.exp(min(kwargs["val_loss"], 20))) if "tokens_per_sec" in kwargs: self.tokens_per_sec.append(float(kwargs["tokens_per_sec"])) if "tflops" in kwargs: self.tflops.append(float(kwargs["tflops"])) if "time_per_step_ms" in kwargs: self.time_per_step_ms.append(float(kwargs["time_per_step_ms"])) if "grad_norm" in kwargs: self.grad_norms.append(float(kwargs["grad_norm"])) g = kwargs["grad_norm"] if g < 1e-7: self.alerts.append((len(self.grad_norms), "vanishing_grad", f"||g||={g:.2e}")) elif g > 1e3: self.alerts.append((len(self.grad_norms), "exploding_grad", f"||g||={g:.2e}")) if "cpu_pct" in kwargs: self.cpu_pcts.append(float(kwargs["cpu_pct"])) if kwargs["cpu_pct"] < 80 and len(self.cpu_pcts) > 2: self.alerts.append((len(self.cpu_pcts), "cpu_underutil", f"cpu={kwargs['cpu_pct']:.1f}%")) if "rss_mb" in kwargs: self.rss_mbs.append(float(kwargs["rss_mb"])) if "eval_acc" in kwargs: self.eval_accs.append(float(kwargs["eval_acc"])) if "overfitting_gap" in kwargs: self.overfitting_gaps.append(float(kwargs["overfitting_gap"])) if "mtp_alpha_mean" in kwargs: self.mtp_alphas.append(float(kwargs["mtp_alpha_mean"])) if "som_quant_loss" in kwargs: self.som_quant_losses.append(float(kwargs["som_quant_loss"])) if "som_entropy" in kwargs: self.som_neighborhood_entropies.append(float(kwargs["som_entropy"])) if "som_sigma" in kwargs: self.som_sigmas.append(float(kwargs["som_sigma"])) if "ewc_lambda" in kwargs: self.ewc_lambdas.append(float(kwargs["ewc_lambda"])) if "rlhf_reward" in kwargs: self.rlhf_rewards.append(float(kwargs["rlhf_reward"])) if "reasoning_steps" in kwargs: self.reasoning_steps.append(int(kwargs["reasoning_steps"]))
def check_loss_spike(self, step: int): if len(self.train_losses) < 3: return recent = self.train_losses[-3:] if recent[1] > 0 and recent[2] > 1.5 * recent[1]: self.alerts.append((step, "loss_spike", f"{recent[1]:.3f} → {recent[2]:.3f}"))
def summary(self) -> dict: return { "n_steps": len(self.train_losses), "final_train_loss": self.train_losses[-1] if self.train_losses else None, "final_val_loss": self.val_losses[-1] if self.val_losses else None, "final_perplexity": self.perplexities[-1] if self.perplexities else None, "final_overfitting_gap": self.overfitting_gaps[-1] if self.overfitting_gaps else None, "final_eval_acc": self.eval_accs[-1] if self.eval_accs else None, "avg_tokens_per_sec": float(np.mean(self.tokens_per_sec)) if self.tokens_per_sec else None, "avg_tflops": float(np.mean(self.tflops)) if self.tflops else None, "avg_time_per_step_ms": float(np.mean(self.time_per_step_ms)) if self.time_per_step_ms else None, "avg_grad_norm": float(np.mean(self.grad_norms)) if self.grad_norms else None, "avg_cpu_pct": float(np.mean(self.cpu_pcts)) if self.cpu_pcts else None, "final_rss_mb": self.rss_mbs[-1] if self.rss_mbs else None, "max_rss_mb": max(self.rss_mbs) if self.rss_mbs else None, "rss_trend": "stable" if (len(self.rss_mbs) < 3 or max(self.rss_mbs) - min(self.rss_mbs) < 100) else "growing", "n_alerts": len(self.alerts), "alerts": self.alerts[:50], "alert_types": list(set(a[1] for a in self.alerts)), "mtp_alpha_mean": self.mtp_alphas[-1] if self.mtp_alphas else None, "som_quant_loss_final": self.som_quant_losses[-1] if self.som_quant_losses else None, "som_entropy_final": self.som_neighborhood_entropies[-1] if self.som_neighborhood_entropies else None, "som_sigma_final": self.som_sigmas[-1] if self.som_sigmas else None, "ewc_lambda_final": self.ewc_lambdas[-1] if self.ewc_lambdas else None, "rlhf_reward_final": self.rlhf_rewards[-1] if self.rlhf_rewards else None, "reasoning_steps_final": self.reasoning_steps[-1] if self.reasoning_steps else None, }
# ==============================================================================# GERAÇÃO DE DADOS — PONTUAÇÃO, TEXTO, PESADOS# ==============================================================================def generate_punctuation_samples(tokenizer, n: int, max_len: int = 24) -> List[dict]: """Datasets de pontuação — samples com vírgulas, pontos, pontos de interrogação.""" templates = [ "hello, how are you today?", "the cat, the dog, and the bird.", "what time is it? it is noon.", "yes, i can do that for you.", "no, thank you, i am fine.", "the sun, the moon, and the stars.", "are you sure? yes, i am sure.", "first, second, third, fourth, fifth.", "i think, therefore i am.", "well, that is interesting, isn't it?", "wait, what? are you serious?", "one, two, three, four, five, six.", "the man said, hello there.", "she asked, where is the book?", "he replied, it is here.", "quick, fast, now, go, run!", "slow, steady, patient, calm.", "red, blue, green, yellow, white.", "open, close, open, close, open.", "up, down, left, right, forward.", ] samples = [] for i in range(n): tmpl = templates[i % len(templates)] # Add some variation: append index token tmpl_full = tmpl tokens = tokenizer.encode(tmpl_full) if len(tokens) < 2: tokens = tokens + [0] * (2 - len(tokens)) if len(tokens) > max_len: tokens = tokens[:max_len] else: tokens = tokens + [0] * (max_len - len(tokens)) input_ids = torch.tensor(tokens, dtype=torch.long) target = input_ids.clone() target[1:] = input_ids[:-1].clone() target[0] = 0 samples.append({ "input_ids": input_ids, "target": target, "type": "punctuation", "caption": tmpl_full, }) return samples
def generate_text_samples(tokenizer, n: int, max_len: int = 24) -> List[dict]: """Datasets de texto — frases completas com estrutura gramatical.""" templates = [ "the quick brown fox jumps over the lazy dog", "she sells seashells by the seashore every day", "peter piper picked a peck of pickled peppers", "how much wood would a woodchuck chuck if able", "i scream you scream we all scream for ice cream", "the rain in spain falls mainly on the plain", "a stitch in time saves nine lives they say", "the early bird catches the worm every morning", "two plus two equals four in basic math class", "three times three is nine as we all know well", "the cat sat on the mat near the warm fire", "the dog ran fast across the green grass field", "birds fly high in the blue sky above us now", "fish swim deep in the cool clear blue lake", "the sun shines bright on the old wooden house", "the moon glows soft in the dark night sky", "rain falls hard on the roof of the cabin", "wind blows cold through the trees in winter", "the tree grows tall in the middle of forest", "the river flows wide past the small village", ] samples = [] for i in range(n): tmpl = templates[i % len(templates)] tokens = tokenizer.encode(tmpl) if len(tokens) < 2: tokens = tokens + [0] * (2 - len(tokens)) if len(tokens) > max_len: tokens = tokens[:max_len] else: tokens = tokens + [0] * (max_len - len(tokens)) input_ids = torch.tensor(tokens, dtype=torch.long) target = input_ids.clone() target[1:] = input_ids[:-1].clone() target[0] = 0 samples.append({ "input_ids": input_ids, "target": target, "type": "text", "caption": tmpl, }) return samples
def generate_heavy_samples(tokenizer, n: int, max_len: int = 24) -> List[dict]: """Datasets pesados — samples multimodais (simulados como longas com contexto).""" # Heavy = multimodal-like, longer sequences, more complex structure templates = [ "the image shows a cat sitting on a mat in the kitchen near the window", "audio transcription of a man speaking loudly about the weather today", "video frame shows a car driving fast on a highway during sunset hour", "the chart displays sales data from q1 to q4 with rising trend clearly", "scientific paper abstract about neural networks and their applications", "code review feedback on a python function that processes large datasets", "legal document clause about property rights and tenant obligations here", "medical report summary indicating patient recovery is on right track now", "financial analysis of quarterly earnings showing strong growth year", "technical specification for a new hardware module with low power design", ] samples = [] for i in range(n): tmpl = templates[i % len(templates)] tokens = tokenizer.encode(tmpl) if len(tokens) < 2: tokens = tokens + [0] * (2 - len(tokens)) if len(tokens) > max_len: tokens = tokens[:max_len] else: tokens = tokens + [0] * (max_len - len(tokens)) input_ids = torch.tensor(tokens, dtype=torch.long) target = input_ids.clone() target[1:] = input_ids[:-1].clone() target[0] = 0 samples.append({ "input_ids": input_ids, "target": target, "type": "heavy", "caption": tmpl, }) return samples
def make_mini_mcq_eval(tokenizer, n_questions=8): questions = [ ("2+2=?", ["3", "4", "5", "6"], 1), ("3*3=?", ["6", "8", "9", "10"], 2), ("5-1=?", ["3", "4", "5", "6"], 1), ("6+6=?", ["10", "11", "12", "13"], 2), ("10/2=?", ["4", "5", "6", "7"], 1), ("7*2=?", ["12", "13", "14", "15"], 2), ("8+1=?", ["8", "9", "10", "11"], 1), ("4*5=?", ["15", "20", "25", "30"], 1), ][:n_questions] encoded = [] for q, opts, ans in questions: q_tokens = tokenizer.encode(q) opt_tokens = [tokenizer.encode(o) for o in opts] encoded.append({ "q_tokens": q_tokens, "opt_tokens": opt_tokens, "answer_idx": ans, }) return encoded
def _save_partial_report(evolution: List[dict], n_params: int, current_stage: str = "", current_n: int = 0, current_epoch: int = 0): """Save partial report (called after each stage) so progress is preserved on crash.""" try: report = { "version": "V6_progressive", "timestamp": time.time(), "note": "Partial report — saved incrementally after each stage. Last update: " f"{current_stage} n={current_n} ep={current_epoch}", "config": { "epochs": EPOCHS, "batch_size": BATCH_SIZE, "max_seq_len": MAX_SEQ_LEN, "num_layers_hyp": NUM_LAYERS_HYP, "stages_punctuation": PUNCTUATION_STAGES, "stages_text": TEXT_STAGES, "stages_heavy": HEAVY_STAGES, }, "evolution": evolution, "n_stages_captured": len(evolution), "n_stages_total_expected": len(PUNCTUATION_STAGES) + len(TEXT_STAGES) + len(HEAVY_STAGES), "n_params": n_params, } report_path = PROJECT_DIR / "BiGRU_T_version" / "v6_progressive_report.json" report_path.write_text(json.dumps(report, indent=2, ensure_ascii=False, default=str)) except Exception as e: print(f" [WARN] _save_partial_report failed: {e}")
def cleanup_binaries(): """Delete training binaries (checkpoints, model_final, etc.) to free disk.""" if CHECKPOINT_DIR.exists(): # Remove .pt, .pth, .bin, .safetensors files for ext in ["*.pt", "*.pth", "*.bin", "*.safetensors", "*.ckpt"]: for f in CHECKPOINT_DIR.glob(ext): try: f.unlink() print(f" [CLEAN] Deleted binary: {f.name}") except Exception: pass # Also clean any model_final binaries model_final_dir = PROJECT_DIR / "BiGRU_T_version" / "model_final" if model_final_dir.exists(): for ext in ["*.pt", "*.pth", "*.bin", "*.safetensors", "*.ckpt"]: for f in model_final_dir.glob(ext): try: f.unlink() print(f" [CLEAN] Deleted model_final binary: {f.name}") except Exception: pass # Cleanup torch and HF caches gc.collect(0) if hasattr(torch, "cuda") and torch.cuda.is_available(): torch.cuda.empty_cache()
# ==============================================================================# TREINO POR ESTÁGIO# ==============================================================================def train_stage( model, optimizer, pipeline, meta_cfg, ewc, mtp, rep_penalty, monitor, mcq_eval, train_samples, val_samples, stage_name: str, stage_n: int, epoch: int = 1, prototypes: List[torch.Tensor] = None,) -> dict: """Treina 1 estágio (1 ou mais epochs). Retorna summary do estágio.""" print(f"\n{'=' * 72}") print(f" STAGE: {stage_name} (n={stage_n}, epoch={epoch})") print(f"{'=' * 72}")
n_train = max(1, int(0.85 * len(train_samples))) train_set = train_samples[:n_train] val_set = val_samples[:max(1, len(val_samples))]
step = 0 t_start = time.perf_counter() process = psutil.Process() n_params = sum(p.numel() for p in model.parameters())
random.shuffle(train_set)
for batch_start in range(0, len(train_set), BATCH_SIZE): batch = train_set[batch_start:batch_start + BATCH_SIZE] if len(batch) < 2: continue
input_ids = torch.stack([s["input_ids"] for s in batch]) targets = torch.stack([s["target"] for s in batch])
t_step_start = time.perf_counter()
# Data augmentation V6 (10% chance) if random.random() < 0.1: input_ids = augment_batch(input_ids, pad_id=0, p_delete=0.05, n_swaps=1, p_dropout=0.02)
# Forward via pipeline try: out = pipeline.forward( input_ids, targets, use_mtp=True, use_reasoning=False, use_rep_penalty=True, return_aux=True, ) logits = out["logits"] loss = out["loss"] aux = out.get("aux") or {} except Exception as e: print(f" [WARN] Pipeline forward failed: {e}") model_out = model(input_ids) if isinstance(model_out, tuple): logits = model_out[0] else: logits = model_out tgt_last = targets[:, -1] loss = F.cross_entropy(logits, tgt_last, ignore_index=-100) aux = {}
# Backward optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) optimizer.step()
# Stats grad_norm = sum( (p.grad ** 2).sum().item() for p in model.parameters() if p.grad is not None ) ** 0.5 t_step = time.perf_counter() - t_step_start cpu_pct = process.cpu_percent() rss_mb = process.memory_info().rss / 1024 / 1024 n_tokens = input_ids.numel() tps = n_tokens / t_step if t_step > 0 else 0 tflops = 6 * n_params * n_tokens / t_step / 1e12 if t_step > 0 else 0
mtp_alpha_val = 0.0 if isinstance(aux.get("mtp"), dict) and "alpha_1" in aux["mtp"]: mtp_alpha_val = float(aux["mtp"]["alpha_1"]) elif hasattr(mtp, "alphas"): try: mtp_alpha_val = float(mtp.alphas[0].item()) except Exception: pass
som_ql = float(aux.get("orq_som_quant_loss", 0.0)) if isinstance(aux.get("orq_som_quant_loss"), (int, float, int, float)) else 0.0 som_s = float(aux.get("orq_som_sigma", 0.0)) if isinstance(aux.get("orq_som_sigma"), (int, float, int, float)) else 0.0
monitor.record( train_loss=float(loss.item()), tokens_per_sec=tps, tflops=tflops, time_per_step_ms=t_step * 1000, grad_norm=grad_norm, cpu_pct=cpu_pct, rss_mb=rss_mb, mtp_alpha_mean=mtp_alpha_val, som_quant_loss=som_ql, som_sigma=som_s, ewc_lambda=float(ewc.lambda_val.item()) if hasattr(ewc, "lambda_val") else 0.0, rlhf_reward=0.0, ) monitor.check_loss_spike(step)
if step % 10 == 0 or step < 5: print(f" step {step+1:3d} | loss={loss.item():.4f} | tps={tps:.0f} | " f"tflops={tflops:.4f} | ||g||={grad_norm:.2f} | cpu={cpu_pct:.0f}% | rss={rss_mb:.0f}MB")
# MetaConfigurator if step % META_INTERVAL == 0 and step > 0: try: val_input = torch.stack([s["input_ids"] for s in val_set[:BATCH_SIZE]]) val_target = torch.stack([s["target"] for s in val_set[:BATCH_SIZE]]) val_target_last = val_target[:, -1] meta_cfg.forward_with_meta(val_input, val_target_last) except Exception as e: pass # silent — meta failure is OK
# Cleanup del logits, loss, input_ids, targets if "aux" in dir(): del aux gc.collect(0)
step += 1
# Validation model.eval() val_losses_epoch = [] with torch.no_grad(): for val_batch_start in range(0, len(val_set), BATCH_SIZE): val_batch = val_set[val_batch_start:val_batch_start + BATCH_SIZE] if len(val_batch) < 1: continue val_input = torch.stack([s["input_ids"] for s in val_batch]) val_target = torch.stack([s["target"] for s in val_batch]) try: val_out = pipeline.forward( val_input, val_target, use_mtp=False, use_reasoning=False, use_rep_penalty=False, return_aux=False, ) val_loss = val_out["loss"] if val_loss is not None: val_losses_epoch.append(float(val_loss.item())) except Exception: model_out = model(val_input) if isinstance(model_out, tuple): val_logits = model_out[0] else: val_logits = model_out tgt_last = val_target[:, -1] vl = F.cross_entropy(val_logits, tgt_last, ignore_index=-100) val_losses_epoch.append(float(vl.item()))
val_loss_mean = float(np.mean(val_losses_epoch)) if val_losses_epoch else 0.0 train_loss_mean = float(np.mean(monitor.train_losses[-step:])) if monitor.train_losses else 0.0 overfitting_gap = train_loss_mean - val_loss_mean ppl = math.exp(min(val_loss_mean, 20))
# MCQ eval eval_acc = 0.0 n_correct = 0 n_total = 0 with torch.no_grad(): for q in mcq_eval: try: q_t = torch.tensor([q["q_tokens"]], dtype=torch.long) if q_t.shape[1] < MAX_SEQ_LEN: q_t = F.pad(q_t, (0, MAX_SEQ_LEN - q_t.shape[1])) else: q_t = q_t[:, :MAX_SEQ_LEN] out = model(q_t) if isinstance(out, tuple): q_logits = out[0] else: q_logits = out if q_logits.dim() == 2: last_logits = q_logits[0] else: last_logits = q_logits[0, -1, :] opt_scores = [] for opt_tokens in q["opt_tokens"]: if len(opt_tokens) > 0: opt_scores.append(float(last_logits[opt_tokens[0]].item())) else: opt_scores.append(0.0) pred_idx = int(np.argmax(opt_scores)) if pred_idx == q["answer_idx"]: n_correct += 1 n_total += 1 except Exception: n_total += 1 if n_total > 0: eval_acc = n_correct / n_total
monitor.record( val_loss=val_loss_mean, overfitting_gap=overfitting_gap, eval_acc=eval_acc, )
elapsed = time.perf_counter() - t_start print(f" STAGE END | val_loss={val_loss_mean:.4f} | train_avg={train_loss_mean:.4f} | " f"ppl={ppl:.2f} | gap={overfitting_gap:+.4f} | eval_acc={eval_acc*100:.2f}% | " f"elapsed={elapsed:.1f}s | n_steps={step}")
# SOM stats som_stats = {} if hasattr(model, "orq_cell") and hasattr(model.orq_cell, "som"): som = model.orq_cell.som som_stats = { "sigma": float(som.sigma_current.item()), "alpha": float(som.alpha_current.item()), "step": int(som.step_count.item()), } print(f" SOM: σ={som_stats['sigma']:.3f}, α={som_stats['alpha']:.4f}, step={som_stats['step']}")
model.train() return { "stage": stage_name, "n_samples": stage_n, "epoch": epoch, "n_steps": step, "elapsed_s": elapsed, "val_loss": val_loss_mean, "train_loss_mean": train_loss_mean, "overfitting_gap": overfitting_gap, "perplexity": ppl, "eval_acc": eval_acc, "final_rss_mb": monitor.rss_mbs[-1] if monitor.rss_mbs else 0.0, "max_rss_mb": max(monitor.rss_mbs) if monitor.rss_mbs else 0.0, "avg_tflops": float(np.mean(monitor.tflops[-step:])) if monitor.tflops else 0.0, "avg_tokens_per_sec": float(np.mean(monitor.tokens_per_sec[-step:])) if monitor.tokens_per_sec else 0.0, "n_alerts_in_stage": sum(1 for a in monitor.alerts if a[0] >= len(monitor.train_losses) - step), "som": som_stats, "n_train": len(train_set), "n_val": len(val_set), }
# ==============================================================================# MAIN# ==============================================================================def main(): print("=" * 72) print("TREINO V6 PROGRESSIVO — esquema de samples crescente") print("=" * 72) print(f" Python: {sys.version.split()[0]}") print(f" Torch: {torch.__version__}") print(f" IPEX ativo: {HAS_IPEX}" + (f" (v{IPEX_VERSION})" if HAS_IPEX else "")) print(f" OneDNN enabled: {torch.backends.mkldnn.enabled}") print(f" ONEDNN_MAX_CPU_ISA: {os.environ.get('ONEDNN_MAX_CPU_ISA')}") print(f" OMP_NUM_THREADS: {os.environ.get('OMP_NUM_THREADS')}") print(f" EPOCHS: {EPOCHS}") print(f" BATCH_SIZE: {BATCH_SIZE}") print(f" MAX_SEQ_LEN: {MAX_SEQ_LEN}") print(f" NUM_LAYERS_HYP: {NUM_LAYERS_HYP}") print(f" Stages PUNCT: {PUNCTUATION_STAGES}") print(f" Stages TEXT: {TEXT_STAGES}") print(f" Stages HEAVY: {HEAVY_STAGES}") print("=" * 72)
# 1. Tokenizer tokenizer_path = str(PROJECT_DIR / "BiGRU_T_version" / "model_final" / "tokenizer" / "tokenizer.json") print(f"\n[INFO] Loading tokenizer: {tokenizer_path}") tokenizer = BBPETokenizer.load(tokenizer_path) global VOCAB_SIZE VOCAB_SIZE = tokenizer.vocab_size print(f"[INFO] Tokenizer loaded: vocab_size={VOCAB_SIZE}")
# 2. Model cfg = UnifiedModelConfig( vocab_size=VOCAB_SIZE, d_model=128, max_seq_len=MAX_SEQ_LEN, bigru_hidden=32, d_transformer=64, nhead_tu=4, d_ff_tu=128, output_dim_u8cell=64, max_modules=8, cache_len=16, d_cache=128, nhead_orq=8, d_ff_orq=256, trainT_dim=128, nhead_train=4, d_ff_train=256, num_layers_train=2, hypT_dim=128, nhead_hyp=4, d_ff_hyp=256, num_layers_hyp=NUM_LAYERS_HYP, use_circular_reasoning=True, circular_n_cycles=3, ) model = UnifiedModel(cfg) n_params = sum(p.numel() for p in model.parameters()) print(f"[INFO] Model params: {n_params:,} ({n_params/1e6:.2f}M)")
# Apply IPEX optimize to model (CPU optimization) if HAS_IPEX: try: model = ipex.optimize(model, level="O1") print(f"[INFO] ipex.optimize applied (level=O1)") except Exception as e: print(f"[WARN] ipex.optimize failed: {e}")
# 3. V6 Components mtp = MTPModule(d_model=128, vocab_size=VOCAB_SIZE, K=4, learnable_alpha=True) if hasattr(model, "token_embedding"): mtp.set_embedding(model.token_embedding)
mtp_adaptive_attn = MTPAdaptiveAttention(d_model=128, nhead=4, window_size=8)
rep_penalty = DynamicRepetitionPenalty( vocab_size=VOCAB_SIZE, embedding_dim=128, window_size=32, ) if hasattr(model, "token_embedding"): rep_penalty.set_embedding(model.token_embedding)
reasoning_engine = ReasoningEngine( d_model=128, vocab_size=VOCAB_SIZE, n_phases=8, max_steps=4, use_bayesian_selector=True, )
ewc = ElasticWeightConsolidation(model, lambda_init=10.0, adaptive_lambda=True)
rlhf_mapping = LearnablePossibilityMapping( model=model, vocab_size=VOCAB_SIZE, d_model=128, )
tool_cache = ToolCache(max_size=64, ttl=600.0) tool_engine = ToolMutationEngine(cache=tool_cache) def echo_tool(x): return x tool_engine.register("echo", echo_tool, "Echo tool for testing") tool_engine.compose("double_echo", "echo", "echo", "Echo composed with echo") tool_agent = ToolAgentCoordinator(mutation_engine=tool_engine, max_workers=2)
pipeline = OptimizedPipeline( model=model, tokenizer=tokenizer, mtp=mtp, mtp_adaptive_attn=mtp_adaptive_attn, rep_penalty=rep_penalty, reasoning_engine=reasoning_engine, ewc=ewc, rlhf_mapping=rlhf_mapping, tool_engine=tool_engine, tool_agent=tool_agent, )
meta_cfg = MetaConfigurator(model, meta_lr=0.01)
optimizer = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=0.01)
monitor = MetricsMonitor() mcq_eval = make_mini_mcq_eval(tokenizer, n_questions=8)
# 4. Run progressive stages evolution: List[dict] = [] cumulative_punct = [] cumulative_text = [] cumulative_heavy = []
# === RESUME SUPPORT: skip already-completed stages === # If a partial report exists, load it and skip those stages RESUME_REPORT = PROJECT_DIR / "BiGRU_T_version" / "v6_progressive_report.json" if RESUME_REPORT.exists(): try: old_report = json.loads(RESUME_REPORT.read_text()) old_evolution = old_report.get("evolution", []) # Filter only complete stages (those with val_loss) completed_stages = [ (e["stage"], e["n_samples"], e["epoch"]) for e in old_evolution if e.get("val_loss") is not None ] if completed_stages: print(f"\n[INFO] RESUME: Found {len(completed_stages)} completed stages from previous run:") for s, n, ep in completed_stages: print(f" - {s} n={n} ep={ep}") # Carry over the old evolution entries evolution = [e for e in old_evolution if e.get("val_loss") is not None] # Filter the stages lists to skip completed ones # (For simplicity, we just skip the entire stage type if its first stage is done) # Actually, we need to skip per-stage, so let's just track which to skip _skip_set = set(completed_stages) def _should_skip(stage, n, ep): return (stage, n, ep) in _skip_set print(f"[INFO] Will skip these stages and continue from where it left off.") else: _should_skip = lambda *args: False except Exception as e: print(f"[WARN] Could not load resume report: {e}") _should_skip = lambda *args: False else: _should_skip = lambda *args: False
# === PUNCTUATION STAGES === print(f"\n[INFO] === PONTUAÇÃO STAGES ===") for stage_n in PUNCTUATION_STAGES: for epoch in range(EPOCHS): if _should_skip("punctuation", stage_n, epoch): print(f"\n[INFO] SKIP: punctuation n={stage_n} ep={epoch} (already done)") continue # Generate stage samples (cumulative) new_samples = generate_punctuation_samples(tokenizer, stage_n, MAX_SEQ_LEN) cumulative_punct.extend(new_samples) # Use up to stage_n samples (replace if too many) random.shuffle(cumulative_punct) train_samples = cumulative_punct[:stage_n] # Make a small val set val_samples = train_samples[-max(2, stage_n // 10):] train_samples = train_samples[:-max(2, stage_n // 10)]
for epoch in range(EPOCHS): if _should_skip("punctuation", stage_n, epoch): continue stage_result = train_stage( model=model, optimizer=optimizer, pipeline=pipeline, meta_cfg=meta_cfg, ewc=ewc, mtp=mtp, rep_penalty=rep_penalty, monitor=monitor, mcq_eval=mcq_eval, train_samples=train_samples, val_samples=val_samples, stage_name=f"punctuation", stage_n=stage_n, epoch=epoch, ) evolution.append(stage_result) # Incremental save (in case of crash) _save_partial_report(evolution, n_params, "punctuation", stage_n, epoch)
# EWC consolidate after each punctuation stage try: with torch.enable_grad(): cons_input = torch.stack([s["input_ids"] for s in train_samples[:BATCH_SIZE]]) cons_target = torch.stack([s["target"] for s in train_samples[:BATCH_SIZE]]) def ewc_criterion(logits, target): if logits.dim() == 2 and target.dim() == 2: target = target[:, -1] return F.cross_entropy(logits, target, ignore_index=-100) ewc.consolidate(cons_input, cons_target, criterion=ewc_criterion, n_samples=1) except Exception as e: print(f" [WARN] EWC consolidate failed: {e}")
# Delete binaries between stages cleanup_binaries()
# === TEXT STAGES === print(f"\n[INFO] === TEXTO STAGES ===") for stage_n in TEXT_STAGES: for epoch in range(EPOCHS): if _should_skip("text", stage_n, epoch): print(f"\n[INFO] SKIP: text n={stage_n} ep={epoch} (already done)") continue new_samples = generate_text_samples(tokenizer, stage_n, MAX_SEQ_LEN) cumulative_text.extend(new_samples) random.shuffle(cumulative_text) train_samples = cumulative_text[:stage_n] val_samples = train_samples[-max(2, stage_n // 10):] train_samples = train_samples[:-max(2, stage_n // 10)]
for epoch in range(EPOCHS): if _should_skip("text", stage_n, epoch): continue stage_result = train_stage( model=model, optimizer=optimizer, pipeline=pipeline, meta_cfg=meta_cfg, ewc=ewc, mtp=mtp, rep_penalty=rep_penalty, monitor=monitor, mcq_eval=mcq_eval, train_samples=train_samples, val_samples=val_samples, stage_name=f"text", stage_n=stage_n, epoch=epoch, ) evolution.append(stage_result) _save_partial_report(evolution, n_params, "text", stage_n, epoch)
# EWC consolidate try: with torch.enable_grad(): cons_input = torch.stack([s["input_ids"] for s in train_samples[:BATCH_SIZE]]) cons_target = torch.stack([s["target"] for s in train_samples[:BATCH_SIZE]]) def ewc_criterion_t(logits, target): if logits.dim() == 2 and target.dim() == 2: target = target[:, -1] return F.cross_entropy(logits, target, ignore_index=-100) ewc.consolidate(cons_input, cons_target, criterion=ewc_criterion_t, n_samples=1) except Exception as e: print(f" [WARN] EWC consolidate failed: {e}")
cleanup_binaries()
# === HEAVY STAGES === print(f"\n[INFO] === PESADOS STAGES ===") for stage_n in HEAVY_STAGES: for epoch in range(EPOCHS): if _should_skip("heavy", stage_n, epoch): print(f"\n[INFO] SKIP: heavy n={stage_n} ep={epoch} (already done)") continue new_samples = generate_heavy_samples(tokenizer, stage_n, MAX_SEQ_LEN) cumulative_heavy.extend(new_samples) random.shuffle(cumulative_heavy) train_samples = cumulative_heavy[:stage_n] val_samples = train_samples[-max(2, stage_n // 10):] train_samples = train_samples[:-max(2, stage_n // 10)]
for epoch in range(EPOCHS): if _should_skip("heavy", stage_n, epoch): continue stage_result = train_stage( model=model, optimizer=optimizer, pipeline=pipeline, meta_cfg=meta_cfg, ewc=ewc, mtp=mtp, rep_penalty=rep_penalty, monitor=monitor, mcq_eval=mcq_eval, train_samples=train_samples, val_samples=val_samples, stage_name=f"heavy", stage_n=stage_n, epoch=epoch, ) evolution.append(stage_result) _save_partial_report(evolution, n_params, "heavy", stage_n, epoch)
# EWC consolidate try: with torch.enable_grad(): cons_input = torch.stack([s["input_ids"] for s in train_samples[:BATCH_SIZE]]) cons_target = torch.stack([s["target"] for s in train_samples[:BATCH_SIZE]]) def ewc_criterion_h(logits, target): if logits.dim() == 2 and target.dim() == 2: target = target[:, -1] return F.cross_entropy(logits, target, ignore_index=-100) ewc.consolidate(cons_input, cons_target, criterion=ewc_criterion_h, n_samples=1) except Exception as e: print(f" [WARN] EWC consolidate failed: {e}")
cleanup_binaries()
# 5. Final summary print(f"\n{'=' * 72}") print(f" EVOLUÇÃO COMPLETA") print(f"{'=' * 72}") print(f" {'stage':<14} {'n':>4} {'ep':>3} {'val_loss':>10} {'ppl':>10} {'eval_acc':>10} {'rss_mb':>8} {'tflops':>8}") print(f" {'-'*14} {'-'*4} {'-'*3} {'-'*10} {'-'*10} {'-'*10} {'-'*8} {'-'*8}") for e in evolution: print(f" {e['stage']:<14} {e['n_samples']:>4} {e['epoch']:>3} " f"{e['val_loss']:>10.4f} {e['perplexity']:>10.2f} " f"{e['eval_acc']*100:>9.2f}% {e['final_rss_mb']:>8.0f} {e['avg_tflops']:>8.4f}")
final_summary = monitor.summary() final_lp = { "T": float(torch.exp(meta_cfg.log_temperature.detach()).item()), "tau": float(torch.exp(meta_cfg.log_tau.detach()).item()), "lambda_ent": float(torch.exp(model.log_lambda_ent.detach()).item()) if hasattr(model, "log_lambda_ent") else None, "init_gate": float(model.circular_reasoning.residual_gate.mean().item()) if hasattr(model, "circular_reasoning") else None, "mtp_alphas": [float(x) for x in mtp.alphas.tolist()] if mtp.K > 1 else [], "rep_penalty_lambda": float(rep_penalty.lambda_val.item()), "ewc_lambda": float(ewc.lambda_val.item()), "rlhf_eta": float(rlhf_mapping.eta.item()), } final_summary["final_learnable_params"] = final_lp final_summary["n_w8a8_layers"] = sum(1 for m in model.modules() if "QuantizedLinear" in type(m).__name__ or "SmoothQuantW8A8" in type(m).__name__)
print(f"\n[INFO] Final learnable params:") for k, v in final_lp.items(): print(f" {k}: {v}")
# 6. Report report = { "version": "V6_progressive", "timestamp": time.time(), "config": { "epochs": EPOCHS, "batch_size": BATCH_SIZE, "max_seq_len": MAX_SEQ_LEN, "num_layers_hyp": NUM_LAYERS_HYP, "stages_punctuation": PUNCTUATION_STAGES, "stages_text": TEXT_STAGES, "stages_heavy": HEAVY_STAGES, }, "xeon_env": { "OMP_NUM_THREADS": os.environ.get("OMP_NUM_THREADS"), "ONEDNN_MAX_CPU_ISA": os.environ.get("ONEDNN_MAX_CPU_ISA"), "IPEX": HAS_IPEX, "IPEX_VERSION": IPEX_VERSION, "torch_version": torch.__version__, "torch_threads": torch.get_num_threads(), "onednn_enabled": torch.backends.mkldnn.enabled, }, "evolution": evolution, "final_summary": final_summary, "n_params": n_params, } report_path = PROJECT_DIR / "BiGRU_T_version" / "v6_progressive_report.json" report_path.write_text(json.dumps(report, indent=2, ensure_ascii=False, default=str)) print(f"\n[INFO] Report saved: {report_path}")
# Final cleanup of binaries cleanup_binaries() print(f"\n[INFO] V6 progressive training complete. {len(evolution)} stages captured.")
if __name__ == "__main__": main()scripts/train_v6_progressive.pyAdded+1056