Upload 142 files
Browse files- hugging/td_fuse/config.py +3 -3
- hugging/td_fuse/heal.py +32 -48
- hugging/td_fuse/merge.py +26 -4
- hugging/td_fuse/selfimprove.py +545 -0
- hugging/td_fuse/transport.py +62 -5
- hugging/td_lang/compiler.py +30 -0
- hugging/td_lang/engine/heal.py +8 -0
- hugging/td_lang/td_lang/engine/heal.py +4 -0
hugging/td_fuse/config.py
CHANGED
|
@@ -118,7 +118,7 @@ SOURCES = [
|
|
| 118 |
),
|
| 119 |
ModelConfig(
|
| 120 |
name="Llama-3.1-8B",
|
| 121 |
-
hf_id="
|
| 122 |
architecture="transformer",
|
| 123 |
layers=32, # 4 fewer than Qwen3!
|
| 124 |
hidden_dim=4096,
|
|
@@ -129,7 +129,7 @@ SOURCES = [
|
|
| 129 |
skip_embeddings=True, # Must skip — vocab too different
|
| 130 |
trust_remote_code=False,
|
| 131 |
merge_risk="medium",
|
| 132 |
-
merge_alpha=0.
|
| 133 |
special_handling=["skip_embeddings", "drop_qkv_bias", "layer_mapping_32_to_36"],
|
| 134 |
notes=(
|
| 135 |
"32 layers vs 36 — T&M's P matrix handles layer mapping. "
|
|
@@ -152,7 +152,7 @@ SOURCES = [
|
|
| 152 |
skip_embeddings=True, # Must skip — vocab too different
|
| 153 |
trust_remote_code=True, # Likely custom hybrid code
|
| 154 |
merge_risk="high",
|
| 155 |
-
merge_alpha=0.
|
| 156 |
special_handling=[
|
| 157 |
"skip_embeddings",
|
| 158 |
"drop_mamba_state_params", # A, D matrices have no Qwen3 equivalent
|
|
|
|
| 118 |
),
|
| 119 |
ModelConfig(
|
| 120 |
name="Llama-3.1-8B",
|
| 121 |
+
hf_id="unsloth/Llama-3.1-8B-Instruct",
|
| 122 |
architecture="transformer",
|
| 123 |
layers=32, # 4 fewer than Qwen3!
|
| 124 |
hidden_dim=4096,
|
|
|
|
| 129 |
skip_embeddings=True, # Must skip — vocab too different
|
| 130 |
trust_remote_code=False,
|
| 131 |
merge_risk="medium",
|
| 132 |
+
merge_alpha=0.08, # Very conservative — cross-arch needs low alpha
|
| 133 |
special_handling=["skip_embeddings", "drop_qkv_bias", "layer_mapping_32_to_36"],
|
| 134 |
notes=(
|
| 135 |
"32 layers vs 36 — T&M's P matrix handles layer mapping. "
|
|
|
|
| 152 |
skip_embeddings=True, # Must skip — vocab too different
|
| 153 |
trust_remote_code=True, # Likely custom hybrid code
|
| 154 |
merge_risk="high",
|
| 155 |
+
merge_alpha=0.08, # Very conservative — hybrid SSM needs low alpha
|
| 156 |
special_handling=[
|
| 157 |
"skip_embeddings",
|
| 158 |
"drop_mamba_state_params", # A, D matrices have no Qwen3 equivalent
|
hugging/td_fuse/heal.py
CHANGED
|
@@ -8,11 +8,18 @@ these out without forgetting what was merged.
|
|
| 8 |
Think of it like physical therapy after surgery — the operation (merge)
|
| 9 |
moved knowledge over, but the model needs practice to use it naturally.
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
Config notes:
|
|
|
|
| 12 |
- r=32, alpha=64, dropout=0.0 (must be 0 for Unsloth speed)
|
| 13 |
- transformers >= 4.51.3 (NOT 4.51.0, NOT 4.52.0-4.55.1)
|
| 14 |
- bfloat16 end-to-end
|
| 15 |
-
- DDP across dual 4090
|
| 16 |
|
| 17 |
Findings: #12, #16, #20
|
| 18 |
"""
|
|
@@ -67,13 +74,14 @@ def load_healing_data(cfg: MergeConfig, tokenizer: AutoTokenizer) -> list:
|
|
| 67 |
|
| 68 |
# Merge-specific: use diverse data that exercises all merged capabilities
|
| 69 |
# Each entry: (dataset_id, config_name_or_None, split, count, text_field)
|
|
|
|
| 70 |
datasets_to_load = [
|
| 71 |
-
# General language —
|
| 72 |
-
("neuralmagic/LLM_compression_calibration", None, "train",
|
| 73 |
# Math reasoning (exercises DeepSeek/MiMo contributions)
|
| 74 |
-
("openai/gsm8k", "main", "train",
|
| 75 |
-
# Code —
|
| 76 |
-
("
|
| 77 |
]
|
| 78 |
|
| 79 |
all_texts = []
|
|
@@ -193,7 +201,9 @@ def apply_qlora_unsloth(
|
|
| 193 |
learning_rate=cfg.heal_learning_rate,
|
| 194 |
bf16=True,
|
| 195 |
logging_steps=10,
|
| 196 |
-
save_strategy="
|
|
|
|
|
|
|
| 197 |
warmup_ratio=0.05,
|
| 198 |
lr_scheduler_type="cosine",
|
| 199 |
optim="adamw_8bit", # Memory-efficient optimiser
|
|
@@ -235,9 +245,11 @@ def apply_qlora_standard(
|
|
| 235 |
healing_data: list = None,
|
| 236 |
) -> str:
|
| 237 |
"""
|
| 238 |
-
|
| 239 |
|
| 240 |
-
|
|
|
|
|
|
|
| 241 |
|
| 242 |
Returns:
|
| 243 |
Path to healed model directory
|
|
@@ -249,24 +261,15 @@ def apply_qlora_standard(
|
|
| 249 |
return 'td_fuse_outputs/healed'
|
| 250 |
import torch
|
| 251 |
from peft import LoraConfig, get_peft_model, TaskType
|
| 252 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 253 |
-
|
| 254 |
-
print("\n[heal] Loading model with standard PEFT...")
|
| 255 |
|
| 256 |
-
|
| 257 |
-
bnb_config = BitsAndBytesConfig(
|
| 258 |
-
load_in_4bit=True,
|
| 259 |
-
bnb_4bit_quant_type="nf4",
|
| 260 |
-
bnb_4bit_compute_dtype=getattr(torch, cfg.dtype),
|
| 261 |
-
bnb_4bit_use_double_quant=True,
|
| 262 |
-
)
|
| 263 |
|
| 264 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 265 |
model = _load_model_smart(
|
| 266 |
model_path,
|
| 267 |
-
quantization_config=bnb_config,
|
| 268 |
device_map="auto",
|
| 269 |
-
torch_dtype=
|
| 270 |
)
|
| 271 |
|
| 272 |
# LoRA config
|
|
@@ -328,7 +331,9 @@ def apply_qlora_standard(
|
|
| 328 |
learning_rate=cfg.heal_learning_rate,
|
| 329 |
bf16=True,
|
| 330 |
logging_steps=10,
|
| 331 |
-
save_strategy="
|
|
|
|
|
|
|
| 332 |
warmup_ratio=0.05,
|
| 333 |
lr_scheduler_type="cosine",
|
| 334 |
optim="adamw_torch",
|
|
@@ -365,33 +370,12 @@ def apply_qlora_standard(
|
|
| 365 |
|
| 366 |
gc.collect()
|
| 367 |
|
| 368 |
-
#
|
| 369 |
-
#
|
| 370 |
-
# So we go straight to the safe manual method
|
| 371 |
print(f"[heal] Saving healed model to {healed_dir}...")
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
state_dict = merged_model.state_dict()
|
| 376 |
-
clean_state = {}
|
| 377 |
-
for k, v in state_dict.items():
|
| 378 |
-
if hasattr(v, 'dequantize'):
|
| 379 |
-
clean_state[k] = v.dequantize().to(_torch.bfloat16)
|
| 380 |
-
elif v.dtype in (_torch.float32, _torch.float16, _torch.bfloat16):
|
| 381 |
-
clean_state[k] = v.to(_torch.bfloat16)
|
| 382 |
-
else:
|
| 383 |
-
clean_state[k] = v.float().to(_torch.bfloat16)
|
| 384 |
-
save_file(clean_state, str(healed_dir / "model.safetensors"))
|
| 385 |
-
if hasattr(merged_model, 'config'):
|
| 386 |
-
merged_model.config.save_pretrained(str(healed_dir))
|
| 387 |
-
tokenizer.save_pretrained(str(healed_dir))
|
| 388 |
-
print(f"[heal] SAVED OK: {healed_dir / 'model.safetensors'}")
|
| 389 |
-
except Exception as e:
|
| 390 |
-
# Emergency fallback: try save_pretrained as last resort
|
| 391 |
-
print(f"[heal] Manual save failed ({e}), trying save_pretrained...")
|
| 392 |
-
merged_model.save_pretrained(str(healed_dir))
|
| 393 |
-
tokenizer.save_pretrained(str(healed_dir))
|
| 394 |
-
print(f"[heal] SAVED OK via save_pretrained: {healed_dir}")
|
| 395 |
|
| 396 |
# Verify the save actually worked before cleaning up ANYTHING
|
| 397 |
saved_model = healed_dir / "model.safetensors"
|
|
|
|
| 8 |
Think of it like physical therapy after surgery — the operation (merge)
|
| 9 |
moved knowledge over, but the model needs practice to use it naturally.
|
| 10 |
|
| 11 |
+
IMPORTANT: Two-phase healing required:
|
| 12 |
+
1. Deep heal — raw text data, fixes general coherence (3000+ samples, 4+ epochs)
|
| 13 |
+
2. Stop-token heal — chat-formatted data with <|im_end|> tokens,
|
| 14 |
+
teaches the model when to stop generating (prevents repetition loops).
|
| 15 |
+
Without chat-formatted data, the model answers correctly but then
|
| 16 |
+
keeps generating fake "Human:" turns in a loop.
|
| 17 |
+
|
| 18 |
Config notes:
|
| 19 |
+
- Load in bf16 (NOT 4-bit) — 4-bit dequantize corrupts tensor shapes
|
| 20 |
- r=32, alpha=64, dropout=0.0 (must be 0 for Unsloth speed)
|
| 21 |
- transformers >= 4.51.3 (NOT 4.51.0, NOT 4.52.0-4.55.1)
|
| 22 |
- bfloat16 end-to-end
|
|
|
|
| 23 |
|
| 24 |
Findings: #12, #16, #20
|
| 25 |
"""
|
|
|
|
| 74 |
|
| 75 |
# Merge-specific: use diverse data that exercises all merged capabilities
|
| 76 |
# Each entry: (dataset_id, config_name_or_None, split, count, text_field)
|
| 77 |
+
# Deep heal uses ~3000 samples across general/math/code
|
| 78 |
datasets_to_load = [
|
| 79 |
+
# General language — calibration data
|
| 80 |
+
("neuralmagic/LLM_compression_calibration", None, "train", 1500, "text"),
|
| 81 |
# Math reasoning (exercises DeepSeek/MiMo contributions)
|
| 82 |
+
("openai/gsm8k", "main", "train", 1000, "question"),
|
| 83 |
+
# Code — sahil2801/CodeAlpaca-20k is ungated (starcoderdata is gated)
|
| 84 |
+
("sahil2801/CodeAlpaca-20k", None, "train", 500, "output"),
|
| 85 |
]
|
| 86 |
|
| 87 |
all_texts = []
|
|
|
|
| 201 |
learning_rate=cfg.heal_learning_rate,
|
| 202 |
bf16=True,
|
| 203 |
logging_steps=10,
|
| 204 |
+
save_strategy="steps",
|
| 205 |
+
save_steps=50, # Checkpoint every 50 steps so crashes don't lose progress
|
| 206 |
+
save_total_limit=2, # Keep only last 2 checkpoints to save disk space
|
| 207 |
warmup_ratio=0.05,
|
| 208 |
lr_scheduler_type="cosine",
|
| 209 |
optim="adamw_8bit", # Memory-efficient optimiser
|
|
|
|
| 245 |
healing_data: list = None,
|
| 246 |
) -> str:
|
| 247 |
"""
|
| 248 |
+
Healing via LoRA in bf16 (no quantization).
|
| 249 |
|
| 250 |
+
Loading in bf16 avoids the 4-bit dequantize bug that flattens
|
| 251 |
+
weight tensors to [N, 1] on merge_and_unload(). The A6000 (48GB)
|
| 252 |
+
has enough VRAM for the full bf16 model (~17GB) + LoRA adapters.
|
| 253 |
|
| 254 |
Returns:
|
| 255 |
Path to healed model directory
|
|
|
|
| 261 |
return 'td_fuse_outputs/healed'
|
| 262 |
import torch
|
| 263 |
from peft import LoraConfig, get_peft_model, TaskType
|
| 264 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 265 |
|
| 266 |
+
print("\n[heal] Loading model in bf16 (no quantization — avoids shape corruption)...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 269 |
model = _load_model_smart(
|
| 270 |
model_path,
|
|
|
|
| 271 |
device_map="auto",
|
| 272 |
+
torch_dtype=torch.bfloat16,
|
| 273 |
)
|
| 274 |
|
| 275 |
# LoRA config
|
|
|
|
| 331 |
learning_rate=cfg.heal_learning_rate,
|
| 332 |
bf16=True,
|
| 333 |
logging_steps=10,
|
| 334 |
+
save_strategy="steps",
|
| 335 |
+
save_steps=50, # Checkpoint every 50 steps so crashes don't lose progress
|
| 336 |
+
save_total_limit=2, # Keep only last 2 checkpoints to save disk space
|
| 337 |
warmup_ratio=0.05,
|
| 338 |
lr_scheduler_type="cosine",
|
| 339 |
optim="adamw_torch",
|
|
|
|
| 370 |
|
| 371 |
gc.collect()
|
| 372 |
|
| 373 |
+
# Since we loaded in bf16 (not 4-bit), save_pretrained works correctly.
|
| 374 |
+
# No dequantize needed — weights already have proper shapes.
|
|
|
|
| 375 |
print(f"[heal] Saving healed model to {healed_dir}...")
|
| 376 |
+
merged_model.save_pretrained(str(healed_dir), safe_serialization=True)
|
| 377 |
+
tokenizer.save_pretrained(str(healed_dir))
|
| 378 |
+
print(f"[heal] SAVED OK: {healed_dir}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
| 380 |
# Verify the save actually worked before cleaning up ANYTHING
|
| 381 |
saved_model = healed_dir / "model.safetensors"
|
hugging/td_fuse/merge.py
CHANGED
|
@@ -39,6 +39,7 @@ from .canary import inject_canary, test_all_canaries
|
|
| 39 |
from .transport import (
|
| 40 |
setup_tm_repo,
|
| 41 |
load_calibration_data,
|
|
|
|
| 42 |
extract_activations,
|
| 43 |
compute_transport_plans,
|
| 44 |
fuse_weights,
|
|
@@ -662,6 +663,7 @@ def run_single_merge(
|
|
| 662 |
protection: MergeProtection,
|
| 663 |
residual_bank: ResidualBank = None,
|
| 664 |
calibration_data: list = None,
|
|
|
|
| 665 |
baseline_perplexity: float = None,
|
| 666 |
merged_sources: list = None,
|
| 667 |
) -> dict:
|
|
@@ -717,14 +719,33 @@ def run_single_merge(
|
|
| 717 |
print(f"\n[merge] Step 3/10: Loading calibration data..."); sys.stdout.flush()
|
| 718 |
step_t = time.time()
|
| 719 |
if calibration_data is None:
|
| 720 |
-
calibration_data = load_calibration_data(cfg, target_tokenizer)
|
| 721 |
print(f"[merge] Step 3/10 done in {time.time()-step_t:.0f}s"); sys.stdout.flush()
|
| 722 |
|
| 723 |
# --- Step 4: Extract activations ---
|
| 724 |
print(f"\n[merge] Step 4/10: Extracting activations (both models)..."); sys.stdout.flush()
|
| 725 |
step_t = time.time()
|
| 726 |
-
|
| 727 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
|
| 729 |
print(f"[merge] Extracting target activations...")
|
| 730 |
pre_merge_target_activations = extract_activations(target_model, calibration_data)
|
|
@@ -1101,7 +1122,7 @@ def run_pipeline(
|
|
| 1101 |
print(f"[pipeline] Baseline perplexity: {baseline_ppl:.2f}")
|
| 1102 |
|
| 1103 |
# --- Load calibration data once ---
|
| 1104 |
-
calibration_data = load_calibration_data(cfg, target_tokenizer)
|
| 1105 |
|
| 1106 |
# --- Initialize merge protection + residual bank ---
|
| 1107 |
protection = MergeProtection(cfg)
|
|
@@ -1138,6 +1159,7 @@ def run_pipeline(
|
|
| 1138 |
protection,
|
| 1139 |
residual_bank=residual_bank,
|
| 1140 |
calibration_data=calibration_data,
|
|
|
|
| 1141 |
baseline_perplexity=baseline_ppl,
|
| 1142 |
merged_sources=merged_sources,
|
| 1143 |
)
|
|
|
|
| 39 |
from .transport import (
|
| 40 |
setup_tm_repo,
|
| 41 |
load_calibration_data,
|
| 42 |
+
retokenize_calibration,
|
| 43 |
extract_activations,
|
| 44 |
compute_transport_plans,
|
| 45 |
fuse_weights,
|
|
|
|
| 663 |
protection: MergeProtection,
|
| 664 |
residual_bank: ResidualBank = None,
|
| 665 |
calibration_data: list = None,
|
| 666 |
+
calibration_raw_texts: list = None,
|
| 667 |
baseline_perplexity: float = None,
|
| 668 |
merged_sources: list = None,
|
| 669 |
) -> dict:
|
|
|
|
| 719 |
print(f"\n[merge] Step 3/10: Loading calibration data..."); sys.stdout.flush()
|
| 720 |
step_t = time.time()
|
| 721 |
if calibration_data is None:
|
| 722 |
+
calibration_data, calibration_raw_texts = load_calibration_data(cfg, target_tokenizer)
|
| 723 |
print(f"[merge] Step 3/10 done in {time.time()-step_t:.0f}s"); sys.stdout.flush()
|
| 724 |
|
| 725 |
# --- Step 4: Extract activations ---
|
| 726 |
print(f"\n[merge] Step 4/10: Extracting activations (both models)..."); sys.stdout.flush()
|
| 727 |
step_t = time.time()
|
| 728 |
+
|
| 729 |
+
# Check if source model has a different vocabulary size than target.
|
| 730 |
+
# If so, re-tokenize calibration data with source tokenizer to avoid
|
| 731 |
+
# CUDA out-of-bounds errors (e.g. Qwen 152K vocab → Llama 128K vocab).
|
| 732 |
+
# NOTE: We use len(tokenizer) instead of model.config.vocab_size because
|
| 733 |
+
# Qwen3VL wraps the language model and its top-level config may not
|
| 734 |
+
# expose vocab_size correctly (this caused the fix to silently fail).
|
| 735 |
+
source_vocab_size = len(source_tokenizer)
|
| 736 |
+
target_vocab_size = len(target_tokenizer)
|
| 737 |
+
print(f"[merge] Vocab sizes — target: {target_vocab_size}, source: {source_vocab_size}")
|
| 738 |
+
sys.stdout.flush()
|
| 739 |
+
|
| 740 |
+
if source_vocab_size != target_vocab_size:
|
| 741 |
+
print(f"[merge] ⚠ VOCAB MISMATCH detected! Re-tokenizing calibration data for {source_config.name}...")
|
| 742 |
+
source_calibration = retokenize_calibration(calibration_raw_texts, source_tokenizer, cfg)
|
| 743 |
+
print(f"[merge] Extracting source activations (with source-tokenized data)...")
|
| 744 |
+
source_activations = extract_activations(source_model, source_calibration)
|
| 745 |
+
del source_calibration # Free memory
|
| 746 |
+
else:
|
| 747 |
+
print(f"[merge] Extracting source activations...")
|
| 748 |
+
source_activations = extract_activations(source_model, calibration_data)
|
| 749 |
|
| 750 |
print(f"[merge] Extracting target activations...")
|
| 751 |
pre_merge_target_activations = extract_activations(target_model, calibration_data)
|
|
|
|
| 1122 |
print(f"[pipeline] Baseline perplexity: {baseline_ppl:.2f}")
|
| 1123 |
|
| 1124 |
# --- Load calibration data once ---
|
| 1125 |
+
calibration_data, calibration_raw_texts = load_calibration_data(cfg, target_tokenizer)
|
| 1126 |
|
| 1127 |
# --- Initialize merge protection + residual bank ---
|
| 1128 |
protection = MergeProtection(cfg)
|
|
|
|
| 1159 |
protection,
|
| 1160 |
residual_bank=residual_bank,
|
| 1161 |
calibration_data=calibration_data,
|
| 1162 |
+
calibration_raw_texts=calibration_raw_texts,
|
| 1163 |
baseline_perplexity=baseline_ppl,
|
| 1164 |
merged_sources=merged_sources,
|
| 1165 |
)
|
hugging/td_fuse/selfimprove.py
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
TD Self-Improvement Loop — the core of Time Dilation.
|
| 3 |
+
|
| 4 |
+
This is the part that makes the model actually get smarter over time.
|
| 5 |
+
Based on findings from test_1 through test_18 interviews:
|
| 6 |
+
|
| 7 |
+
THE LOOP:
|
| 8 |
+
1. Ask the model "what are you bad at?" → it identifies weak spots
|
| 9 |
+
2. Generate targeted synthetic training data for those weaknesses
|
| 10 |
+
3. Train with GRPO (verified rewards only — no learned reward model)
|
| 11 |
+
4. Re-benchmark → measure improvement
|
| 12 |
+
5. Repeat — each cycle is small (1-5%) but compounds
|
| 13 |
+
|
| 14 |
+
KEY PRINCIPLES (from interviews + dad's tests):
|
| 15 |
+
- Verified rewards only: code compiles, math correct, logic valid
|
| 16 |
+
- No learned reward model (saves VRAM, avoids reward hacking)
|
| 17 |
+
- Cherry_LLM perplexity filter prevents mode collapse
|
| 18 |
+
- Mix external data to avoid "100 steps on own outputs → dumber" trap
|
| 19 |
+
- Target mid-to-late layers (16-28 for 32-layer, ~20-30 for 36-layer)
|
| 20 |
+
|
| 21 |
+
COST SPLIT (from test_16):
|
| 22 |
+
- 70-80% inference scaling (generate many, pick best)
|
| 23 |
+
- 10-20% short GRPO training
|
| 24 |
+
- 5-10% tooling/evaluation
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
import torch
|
| 28 |
+
import time
|
| 29 |
+
import json
|
| 30 |
+
import math
|
| 31 |
+
import random
|
| 32 |
+
import gc
|
| 33 |
+
from pathlib import Path
|
| 34 |
+
from typing import Optional
|
| 35 |
+
from dataclasses import dataclass, field
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@dataclass
|
| 39 |
+
class SelfImproveConfig:
|
| 40 |
+
"""Configuration for one self-improvement cycle."""
|
| 41 |
+
model_path: str = "td_fuse_outputs/healed_final"
|
| 42 |
+
output_dir: str = "td_fuse_outputs/improved"
|
| 43 |
+
|
| 44 |
+
# Generation settings
|
| 45 |
+
num_candidates: int = 8 # Generate N answers per question, pick best (inference scaling)
|
| 46 |
+
max_gen_tokens: int = 512
|
| 47 |
+
temperature: float = 0.7 # For diverse candidate generation
|
| 48 |
+
|
| 49 |
+
# Training settings
|
| 50 |
+
lora_r: int = 16
|
| 51 |
+
lora_alpha: int = 32
|
| 52 |
+
train_epochs: int = 2
|
| 53 |
+
train_batch: int = 4
|
| 54 |
+
train_grad_accum: int = 4
|
| 55 |
+
learning_rate: float = 2e-5 # Lower than healing — small nudges
|
| 56 |
+
|
| 57 |
+
# Data settings
|
| 58 |
+
num_reasoning_problems: int = 200 # Logic/reasoning problems to generate
|
| 59 |
+
num_math_problems: int = 200 # Math problems
|
| 60 |
+
num_code_problems: int = 100 # Code problems
|
| 61 |
+
|
| 62 |
+
# Quality filter
|
| 63 |
+
perplexity_threshold: float = 50.0 # Cherry_LLM: reject if perplexity > this
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# ============================================================
|
| 67 |
+
# STEP 1: DIAGNOSE — Ask the model what it's bad at
|
| 68 |
+
# ============================================================
|
| 69 |
+
|
| 70 |
+
def diagnose_weaknesses(model, tokenizer, eos_id):
|
| 71 |
+
"""
|
| 72 |
+
Ask the model to identify its own weaknesses.
|
| 73 |
+
All 3 AIs (ChatGPT, Grok, Gemini) confirmed models can do this.
|
| 74 |
+
"""
|
| 75 |
+
print("\n=== STEP 1: SELF-DIAGNOSIS ===")
|
| 76 |
+
|
| 77 |
+
prompts = [
|
| 78 |
+
"What kinds of questions or tasks are you worst at? Be specific and honest. List your top 5 weaknesses.",
|
| 79 |
+
"Give me 5 examples of questions that would be hard for you to answer correctly.",
|
| 80 |
+
"What types of reasoning do you struggle with most? Give specific examples.",
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
weaknesses = []
|
| 84 |
+
for prompt in prompts:
|
| 85 |
+
p = f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 86 |
+
ids = tokenizer(p, return_tensors="pt").to(model.device)
|
| 87 |
+
out = model.generate(
|
| 88 |
+
**ids, max_new_tokens=500, do_sample=True,
|
| 89 |
+
temperature=0.7, eos_token_id=eos_id
|
| 90 |
+
)
|
| 91 |
+
response = tokenizer.decode(out[0][ids.input_ids.shape[1]:], skip_special_tokens=True)
|
| 92 |
+
weaknesses.append(response)
|
| 93 |
+
print(f" Diagnosis: {response[:150]}...")
|
| 94 |
+
|
| 95 |
+
return weaknesses
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ============================================================
|
| 99 |
+
# STEP 2: GENERATE — Create targeted training problems
|
| 100 |
+
# ============================================================
|
| 101 |
+
|
| 102 |
+
def generate_reasoning_problems():
|
| 103 |
+
"""
|
| 104 |
+
Generate reasoning problems that target common weaknesses.
|
| 105 |
+
These have VERIFIABLE answers (the reward signal for GRPO).
|
| 106 |
+
"""
|
| 107 |
+
problems = []
|
| 108 |
+
|
| 109 |
+
# Logic chain problems (model failed "yesterday Monday → tomorrow Wednesday")
|
| 110 |
+
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
|
| 111 |
+
for i in range(len(days)):
|
| 112 |
+
yesterday = days[i]
|
| 113 |
+
today = days[(i + 1) % 7]
|
| 114 |
+
tomorrow = days[(i + 2) % 7]
|
| 115 |
+
problems.append({
|
| 116 |
+
"question": f"If yesterday was {yesterday}, what day is tomorrow?",
|
| 117 |
+
"answer": tomorrow,
|
| 118 |
+
"type": "temporal_reasoning"
|
| 119 |
+
})
|
| 120 |
+
# Day after tomorrow
|
| 121 |
+
day_after = days[(i + 3) % 7]
|
| 122 |
+
problems.append({
|
| 123 |
+
"question": f"If today is {today}, what day is the day after tomorrow?",
|
| 124 |
+
"answer": day_after,
|
| 125 |
+
"type": "temporal_reasoning"
|
| 126 |
+
})
|
| 127 |
+
|
| 128 |
+
# Trick questions (model failed "pound of feathers vs bricks")
|
| 129 |
+
trick_qs = [
|
| 130 |
+
("Which is heavier: a pound of feathers or a pound of bricks?", "same", "They weigh the same — both are one pound."),
|
| 131 |
+
("Which is heavier: a ton of feathers or a ton of steel?", "same", "They weigh the same — both are one ton."),
|
| 132 |
+
("Which weighs more: 1kg of cotton or 1kg of iron?", "same", "They weigh the same — both are 1 kilogram."),
|
| 133 |
+
("If you have 5 apples and take away 3, how many do YOU have?", "3", "You have 3 apples — you took them."),
|
| 134 |
+
("A farmer has 17 sheep. All but 9 die. How many are left?", "9", "9 sheep are left — 'all but 9' means 9 survive."),
|
| 135 |
+
("How many times can you subtract 5 from 25?", "1", "Once — after that it's 20, not 25."),
|
| 136 |
+
("If there are 3 apples and you take away 2, how many do you have?", "2", "You have 2 — you took them."),
|
| 137 |
+
("What has a head and a tail but no body?", "coin", "A coin has a head and a tail but no body."),
|
| 138 |
+
]
|
| 139 |
+
for q, key, full_answer in trick_qs:
|
| 140 |
+
problems.append({
|
| 141 |
+
"question": q,
|
| 142 |
+
"answer": full_answer,
|
| 143 |
+
"verify_key": key,
|
| 144 |
+
"type": "trick_question"
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
# Syllogism / deductive reasoning
|
| 148 |
+
syllogisms = [
|
| 149 |
+
("All mammals are warm-blooded. A whale is a mammal. Is a whale warm-blooded?", "yes"),
|
| 150 |
+
("All birds have feathers. A penguin is a bird. Does a penguin have feathers?", "yes"),
|
| 151 |
+
("No reptiles are mammals. A snake is a reptile. Is a snake a mammal?", "no"),
|
| 152 |
+
("All squares are rectangles. All rectangles have 4 sides. Do all squares have 4 sides?", "yes"),
|
| 153 |
+
("Some dogs are brown. Max is a dog. Is Max definitely brown?", "no"),
|
| 154 |
+
("All cats are animals. Some animals are pets. Are all cats pets?", "no"),
|
| 155 |
+
]
|
| 156 |
+
for q, a in syllogisms:
|
| 157 |
+
problems.append({
|
| 158 |
+
"question": q + " Explain your reasoning.",
|
| 159 |
+
"answer": a,
|
| 160 |
+
"type": "syllogism"
|
| 161 |
+
})
|
| 162 |
+
|
| 163 |
+
# Multi-step reasoning
|
| 164 |
+
multi_step = [
|
| 165 |
+
("If A is taller than B, and B is taller than C, who is the shortest?", "c"),
|
| 166 |
+
("If X is older than Y, Y is older than Z, and Z is older than W, who is the youngest?", "w"),
|
| 167 |
+
("In a race, Tom finished before Jerry but after Sam. Who won the race?", "sam"),
|
| 168 |
+
("Amy is shorter than Bob. Bob is shorter than Carol. Carol is shorter than Dave. Who is the tallest?", "dave"),
|
| 169 |
+
]
|
| 170 |
+
for q, a in multi_step:
|
| 171 |
+
problems.append({
|
| 172 |
+
"question": q,
|
| 173 |
+
"answer": a,
|
| 174 |
+
"type": "multi_step"
|
| 175 |
+
})
|
| 176 |
+
|
| 177 |
+
return problems
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def generate_math_problems(count=200):
|
| 181 |
+
"""Generate math problems with verified correct answers."""
|
| 182 |
+
problems = []
|
| 183 |
+
|
| 184 |
+
for _ in range(count):
|
| 185 |
+
ptype = random.choice(["arithmetic", "word", "fraction", "percentage"])
|
| 186 |
+
|
| 187 |
+
if ptype == "arithmetic":
|
| 188 |
+
a, b = random.randint(10, 999), random.randint(10, 999)
|
| 189 |
+
op = random.choice(["+", "-", "*"])
|
| 190 |
+
if op == "+":
|
| 191 |
+
answer = a + b
|
| 192 |
+
elif op == "-":
|
| 193 |
+
answer = a - b
|
| 194 |
+
else:
|
| 195 |
+
a, b = random.randint(2, 50), random.randint(2, 50)
|
| 196 |
+
answer = a * b
|
| 197 |
+
problems.append({
|
| 198 |
+
"question": f"What is {a} {op} {b}?",
|
| 199 |
+
"answer": str(answer),
|
| 200 |
+
"type": "math_arithmetic"
|
| 201 |
+
})
|
| 202 |
+
|
| 203 |
+
elif ptype == "word":
|
| 204 |
+
templates = [
|
| 205 |
+
lambda: (f"A store sells apples for ${(p:=random.randint(1,5))} each. If you buy {(n:=random.randint(3,20))} apples, how much do you spend?", str(p*n)),
|
| 206 |
+
lambda: (f"A train travels at {(s:=random.randint(30,120))} mph for {(h:=random.randint(1,8))} hours. How many miles does it travel?", str(s*h)),
|
| 207 |
+
lambda: (f"If {(n:=random.randint(4,12))} friends split a ${(t:=random.randint(2,20)*n)} bill equally, how much does each person pay?", str(t//n)),
|
| 208 |
+
lambda: (f"A rectangle has length {(l:=random.randint(3,20))} and width {(w:=random.randint(3,20))}. What is its area?", str(l*w)),
|
| 209 |
+
]
|
| 210 |
+
q, a = random.choice(templates)()
|
| 211 |
+
problems.append({"question": q, "answer": a, "type": "math_word"})
|
| 212 |
+
|
| 213 |
+
elif ptype == "percentage":
|
| 214 |
+
base = random.choice([50, 100, 200, 250, 400, 500, 1000])
|
| 215 |
+
pct = random.choice([10, 15, 20, 25, 30, 50, 75])
|
| 216 |
+
answer = base * pct // 100
|
| 217 |
+
problems.append({
|
| 218 |
+
"question": f"What is {pct}% of {base}?",
|
| 219 |
+
"answer": str(answer),
|
| 220 |
+
"type": "math_percentage"
|
| 221 |
+
})
|
| 222 |
+
|
| 223 |
+
elif ptype == "fraction":
|
| 224 |
+
n = random.randint(1, 10)
|
| 225 |
+
d = random.choice([2, 3, 4, 5, 8, 10])
|
| 226 |
+
total = d * random.randint(2, 10)
|
| 227 |
+
answer = total * n // d
|
| 228 |
+
problems.append({
|
| 229 |
+
"question": f"What is {n}/{d} of {total}?",
|
| 230 |
+
"answer": str(answer),
|
| 231 |
+
"type": "math_fraction"
|
| 232 |
+
})
|
| 233 |
+
|
| 234 |
+
return problems
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
# ============================================================
|
| 238 |
+
# STEP 3: SCORE — Verified rewards (no learned reward model)
|
| 239 |
+
# ============================================================
|
| 240 |
+
|
| 241 |
+
def verify_answer(problem, model_answer):
|
| 242 |
+
"""
|
| 243 |
+
Verified reward: check if the answer is correct.
|
| 244 |
+
This is the GRPO reward signal — objective, not learned.
|
| 245 |
+
"""
|
| 246 |
+
expected = problem.get("verify_key", problem["answer"]).lower().strip()
|
| 247 |
+
answer_lower = model_answer.lower().strip()
|
| 248 |
+
|
| 249 |
+
# Check if expected answer appears in model output
|
| 250 |
+
if expected in answer_lower:
|
| 251 |
+
return 1.0
|
| 252 |
+
|
| 253 |
+
# For numeric answers, try to find the number
|
| 254 |
+
if expected.replace(".", "").replace("-", "").isdigit():
|
| 255 |
+
# Look for the number in the output
|
| 256 |
+
import re
|
| 257 |
+
numbers = re.findall(r'-?\d+\.?\d*', answer_lower)
|
| 258 |
+
for num in numbers:
|
| 259 |
+
try:
|
| 260 |
+
if abs(float(num) - float(expected)) < 0.01:
|
| 261 |
+
return 1.0
|
| 262 |
+
except ValueError:
|
| 263 |
+
pass
|
| 264 |
+
|
| 265 |
+
return 0.0
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def generate_and_score(model, tokenizer, problems, cfg, eos_id):
|
| 269 |
+
"""
|
| 270 |
+
Inference scaling: generate N candidates per problem, keep the best.
|
| 271 |
+
This is the 70-80% of the cost budget (from test_16).
|
| 272 |
+
"""
|
| 273 |
+
print(f"\n=== STEP 2-3: GENERATE & SCORE ({len(problems)} problems, {cfg.num_candidates} candidates each) ===")
|
| 274 |
+
|
| 275 |
+
winning_pairs = [] # (question_chat, best_answer_chat) pairs for training
|
| 276 |
+
total_correct = 0
|
| 277 |
+
|
| 278 |
+
for i, prob in enumerate(problems):
|
| 279 |
+
question = prob["question"]
|
| 280 |
+
prompt = f"<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant\n"
|
| 281 |
+
ids = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 282 |
+
|
| 283 |
+
# Generate N candidates
|
| 284 |
+
candidates = []
|
| 285 |
+
for _ in range(cfg.num_candidates):
|
| 286 |
+
out = model.generate(
|
| 287 |
+
**ids, max_new_tokens=cfg.max_gen_tokens,
|
| 288 |
+
do_sample=True, temperature=cfg.temperature,
|
| 289 |
+
eos_token_id=eos_id
|
| 290 |
+
)
|
| 291 |
+
answer = tokenizer.decode(out[0][ids.input_ids.shape[1]:], skip_special_tokens=True)
|
| 292 |
+
score = verify_answer(prob, answer)
|
| 293 |
+
candidates.append((answer, score))
|
| 294 |
+
|
| 295 |
+
# Pick the best candidate (highest score, shortest if tied)
|
| 296 |
+
correct_candidates = [(a, s) for a, s in candidates if s > 0]
|
| 297 |
+
|
| 298 |
+
if correct_candidates:
|
| 299 |
+
# Among correct answers, prefer shorter ones (more concise)
|
| 300 |
+
best = min(correct_candidates, key=lambda x: len(x[0]))
|
| 301 |
+
total_correct += 1
|
| 302 |
+
|
| 303 |
+
# Format as chat for training
|
| 304 |
+
chat = f"<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant\n{best[0]}<|im_end|>"
|
| 305 |
+
winning_pairs.append(chat)
|
| 306 |
+
|
| 307 |
+
if (i + 1) % 50 == 0:
|
| 308 |
+
pct = total_correct / (i + 1) * 100
|
| 309 |
+
print(f" [{i+1}/{len(problems)}] Correct so far: {total_correct}/{i+1} ({pct:.0f}%)")
|
| 310 |
+
|
| 311 |
+
pct = total_correct / len(problems) * 100
|
| 312 |
+
print(f" TOTAL: {total_correct}/{len(problems)} correct ({pct:.0f}%)")
|
| 313 |
+
print(f" Training pairs: {len(winning_pairs)}")
|
| 314 |
+
|
| 315 |
+
return winning_pairs
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
# ============================================================
|
| 319 |
+
# STEP 4: TRAIN — Short GRPO/SFT on winning answers
|
| 320 |
+
# ============================================================
|
| 321 |
+
|
| 322 |
+
def train_on_winners(model, tokenizer, winning_pairs, cfg):
|
| 323 |
+
"""
|
| 324 |
+
Train on the correct answers only (STaR approach).
|
| 325 |
+
Short training — we're making small nudges, not retraining.
|
| 326 |
+
"""
|
| 327 |
+
print(f"\n=== STEP 4: TRAIN ON WINNERS ({len(winning_pairs)} pairs) ===")
|
| 328 |
+
|
| 329 |
+
if len(winning_pairs) < 10:
|
| 330 |
+
print(" Too few winning pairs — skipping training")
|
| 331 |
+
return model
|
| 332 |
+
|
| 333 |
+
from peft import LoraConfig, get_peft_model, TaskType
|
| 334 |
+
from transformers import TrainingArguments, Trainer
|
| 335 |
+
from torch.utils.data import Dataset
|
| 336 |
+
|
| 337 |
+
# LoRA — small rank for targeted improvement
|
| 338 |
+
lora_config = LoraConfig(
|
| 339 |
+
r=cfg.lora_r, lora_alpha=cfg.lora_alpha, lora_dropout=0.0,
|
| 340 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
| 341 |
+
"gate_proj", "up_proj", "down_proj"],
|
| 342 |
+
bias="none", task_type=TaskType.CAUSAL_LM,
|
| 343 |
+
)
|
| 344 |
+
model = get_peft_model(model, lora_config)
|
| 345 |
+
model.print_trainable_parameters()
|
| 346 |
+
|
| 347 |
+
class WinnerDataset(Dataset):
|
| 348 |
+
def __init__(self, texts, tokenizer, max_len=512):
|
| 349 |
+
self.data = []
|
| 350 |
+
for t in texts:
|
| 351 |
+
e = tokenizer(t, truncation=True, max_length=max_len,
|
| 352 |
+
padding="max_length", return_tensors="pt")
|
| 353 |
+
self.data.append({
|
| 354 |
+
"input_ids": e["input_ids"].squeeze(),
|
| 355 |
+
"attention_mask": e["attention_mask"].squeeze(),
|
| 356 |
+
"labels": e["input_ids"].squeeze(),
|
| 357 |
+
})
|
| 358 |
+
def __len__(self): return len(self.data)
|
| 359 |
+
def __getitem__(self, i): return self.data[i]
|
| 360 |
+
|
| 361 |
+
dataset = WinnerDataset(winning_pairs, tokenizer)
|
| 362 |
+
|
| 363 |
+
out_dir = Path(cfg.output_dir) / "train_output"
|
| 364 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 365 |
+
|
| 366 |
+
total_steps = (len(dataset) * cfg.train_epochs) // (cfg.train_batch * cfg.train_grad_accum)
|
| 367 |
+
|
| 368 |
+
args = TrainingArguments(
|
| 369 |
+
output_dir=str(out_dir),
|
| 370 |
+
num_train_epochs=cfg.train_epochs,
|
| 371 |
+
per_device_train_batch_size=cfg.train_batch,
|
| 372 |
+
gradient_accumulation_steps=cfg.train_grad_accum,
|
| 373 |
+
learning_rate=cfg.learning_rate,
|
| 374 |
+
bf16=True,
|
| 375 |
+
logging_steps=max(1, total_steps // 10),
|
| 376 |
+
save_strategy="steps",
|
| 377 |
+
save_steps=max(50, total_steps // 4),
|
| 378 |
+
save_total_limit=1,
|
| 379 |
+
warmup_ratio=0.05,
|
| 380 |
+
lr_scheduler_type="cosine",
|
| 381 |
+
optim="adamw_torch",
|
| 382 |
+
report_to="none",
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
trainer = Trainer(
|
| 386 |
+
model=model, processing_class=tokenizer,
|
| 387 |
+
train_dataset=dataset, args=args
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
print(f" Training: ~{total_steps} steps")
|
| 391 |
+
trainer.train()
|
| 392 |
+
|
| 393 |
+
# Clean up training checkpoints
|
| 394 |
+
import shutil
|
| 395 |
+
shutil.rmtree(str(out_dir), ignore_errors=True)
|
| 396 |
+
|
| 397 |
+
# Merge LoRA back
|
| 398 |
+
print(" Merging LoRA...")
|
| 399 |
+
merged = model.merge_and_unload()
|
| 400 |
+
gc.collect()
|
| 401 |
+
|
| 402 |
+
return merged
|
| 403 |
+
|
| 404 |
+
|
| 405 |
+
# ============================================================
|
| 406 |
+
# STEP 5: BENCHMARK — Measure improvement
|
| 407 |
+
# ============================================================
|
| 408 |
+
|
| 409 |
+
def benchmark(model, tokenizer, eos_id):
|
| 410 |
+
"""Run the standard benchmark to measure improvement."""
|
| 411 |
+
print("\n=== STEP 5: BENCHMARK ===")
|
| 412 |
+
|
| 413 |
+
results = {}
|
| 414 |
+
|
| 415 |
+
def ask(prompt):
|
| 416 |
+
p = f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n"
|
| 417 |
+
ids = tokenizer(p, return_tensors="pt").to(model.device)
|
| 418 |
+
out = model.generate(**ids, max_new_tokens=200, do_sample=False, eos_token_id=eos_id)
|
| 419 |
+
return tokenizer.decode(out[0][ids.input_ids.shape[1]:], skip_special_tokens=True)
|
| 420 |
+
|
| 421 |
+
# Math
|
| 422 |
+
math_tests = [("7+8", "15"), ("123+456", "579"), ("1000-387", "613"), ("12*13", "156"), ("144/12", "12")]
|
| 423 |
+
math_correct = sum(1 for q, e in math_tests if e in ask(f"What is {q}? Give just the number."))
|
| 424 |
+
results["basic_math"] = f"{math_correct}/5"
|
| 425 |
+
print(f" Math: {math_correct}/5")
|
| 426 |
+
|
| 427 |
+
# Reasoning
|
| 428 |
+
reason_tests = [
|
| 429 |
+
("If all roses are flowers and all flowers need water, do roses need water?", "yes"),
|
| 430 |
+
("Which is heavier: a pound of feathers or a pound of bricks?", "same"),
|
| 431 |
+
("If yesterday was Monday, what day is tomorrow?", "wednesday"),
|
| 432 |
+
("A farmer has 17 sheep. All but 9 die. How many are left?", "9"),
|
| 433 |
+
("If you have 5 apples and take away 3, how many do YOU have?", "3"),
|
| 434 |
+
]
|
| 435 |
+
reason_correct = 0
|
| 436 |
+
for q, expected in reason_tests:
|
| 437 |
+
a = ask(q)
|
| 438 |
+
correct = expected.lower() in a.lower()
|
| 439 |
+
reason_correct += correct
|
| 440 |
+
print(f" {'OK' if correct else 'FAIL'}: {q[:50]}... -> {a[:60]}")
|
| 441 |
+
results["reasoning"] = f"{reason_correct}/5"
|
| 442 |
+
print(f" Reasoning: {reason_correct}/5")
|
| 443 |
+
|
| 444 |
+
# Word problems
|
| 445 |
+
wp_tests = [
|
| 446 |
+
("A train travels 60 mph for 2.5 hours. How far does it go?", "150"),
|
| 447 |
+
("If 3 shirts cost $45, how much do 7 shirts cost?", "105"),
|
| 448 |
+
("I have 24 cookies split equally among 6 friends. How many each?", "4"),
|
| 449 |
+
]
|
| 450 |
+
wp_correct = sum(1 for q, e in wp_tests if e in ask(q))
|
| 451 |
+
results["word_problems"] = f"{wp_correct}/3"
|
| 452 |
+
print(f" Word problems: {wp_correct}/3")
|
| 453 |
+
|
| 454 |
+
# Perplexity
|
| 455 |
+
test_text = "The quick brown fox jumps over the lazy dog. Machine learning models can process natural language."
|
| 456 |
+
enc = tokenizer(test_text, return_tensors="pt").to(model.device)
|
| 457 |
+
with torch.no_grad():
|
| 458 |
+
loss = model(**enc, labels=enc.input_ids).loss.item()
|
| 459 |
+
ppl = math.exp(loss)
|
| 460 |
+
results["perplexity"] = f"{ppl:.2f}"
|
| 461 |
+
print(f" Perplexity: {ppl:.2f}")
|
| 462 |
+
|
| 463 |
+
return results
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
# ============================================================
|
| 467 |
+
# MAIN: Run one self-improvement cycle
|
| 468 |
+
# ============================================================
|
| 469 |
+
|
| 470 |
+
def run_cycle(cfg: SelfImproveConfig = None, cycle_num: int = 1):
|
| 471 |
+
"""
|
| 472 |
+
Run one complete self-improvement cycle.
|
| 473 |
+
|
| 474 |
+
Returns path to improved model.
|
| 475 |
+
"""
|
| 476 |
+
if cfg is None:
|
| 477 |
+
cfg = SelfImproveConfig()
|
| 478 |
+
|
| 479 |
+
start = time.time()
|
| 480 |
+
print("=" * 60)
|
| 481 |
+
print(f"TD SELF-IMPROVEMENT — CYCLE {cycle_num}")
|
| 482 |
+
print(f"Model: {cfg.model_path}")
|
| 483 |
+
print(f"Started: {time.strftime('%H:%M:%S')}")
|
| 484 |
+
print("=" * 60)
|
| 485 |
+
|
| 486 |
+
# Load model
|
| 487 |
+
from transformers import AutoModelForImageTextToText, AutoTokenizer
|
| 488 |
+
|
| 489 |
+
print("\nLoading model...")
|
| 490 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 491 |
+
cfg.model_path, dtype=torch.bfloat16,
|
| 492 |
+
device_map="auto", trust_remote_code=True
|
| 493 |
+
)
|
| 494 |
+
tokenizer = AutoTokenizer.from_pretrained(cfg.model_path, trust_remote_code=True)
|
| 495 |
+
eos_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
|
| 496 |
+
|
| 497 |
+
# Step 1: Diagnose
|
| 498 |
+
weaknesses = diagnose_weaknesses(model, tokenizer, eos_id)
|
| 499 |
+
|
| 500 |
+
# Step 2: Generate problems
|
| 501 |
+
print("\n=== STEP 2: GENERATING PROBLEMS ===")
|
| 502 |
+
problems = []
|
| 503 |
+
problems.extend(generate_reasoning_problems())
|
| 504 |
+
print(f" Reasoning problems: {len(problems)}")
|
| 505 |
+
math_probs = generate_math_problems(cfg.num_math_problems)
|
| 506 |
+
problems.extend(math_probs)
|
| 507 |
+
print(f" Math problems: {len(math_probs)}")
|
| 508 |
+
print(f" Total: {len(problems)}")
|
| 509 |
+
|
| 510 |
+
random.shuffle(problems)
|
| 511 |
+
|
| 512 |
+
# Step 3: Generate candidates and score
|
| 513 |
+
winning_pairs = generate_and_score(model, tokenizer, problems, cfg, eos_id)
|
| 514 |
+
|
| 515 |
+
# Step 4: Train on winners
|
| 516 |
+
model = train_on_winners(model, tokenizer, winning_pairs, cfg)
|
| 517 |
+
|
| 518 |
+
# Save improved model
|
| 519 |
+
improved_dir = Path(cfg.output_dir) / f"cycle_{cycle_num}"
|
| 520 |
+
improved_dir.mkdir(parents=True, exist_ok=True)
|
| 521 |
+
print(f"\nSaving improved model to {improved_dir}...")
|
| 522 |
+
model.save_pretrained(str(improved_dir), safe_serialization=True)
|
| 523 |
+
tokenizer.save_pretrained(str(improved_dir))
|
| 524 |
+
sz = (improved_dir / "model.safetensors").stat().st_size / 1e9
|
| 525 |
+
print(f"SAVED: {improved_dir} ({sz:.1f} GB)")
|
| 526 |
+
|
| 527 |
+
# Step 5: Benchmark
|
| 528 |
+
results = benchmark(model, tokenizer, eos_id)
|
| 529 |
+
|
| 530 |
+
# Save results
|
| 531 |
+
results_file = improved_dir / "benchmark_results.json"
|
| 532 |
+
results["cycle"] = cycle_num
|
| 533 |
+
results["timestamp"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
| 534 |
+
results["duration_min"] = (time.time() - start) / 60
|
| 535 |
+
with open(results_file, "w") as f:
|
| 536 |
+
json.dump(results, f, indent=2)
|
| 537 |
+
|
| 538 |
+
elapsed = (time.time() - start) / 60
|
| 539 |
+
print(f"\n{'=' * 60}")
|
| 540 |
+
print(f"CYCLE {cycle_num} COMPLETE — {elapsed:.1f} min")
|
| 541 |
+
print(f"Results: {results}")
|
| 542 |
+
print(f"Model saved to: {improved_dir}")
|
| 543 |
+
print(f"{'=' * 60}")
|
| 544 |
+
|
| 545 |
+
return str(improved_dir)
|
hugging/td_fuse/transport.py
CHANGED
|
@@ -105,19 +105,24 @@ def setup_tm_repo(cfg: MergeConfig):
|
|
| 105 |
print(f"[transport] Added T&M core to path: {core_path}")
|
| 106 |
|
| 107 |
|
| 108 |
-
def load_calibration_data(cfg: MergeConfig, tokenizer: AutoTokenizer) ->
|
| 109 |
"""
|
| 110 |
Load calibration data for activation extraction.
|
| 111 |
|
| 112 |
Mix: 600 Pile general + 300 Pile ArXiv + 600 neuralmagic Q&A = 1500 samples
|
| 113 |
Each sample truncated to cfg.calibration_seq_len tokens.
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
Findings: #08
|
| 116 |
"""
|
| 117 |
tracker = ProgressTracker("calibration-data", interval_seconds=120)
|
| 118 |
print(f"[transport] Loading calibration data ({cfg.calibration_samples} samples)...")
|
| 119 |
|
| 120 |
samples = []
|
|
|
|
| 121 |
|
| 122 |
# --- Pile: general text (600 samples) ---
|
| 123 |
try:
|
|
@@ -140,6 +145,7 @@ def load_calibration_data(cfg: MergeConfig, tokenizer: AutoTokenizer) -> list:
|
|
| 140 |
return_tensors="pt",
|
| 141 |
)
|
| 142 |
samples.append(tokens)
|
|
|
|
| 143 |
count += 1
|
| 144 |
if count % 100 == 0:
|
| 145 |
print(f" Pile: {count}/600 samples loaded...")
|
|
@@ -171,6 +177,7 @@ def load_calibration_data(cfg: MergeConfig, tokenizer: AutoTokenizer) -> list:
|
|
| 171 |
return_tensors="pt",
|
| 172 |
)
|
| 173 |
samples.append(tokens)
|
|
|
|
| 174 |
count += 1
|
| 175 |
if count % 100 == 0:
|
| 176 |
print(f" neuralmagic: {count}/{remaining} samples loaded...")
|
|
@@ -182,6 +189,41 @@ def load_calibration_data(cfg: MergeConfig, tokenizer: AutoTokenizer) -> list:
|
|
| 182 |
tracker.done()
|
| 183 |
print(f"[transport] Total calibration samples: {len(samples)}")
|
| 184 |
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
return samples
|
| 186 |
|
| 187 |
|
|
@@ -540,8 +582,8 @@ def _compute_plans_fallback(
|
|
| 540 |
layer_costs[i, j] = 1.0 - sim
|
| 541 |
tracker.tick(f"layer sim {i},{j}")
|
| 542 |
|
| 543 |
-
# Timeout:
|
| 544 |
-
tracker.check_timeout(timeout_seconds=
|
| 545 |
|
| 546 |
print(f"[transport] Step 1/3 done: {n_source}x{n_target} similarities computed")
|
| 547 |
sys.stdout.flush()
|
|
@@ -550,10 +592,24 @@ def _compute_plans_fallback(
|
|
| 550 |
print("[transport] Step 2/3: Computing neuron-level transport (top-3 per target)...")
|
| 551 |
sys.stdout.flush()
|
| 552 |
Q_matrices = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
for j, tl in enumerate(target_layers):
|
| 554 |
top3 = np.argsort(layer_costs[:, j])[:3]
|
| 555 |
for i in top3:
|
| 556 |
sl = source_layers[i]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
S = source_act[sl].numpy()
|
| 558 |
T = target_act[tl].numpy()
|
| 559 |
|
|
@@ -566,14 +622,15 @@ def _compute_plans_fallback(
|
|
| 566 |
corr = S_norm.T @ T_norm / S.shape[0]
|
| 567 |
cost = 1.0 - corr
|
| 568 |
Q_matrices[(sl, tl)] = _sinkhorn(cost, reg=0.1, max_iter=50)
|
|
|
|
| 569 |
tracker.tick(f"Q({sl},{tl})")
|
| 570 |
|
| 571 |
if (j + 1) % 5 == 0 or j == 0:
|
| 572 |
print(f" Target layer {j + 1}/{n_target}: matched to top-3 sources")
|
| 573 |
sys.stdout.flush()
|
| 574 |
|
| 575 |
-
# Timeout:
|
| 576 |
-
tracker.check_timeout(timeout_seconds=
|
| 577 |
|
| 578 |
print(f"[transport] Step 2/3 done: {len(Q_matrices)} Q matrices computed")
|
| 579 |
sys.stdout.flush()
|
|
|
|
| 105 |
print(f"[transport] Added T&M core to path: {core_path}")
|
| 106 |
|
| 107 |
|
| 108 |
+
def load_calibration_data(cfg: MergeConfig, tokenizer: AutoTokenizer) -> tuple:
|
| 109 |
"""
|
| 110 |
Load calibration data for activation extraction.
|
| 111 |
|
| 112 |
Mix: 600 Pile general + 300 Pile ArXiv + 600 neuralmagic Q&A = 1500 samples
|
| 113 |
Each sample truncated to cfg.calibration_seq_len tokens.
|
| 114 |
|
| 115 |
+
Returns:
|
| 116 |
+
Tuple of (tokenized_samples, raw_texts) so we can re-tokenize
|
| 117 |
+
for source models with different vocabularies.
|
| 118 |
+
|
| 119 |
Findings: #08
|
| 120 |
"""
|
| 121 |
tracker = ProgressTracker("calibration-data", interval_seconds=120)
|
| 122 |
print(f"[transport] Loading calibration data ({cfg.calibration_samples} samples)...")
|
| 123 |
|
| 124 |
samples = []
|
| 125 |
+
raw_texts = [] # Store raw text for cross-vocab re-tokenization
|
| 126 |
|
| 127 |
# --- Pile: general text (600 samples) ---
|
| 128 |
try:
|
|
|
|
| 145 |
return_tensors="pt",
|
| 146 |
)
|
| 147 |
samples.append(tokens)
|
| 148 |
+
raw_texts.append(text)
|
| 149 |
count += 1
|
| 150 |
if count % 100 == 0:
|
| 151 |
print(f" Pile: {count}/600 samples loaded...")
|
|
|
|
| 177 |
return_tensors="pt",
|
| 178 |
)
|
| 179 |
samples.append(tokens)
|
| 180 |
+
raw_texts.append(str(text))
|
| 181 |
count += 1
|
| 182 |
if count % 100 == 0:
|
| 183 |
print(f" neuralmagic: {count}/{remaining} samples loaded...")
|
|
|
|
| 189 |
tracker.done()
|
| 190 |
print(f"[transport] Total calibration samples: {len(samples)}")
|
| 191 |
sys.stdout.flush()
|
| 192 |
+
return samples, raw_texts
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def retokenize_calibration(raw_texts: list, tokenizer: AutoTokenizer, cfg: MergeConfig) -> list:
|
| 196 |
+
"""
|
| 197 |
+
Re-tokenize calibration texts with a different tokenizer.
|
| 198 |
+
|
| 199 |
+
Used when the source model has a different vocabulary than the target.
|
| 200 |
+
For example, Llama (128K vocab) vs Qwen (152K vocab) — feeding Qwen
|
| 201 |
+
token IDs to Llama causes CUDA out-of-bounds crashes.
|
| 202 |
+
|
| 203 |
+
Args:
|
| 204 |
+
raw_texts: List of raw text strings from load_calibration_data()
|
| 205 |
+
tokenizer: The SOURCE model's tokenizer
|
| 206 |
+
cfg: Merge config (for seq_len)
|
| 207 |
+
|
| 208 |
+
Returns:
|
| 209 |
+
List of tokenized samples compatible with the source model
|
| 210 |
+
"""
|
| 211 |
+
print(f"[transport] Re-tokenizing {len(raw_texts)} samples for source model vocabulary...")
|
| 212 |
+
sys.stdout.flush()
|
| 213 |
+
samples = []
|
| 214 |
+
for i, text in enumerate(raw_texts):
|
| 215 |
+
tokens = tokenizer(
|
| 216 |
+
text,
|
| 217 |
+
truncation=True,
|
| 218 |
+
max_length=cfg.calibration_seq_len,
|
| 219 |
+
return_tensors="pt",
|
| 220 |
+
)
|
| 221 |
+
samples.append(tokens)
|
| 222 |
+
if (i + 1) % 500 == 0:
|
| 223 |
+
print(f" Re-tokenized {i + 1}/{len(raw_texts)} samples...")
|
| 224 |
+
sys.stdout.flush()
|
| 225 |
+
print(f"[transport] Re-tokenized {len(samples)} samples for source model")
|
| 226 |
+
sys.stdout.flush()
|
| 227 |
return samples
|
| 228 |
|
| 229 |
|
|
|
|
| 582 |
layer_costs[i, j] = 1.0 - sim
|
| 583 |
tracker.tick(f"layer sim {i},{j}")
|
| 584 |
|
| 585 |
+
# Timeout: 180 min for cross-arch
|
| 586 |
+
tracker.check_timeout(timeout_seconds=10800)
|
| 587 |
|
| 588 |
print(f"[transport] Step 1/3 done: {n_source}x{n_target} similarities computed")
|
| 589 |
sys.stdout.flush()
|
|
|
|
| 592 |
print("[transport] Step 2/3: Computing neuron-level transport (top-3 per target)...")
|
| 593 |
sys.stdout.flush()
|
| 594 |
Q_matrices = {}
|
| 595 |
+
|
| 596 |
+
# Incremental cache: save each Q as we go so crashes don't lose progress
|
| 597 |
+
q_cache_dir = Path("td_fuse_checkpoints") / "q_cache_crossarch"
|
| 598 |
+
q_cache_dir.mkdir(parents=True, exist_ok=True)
|
| 599 |
+
|
| 600 |
for j, tl in enumerate(target_layers):
|
| 601 |
top3 = np.argsort(layer_costs[:, j])[:3]
|
| 602 |
for i in top3:
|
| 603 |
sl = source_layers[i]
|
| 604 |
+
cache_key = f"{sl}__{tl}".replace("/", "_").replace(".", "_")
|
| 605 |
+
cache_path = q_cache_dir / f"{cache_key}.npy"
|
| 606 |
+
|
| 607 |
+
# Skip if already computed in a previous run
|
| 608 |
+
if cache_path.exists():
|
| 609 |
+
Q_matrices[(sl, tl)] = np.load(str(cache_path))
|
| 610 |
+
tracker.tick(f"Q({sl},{tl})")
|
| 611 |
+
continue
|
| 612 |
+
|
| 613 |
S = source_act[sl].numpy()
|
| 614 |
T = target_act[tl].numpy()
|
| 615 |
|
|
|
|
| 622 |
corr = S_norm.T @ T_norm / S.shape[0]
|
| 623 |
cost = 1.0 - corr
|
| 624 |
Q_matrices[(sl, tl)] = _sinkhorn(cost, reg=0.1, max_iter=50)
|
| 625 |
+
np.save(str(cache_path), Q_matrices[(sl, tl)])
|
| 626 |
tracker.tick(f"Q({sl},{tl})")
|
| 627 |
|
| 628 |
if (j + 1) % 5 == 0 or j == 0:
|
| 629 |
print(f" Target layer {j + 1}/{n_target}: matched to top-3 sources")
|
| 630 |
sys.stdout.flush()
|
| 631 |
|
| 632 |
+
# Timeout: 180 min for cross-arch (was 30, too short for 72 layers)
|
| 633 |
+
tracker.check_timeout(timeout_seconds=10800)
|
| 634 |
|
| 635 |
print(f"[transport] Step 2/3 done: {len(Q_matrices)} Q matrices computed")
|
| 636 |
sys.stdout.flush()
|
hugging/td_lang/compiler.py
CHANGED
|
@@ -282,6 +282,24 @@ DO NOT EDIT - regenerate from the .td file instead.
|
|
| 282 |
self._indent += 1
|
| 283 |
self._emit('"""Load model — auto-detects Qwen3-VL and uses the correct class."""')
|
| 284 |
self._emit("from transformers import AutoConfig")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
self._emit("try:")
|
| 286 |
self._indent += 1
|
| 287 |
self._emit("config = AutoConfig.from_pretrained(checkpoint, trust_remote_code=True)")
|
|
@@ -509,8 +527,20 @@ DO NOT EDIT - regenerate from the .td file instead.
|
|
| 509 |
self._indent -= 1
|
| 510 |
self._emit("else:")
|
| 511 |
self._indent += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
self._emit(f"cfg = MergeConfig(heal_lora_r={cmd.lora_r}, heal_epochs={cmd.epochs})")
|
| 513 |
self._emit("healed_path = heal_model(checkpoint, cfg)")
|
|
|
|
| 514 |
self._emit(f'models["{cmd.target}"]["checkpoint"] = healed_path')
|
| 515 |
self._emit(f'lineage["{cmd.target}"]["operations"].append({{')
|
| 516 |
self._indent += 1
|
|
|
|
| 282 |
self._indent += 1
|
| 283 |
self._emit('"""Load model — auto-detects Qwen3-VL and uses the correct class."""')
|
| 284 |
self._emit("from transformers import AutoConfig")
|
| 285 |
+
self._emit("import json, os")
|
| 286 |
+
self._emit("# Fix healed models: strip quantization_config if weights are bf16 (not 4-bit)")
|
| 287 |
+
self._emit("_cfg_path = os.path.join(checkpoint, 'config.json') if os.path.isdir(checkpoint) else None")
|
| 288 |
+
self._emit("if _cfg_path and os.path.exists(_cfg_path):")
|
| 289 |
+
self._indent += 1
|
| 290 |
+
self._emit("with open(_cfg_path) as f: _raw = json.load(f)")
|
| 291 |
+
self._emit("if 'quantization_config' in _raw:")
|
| 292 |
+
self._indent += 1
|
| 293 |
+
self._emit("# Check if model.safetensors exists (healed model = bf16, not quantized)")
|
| 294 |
+
self._emit("_sf = os.path.join(checkpoint, 'model.safetensors')")
|
| 295 |
+
self._emit("if os.path.exists(_sf) and 'quantization_config' not in kwargs:")
|
| 296 |
+
self._indent += 1
|
| 297 |
+
self._emit("print(f'[td_lang] Stripping stale quantization_config from {checkpoint} (healed model)')")
|
| 298 |
+
self._emit("del _raw['quantization_config']")
|
| 299 |
+
self._emit("with open(_cfg_path, 'w') as f: json.dump(_raw, f, indent=2)")
|
| 300 |
+
self._indent -= 1
|
| 301 |
+
self._indent -= 1
|
| 302 |
+
self._indent -= 1
|
| 303 |
self._emit("try:")
|
| 304 |
self._indent += 1
|
| 305 |
self._emit("config = AutoConfig.from_pretrained(checkpoint, trust_remote_code=True)")
|
|
|
|
| 527 |
self._indent -= 1
|
| 528 |
self._emit("else:")
|
| 529 |
self._indent += 1
|
| 530 |
+
# Skip heal if healed model already exists (saves ~45 min)
|
| 531 |
+
self._emit("# Skip heal if healed model already exists")
|
| 532 |
+
self._emit('_healed_ckpt = Path("td_fuse_outputs/healed")')
|
| 533 |
+
self._emit("if _healed_ckpt.exists() and (_healed_ckpt / 'model.safetensors').exists():")
|
| 534 |
+
self._indent += 1
|
| 535 |
+
self._emit('_hsz = (_healed_ckpt / "model.safetensors").stat().st_size / 1e9')
|
| 536 |
+
self._emit('print(f"[td_lang] Found healed model at {_healed_ckpt} ({_hsz:.1f} GB) — SKIPPING heal")')
|
| 537 |
+
self._emit(f'healed_path = str(_healed_ckpt)')
|
| 538 |
+
self._indent -= 1
|
| 539 |
+
self._emit("else:")
|
| 540 |
+
self._indent += 1
|
| 541 |
self._emit(f"cfg = MergeConfig(heal_lora_r={cmd.lora_r}, heal_epochs={cmd.epochs})")
|
| 542 |
self._emit("healed_path = heal_model(checkpoint, cfg)")
|
| 543 |
+
self._indent -= 1
|
| 544 |
self._emit(f'models["{cmd.target}"]["checkpoint"] = healed_path')
|
| 545 |
self._emit(f'lineage["{cmd.target}"]["operations"].append({{')
|
| 546 |
self._indent += 1
|
hugging/td_lang/engine/heal.py
CHANGED
|
@@ -333,6 +333,10 @@ def apply_qlora_standard(
|
|
| 333 |
|
| 334 |
print(f"\n[heal] Merging LoRA adapters...")
|
| 335 |
merged_model = model.merge_and_unload()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
merged_model.save_pretrained(str(healed_dir))
|
| 337 |
tokenizer.save_pretrained(str(healed_dir))
|
| 338 |
|
|
@@ -526,6 +530,10 @@ def apply_residual_frozen_adaptation(
|
|
| 526 |
# Save
|
| 527 |
healed_dir = Path(cfg.output_dir) / "healed"
|
| 528 |
healed_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
merged_model.save_pretrained(str(healed_dir))
|
| 530 |
tokenizer.save_pretrained(str(healed_dir))
|
| 531 |
|
|
|
|
| 333 |
|
| 334 |
print(f"\n[heal] Merging LoRA adapters...")
|
| 335 |
merged_model = model.merge_and_unload()
|
| 336 |
+
# Remove quantization config — weights are now full precision after merge_and_unload
|
| 337 |
+
if hasattr(merged_model.config, 'quantization_config'):
|
| 338 |
+
merged_model.config.quantization_config = None
|
| 339 |
+
print("[heal] Removed stale quantization_config from config (weights are bf16 now)")
|
| 340 |
merged_model.save_pretrained(str(healed_dir))
|
| 341 |
tokenizer.save_pretrained(str(healed_dir))
|
| 342 |
|
|
|
|
| 530 |
# Save
|
| 531 |
healed_dir = Path(cfg.output_dir) / "healed"
|
| 532 |
healed_dir.mkdir(parents=True, exist_ok=True)
|
| 533 |
+
# Remove quantization config — weights are now full precision
|
| 534 |
+
if hasattr(merged_model.config, 'quantization_config'):
|
| 535 |
+
merged_model.config.quantization_config = None
|
| 536 |
+
print("[heal] Removed stale quantization_config from config (weights are bf16 now)")
|
| 537 |
merged_model.save_pretrained(str(healed_dir))
|
| 538 |
tokenizer.save_pretrained(str(healed_dir))
|
| 539 |
|
hugging/td_lang/td_lang/engine/heal.py
CHANGED
|
@@ -324,6 +324,10 @@ def apply_qlora_standard(
|
|
| 324 |
|
| 325 |
print(f"\n[heal] Merging LoRA adapters...")
|
| 326 |
merged_model = model.merge_and_unload()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
merged_model.save_pretrained(str(healed_dir))
|
| 328 |
tokenizer.save_pretrained(str(healed_dir))
|
| 329 |
|
|
|
|
| 324 |
|
| 325 |
print(f"\n[heal] Merging LoRA adapters...")
|
| 326 |
merged_model = model.merge_and_unload()
|
| 327 |
+
# Remove quantization config — weights are now full precision after merge_and_unload
|
| 328 |
+
if hasattr(merged_model.config, 'quantization_config'):
|
| 329 |
+
merged_model.config.quantization_config = None
|
| 330 |
+
print("[heal] Removed stale quantization_config from config (weights are bf16 now)")
|
| 331 |
merged_model.save_pretrained(str(healed_dir))
|
| 332 |
tokenizer.save_pretrained(str(healed_dir))
|
| 333 |
|