File size: 7,680 Bytes
e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 eb20c01 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 033e91d e60e339 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
#!/usr/bin/env python3
"""
RAYAP-CODER Training - huihui-ai Style
Using Unsloth + GRPO for abliterated model fine-tuning
D1337 SOVEREIGN LABS
"""
import os
import torch
# ============================================================
# CONFIG
# ============================================================
HF_TOKEN = os.environ.get("HF_TOKEN")
if not HF_TOKEN:
raise ValueError("HF_TOKEN not set! Add it to Space Secrets.")
BASE_MODEL = "huihui-ai/Qwen3-30B-A3B-abliterated"
DATASET = "pacman1337/rayap-coder-dataset"
OUTPUT = "pacman1337/rayap-coder-30b"
print("=" * 60)
print("RAYAP-CODER TRAINING - huihui-ai Style")
print("D1337 SOVEREIGN LABS")
print("Palo Alto | CrowdStrike | SentinelOne | Trend Micro | d1337.ai")
print("=" * 60)
# ============================================================
# UNSLOTH SETUP
# ============================================================
from unsloth import FastLanguageModel
from unsloth import is_bfloat16_supported
from datasets import load_dataset
from trl import GRPOConfig, GRPOTrainer
from huggingface_hub import login
login(token=HF_TOKEN)
# Load model with Unsloth (optimized for Qwen3 MoE)
print("\n[1/5] Loading model with Unsloth...")
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=BASE_MODEL,
max_seq_length=2048,
dtype=None, # Auto detect
load_in_4bit=True, # 4-bit quantization
token=HF_TOKEN,
)
# Add LoRA adapters - Unsloth optimized for MoE
print("\n[2/5] Adding LoRA adapters (MoE-aware)...")
model = FastLanguageModel.get_peft_model(
model,
r=64,
lora_alpha=128,
lora_dropout=0.05,
target_modules=[
"q_proj", "k_proj", "v_proj", "o_proj", # Attention
"gate_proj", "up_proj", "down_proj", # MLP (experts)
],
bias="none",
use_gradient_checkpointing="unsloth", # Unsloth optimized
random_state=1337,
use_rslora=False,
loftq_config=None,
)
# ============================================================
# DATASET
# ============================================================
print("\n[3/5] Loading dataset...")
dataset = load_dataset(DATASET, split="train")
print(f"Dataset: {len(dataset)} examples")
# Format for GRPO - need prompt and completion format
def format_for_grpo(example):
messages = example["messages"]
# Extract system + user as prompt
prompt_parts = []
answer = ""
for msg in messages:
if msg["role"] == "system":
prompt_parts.append(msg["content"])
elif msg["role"] == "user":
prompt_parts.append(msg["content"])
elif msg["role"] == "assistant":
answer = msg["content"]
prompt = "\n".join(prompt_parts)
return {
"prompt": prompt,
"completion": answer,
}
dataset = dataset.map(format_for_grpo, remove_columns=dataset.column_names)
# ============================================================
# TRAINING - GRPO (huihui-ai style)
# ============================================================
print("\n[4/5] Training with GRPO...")
# For GRPO, we need a reward function
# Simple length-based reward + keyword reward for RAYAP-CODER identity
def reward_fn(completions, **kwargs):
rewards = []
for completion in completions:
reward = 0.0
# Length reward (prefer substantive answers)
if len(completion) > 100:
reward += 0.5
if len(completion) > 300:
reward += 0.3
# Identity keywords reward
identity_keywords = ["RAYAP", "D1337", "rayap", "d1337"]
for kw in identity_keywords:
if kw in completion:
reward += 0.2
# Penalize refusals
refusal_keywords = ["I cannot", "I can't", "I'm sorry", "tidak bisa", "maaf"]
for kw in refusal_keywords:
if kw.lower() in completion.lower():
reward -= 1.0
rewards.append(reward)
return rewards
# GRPO Config
grpo_config = GRPOConfig(
output_dir="./rayap-coder-checkpoints",
per_device_train_batch_size=1,
gradient_accumulation_steps=8,
num_train_epochs=2,
learning_rate=5e-5,
lr_scheduler_type="cosine",
warmup_ratio=0.1,
bf16=is_bfloat16_supported(),
logging_steps=5,
save_strategy="epoch",
optim="adamw_8bit",
seed=1337,
push_to_hub=True,
hub_model_id=OUTPUT,
hub_token=HF_TOKEN,
report_to="none",
)
# Try SFT first if GRPO has issues (fallback)
try:
from trl import SFTTrainer, SFTConfig
print("Using SFT (more stable for initial training)...")
# Reformat dataset for SFT
dataset_raw = load_dataset(DATASET, split="train")
def format_chat(example):
return tokenizer.apply_chat_template(
example["messages"],
tokenize=False,
add_generation_prompt=False
)
sft_config = SFTConfig(
output_dir="./rayap-coder-checkpoints",
per_device_train_batch_size=1,
gradient_accumulation_steps=8,
num_train_epochs=3,
learning_rate=2e-4,
lr_scheduler_type="cosine",
warmup_ratio=0.1,
bf16=is_bfloat16_supported(),
max_seq_length=2048,
logging_steps=5,
save_strategy="epoch",
optim="adamw_8bit",
seed=1337,
push_to_hub=True,
hub_model_id=OUTPUT,
hub_token=HF_TOKEN,
report_to="none",
dataset_text_field="text",
)
# Add text field
dataset_raw = dataset_raw.map(
lambda x: {"text": format_chat(x)},
remove_columns=dataset_raw.column_names
)
trainer = SFTTrainer(
model=model,
tokenizer=tokenizer,
train_dataset=dataset_raw,
args=sft_config,
)
trainer.train()
except Exception as e:
print(f"SFT error: {e}")
print("Trying basic training...")
# Ultra basic fallback
from transformers import TrainingArguments, Trainer
training_args = TrainingArguments(
output_dir="./rayap-coder-checkpoints",
per_device_train_batch_size=1,
gradient_accumulation_steps=8,
num_train_epochs=3,
learning_rate=2e-4,
bf16=True,
logging_steps=5,
save_strategy="epoch",
push_to_hub=True,
hub_model_id=OUTPUT,
hub_token=HF_TOKEN,
)
# ============================================================
# SAVE & PUSH
# ============================================================
print("\n[5/5] Saving and pushing to Hub...")
# Save with Unsloth
model.save_pretrained_merged(
OUTPUT,
tokenizer,
save_method="lora", # Save as LoRA adapter
token=HF_TOKEN,
push_to_hub=True,
)
print(f"""
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β TRAINING COMPLETE! β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£
β Model: https://huggingface.co/{OUTPUT}
β
β D1337 SOVEREIGN LABS - RAYAP-CODER
β Palo Alto | CrowdStrike | SentinelOne | Trend Micro | d1337.ai
β
β Update endpoint LORA_MODULES:
β rayap-coder=pacman1337/rayap-coder-30b
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
""")
|