Skip to content

Commit ed6b884

Browse files
committed
chore: update scoring function with option to regulate the influence of CO2 savings
1 parent 359ff99 commit ed6b884

8 files changed

Lines changed: 52 additions & 11 deletions

File tree

TheGreenEpochWeb/src/data/store.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface AppStore {
3434
addScenario: (s: Scenario) => void;
3535
updateScenario: (id: string, s: Scenario) => void;
3636
deleteScenario: (id: string) => void;
37-
runAllScenarios: (onProgress?: (done: number, total: number) => void) => Promise<SimResult[]>;
37+
runAllScenarios: (onProgress?: (done: number, total: number) => void, alpha?: number) => Promise<SimResult[]>;
3838
allScenarios: () => Scenario[];
3939
addResult: (r: SimResult) => void;
4040
clearResults: () => void;
@@ -90,7 +90,7 @@ export function AppProvider(props: { children: JSX.Element }) {
9090
return [...builtin, ...user];
9191
},
9292

93-
async runAllScenarios(onProgress?: (done: number, total: number) => void) {
93+
async runAllScenarios(onProgress?: (done: number, total: number) => void, alpha: number = 1) {
9494
setState("batchResults", []);
9595

9696
const scenarios = store.allScenarios();
@@ -116,6 +116,7 @@ export function AppProvider(props: { children: JSX.Element }) {
116116
}
117117
onProgress?.(done, total);
118118
},
119+
alpha,
119120
);
120121

121122
return state.batchResults;

TheGreenEpochWeb/src/domain/result.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ export function computeSavingsPct(
2020
: 0;
2121
}
2222

23-
export function computeScore(savingsPct: number, overheadPct: number): number {
24-
return savingsPct / Math.max(overheadPct, 0.001);
23+
export function computeScore(
24+
savingsPct: number,
25+
overheadPct: number,
26+
budgetPct: number,
27+
alpha: number = 1,
28+
): number {
29+
const savingsNorm = savingsPct / 100;
30+
const overheadNorm = overheadPct / Math.max(budgetPct, 0.001);
31+
return alpha * savingsNorm - (1 - alpha) * overheadNorm;
2532
}
2633

2734
export function computeIsOk(
@@ -53,14 +60,15 @@ export function buildSimResult(
5360
emissionsSeries: number[];
5461
tokensRemainingSeries: number[];
5562
},
63+
alpha: number = 1,
5664
): SimResult {
5765
const tps = tokensPerSecond(profile.gpuCount) || 1;
5866
const overheadS = lastProgress.pausedS + lastProgress.checkpointS;
5967
const actualOverheadPct = computeOverheadPct(lastProgress.pausedS, lastProgress.checkpointS, lastProgress.tokensTotal, tps);
6068
const totalEm = lastProgress.totalEmissionsG / 1000;
6169
const baselineEm = baselineProgress.totalEmissionsG / 1000;
6270
const co2SavingsPct = computeSavingsPct(totalEm, baselineEm);
63-
const score = computeScore(co2SavingsPct, actualOverheadPct);
71+
const score = computeScore(co2SavingsPct, actualOverheadPct, simConfig.overheadBudgetPct, alpha);
6472
const tokensProcessed = lastProgress.tokensTotal - lastProgress.tokensRemaining;
6573
const pausedTimeH = lastProgress.pausedS / 3600;
6674
const checkpointOverheadH = lastProgress.checkpointS / 3600;

TheGreenEpochWeb/src/engine/runall-entry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface StartMessage {
99
profiles: Record<string, TrainingProfile>;
1010
co2Cache: Record<string, CO2Timeline>;
1111
scenarios: Scenario[];
12+
alpha: number;
1213
}
1314

1415
function fullProfile(profile: TrainingProfile, c: Constants): FullProfile {
@@ -33,6 +34,7 @@ function runOnce(
3334
thetaPause: number,
3435
thetaResume: number,
3536
scenario: Scenario,
37+
alpha: number = 1,
3638
): SimResult {
3739
const baseline: import("../domain/types").SimProgress[] = [];
3840
for (const p of simulateStepwise(profile, neverPausePolicy(), timeline, config)) {
@@ -67,12 +69,12 @@ function runOnce(
6769
stateSeries,
6870
emissionsSeries,
6971
tokensRemainingSeries,
70-
});
72+
}, alpha);
7173
}
7274

7375
self.onmessage = (e: MessageEvent<StartMessage>) => {
7476
if (e.data.type !== "start") return;
75-
const { constants, profiles, co2Cache, scenarios } = e.data;
77+
const { constants, profiles, co2Cache, scenarios, alpha } = e.data;
7678

7779
const totalRuns = scenarios.reduce((s, sc) => s + sc.thresholds.length * sc.startTimes.length, 0);
7880
let done = 0;
@@ -99,7 +101,7 @@ self.onmessage = (e: MessageEvent<StartMessage>) => {
99101
const thetaResume = scenario.hysteresis[ti];
100102
const config = simConfig(scenario, startTime, scenario.overheadBudgetPct);
101103

102-
const result = runOnce(fp, timeline, config, thetaPause, thetaResume, scenario);
104+
const result = runOnce(fp, timeline, config, thetaPause, thetaResume, scenario, alpha);
103105
done++;
104106

105107
(self as any).postMessage({ type: "result", result, done, total: totalRuns });

TheGreenEpochWeb/src/engine/runall.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export function runAllInWorker(
1010
co2Cache: Record<string, CO2Timeline>,
1111
scenarios: Scenario[],
1212
onResult: (result: SimResult, done: number, total: number) => void,
13+
alpha: number = 1,
1314
): Promise<void> {
1415
return new Promise((resolve, reject) => {
1516
const worker = new Worker(new URL("./runall-entry", import.meta.url), { type: "module" });
@@ -37,6 +38,7 @@ export function runAllInWorker(
3738
profiles: stripProxies(profiles),
3839
co2Cache: stripProxies(co2Cache),
3940
scenarios: stripProxies(scenarios),
41+
alpha,
4042
});
4143
});
4244
}

TheGreenEpochWeb/src/engine/worker-entry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface StartMessage {
1616
maxIterations: number;
1717
minStep: number;
1818
shrinkFactor: number;
19+
alpha: number;
1920
};
2021
startTimeIdx: number;
2122
}
@@ -62,12 +63,14 @@ self.onmessage = (e: MessageEvent<StartMessage>) => {
6263
const baselineEm = baselineLast.totalEmissionsG / 1000;
6364
const co2SavingsPct = baselineEm > 0 ? (baselineEm - totalEm) / baselineEm * 100 : 0;
6465

66+
const savingsNorm = co2SavingsPct / 100;
67+
const overheadNorm = actualOverheadPct / Math.max(options.overheadBudgetPct, 0.001);
6568
iterPoints.push({
6669
thetaPause: pt.thetaPause,
6770
thetaResume: pt.thetaResume,
6871
actualOverheadPct,
6972
co2SavingsPct,
70-
score: co2SavingsPct / Math.max(actualOverheadPct, 0.001),
73+
score: options.alpha * savingsNorm - (1 - options.alpha) * overheadNorm,
7174
numPauses: last.numPauses,
7275
totalEmissionsKgco2: totalEm,
7376
baselineEmissionsKgco2: baselineEm,

TheGreenEpochWeb/src/engine/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export interface AdaptiveOptions {
77
maxIterations: number;
88
minStep: number;
99
shrinkFactor: number;
10+
alpha: number;
1011
}
1112

1213
/**

TheGreenEpochWeb/src/pages/LiveSimPage.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function LiveSimPage() {
3535
const [scrollMode, setScrollMode] = createSignal(true);
3636
const [windowSize, setWindowSize] = createSignal(300);
3737
const [simResult, setSimResult] = createSignal<SimResult | null>(null);
38+
const [alpha, setAlpha] = createSignal(1);
3839

3940
let cancelFlag = false;
4041

@@ -134,7 +135,7 @@ export function LiveSimPage() {
134135
setTokensRemainingSeries(tokensRemainingArr.slice());
135136
setRunning(false);
136137
setFinished(true);
137-
const result = saveResult(lastProgress, labels, co2PointsArr, stateArr, emissionsArr, tokensRemainingArr, full, sc, thresholdIdx(), startIdx(), tl, app);
138+
const result = saveResult(lastProgress, labels, co2PointsArr, stateArr, emissionsArr, tokensRemainingArr, full, sc, thresholdIdx(), startIdx(), tl, app, alpha());
138139
if (result) setSimResult(result);
139140
}
140141
};
@@ -206,6 +207,16 @@ export function LiveSimPage() {
206207
<span class="text-xs text-fg-muted w-8 tabular-nums">{windowSize()}</span>
207208
</Show>
208209

210+
<div class="flex items-center gap-1.5">
211+
<label class="text-xs text-fg-muted whitespace-nowrap">{"\u03B1"} (CO₂ weight):</label>
212+
<input
213+
type="number" value={alpha()}
214+
onInput={e => setAlpha(+e.currentTarget.value || 0)}
215+
step="0.1" min="0" max="1"
216+
class="w-16 bg-surface-2 border border-border-default/50 rounded px-2 py-1.5 text-sm text-fg-body tabular-nums focus:outline-none focus:border-accent"
217+
/>
218+
</div>
219+
209220
<button
210221
onClick={runSim}
211222
disabled={running()}
@@ -384,6 +395,7 @@ function saveResult(
384395
lastP: SimProgress, allLabels: string[], allCo2: number[], allStates: string[], allEmissions: number[], allTokensRemaining: number[],
385396
full: FullProfile, sc: Scenario, ti: number, si: number, tl: CO2Timeline,
386397
app: ReturnType<typeof useApp>,
398+
alpha: number = 1,
387399
): SimResult | null {
388400
try {
389401
const simConfig: SimConfig = {
@@ -409,7 +421,7 @@ function saveResult(
409421
stateSeries: allStates,
410422
emissionsSeries: allEmissions,
411423
tokensRemainingSeries: allTokensRemaining,
412-
});
424+
}, alpha);
413425
app.addResult(result);
414426
return result;
415427
} catch (e) {

TheGreenEpochWeb/src/pages/OptimizePage.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function OptimizePage() {
9090
const [tpMax, setTpMax] = createSignal(500);
9191
const [resolution, setResolution] = createSignal(10);
9292
const [budget, setBudget] = createSignal(200);
93+
const [alpha, setAlpha] = createSignal(1);
9394
const [running, setRunning] = createSignal(false);
9495
const [iterMsg, setIterMsg] = createSignal("");
9596
const [points, setPoints] = createSignal<SweepPoint[]>([]);
@@ -201,6 +202,7 @@ export function OptimizePage() {
201202
maxIterations: 6,
202203
minStep: 3,
203204
shrinkFactor: 0.45,
205+
alpha: alpha(),
204206
},
205207
0,
206208
(iter, iterPts, best) => {
@@ -346,6 +348,16 @@ export function OptimizePage() {
346348
class="w-full bg-surface-3 border border-border-default/50 rounded px-3 py-2 text-sm text-fg-body focus:outline-none focus:border-accent"
347349
/>
348350
</div>
351+
352+
<div>
353+
<label class="block text-xs font-medium text-fg-muted mb-1">{"\u03B1"} (CO₂ weight)</label>
354+
<input
355+
type="number" value={alpha()}
356+
onInput={e => setAlpha(+e.currentTarget.value || 0)}
357+
step="0.1" min="0" max="1"
358+
class="w-full bg-surface-3 border border-border-default/50 rounded px-3 py-2 text-sm text-fg-body focus:outline-none focus:border-accent"
359+
/>
360+
</div>
349361
</div>
350362

351363
<div class="flex items-center gap-3">

0 commit comments

Comments
 (0)