File size: 5,098 Bytes
db744c6
 
 
 
 
 
 
 
e3278c7
db744c6
 
 
e3278c7
 
db744c6
 
 
 
e3278c7
 
db744c6
 
 
 
e3278c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db744c6
 
 
 
e3278c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db744c6
 
 
 
 
 
 
 
 
e3278c7
 
 
 
 
 
 
 
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
---
library_name: transformers
tags: []
---

# Model Card for Model ID

<!-- Provide a quick summary of what the model is/does. -->
Classification model finetuned for prompt-model routing based on code prompt difficulty.

## Model Details

Base model: answerdotai/ModernBERT-base

### Model Description

<!-- Provide a longer summary of what this model is. -->

- **Developed by:** [Christian @ Prime Intellect]
- **Finetuned from model:** [answerdotai/ModernBERT-base]


## How to Get Started with the Model

<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
```
from transformers import pipeline
import torch

# Load the model
classifier = pipeline(
    "text-classification",
    model="cdreetz/modern-bert-router",
    device=0 if torch.cuda.is_available() else -1
)

# Test easy problem
easy_problem = """
Write a function that returns the sum of two numbers.

Example:
Input: add(2, 3)
Output: 5
"""

# Test hard problem
hard_problem = """
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree.
A path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections.
The path must contain at least one node and does not need to go through the root.

Example:
Input: root = [1,2,3]
Output: 6 (2 -> 1 -> 3)
"""

# Run predictions
result_easy = classifier(easy_problem)[0]
result_hard = classifier(hard_problem)[0]

print("Easy Problem:")
print(f"  Difficulty: {result_easy['label']}")
print(f"  Confidence: {result_easy['score']:.2%}\n")

print("Hard Problem:")
print(f"  Difficulty: {result_hard['label']}")
print(f"  Confidence: {result_hard['score']:.2%}")
```


## Training Details

```
# /// script
# dependencies = [
#   "chatan",
#   "transformers",
#   "datasets",
#   "torch",
#   "accelerate",
#   "scikit-learn",
#   "triton",
#   "huggingface_hub"
# ]
# ///

import os
import asyncio
import chatan as ch
from datasets import Dataset as hf_ds
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
import numpy as np
from sklearn.metrics import accuracy_score, f1_score
import torch
import triton
from huggingface_hub import login

#torch._dynamo.config.suppress_errors = True

login()

async def create_dataset():
    gen = ch.async_generator("openai", os.getenv("OPENAI_API_KEY"))

    ds = ch.async_dataset({
        "difficulty": ch.sample.choice(["easy", "hard"]),
        "text": gen("write a coding problem of difficulty {difficulty}")
    })

    df = await ds.generate(n=1000, max_concurrent_rows=500)
    df['labels'] = df['difficulty'].map({"easy": 0, "hard": 1})
    dataset = hf_ds.from_pandas(df[['text', 'labels']])
    return dataset.train_test_split(test_size=0.2, seed=42)


def train(dataset):
    tokenizer = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base")
    model = AutoModelForSequenceClassification.from_pretrained(
        "answerdotai/ModernBERT-base",
        num_labels=2,
        label2id={"easy": 0, "hard": 1},
        id2label={0: "easy", 1: "hard"},
        problem_type="single_label_classification"
    )


    def tokenize(examples):
        return tokenizer(
            examples['text'], 
            padding='max_length', 
            truncation=True, 
            max_length=512
        )

    tokenized = dataset.map(tokenize, batched=True, remove_columns=['text'])
    tokenized.set_format('torch')

    training_args = TrainingArguments(
        output_dir="./modernbert-router",
        eval_strategy="epoch",
        num_train_epochs=3,
        per_device_train_batch_size=16,
        learning_rate=2e-5,
        save_strategy="epoch",
        load_best_model_at_end=True
    )

    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized["train"],
        eval_dataset=tokenized["test"],
        processing_class=tokenizer,
        compute_metrics=lambda p: {
            "accuracy": accuracy_score(p.label_ids, np.argmax(p.predictions, axis=1)),
            "f1": f1_score(p.label_ids, np.argmax(p.predictions, axis=1))
        }
    )

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

    model.save_pretrained("./modernbert-router")
    tokenizer.save_pretrained("./modernbert-router")

    print("donezo")

    #model = AutoModelForSequenceClassification.from_pretrained("./modernbert-router")
    #tokenizer = AutoTokenizer.from_pretrained("./modernbert-router")

    #model.push_to_hub("cdreetz/modern-bert-router")
    #tokenizer.push_to_hub("cdreetz/modern-bert-router")



if __name__ == "__main__":
    dataset = asyncio.run(create_dataset())
    train(dataset)
```




## Citation [optional]

<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->

**BibTeX:**
```
@software{modern-bert-router,
  author = {Reetz, Christian},
  title = {ModernBERTRouter},
  url = {https://huggingface.co/cdreetz/modern-bert-router/},
  year = {2025}
}
```