File size: 17,322 Bytes
8fd03ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<div class="throughput-drops-comparison"></div>
<style>
  .throughput-drops-comparison { position: relative; }
  .throughput-drops-comparison .axis-label { fill: var(--text-color); font-size: 12px; font-weight: 700; }
  .throughput-drops-comparison .axes path, .throughput-drops-comparison .axes line { stroke: var(--axis-color); }
  .throughput-drops-comparison .axes text { fill: var(--tick-color); }
  .throughput-drops-comparison .grid line { stroke: var(--grid-color); }
  .throughput-drops-comparison .chart-card { background: var(--surface-bg); border: 1px solid var(--border-color); border-radius: 10px; padding: 8px; }
  .throughput-drops-comparison .chart-header { display:flex; align-items:flex-start; justify-content:flex-start; gap:12px; margin: 8px 0 0 0; flex-wrap: wrap; }
  .throughput-drops-comparison .legend-bottom { display:flex; align-items:center; justify-content:flex-start; font-size:12px; color: var(--text-color); }
  .throughput-drops-comparison .legend-bottom .items { display:flex; flex-wrap:wrap; gap:8px 14px; }
  .throughput-drops-comparison .legend-bottom .item { display:inline-flex; align-items:center; gap:6px; white-space:nowrap; }
  .throughput-drops-comparison .legend-bottom .swatch { width:14px; height:14px; border-radius:3px; border:1px solid var(--border-color); display:inline-block; }
  .throughput-drops-comparison .legend-bottom .legend-title { font-size: 12px; font-weight: 700; color: var(--text-color); }
  .throughput-drops-comparison .legend-bottom { flex-direction: column; align-items: flex-start; gap: 6px; }
  .throughput-drops-comparison .lines path.active { stroke-width: 3; }
  .throughput-drops-comparison .d3-tooltip { z-index: var(--z-elevated); backdrop-filter: saturate(1.12) blur(8px); }
  .throughput-drops-comparison .d3-tooltip__inner { display:flex; flex-direction:column; gap:6px; min-width: 220px; }
  .throughput-drops-comparison .d3-tooltip__inner > div:first-child { font-weight: 800; letter-spacing: 0.1px; margin-bottom: 0; }
  .throughput-drops-comparison .d3-tooltip__inner > div:nth-child(2) { font-size: 11px; color: var(--muted-color); display: block; margin-top: -4px; margin-bottom: 2px; letter-spacing: 0.1px; }
  .throughput-drops-comparison .d3-tooltip__color-dot { display:inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--border-color); }
  /* Ghosting on hover */
  .throughput-drops-comparison.hovering .legend-bottom .item.ghost { opacity: .35; }
  .throughput-drops-comparison.hovering .lines path.ghost { opacity: .25; }
  .throughput-drops-comparison.hovering .points circle.ghost { opacity: .25; }
</style>
<script>
  (() => {
    const ensureD3 = (cb) => {
      if (window.d3 && typeof window.d3.select === 'function') return cb();
      let s = document.getElementById('d3-cdn-script');
      if (!s) { s = document.createElement('script'); s.id = 'd3-cdn-script'; s.src = 'https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js'; document.head.appendChild(s); }
      const onReady = () => { if (window.d3 && typeof window.d3.select === 'function') cb(); };
      s.addEventListener('load', onReady, { once: true }); if (window.d3) onReady();
    };

    const bootstrap = () => {
      const scriptEl = document.currentScript;
      let container = scriptEl ? scriptEl.previousElementSibling : null;
      if (!(container && container.classList && container.classList.contains('throughput-drops-comparison'))){
        const cs = Array.from(document.querySelectorAll('.throughput-drops-comparison')).filter(el => !(el.dataset && el.dataset.mounted === 'true'));
        container = cs[cs.length - 1] || null;
      }
      if (!container) return;
      if (container.dataset) { if (container.dataset.mounted === 'true') return; container.dataset.mounted = 'true'; }

      // Tooltip
      container.style.position = container.style.position || 'relative';
      let tip = container.querySelector('.d3-tooltip'); let tipInner;
      if (!tip) {
        tip = document.createElement('div'); tip.className = 'd3-tooltip';
        Object.assign(tip.style, {
          position:'absolute', top:'0px', left:'0px', transform:'translate(-9999px, -9999px)', pointerEvents:'none',
          padding:'8px 10px', borderRadius:'8px', fontSize:'12px', lineHeight:'1.35', border:'1px solid var(--border-color)',
          background:'var(--surface-bg)', color:'var(--text-color)', boxShadow:'0 4px 24px rgba(0,0,0,.18)', opacity:'0', transition:'opacity .12s ease'
        });
        tipInner = document.createElement('div'); tipInner.className = 'd3-tooltip__inner'; tipInner.style.textAlign='left'; tip.appendChild(tipInner); container.appendChild(tip);
      } else { tipInner = tip.querySelector('.d3-tooltip__inner') || tip; }

      // Header (legend) placed after the chart
      const header = document.createElement('div'); header.className = 'chart-header';
      const legendBottom = document.createElement('div'); legendBottom.className = 'legend-bottom'; header.appendChild(legendBottom);

      // Chart card (SVG)
      const card = document.createElement('div'); card.className = 'chart-card'; container.appendChild(card);
      container.appendChild(header);
      
      // SVG
      const svg = d3.select(card).append('svg').attr('width','100%').style('display','block');
      const gRoot = svg.append('g');
      const gGrid = gRoot.append('g').attr('class','grid');
      const gAxes = gRoot.append('g').attr('class','axes');
      const gLines = gRoot.append('g').attr('class','lines');
      const gPoints = gRoot.append('g').attr('class','points');
      const overlay = gRoot.append('rect').attr('fill','transparent').style('cursor','crosshair');
      const hoverLine = gRoot.append('line').attr('stroke-width',1).style('display','none');

      // State/data
      let width = 800, height = 480; const margin = { top: 16, right: 32, bottom: 44, left: 80 };
      const xScale = d3.scaleLinear();
      const yScale = d3.scaleLinear();
      const lineGen = d3.line().x(d => xScale(d.step)).y(d => yScale(d.value));
      let data = [];

      // Colors - following guidelines to use ColorPalettes
      let currentColors = ['var(--primary-color, #4e79a7)', 'var(--primary-color, #4e79a7)'];
      
      function refreshPalette(){
        try { 
          if (window.ColorPalettes && typeof window.ColorPalettes.getColors === 'function') {
            const colors = window.ColorPalettes.getColors('categorical', 2);
            if (colors && colors.length >= 2) {
              currentColors = colors;
              // Re-render with new colors
              if (data.length > 0) render();
              return;
            }
          }
        } catch(_){}
        // Fallback to CSS variable or default
        currentColors = ['var(--primary-color, #4e79a7)', '#e15759'];
        // Re-render with fallback colors
        if (data.length > 0) render();
      }
      
      function getColors(){
        return currentColors;
      }
      
      // Format helper for thousands (5000 -> 5k, 1500 -> 1.5k)
      function formatK(v){
        const abs = Math.abs(v);
        if (abs >= 1000) {
          const n = v / 1000;
          const s = d3.format('.1f')(n);
          return (s.endsWith('.0') ? s.slice(0, -2) : s) + 'k';
        }
        return d3.format('d')(v);
      }

      // Format helper for throughput values
      function formatThroughput(v){
        if (v >= 1000) {
          return d3.format('.1f')(v / 1000) + 'k';
        }
        return d3.format('.1f')(v);
      }

      function updateLayout(){
        const axisColor = getComputedStyle(container).getPropertyValue('--axis-color').trim() || 'rgba(0,0,0,0.25)';
        width = container.clientWidth || 800;
        height = Math.max(280, Math.round(width / 3));
        svg.attr('width', width).attr('height', height);
        gRoot.attr('transform', `translate(${margin.left},${margin.top})`);
        const innerWidth = width - margin.left - margin.right;
        const innerHeight = height - margin.top - margin.bottom;
        overlay.attr('x',0).attr('y',0).attr('width', innerWidth).attr('height', innerHeight);
        hoverLine.attr('y1',0).attr('y2', innerHeight).attr('stroke', axisColor);
        return { innerWidth, innerHeight };
      }

      function render(){
        if (data.length === 0) return;
        
        const { innerWidth, innerHeight } = updateLayout();
        
        // Sort data by step
        const sortedData = data.slice().sort((a, b) => a.step - b.step);
        
        // Prepare series data
        const series = [
          {
            name: 'Throughput (main run)',
            values: sortedData.map(d => ({ step: d.step, value: d.throughput_drops }))
          },
          {
            name: 'Throughput (ablations)',
            values: sortedData.map(d => ({ step: d.step, value: d.throughput_no_drops }))
          }
        ];
        
        // domains
        const minStep = d3.min(sortedData, d => d.step);
        const maxStep = d3.max(sortedData, d => d.step);
        const minValue = d3.min(sortedData, d => Math.min(d.throughput_drops, d.throughput_no_drops));
        const maxValue = d3.max(sortedData, d => Math.max(d.throughput_drops, d.throughput_no_drops));
        
        xScale.domain([minStep, maxStep]).range([0, innerWidth]);
        yScale.domain([minValue, maxValue]).nice().range([innerHeight, 0]);

        // grid
        gGrid.selectAll('*').remove();
        gGrid.selectAll('line').data(yScale.ticks(6)).join('line')
          .attr('x1',0).attr('x2', innerWidth).attr('y1', d=>yScale(d)).attr('y2', d=>yScale(d))
          .attr('stroke','var(--grid-color)').attr('stroke-width',1).attr('shape-rendering','crispEdges');

        // axes
        gAxes.selectAll('*').remove();
        gAxes.append('g').attr('transform', `translate(0,${innerHeight})`).call(d3.axisBottom(xScale).ticks(8).tickFormat(formatK)).call(g=>{ g.selectAll('path, line').attr('stroke','var(--axis-color)'); g.selectAll('text').attr('fill','var(--tick-color)').style('font-size','12px'); });
        gAxes.append('g').call(d3.axisLeft(yScale).ticks(6).tickFormat(formatThroughput)).call(g=>{ g.selectAll('path, line').attr('stroke','var(--axis-color)'); g.selectAll('text').attr('fill','var(--tick-color)').style('font-size','12px'); });
        gAxes.append('text').attr('class','axis-label').attr('text-anchor','middle').attr('x', innerWidth/2).attr('y', innerHeight + 38).text('Training Step');
        gAxes.append('text').attr('class','axis-label').attr('text-anchor','middle').attr('transform', `translate(${-60}, ${innerHeight/2}) rotate(-90)`).text('Tokens/sec/GPU');

        // lines
        const colors = getColors();
        gLines.selectAll('*').remove();
        series.forEach((s, i) => {
          gLines.append('path')
            .attr('class', `line line-${i}`)
            .attr('data-series', s.name)
            .attr('fill','none')
            .attr('stroke', colors[i % colors.length])
            .attr('stroke-width', 2)
            .attr('d', lineGen(s.values));
        });

        // point markers
        gPoints.selectAll('*').remove();
        series.forEach((s, i) => {
          gPoints.selectAll(`circle.point-${i}`).data(s.values).join('circle')
            .attr('class', `point point-${i}`)
            .attr('data-series', s.name)
            .attr('r', 2)
            .attr('fill', colors[i % colors.length])
            .attr('fill-opacity', 0.6)
            .attr('cx', d=>xScale(d.step))
            .attr('cy', d=>yScale(d.value));
        });

        // legend
        legendBottom.innerHTML = `<div class="legend-title">Legend</div><div class="items">${series.map((s, i) => `<span class="item" data-series="${s.name}"><span class="swatch" style="background:${colors[i % colors.length]}"></span><span>${s.name}</span></span>`).join('')}</div>`;

        // Legend hover → ghost lines/points
        try {
          const legendNode = legendBottom;
          legendNode.querySelectorAll('.item').forEach(el => {
            el.addEventListener('mouseenter', () => {
              const seriesName = el.getAttribute('data-series'); if (!seriesName) return;
              container.classList.add('hovering');
              gLines.selectAll('path.line').classed('ghost', s => s.getAttribute && s.getAttribute('data-series') !== seriesName);
              gPoints.selectAll('circle.point').classed('ghost', p => p.getAttribute && p.getAttribute('data-series') !== seriesName);
              legendNode.querySelectorAll('.item').forEach(it => it.classList.toggle('ghost', it.getAttribute('data-series') !== seriesName));
            });
            el.addEventListener('mouseleave', () => {
              container.classList.remove('hovering');
              gLines.selectAll('path.line').classed('ghost', false);
              gPoints.selectAll('circle.point').classed('ghost', false);
              legendNode.querySelectorAll('.item').forEach(it => it.classList.remove('ghost'));
            });
          });
        } catch {}

        // hover
        function onMove(ev){
          const [mx, my] = d3.pointer(ev, overlay.node());
          const sx = xScale.invert(mx);
          
          // Find nearest step
          const steps = Array.from(new Set(sortedData.map(d => d.step))).sort((a,b) => a - b);
          const nearest = steps.reduce((best, s) => Math.abs(s - sx) < Math.abs(best - sx) ? s : best, steps[0]);
          const xpx = xScale(nearest);
          hoverLine.style('display', null).attr('x1', xpx).attr('x2', xpx);
          
          // Find data point for this step
          const dataPoint = sortedData.find(d => d.step === nearest);
          if (!dataPoint) return;
          
          // tooltip content
          let html = `<div style="font-weight:800;letter-spacing:.1px;">Throughput Comparison</div><div style="font-size:11px;color:var(--muted-color);margin-top:-4px;margin-bottom:2px;">Step ${formatK(nearest)}</div>`;
          
          series.forEach((s, i) => {
            const value = s.name === 'Throughput (main run)' ? dataPoint.throughput_drops : dataPoint.throughput_no_drops;
            html += `<div style="display:flex;align-items:center;gap:6px;white-space:nowrap;"><span class="d3-tooltip__color-dot" style="background:${colors[i % colors.length]}"></span><strong>${s.name}</strong><span style="margin-left:auto;">${formatThroughput(value)}</span></div>`;
          });
          
          tipInner.innerHTML = html; 
          tip.style.opacity = '1'; 
          tip.style.transform = `translate(${Math.round(mx + margin.left + 12)}px, ${Math.round(my + margin.top + 12)}px)`;
        }
        
        function onLeave(){ 
          tip.style.opacity='0'; 
          tip.style.transform='translate(-9999px, -9999px)'; 
          hoverLine.style('display','none'); 
        }
        
        overlay.on('mousemove', onMove).on('mouseleave', onLeave);
      }

      // load CSV and init
      (async () => {
        try {
          // Try multiple possible paths for the CSV file
          const csvPaths = [
            '/data/throughput_drops_comparison_before_after.csv',
            './assets/data/throughput_drops_comparison_before_after.csv',
            '../assets/data/throughput_drops_comparison_before_after.csv',
            '../../assets/data/throughput_drops_comparison_before_after.csv'
          ];
          
          let csvText = null;
          for (const path of csvPaths) {
            try {
              const response = await fetch(path, { cache: 'no-cache' });
              if (response.ok) {
                csvText = await response.text();
                break;
              }
            } catch(_) {}
          }
          
          if (!csvText) {
            throw new Error('CSV file not found: throughput_drops_comparison_before_after.csv');
          }
          
          const rows = d3.csvParse(csvText);
          
          // Parse the data
          data = rows.map(d => ({
            step: +d.Step,
            throughput_drops: +d.throughput_drops,
            throughput_no_drops: +d.throughput_no_drops
          })).filter(d => !isNaN(d.step) && !isNaN(d.throughput_drops) && !isNaN(d.throughput_no_drops));
          
          // Initialize palette and listen for changes
          refreshPalette();
          document.addEventListener('palettes:updated', refreshPalette);
          
          render();
          
          const rerender = () => render();
          if (window.ResizeObserver) { 
            const ro = new ResizeObserver(() => rerender()); 
            ro.observe(container); 
          } else { 
            window.addEventListener('resize', rerender); 
          }
        } catch (e) {
          const pre = document.createElement('pre'); 
          pre.textContent = 'CSV load error: ' + (e && e.message ? e.message : e);
          pre.style.color = 'var(--danger, #b00020)'; 
          pre.style.fontSize = '12px'; 
          pre.style.whiteSpace = 'pre-wrap'; 
          container.appendChild(pre);
        }
      })();
    };

    if (document.readyState === 'loading') { 
      document.addEventListener('DOMContentLoaded', () => ensureD3(bootstrap), { once: true }); 
    } else { 
      ensureD3(bootstrap); 
    }
  })();
</script>