| import os |
| import sys |
| import torch |
| from datasets import load_dataset |
| from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer |
| from peft import LoraConfig, get_peft_model |
|
|
| |
| PROJECT_DIR = "<your project>" |
| ADAPTER_DIR = os.path.join(PROJECT_DIR, "adapters") |
| MODEL_ID = "mlabonne/gemma-3-12b-it-abliterated" |
| DATASET_ID = "iamtarun/python_code_instructions_18k_alpaca" |
|
|
| |
| os.makedirs(ADAPTER_DIR, exist_ok=True) |
|
|
| |
| |
| sys.path.append(PROJECT_DIR) |
| from otitans_surgery import inject_orthogonal_memory |
| |
|
|
| def main(): |
| print(f"[*] Waking the Forge for Python Expert Engram...") |
| |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| if tokenizer.pad_token is None: |
| tokenizer.pad_token = tokenizer.eos_token |
|
|
| print(f"[*] Pulling Dataset: {DATASET_ID}") |
| dataset = load_dataset(DATASET_ID, split="train") |
|
|
| |
| |
| EXPERT_SYSTEM_PROMPT = "You are the Polymath Python Expert. You do not output textbook examples; you output production-grade, highly optimized, and architecturally sound Python code. Prioritize advanced libraries (e.g., asyncio), secure protocols, and robust error handling." |
|
|
| def format_and_tokenize(examples): |
| formatted_texts = [] |
| |
| for instruction, inp, output in zip(examples['instruction'], examples['input'], examples['output']): |
| user_msg = instruction |
| if inp.strip(): |
| user_msg += f"\n\nContext:\n{inp}" |
| |
| messages = [ |
| {"role": "system", "content": EXPERT_SYSTEM_PROMPT}, |
| {"role": "user", "content": user_msg}, |
| {"role": "assistant", "content": output} |
| ] |
| |
| |
| formatted_texts.append(tokenizer.apply_chat_template(messages, tokenize=False)) |
| |
| tokenized = tokenizer(formatted_texts, truncation=True, max_length=2048, padding="max_length") |
| |
| |
| |
| tokenized["token_type_ids"] = [[0] * len(ids) for ids in tokenized["input_ids"]] |
| |
| tokenized["labels"] = [ids.copy() for ids in tokenized["input_ids"]] |
| return tokenized |
| print("[*] Formatting and injecting Expert System Prompt...") |
| tokenized_datasets = dataset.map( |
| format_and_tokenize, |
| batched=True, |
| remove_columns=dataset.column_names, |
| desc="Tokenizing Dataset" |
| ) |
|
|
| |
| print("[*] Loading 12B Foundation Weights into VRAM...") |
| model = AutoModelForCausalLM.from_pretrained( |
| MODEL_ID, |
| torch_dtype=torch.bfloat16, |
| device_map="auto" |
| ) |
|
|
| |
| |
| print("[*] Applying Orthogonal Penalty Matrix to Attention Vectors...") |
| |
| |
| peft_config = LoraConfig( |
| r=16, |
| lora_alpha=32.0, |
| target_modules=["q_proj", "v_proj"], |
| lora_dropout=0.05, |
| bias="none", |
| task_type="CAUSAL_LM" |
| ) |
| model = get_peft_model(model, peft_config) |
| model.print_trainable_parameters() |
|
|
| |
| training_args = TrainingArguments( |
| output_dir=os.path.join(PROJECT_DIR, "temp_python_checkpoint"), |
| per_device_train_batch_size=1, |
| gradient_accumulation_steps=8, |
| learning_rate=2e-5, |
| num_train_epochs=1, |
| logging_steps=50, |
| bf16=True, |
| report_to="none", |
| optim="adamw_torch" |
| ) |
|
|
| |
| trainer = Trainer( |
| model=model, |
| args=training_args, |
| train_dataset=tokenized_datasets, |
| ) |
|
|
| print("\n[*] Commencing SFT. Forging the code_python engram...") |
| trainer.train() |
|
|
| |
| final_output_path = os.path.join(ADAPTER_DIR, "otitans_code_python.pt") |
| print(f"[*] Extracting specialized memory states to {final_output_path}...") |
| |
| |
| adapter_state_dict = {k: v.cpu() for k, v in model.state_dict().items() if "lora" in k} |
| torch.save(adapter_state_dict, final_output_path) |
|
|
| print(f"[*] Engram Forge Complete. The Polymath Swarm is now armed.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|