-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
348 lines (269 loc) · 9.87 KB
/
utils.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from typing import Union, Literal
import polars as pl
import dicttoxml
import tqdm
import requests
import os
import html
import re
import numpy as np
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import math
def cache_dataframe(path):
def decorator(func):
cache = {}
def wrapper(*args, **kwargs):
if path in cache:
return cache[path]
if os.path.exists(path):
df = pl.read_parquet(path)
else:
df = func(*args, **kwargs)
print(f"Caching dataframe to {path}")
df.write_parquet(path)
cache[path] = df
return df
def bust_cache():
if path in cache:
del cache[path]
if os.path.exists(path):
os.remove(path)
print(f"Cache busted for {path}")
wrapper.bust_cache = bust_cache
return wrapper
return decorator
@cache_dataframe("./data/full_dataset.parquet")
def full_dataset() -> pl.DataFrame:
from datasets import load_dataset, Dataset
dataset: Dataset = load_dataset("OpenPipe/hacker-news", split="train")
return dataset.to_polars()
def unescape_html(text):
unescaped = html.unescape(text).replace("<p>", "\n\n")
return re.sub(r'<a href="([^"]+)"[^>]*>[^<]+</a>', r"\1", unescaped)
@cache_dataframe("./data/augmented_comments.parquet")
def augmented_comments() -> pl.DataFrame:
df = full_dataset()
comments_df = df.filter((pl.col("type") == "comment"))
comments_df = comments_df.select(
pl.col("id", "by", "time", "text", "parent", "top_level_parent", "kids")
)
# add a new column siblings_count
comments_df = comments_df.with_columns(
[pl.col("id").count().over("parent").alias("siblings_count")]
)
sibling_ranks = dict()
# iterate over the rows of the dataframe
for groupable_comment in df.select(pl.col("kids")).iter_rows():
kids = groupable_comment[0]
if kids is not None:
for i, kid in enumerate(kids):
sibling_ranks[kid] = i + 1
# Find the maximum value in the sibling_ranks dictionary
max_sibling_rank = max(sibling_ranks.values())
# Print the maximum value
print(f"The maximum sibling rank is: {max_sibling_rank}")
print(f"The number of sibling ranks is: {len(sibling_ranks)}")
comments_df = comments_df.with_columns(
[
pl.col("id")
.replace_strict(
list(sibling_ranks.keys()), list(sibling_ranks.values()), default=-1
)
.alias("sibling_rank")
]
)
del sibling_ranks
comments_df = comments_df.with_columns(pl.lit(-1).alias("nested_level"))
comments_df = comments_df.with_columns(
pl.when(pl.col("top_level_parent") == pl.col("parent"))
.then(0)
.otherwise(pl.col("nested_level"))
.alias("nested_level")
)
for i in tqdm.tqdm(range(1, 25), desc="Calculating nested levels"):
parent_ids = comments_df.filter(pl.col("nested_level") == i - 1)["id"]
comments_df = comments_df.with_columns(
pl.when(pl.col("parent").is_in(parent_ids))
.then(i)
.otherwise(pl.col("nested_level"))
.alias("nested_level")
)
progress_bar = tqdm.tqdm(total=len(comments_df), desc="Unescaping HTML")
def unescape_html_wrapper(text):
progress_bar.update(1)
return unescape_html(text)
comments_df = comments_df.with_columns(
pl.col("text")
.map_elements(unescape_html_wrapper, return_dtype=pl.Utf8)
.alias("text")
)
return comments_df
def build_all_prompts(
ids: Union[list[int], pl.Series], version: Literal["v1", "v2"]
) -> list[str]:
if isinstance(ids, pl.Series):
ids = ids.to_list()
build_prompt = build_prompt_v1 if version == "v1" else build_prompt_v2
prompts = []
for id in tqdm.tqdm(ids, desc="Building prompts"):
prompts.append(build_prompt(id))
return prompts
def build_prompt_v1(comment_id: int) -> str:
df = full_dataset()
comment = df.row(comment_id, named=True)
story = df.row(comment["top_level_parent"], named=True)
data = {
"instructions": "Your goal is to analyze the following comment and estimate how highly it will be upvoted by the Hacker News community.",
"comment": {
"author": comment["by"],
"text": comment["text"],
"parent_chain": [],
},
"story": {"title": story["title"]},
}
current_parent = df.row(comment["parent"], named=True)
while current_parent["id"] != story["id"]:
data["comment"]["parent_chain"].append(
{"author": current_parent["by"], "text": current_parent["text"]}
)
current_parent = df.row(current_parent["parent"], named=True)
if story["url"] is not None:
data["story"]["url"] = story["url"]
if story["text"] is not None:
data["story"]["text"] = story["text"]
xml: bytes = dicttoxml.dicttoxml(data, attr_type=False, root=False)
return xml.decode("utf-8")
def build_prompt_v2(comment_id: int) -> str:
df = full_dataset()
comment = df.row(comment_id, named=True)
story = df.row(comment["top_level_parent"], named=True)
data = {
"story": {"title": story["title"]},
"parent_chain": [],
"comment": {
"author": comment["by"],
"text": comment["text"],
},
}
current_parent = df.row(comment["parent"], named=True)
while current_parent["id"] != story["id"]:
data["parent_chain"].append(
{"author": current_parent["by"], "text": current_parent["text"]}
)
current_parent = df.row(current_parent["parent"], named=True)
if story["url"] is not None:
data["story"]["url"] = story["url"]
if story["text"] is not None:
data["story"]["text"] = story["text"]
xml: bytes = dicttoxml.dicttoxml(data, attr_type=False, root=False)
return xml.decode("utf-8")
def run_inference_sglang(
prompts: Union[list[str], pl.Series], chunk_size: int = 100
) -> list[float]:
if isinstance(prompts, pl.Series):
prompts = prompts.to_list()
# Chunk prompts into lists of INFERENCE_CHUNK_SIZE
chunks = [prompts[i : i + chunk_size] for i in range(0, len(prompts), chunk_size)]
rewards = []
for chunk in tqdm.tqdm(chunks, desc="Running inference"):
json_data = {
"conv": chunk,
}
response = requests.post("http://127.0.0.1:30000/judge", json=json_data).json()
rewards.extend([x["embedding"][0] for x in response])
return rewards
def with_story_info(comments_df: pl.DataFrame) -> pl.DataFrame:
stories_df = (
dataset()
.filter(pl.col("type") == "story")
.select(pl.col("id", "title", "url"))
.rename(
{
"id": "story_id",
"title": "story_title",
"url": "story_url",
}
)
)
return comments_df.join(
stories_df, left_on="top_level_parent", right_on="story_id", how="left"
)
@cache_dataframe("./data/stories_dataset.parquet")
def stories_dataset() -> pl.DataFrame:
stories = full_dataset().filter(
(pl.col("type") == "story")
& pl.col("time").is_not_null()
& pl.col("text").is_not_null()
& pl.col("url").is_null()
& pl.col("deleted").is_null()
& pl.col("dead").is_null()
)
# There's a weird discontinuity in late 2015, just ignore it
stories = stories.filter(pl.col("time") >= pl.datetime(2016, 1, 1))
# Add a log score, it's a very skewed distribution
stories = stories.with_columns(pl.col("score").log().alias("log_score"))
progress_bar = tqdm.tqdm(total=len(stories), desc="Serializing stories")
def serialize_story(story):
progress_bar.update(1)
escaped_story = html.unescape(story["text"]).replace("<p>", "\n\n")
return f"""
{story["title"]}
{story["by"]}, {story["time"].strftime("%Y-%m-%d")}
{escaped_story}
"""
stories = stories.with_columns(
pl.struct(["title", "by", "time", "text"])
.map_elements(serialize_story, return_dtype=pl.Utf8)
.alias("serialized")
)
progress_bar.close()
stories = stories.sample(fraction=1, shuffle=True, seed=42)
split_assignments = np.random.choice(
["train", "test", "val"], size=len(stories), p=[0.8, 0.1, 0.1]
)
stories = stories.with_columns(pl.Series("split", split_assignments))
return stories.select(
"id",
"title",
"by",
"text",
"score",
"descendants",
"time",
"log_score",
"serialized",
"split",
)
def calculate_metrics_by_split(df: pl.DataFrame) -> pl.DataFrame:
"""
Calculate correlation and RMSE metrics for each split in the dataset.
Args:
df: DataFrame with log_score, predictions and split columns
Returns:
DataFrame with metrics for each split
"""
metrics = []
for split in df["split"].unique():
split_df = df.filter(pl.col("split") == split)
# Calculate baseline (mean) metrics
average_score = split_df["log_score"].mean()
rmse_baseline = math.sqrt(
(split_df["log_score"] - average_score).pow(2).sum() / len(split_df)
)
# Calculate model metrics
rmse_model = math.sqrt(
(split_df["log_score"] - split_df["predictions"]).pow(2).sum()
/ len(split_df)
)
correlation_model = split_df.select(pl.corr("log_score", "predictions"))[
"log_score"
][0]
metrics.append(
{
"split": split,
"baseline_rmse": rmse_baseline,
"model_rmse": rmse_model,
"model_correlation": correlation_model,
}
)
return pl.DataFrame(metrics)