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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--
2
+ Efficiency Leverage Chart
3
+
4
+ Graphique spécifique montrant la définition de l'Efficiency Leverage (EL)
5
+ avec deux courbes de scaling de loss (Ling-Dense et Ling-MoE)
6
+ -->
7
+ <div class="efficiency-leverage-chart"></div>
8
+ <style>
9
+ .efficiency-leverage-chart {
10
+ position: relative;
11
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
12
+ }
13
+
14
+ .efficiency-leverage-chart .axis-label {
15
+ fill: var(--text-color);
16
+ font-size: 13px;
17
+ font-weight: 500;
18
+ opacity: 0.9;
19
+ stroke: var(--page-bg, white);
20
+ stroke-width: 6px;
21
+ paint-order: stroke fill;
22
+ }
23
+
24
+ .efficiency-leverage-chart .axes path {
25
+ display: none;
26
+ }
27
+
28
+ .efficiency-leverage-chart .axes line {
29
+ stroke: var(--axis-color, #999);
30
+ }
31
+
32
+ .efficiency-leverage-chart .axes text {
33
+ fill: var(--tick-color, #666);
34
+ font-size: 11px;
35
+ }
36
+
37
+ .efficiency-leverage-chart .grid line {
38
+ stroke: var(--grid-color, #e0e0e0);
39
+ stroke-opacity: 0.5;
40
+ }
41
+
42
+ .efficiency-leverage-chart .loss-curve {
43
+ fill: none;
44
+ stroke-width: 2.5;
45
+ }
46
+
47
+ .efficiency-leverage-chart .reference-line {
48
+ stroke: #333;
49
+ stroke-width: 2;
50
+ stroke-dasharray: 8, 4;
51
+ }
52
+
53
+ .efficiency-leverage-chart .marker-circle {
54
+ stroke-width: 2.5;
55
+ fill: white;
56
+ }
57
+
58
+ .efficiency-leverage-chart .marker-line {
59
+ stroke-width: 1.5;
60
+ stroke-dasharray: 4, 3;
61
+ }
62
+
63
+ .efficiency-leverage-chart .curve-label {
64
+ font-size: 12px;
65
+ font-weight: 600;
66
+ stroke: var(--page-bg, white);
67
+ stroke-width: 4px;
68
+ paint-order: stroke fill;
69
+ }
70
+
71
+ .efficiency-leverage-chart .formula-container {
72
+ text-align: center;
73
+ margin-top: 20px;
74
+ font-size: 18px;
75
+ color: var(--text-color);
76
+ }
77
+
78
+ .efficiency-leverage-chart .formula-container sub {
79
+ font-size: 0.7em;
80
+ vertical-align: sub;
81
+ line-height: 0;
82
+ }
83
+
84
+ .efficiency-leverage-chart .formula-fraction {
85
+ display: inline-block;
86
+ vertical-align: middle;
87
+ text-align: center;
88
+ }
89
+
90
+ .efficiency-leverage-chart .formula-numerator,
91
+ .efficiency-leverage-chart .formula-denominator {
92
+ display: block;
93
+ padding: 3px 8px;
94
+ font-weight: 600;
95
+ }
96
+
97
+ .efficiency-leverage-chart .formula-numerator {
98
+ border-bottom: 2px solid var(--text-color);
99
+ background: #4c8bb8;
100
+ color: white;
101
+ border-radius: 6px 6px 0 0;
102
+ }
103
+
104
+ .efficiency-leverage-chart .formula-denominator {
105
+ background: #e8904f;
106
+ color: white;
107
+ border-radius: 0 0 6px 6px;
108
+ }
109
+
110
+ .efficiency-leverage-chart .chart-title {
111
+ text-align: center;
112
+ font-size: 14px;
113
+ font-weight: 700;
114
+ margin-top: 16px;
115
+ color: var(--text-color);
116
+ }
117
+
118
+ .efficiency-leverage-chart__header {
119
+ display: flex;
120
+ align-items: center;
121
+ justify-content: center;
122
+ margin-bottom: 20px;
123
+ }
124
+
125
+ .efficiency-leverage-chart__header .legend-bottom {
126
+ display: flex;
127
+ flex-direction: column;
128
+ align-items: center;
129
+ gap: 8px;
130
+ font-size: 12px;
131
+ color: var(--text-color);
132
+ }
133
+
134
+ .efficiency-leverage-chart__header .legend-bottom .legend-title {
135
+ font-size: 12px;
136
+ font-weight: 700;
137
+ color: var(--text-color);
138
+ }
139
+
140
+ .efficiency-leverage-chart__header .legend-bottom .items {
141
+ display: flex;
142
+ flex-wrap: wrap;
143
+ gap: 8px 16px;
144
+ justify-content: center;
145
+ }
146
+
147
+ .efficiency-leverage-chart__header .legend-bottom .item {
148
+ display: inline-flex;
149
+ align-items: center;
150
+ gap: 6px;
151
+ white-space: nowrap;
152
+ font-size: 12px;
153
+ }
154
+
155
+ .efficiency-leverage-chart__header .legend-bottom .swatch {
156
+ width: 14px;
157
+ height: 14px;
158
+ border-radius: 3px;
159
+ border: 1px solid var(--border-color);
160
+ display: inline-block;
161
+ }
162
+ </style>
163
+ <script>
164
+ (() => {
165
+ const ensureD3 = (cb) => {
166
+ if (window.d3 && typeof window.d3.select === 'function') return cb();
167
+ let s = document.getElementById('d3-cdn-script');
168
+ if (!s) {
169
+ s = document.createElement('script');
170
+ s.id = 'd3-cdn-script';
171
+ s.src = 'https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js';
172
+ document.head.appendChild(s);
173
+ }
174
+ const onReady = () => { if (window.d3 && typeof window.d3.select === 'function') cb(); };
175
+ s.addEventListener('load', onReady, { once: true });
176
+ if (window.d3) onReady();
177
+ };
178
+
179
+ const bootstrap = () => {
180
+ const scriptEl = document.currentScript;
181
+ let container = scriptEl ? scriptEl.previousElementSibling : null;
182
+
183
+ if (!(container && container.classList && container.classList.contains('efficiency-leverage-chart'))) {
184
+ let currentEl = scriptEl;
185
+ while (currentEl && currentEl.parentNode) {
186
+ currentEl = currentEl.parentNode;
187
+ const found = currentEl.querySelector && currentEl.querySelector('.efficiency-leverage-chart:not([data-mounted="true"])');
188
+ if (found) {
189
+ container = found;
190
+ break;
191
+ }
192
+ }
193
+
194
+ if (!container) {
195
+ const cs = Array.from(document.querySelectorAll('.efficiency-leverage-chart')).filter(el => !(el.dataset && el.dataset.mounted === 'true'));
196
+ container = cs[0] || null;
197
+ }
198
+ }
199
+
200
+ if (!container) return;
201
+ if (container.dataset) {
202
+ if (container.dataset.mounted === 'true') return;
203
+ container.dataset.mounted = 'true';
204
+ }
205
+
206
+ const d3 = window.d3;
207
+
208
+ // Les deux courbes sont parallèles (même forme) mais décalées horizontalement
209
+ // MoE est plus efficace = atteint la même loss avec moins de FLOPs
210
+
211
+ const denseFlopMarker = 5e22;
212
+ const moeFlopMarker = 1e22;
213
+ const targetLoss = 1.6;
214
+
215
+ // Décalage horizontal en log-space (MoE est ~5x plus efficace)
216
+ const efficiencyRatio = denseFlopMarker / moeFlopMarker; // ~5
217
+
218
+ // Coefficients pour Ling-Dense (bleue)
219
+ // Points: (10^19, 2.65), (5e22, 1.6), (10^25, 1.15)
220
+ const x1_dense = 19, y1_dense = 2.85;
221
+ const x2_dense = Math.log10(5e22), y2_dense = 1.6;
222
+ const x3_dense = 25, y3_dense = 1.15;
223
+
224
+ const denom_dense = (x1_dense - x2_dense) * (x1_dense - x3_dense) * (x2_dense - x3_dense);
225
+ const a_dense = (x3_dense * (y2_dense - y1_dense) + x2_dense * (y1_dense - y3_dense) + x1_dense * (y3_dense - y2_dense)) / denom_dense;
226
+ const b_dense = (x3_dense * x3_dense * (y1_dense - y2_dense) + x2_dense * x2_dense * (y3_dense - y1_dense) + x1_dense * x1_dense * (y2_dense - y3_dense)) / denom_dense;
227
+ const c_dense = (x2_dense * x3_dense * (x2_dense - x3_dense) * y1_dense + x3_dense * x1_dense * (x3_dense - x1_dense) * y2_dense + x1_dense * x2_dense * (x1_dense - x2_dense) * y3_dense) / denom_dense;
228
+
229
+ // Coefficients pour Ling-MoE (orange) - descend plus vite
230
+ // Points: (10^19, 2.5), (1e22, 1.6), (10^25, 1.02)
231
+ const x1_moe = 19.4, y1_moe = 2.5;
232
+ const x2_moe = Math.log10(1e22), y2_moe = 1.6;
233
+ const x3_moe = 25, y3_moe = 1.01;
234
+
235
+ const denom_moe = (x1_moe - x2_moe) * (x1_moe - x3_moe) * (x2_moe - x3_moe);
236
+ const a_moe = (x3_moe * (y2_moe - y1_moe) + x2_moe * (y1_moe - y3_moe) + x1_moe * (y3_moe - y2_moe)) / denom_moe;
237
+ const b_moe = (x3_moe * x3_moe * (y1_moe - y2_moe) + x2_moe * x2_moe * (y3_moe - y1_moe) + x1_moe * x1_moe * (y2_moe - y3_moe)) / denom_moe;
238
+ const c_moe = (x2_moe * x3_moe * (x2_moe - x3_moe) * y1_moe + x3_moe * x1_moe * (x3_moe - x1_moe) * y2_moe + x1_moe * x2_moe * (x1_moe - x2_moe) * y3_moe) / denom_moe;
239
+
240
+ // Génération de courbe Dense
241
+ const generateDenseCurve = (numPoints = 200) => {
242
+ const data = [];
243
+ const logStart = 19;
244
+ const logEnd = 25; // Limité à 10^25
245
+ const step = (logEnd - logStart) / (numPoints - 1);
246
+
247
+ for (let i = 0; i < numPoints; i++) {
248
+ const logFlops = logStart + i * step;
249
+ const flops = Math.pow(10, logFlops);
250
+ const logF = Math.log10(flops);
251
+ const loss = a_dense * logF * logF + b_dense * logF + c_dense;
252
+
253
+ // Ne garder que les points dans les limites du graphique
254
+ if (loss >= 0.9 && loss <= 2.6) {
255
+ data.push({ flops, loss });
256
+ }
257
+ }
258
+ return data;
259
+ };
260
+
261
+ // Génération de courbe MoE (utilise ses propres coefficients)
262
+ const generateMoeCurve = (numPoints = 200) => {
263
+ const data = [];
264
+ const logStart = 19;
265
+ const logEnd = 25; // Limité à 10^25
266
+ const step = (logEnd - logStart) / (numPoints - 1);
267
+
268
+ for (let i = 0; i < numPoints; i++) {
269
+ const logFlops = logStart + i * step;
270
+ const flops = Math.pow(10, logFlops);
271
+ const logF = Math.log10(flops);
272
+ const loss = a_moe * logF * logF + b_moe * logF + c_moe;
273
+
274
+ // Ne garder que les points dans les limites du graphique
275
+ if (loss >= 0.9 && loss <= 2.6) {
276
+ data.push({ flops, loss });
277
+ }
278
+ }
279
+ return data;
280
+ };
281
+
282
+ const lingDenseData = generateDenseCurve();
283
+ const lingMoeData = generateMoeCurve();
284
+
285
+ // Points marqués (calculés avec les fonctions pour être précis)
286
+ const denseMarker = {
287
+ flops: denseFlopMarker,
288
+ loss: targetLoss
289
+ };
290
+ const moeMarker = {
291
+ flops: moeFlopMarker,
292
+ loss: targetLoss
293
+ };
294
+
295
+ // Couleurs
296
+ const denseColor = '#4c8bb8'; // Bleu
297
+ const moeColor = '#e8904f'; // Orange
298
+
299
+ // Dimensions
300
+ const margin = { top: 30, right: 40, bottom: 60, left: 60 };
301
+ let width = 700;
302
+ let height = 420;
303
+
304
+ function render() {
305
+ container.innerHTML = '';
306
+
307
+ // Légende en haut (créée en premier)
308
+ const headerDiv = document.createElement('div');
309
+ headerDiv.className = 'efficiency-leverage-chart__header';
310
+ headerDiv.innerHTML = `
311
+ <div class="legend-bottom">
312
+ <div class="legend-title">Loss Scaling Curve</div>
313
+ <div class="items">
314
+ <span class="item">
315
+ <span class="swatch" style="background: ${denseColor}"></span>
316
+ <span>Ling-Dense</span>
317
+ </span>
318
+ <span class="item">
319
+ <span class="swatch" style="background: ${moeColor}"></span>
320
+ <span>Ling-MoE</span>
321
+ </span>
322
+ </div>
323
+ </div>
324
+ `;
325
+ container.appendChild(headerDiv);
326
+
327
+ const rect = container.getBoundingClientRect();
328
+ width = Math.max(500, Math.round(rect && rect.width ? rect.width : (container.clientWidth || 700)));
329
+ height = Math.max(380, Math.round(width / 1.75));
330
+
331
+ const innerWidth = width - margin.left - margin.right;
332
+ const innerHeight = height - margin.top - margin.bottom;
333
+
334
+ // SVG
335
+ const svg = d3.select(container)
336
+ .append('svg')
337
+ .attr('width', width)
338
+ .attr('height', height);
339
+
340
+ const g = svg.append('g')
341
+ .attr('transform', `translate(${margin.left},${margin.top})`);
342
+
343
+ // Échelles
344
+ const xScale = d3.scaleLog()
345
+ .domain([1e19, 1e25])
346
+ .range([0, innerWidth]);
347
+
348
+ const yScale = d3.scaleLinear()
349
+ .domain([0.9, 2.6])
350
+ .range([innerHeight, 0]);
351
+
352
+ // Grille horizontale
353
+ const yTicks = [1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4];
354
+ g.append('g')
355
+ .attr('class', 'grid')
356
+ .selectAll('line')
357
+ .data(yTicks)
358
+ .join('line')
359
+ .attr('x1', 0)
360
+ .attr('x2', innerWidth)
361
+ .attr('y1', d => yScale(d))
362
+ .attr('y2', d => yScale(d));
363
+
364
+ // Axes
365
+ const xAxis = d3.axisBottom(xScale)
366
+ .tickValues([1e19, 1e20, 1e21, 1e22, 1e23, 1e24]);
367
+
368
+ const yAxis = d3.axisLeft(yScale)
369
+ .tickValues(yTicks)
370
+ .tickFormat(d3.format('.1f'));
371
+
372
+ const xAxisG = g.append('g')
373
+ .attr('class', 'axes')
374
+ .attr('transform', `translate(0,${innerHeight})`)
375
+ .call(xAxis);
376
+
377
+ // Remplacer les labels de l'axe X par des tspans avec exposants
378
+ xAxisG.selectAll('.tick text')
379
+ .text('')
380
+ .each(function (d) {
381
+ const exp = Math.round(Math.log10(d));
382
+ const textEl = d3.select(this);
383
+ textEl.append('tspan').text('10');
384
+ textEl.append('tspan')
385
+ .attr('font-size', '0.7em')
386
+ .attr('dy', '-0.5em')
387
+ .text(exp);
388
+ });
389
+
390
+ g.append('g')
391
+ .attr('class', 'axes')
392
+ .call(yAxis);
393
+
394
+ // Labels des axes
395
+ g.append('text')
396
+ .attr('class', 'axis-label')
397
+ .attr('x', innerWidth / 2)
398
+ .attr('y', innerHeight + 45)
399
+ .attr('text-anchor', 'middle')
400
+ .text('FLOPs');
401
+
402
+ g.append('text')
403
+ .attr('class', 'axis-label')
404
+ .attr('transform', 'rotate(-90)')
405
+ .attr('x', -innerHeight / 2)
406
+ .attr('y', -45)
407
+ .attr('text-anchor', 'middle')
408
+ .text('Loss');
409
+
410
+ // Ligne de référence à 1.6
411
+ g.append('line')
412
+ .attr('class', 'reference-line')
413
+ .attr('x1', 0)
414
+ .attr('x2', innerWidth)
415
+ .attr('y1', yScale(1.6))
416
+ .attr('y2', yScale(1.6));
417
+
418
+ // Générateur de courbe
419
+ const lineGen = d3.line()
420
+ .x(d => xScale(d.flops))
421
+ .y(d => yScale(d.loss))
422
+ .curve(d3.curveMonotoneX);
423
+
424
+ // Courbe Ling-Dense
425
+ g.append('path')
426
+ .datum(lingDenseData)
427
+ .attr('class', 'loss-curve')
428
+ .attr('d', lineGen)
429
+ .attr('stroke', denseColor);
430
+
431
+ // Courbe Ling-MoE
432
+ g.append('path')
433
+ .datum(lingMoeData)
434
+ .attr('class', 'loss-curve')
435
+ .attr('d', lineGen)
436
+ .attr('stroke', moeColor);
437
+
438
+ // Lignes verticales pointillées pour les marqueurs
439
+ g.append('line')
440
+ .attr('class', 'marker-line')
441
+ .attr('x1', xScale(denseMarker.flops))
442
+ .attr('x2', xScale(denseMarker.flops))
443
+ .attr('y1', yScale(denseMarker.loss))
444
+ .attr('y2', innerHeight)
445
+ .attr('stroke', denseColor);
446
+
447
+ g.append('line')
448
+ .attr('class', 'marker-line')
449
+ .attr('x1', xScale(moeMarker.flops))
450
+ .attr('x2', xScale(moeMarker.flops))
451
+ .attr('y1', yScale(moeMarker.loss))
452
+ .attr('y2', innerHeight)
453
+ .attr('stroke', moeColor);
454
+
455
+ // Cercles marqueurs
456
+ g.append('circle')
457
+ .attr('class', 'marker-circle')
458
+ .attr('cx', xScale(denseMarker.flops))
459
+ .attr('cy', yScale(denseMarker.loss))
460
+ .attr('r', 7)
461
+ .attr('stroke', denseColor);
462
+
463
+ g.append('circle')
464
+ .attr('class', 'marker-circle')
465
+ .attr('cx', xScale(moeMarker.flops))
466
+ .attr('cy', yScale(moeMarker.loss))
467
+ .attr('r', 7)
468
+ .attr('stroke', moeColor);
469
+
470
+ // Labels des courbes (avec fond blanc)
471
+ const denseLabel = g.append('text')
472
+ .attr('class', 'curve-label')
473
+ .attr('x', xScale(5e22))
474
+ .attr('y', yScale(1.1))
475
+ .attr('text-anchor', 'middle')
476
+ .attr('fill', denseColor);
477
+ denseLabel.append('tspan').text('C');
478
+ denseLabel.append('tspan')
479
+ .attr('font-size', '9px')
480
+ .attr('dy', '4')
481
+ .text('dense');
482
+
483
+ const moeLabel = g.append('text')
484
+ .attr('class', 'curve-label')
485
+ .attr('x', xScale(1e22))
486
+ .attr('y', yScale(1.1))
487
+ .attr('text-anchor', 'middle')
488
+ .attr('fill', moeColor);
489
+ moeLabel.append('tspan').text('C');
490
+ moeLabel.append('tspan')
491
+ .attr('font-size', '9px')
492
+ .attr('dy', '4')
493
+ .text('moe');
494
+
495
+ // Formule en dessous
496
+ const formulaDiv = document.createElement('div');
497
+ formulaDiv.className = 'formula-container';
498
+ formulaDiv.innerHTML = `
499
+ <div style="display: inline-flex; align-items: center; gap: 12px;">
500
+ <span style="font-style: italic; font-size: 16px;">EL</span>
501
+ <span style="font-size: 16px;">(</span>
502
+ <span style="display: inline-flex; align-items: baseline;">
503
+ <span style="font-style: italic; font-size: 14px;">𝒳</span>
504
+ <span style="font-size: 10px; transform: translateY(3px); display: inline-block;">MoE</span>
505
+ </span>
506
+ <span style="font-size: 16px;">|</span>
507
+ <span style="display: inline-flex; align-items: baseline;">
508
+ <span style="font-style: italic; font-size: 14px;">𝒳</span>
509
+ <span style="font-size: 10px; transform: translateY(3px); display: inline-block;">Dense</span>
510
+ </span>
511
+ <span style="font-size: 16px;">) =</span>
512
+ <div class="formula-fraction">
513
+ <span class="formula-numerator">C<sub>Dense</sub></span>
514
+ <span class="formula-denominator">C<sub>MoE</sub></span>
515
+ </div>
516
+ </div>
517
+ `;
518
+ container.appendChild(formulaDiv);
519
+
520
+ // Titre
521
+ const titleDiv = document.createElement('div');
522
+ titleDiv.className = 'chart-title';
523
+ titleDiv.textContent = 'Definition of Efficiency Leverage (EL)';
524
+ container.appendChild(titleDiv);
525
+ }
526
+
527
+ render();
528
+
529
+ // Responsive
530
+ const ro = window.ResizeObserver ? new ResizeObserver(() => render()) : null;
531
+ if (ro) ro.observe(container);
532
+ };
533
+
534
+ if (document.readyState === 'loading') {
535
+ document.addEventListener('DOMContentLoaded', () => ensureD3(bootstrap), { once: true });
536
+ } else {
537
+ ensureD3(bootstrap);
538
+ }
539
+ })();
540
+ </script>