Spaces:
Running
Running
File size: 11,775 Bytes
7c5b7bd |
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
import { getColor, COLORS } from "./colors.mjs"
import Plotly from "plotly.js-basic-dist-min"
import _ from "lodash"
const DATA_FOLDER = "assets/data/plots"
const LINE_SETTINGS = {
width: 2.5,
type: "scatter",
mode: "lines",
}
const BAR_SETTINGS = {
width: 0.5,
type: "bar",
opacity: 0.9,
marker: {
line: {
width: 1.0
}
}
}
const METRIC_ID_TO_PRIORITY = {
"agg_score": 0,
"hellaswag/acc_norm": 1,
"arc/acc_norm": 2,
"mmlu/acc_norm": 3,
"openbookqa/acc_norm": 4,
"commonsense_qa/acc_norm": 5,
"piqa/acc_norm": 6,
"siqa/acc_norm": 7,
"winogrande/acc_norm": 8,
// Stats
"lines_ended_with_punct": 0,
"lines_chars": 1,
"short_lines": 2,
}
const TASK_ID_TO_NAME = {
// Ablations
agg_score: "Aggregate Score",
"commonsense_qa/acc_norm": "Commonsense QA",
"hellaswag/acc_norm": "HellaSwag",
"openbookqa/acc_norm": "OpenBook QA",
"piqa/acc_norm": "PIQA",
"siqa/acc_norm": "Social IQA",
"winogrande/acc_norm": "WinoGrande",
"arc/acc_norm": "ARC",
"mmlu/acc_norm": "MMLU",
// Stats
"lines_ended_with_punct": "Lines Ended With Punctuation",
"lines_chars": "Lines Chars",
"short_lines": "Short Lines",
};
const DATASET_ID_TO_NAME = {
pii_removed: "Fineweb",
allenai_c4_en: "C4",
"tiiuae_falcon-refinedweb_data": "RefinedWeb",
"red-pajama-v2_jsonl-deduplicated-extract": "RedPajamaV2",
"dolma-sample": "Dolma1.6",
dedup_minhash_independent_output: "Independent Dedup MinHash",
"dedup_minhash_CC-MAIN-2013-48_output": "Full MinHash CC-MAIN-2013-48",
"dedup_minhash_independent_output_CC-MAIN-2013-48": "Independent MinHash CC-MAIN-2013-48",
"ind_minhash-CC-MAIN-2019-18": "Independent MinHash CC-MAIN-2019-18",
"wet-extraction-2019-18": "WET Extraction 2019-18",
"dedup_minhash_CC-MAIN-2013-48_output": "Full MinHash CC-MAIN-2013-48",
"dedup_minhash_independent_output_CC-MAIN-2013-48": "Independent MinHash CC-MAIN-2013-48",
};
const DEFAULT_SETTINGS = {
slider: {
max: 30,
min: 0,
default: 0,
},
defaultMetric: "agg_score",
type: "line"
};
const DEFAULT_LAYOUT = {
font: {
family: "apple-system, Arial, sans-serif",
},
title: {
text: "Plot Title",
font: {
size: 19,
family: "apple-system, Arial, sans-serif",
},
},
xaxis: {
title: {
text: "Training tokens (billions)",
font: {
size: 15,
family: "apple-system, Arial, sans-serif",
},
},
tickfont: {
size: 14,
family: "apple-system, Arial, sans-serif",
},
showgrid: false,
mirror: true,
ticks: "outside",
showline: true,
},
yaxis: {
title: {
text: "Agg Score",
font: {
size: 15,
family: "apple-system, Arial, sans-serif",
},
standoff: 10,
},
showgrid: false,
mirror: true,
ticks: "outside",
showline: true,
tickfont: {
size: 14,
family: "apple-system, Arial, sans-serif",
},
},
yaxis2: {
title: {
text: "Words Contamination",
font: {
size: 15,
family: "apple-system, Arial, sans-serif",
},
standoff: 10,
},
tickfont: {
size: 14,
family: "apple-system, Arial, sans-serif",
},
showgrid: false,
ticks: "outside",
},
legend: {
orientation: "v",
xanchor: "right",
yanchor: "bottom",
x: 1,
y: 0,
font: {
size: 14,
family: "apple-system, Arial, sans-serif",
},
bgcolor: "rgba(0,0,0,0)",
},
margin: {
t: 30,
b: 50,
},
height: 400,
};
const getAutoRange = (traces) => {
let minX = Math.min(...traces.flatMap((trace) => trace.x));
let maxX = Math.max(...traces.flatMap((trace) => trace.x));
return [minX * 0.95, maxX * 1.05];
};
const getColorForTrace = (traceName, colorsMapping, index) => {
// First check if the color already exists in colorsMaping and if so return it
const reusedColor = colorsMapping.get(traceName)
if (reusedColor) {
return reusedColor
}
let color = getColor(index)
while (colorsMapping.has(color) && index < COLORS.length) {
index += 1
color = getColor(index)
}
colorsMapping.set(traceName, color)
return color
}
const createAblationPlottingElements = (
plotElement,
indexMapping,
settings
) => {
const plot = document.createElement("figure");
const controls = document.createElement("div");
plot.classList.add("plotly");
controls.classList.add("plotly_controls");
plotElement.appendChild(plot);
plotElement.appendChild(controls);
const metricOptions = Object.keys(indexMapping).filter(
(metric) => metric in TASK_ID_TO_NAME
);
// Dropdown
let dropdown = undefined
if (metricOptions.length > 1) {
const dropdownLabel = document.createElement("label");
dropdownLabel.textContent = "Metric:";
dropdown = document.createElement("select");
dropdown.innerHTML = metricOptions
.sort((a, b) => (METRIC_ID_TO_PRIORITY[a] ?? 0) - (METRIC_ID_TO_PRIORITY[b] ?? 0))
.map(
(option) =>
`<option value="${option}">${TASK_ID_TO_NAME[option]}</option>`
)
.join("");
dropdown.value = settings.defaultMetric;
const dropdownContainer = document.createElement("div");
dropdownContainer.classList.add("plotly_input_container");
dropdownContainer.appendChild(dropdownLabel);
dropdownContainer.appendChild(dropdown);
controls.appendChild(dropdownContainer);
}
let slider = undefined;
if (settings.slider !== null) {
const sliderLabel = document.createElement("label");
sliderLabel.textContent = "Rolling window:";
slider = document.createElement("input");
slider.type = "range";
slider.min = settings.slider.min;
slider.max = settings.slider.max;
slider.value = settings.slider.default;
// current value
const sliderValue = document.createElement("span");
sliderValue.textContent = slider.value;
slider.addEventListener("input", () => {
sliderValue.textContent = slider.value;
});
const sliderInputContainer = document.createElement("div");
sliderInputContainer.classList.add("plotly_slider");
sliderInputContainer.appendChild(slider);
sliderInputContainer.appendChild(sliderValue);
const sliderContainer = document.createElement("div");
sliderContainer.classList.add("plotly_input_container");
sliderContainer.appendChild(sliderLabel);
sliderContainer.appendChild(sliderInputContainer);
controls.appendChild(sliderContainer);
}
let caption = undefined
if (settings.caption) {
caption = document.createElement("figcaption");
caption.classList.add("plotly_caption");
caption.textContent = settings.caption;
plotElement.appendChild(caption);
}
return { dropdown, slider, plot, caption };
};
const rollingWindow = function (data, windowSize) {
if (windowSize === 0) {
return data;
}
const rollingData = [];
// Start at halfWindowSize to ensure we can get a full window
for (let i = windowSize; i < data.length; i++) {
const windowStart = i - windowSize;
const windowEnd = i;
const windowData = data.slice(windowStart, windowEnd);
const windowAverage =
windowData.reduce((acc, value) => acc + value, 0) /
windowData.length;
rollingData.push(windowAverage);
}
return rollingData;
};
const createTraces = (data, settings, colorsMapping, sliderValue) => {
if (!data) {
return []
}
const res = Array.from(Object.entries(data)).map(([key, traceData], index) => {
const y = rollingWindow(traceData.y, sliderValue);
const x = traceData.x.slice(0, y.length);
const plotSettings = settings?.type === "bar" ? BAR_SETTINGS : LINE_SETTINGS;
const traceColor = traceData.color ?? getColorForTrace(key, colorsMapping, index)
const trace = _.merge({}, {
x: x,
y: y,
name: traceData.label ?? DATASET_ID_TO_NAME[key] ?? key,
marker: {
color: traceColor,
},
line: {
color: traceColor,
},
yaxis: traceData.yaxis ?? "y1"
}, plotSettings);
return trace
});
return res
}
export const init_ablation_plot = function () {
const plotElements = document.querySelectorAll('[id^="plot-"]');
plotElements.forEach(async (plotElement) => {
const plotName = plotElement.id.replace("plot-", "");
const indexData = await fetch(`${DATA_FOLDER}/${plotName}/index.json`).then(
(response) => response.json()
);
const settings = _.merge({}, DEFAULT_SETTINGS, indexData.settings);
const indexMapping = indexData.files;
const { dropdown, slider, plot } = createAblationPlottingElements(
plotElement,
indexMapping,
settings
);
plot.id = `graph-${plotName}`;
if (dropdown !== undefined) {
dropdown.addEventListener("change", () => updatePlot(dropdown, slider));
}
let timeoutId;
// Debounce the slider
if (slider !== undefined) {
slider.addEventListener("input", () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
updatePlot(dropdown, slider);
}, 500);
});
}
// Shared plot
Plotly.newPlot(plot, []);
// This is to ensure that the colors are consistent acrros different metrics
const colorsMapping = new Map()
async function updatePlot(dropdown, slider) {
const metricName = dropdown?.value ?? settings.defaultMetric;
const sliderValue = parseInt(slider?.value ?? 0);
const metricData = await fetch(
`${DATA_FOLDER}/${plotName}/${indexMapping[metricName]["file"]}`
).then((response) => response.json());
const traces = (metricData?.traces ?? []).concat(createTraces(metricData.data, settings, colorsMapping, sliderValue))
const width = plot.parentElement.offsetWidth;
const layout = _.merge(
{},
DEFAULT_LAYOUT,
{
width: width,
yaxis: { title: { text: TASK_ID_TO_NAME[metricName] } },
xaxis: {
range: null
},
},
metricData.layout
);
Plotly.react(plot, traces, layout);
window.addEventListener("resize", () => {
// If the window size is smaller than 768, we don't care as it's not shown
if (window.innerWidth < 768) {
return;
}
Plotly.relayout(plot, {
width: plot.parentElement.offsetWidth,
});
});
}
// Initial plot
updatePlot(dropdown, slider);
});
}; |