Upload finetune_coding.py with huggingface_hub
Browse files- finetune_coding.py +18 -9
finetune_coding.py
CHANGED
|
@@ -80,8 +80,13 @@ def format_roman(example):
|
|
| 80 |
def format_coderforge(example):
|
| 81 |
"""
|
| 82 |
CoderForge agentic trajectories: messages is a JSON string in OpenHands format.
|
| 83 |
-
Merges all assistant+tool turns into a single Apertus assistant message
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
| 86 |
try:
|
| 87 |
raw = json.loads(example["messages"])
|
|
@@ -110,6 +115,11 @@ def format_coderforge(example):
|
|
| 110 |
agentic_started = True
|
| 111 |
tool_calls_raw = msg.get("tool_calls") or []
|
| 112 |
if tool_calls_raw:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
calls = [
|
| 114 |
ToolCall(
|
| 115 |
name=tc["function"]["name"],
|
|
@@ -122,7 +132,8 @@ def format_coderforge(example):
|
|
| 122 |
agentic_blocks.append(
|
| 123 |
AssistantBlock(type=BlockType.TOOL_CALLS, calls=calls)
|
| 124 |
)
|
| 125 |
-
|
|
|
|
| 126 |
agentic_blocks.append(
|
| 127 |
AssistantBlock(type=BlockType.RESPONSE, text=content)
|
| 128 |
)
|
|
@@ -139,8 +150,6 @@ def format_coderforge(example):
|
|
| 139 |
if not agentic_blocks:
|
| 140 |
return {"text": None}
|
| 141 |
|
| 142 |
-
# If the last block is TOOL_CALLS or TOOL_OUTPUTS (no final text response),
|
| 143 |
-
# the trajectory is still useful — leave it as-is.
|
| 144 |
all_msgs = system_msgs + user_msgs + [Message.assistant_with_blocks(agentic_blocks)]
|
| 145 |
try:
|
| 146 |
return {"text": formatter.format_conversation(Conversation(messages=all_msgs))}
|
|
@@ -221,15 +230,15 @@ config = SFTConfig(
|
|
| 221 |
hub_strategy="every_save",
|
| 222 |
|
| 223 |
dataset_text_field="text",
|
| 224 |
-
max_length=
|
| 225 |
|
| 226 |
num_train_epochs=2,
|
| 227 |
-
per_device_train_batch_size=
|
| 228 |
per_device_eval_batch_size=1,
|
| 229 |
-
gradient_accumulation_steps=
|
| 230 |
learning_rate=2e-4,
|
| 231 |
lr_scheduler_type="cosine",
|
| 232 |
-
|
| 233 |
bf16=True,
|
| 234 |
gradient_checkpointing=True,
|
| 235 |
|
|
|
|
| 80 |
def format_coderforge(example):
|
| 81 |
"""
|
| 82 |
CoderForge agentic trajectories: messages is a JSON string in OpenHands format.
|
| 83 |
+
Merges all assistant+tool turns into a single Apertus assistant message.
|
| 84 |
+
|
| 85 |
+
Block mapping:
|
| 86 |
+
assistant with tool_calls → THOUGHTS (the explanation) + TOOL_CALLS (the action)
|
| 87 |
+
tool result → TOOL_OUTPUTS
|
| 88 |
+
assistant without tool_calls (final) → RESPONSE
|
| 89 |
+
This ordering is valid in the Apertus format (RESPONSE may not precede TOOL_OUTPUTS).
|
| 90 |
"""
|
| 91 |
try:
|
| 92 |
raw = json.loads(example["messages"])
|
|
|
|
| 115 |
agentic_started = True
|
| 116 |
tool_calls_raw = msg.get("tool_calls") or []
|
| 117 |
if tool_calls_raw:
|
| 118 |
+
# Content alongside tool_calls is the model's reasoning → THOUGHTS
|
| 119 |
+
if content:
|
| 120 |
+
agentic_blocks.append(
|
| 121 |
+
AssistantBlock(type=BlockType.THOUGHTS, text=content)
|
| 122 |
+
)
|
| 123 |
calls = [
|
| 124 |
ToolCall(
|
| 125 |
name=tc["function"]["name"],
|
|
|
|
| 132 |
agentic_blocks.append(
|
| 133 |
AssistantBlock(type=BlockType.TOOL_CALLS, calls=calls)
|
| 134 |
)
|
| 135 |
+
elif content:
|
| 136 |
+
# No tool calls: this is a final text response
|
| 137 |
agentic_blocks.append(
|
| 138 |
AssistantBlock(type=BlockType.RESPONSE, text=content)
|
| 139 |
)
|
|
|
|
| 150 |
if not agentic_blocks:
|
| 151 |
return {"text": None}
|
| 152 |
|
|
|
|
|
|
|
| 153 |
all_msgs = system_msgs + user_msgs + [Message.assistant_with_blocks(agentic_blocks)]
|
| 154 |
try:
|
| 155 |
return {"text": formatter.format_conversation(Conversation(messages=all_msgs))}
|
|
|
|
| 230 |
hub_strategy="every_save",
|
| 231 |
|
| 232 |
dataset_text_field="text",
|
| 233 |
+
max_length=2048, # 4096 caused OOM on a10g-large; 2048 fits with room to spare
|
| 234 |
|
| 235 |
num_train_epochs=2,
|
| 236 |
+
per_device_train_batch_size=1, # reduced from 2 to avoid OOM
|
| 237 |
per_device_eval_batch_size=1,
|
| 238 |
+
gradient_accumulation_steps=16, # keeps effective batch at 16
|
| 239 |
learning_rate=2e-4,
|
| 240 |
lr_scheduler_type="cosine",
|
| 241 |
+
warmup_steps=100,
|
| 242 |
bf16=True,
|
| 243 |
gradient_checkpointing=True,
|
| 244 |
|