File size: 5,204 Bytes
93bc661
 
 
 
 
 
 
 
ff1294d
93bc661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97b2506
93bc661
 
 
 
 
 
3805997
93bc661
 
192ad70
 
 
 
 
 
 
 
3805997
93bc661
3805997
93bc661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3805997
93bc661
 
 
 
3805997
93bc661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e942cad
93bc661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff1294d
 
 
 
 
 
 
97b2506
 
 
 
 
 
 
3805997
93bc661
97b2506
 
93bc661
 
 
 
 
 
3805997
93bc661
 
3805997
93bc661
 
 
3805997
 
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
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "trl>=0.12.0",
#     "peft>=0.7.0",
#     "transformers>=4.36.0",
#     "accelerate>=0.24.0",
#     "bitsandbytes>=0.41.0",
#     "datasets>=2.0.0",
#     "trackio",
# ]
# ///

"""
Fine-tune swiss-ai/Apertus-8B-2509 on marcodsn/SOC-2508 (Synthetic Online Conversations).

Preserves the full multi-participant chat structure: each conversation is formatted
as ChatML with custom roles (persona usernames) rather than collapsing to user/assistant.
Loss is computed on ALL tokens so the model learns every participant's voice.
"""

import trackio
from datasets import load_dataset
from peft import LoraConfig
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from trl import SFTConfig, SFTTrainer

MODEL_ID = "swiss-ai/Apertus-8B-2509"
DATASET_ID = "marcodsn/SOC-2508"
OUTPUT_REPO = "Colby/apertus-8b-soc"

print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)

# Apertus-8B tokenizer has no chat_template set; define ChatML explicitly.
# Custom roles (persona usernames) are preserved as-is in the im_start tag.
tokenizer.chat_template = (
    "{% for message in messages %}"
    "<|im_start|>{{ message['role'] }}\n{{ message['content'] }}<|im_end|>\n"
    "{% endfor %}"
)

print("Loading dataset...")
dataset = load_dataset(DATASET_ID, split="train")
print(f"Loaded {len(dataset)} conversations")


def format_conversation(example):
    """
    Convert a SOC conversation to a ChatML text string for training.

    Structure:
      - system turn: full persona bios, relationship, and situation context
      - one turn per chat_parts entry, role = sender's username, content = all messages joined

    Using apply_chat_template + dataset_text_field trains on all tokens (all participants),
    which is correct for multi-participant chat — there is no single "assistant" role.
    """
    exp = example["experience"]
    p1, p2 = exp["persona1"], exp["persona2"]
    id_to_username = {
        p1["id"]: p1["username"],
        p2["id"]: p2["username"],
    }

    system_content = (
        f"Participants:\n"
        f"- {p1['name']} (@{p1['username']}, age {p1['age']}): {p1['background']} "
        f"Chatting style: {p1['chatting_style']}\n"
        f"- {p2['name']} (@{p2['username']}, age {p2['age']}): {p2['background']} "
        f"Chatting style: {p2['chatting_style']}\n"
        f"Relationship: {exp['relationship']}\n"
        f"Situation: {exp['situation']}"
    )

    messages = [{"role": "system", "content": system_content}]
    for turn in example["chat_parts"]:
        username = id_to_username.get(turn["sender"], turn["sender"])
        content = "\n".join(turn["messages"])
        messages.append({"role": username, "content": content})

    text = tokenizer.apply_chat_template(
        messages, tokenize=False, add_generation_prompt=False
    )
    return {"text": text}


print("Formatting conversations to ChatML...")
dataset = dataset.map(format_conversation, remove_columns=dataset.column_names)
split = dataset.train_test_split(test_size=0.05, seed=42)
train_dataset = split["train"]
eval_dataset = split["test"]
print(f"  Train: {len(train_dataset)}  Eval: {len(eval_dataset)}")

peft_config = LoraConfig(
    r=16,
    lora_alpha=32,
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
    target_modules="all-linear",
)

config = SFTConfig(
    # Hub push — ephemeral environment, must push or results are lost
    output_dir="apertus-8b-soc",
    push_to_hub=True,
    hub_model_id=OUTPUT_REPO,
    hub_strategy="every_save",

    # Train on ALL tokens (all participant voices, not just "assistant")
    dataset_text_field="text",
    max_length=2048,

    # Hyperparameters
    num_train_epochs=2,
    per_device_train_batch_size=2,
    per_device_eval_batch_size=1,   # eval disables grad checkpointing; keep small to avoid OOM
    gradient_accumulation_steps=8,  # effective batch = 16
    learning_rate=2e-4,
    lr_scheduler_type="cosine",
    warmup_ratio=0.05,
    bf16=True,
    gradient_checkpointing=True,

    # Checkpointing
    logging_steps=10,
    save_strategy="steps",
    save_steps=100,
    save_total_limit=2,
    eval_strategy="steps",
    eval_steps=100,

    # Monitoring
    report_to="trackio",
    project="apertus-soc-finetune",
    run_name="apertus-8b-soc-v1",
)

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype="bfloat16",
    bnb_4bit_use_double_quant=True,
)

print("Loading model with 4-bit quantization (QLoRA)...")
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    quantization_config=bnb_config,
    device_map="auto",
)

print("Initializing trainer...")
trainer = SFTTrainer(
    model=model,
    processing_class=tokenizer,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    peft_config=peft_config,
    args=config,
)

print("Starting training...")
trainer.train()

print("Pushing to Hub...")
trainer.push_to_hub()
trackio.finish()

print(f"Done! Model at: https://huggingface.co/{OUTPUT_REPO}")
print(f"Metrics at: https://huggingface.co/spaces/Colby/trackio")