forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurriculum.py
More file actions
53 lines (39 loc) · 1.44 KB
/
Copy pathcurriculum.py
File metadata and controls
53 lines (39 loc) · 1.44 KB
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
"""Curriculum learning utilities — sort datasets by difficulty for staged training."""
from __future__ import annotations
import json
def sort_by_length(data: list[dict]) -> list[dict]:
"""Sort dataset rows by text length (short → long).
Supports both 'text' field and 'messages' format.
"""
def _row_length(row: dict) -> int:
if "text" in row:
return len(str(row["text"]))
if "messages" in row:
return sum(len(str(msg.get("content", ""))) for msg in row["messages"])
# Fallback: stringify entire row
return len(json.dumps(row))
return sorted(data, key=_row_length)
def create_buckets(data: list, num_buckets: int) -> list[list]:
"""Split sorted data into N roughly equal buckets.
Args:
data: Pre-sorted list (easy → hard).
num_buckets: Number of difficulty stages.
Returns:
List of lists, each representing a difficulty bucket.
"""
if num_buckets <= 0:
return [data]
total = len(data)
if total == 0:
return [[] for _ in range(num_buckets)]
bucket_size = total // num_buckets
remainder = total % num_buckets
buckets = []
start = 0
for idx in range(num_buckets):
# Distribute remainder across first `remainder` buckets
extra = 1 if idx < remainder else 0
end = start + bucket_size + extra
buckets.append(data[start:end])
start = end
return buckets