From ec8e85f40d36b21bf5993b3e3b7a397aee7fafa3 Mon Sep 17 00:00:00 2001 From: Bihan Rana Date: Tue, 24 Nov 2026 12:41:10 +0545 Subject: [PATCH 1/2] Support Concurrency Sweep in Web UI --- frontend/src/components/index.ts | 1 + frontend/src/libs/presets.ts | 89 ++++++++++++------- frontend/src/locale/en.json | 2 + .../pages/Presets/Details/Benchmark/index.tsx | 85 +++++++++++++----- .../Presets/Details/Constraints/index.tsx | 13 ++- frontend/src/types/preset.d.ts | 9 +- 6 files changed, 143 insertions(+), 56 deletions(-) diff --git a/frontend/src/components/index.ts b/frontend/src/components/index.ts index 288f810065..5c4dbb5a00 100644 --- a/frontend/src/components/index.ts +++ b/frontend/src/components/index.ts @@ -57,6 +57,7 @@ export type { ChatBubbleProps } from '@cloudscape-design/chat-components/chat-bu export { default as Avatar } from '@cloudscape-design/chat-components/avatar'; export type { AvatarProps } from '@cloudscape-design/chat-components/avatar'; export { default as LineChart } from '@cloudscape-design/components/line-chart'; +export { default as BarChart } from '@cloudscape-design/components/bar-chart'; export { default as PropertyFilter } from '@cloudscape-design/components/property-filter'; export type { PropertyFilterProps } from '@cloudscape-design/components/property-filter'; export { default as DateRangePicker } from '@cloudscape-design/components/date-range-picker'; diff --git a/frontend/src/libs/presets.ts b/frontend/src/libs/presets.ts index 0153fb2a83..a3c8ab3028 100644 --- a/frontend/src/libs/presets.ts +++ b/frontend/src/libs/presets.ts @@ -24,6 +24,13 @@ export const formatTokenCount = (value: number): string => { return String(value); }; +/** As the CLI prints a sweep: one value when they agree, otherwise `min..max`. */ +export const formatTokenRange = (values: number[]): string => { + const low = Math.min(...values); + const high = Math.max(...values); + return low === high ? formatTokenCount(low) : `${formatTokenCount(low)}..${formatTokenCount(high)}`; +}; + const trimZero = (value: string): string => (value.endsWith('.0') ? value.slice(0, -2) : value); /** As the CLI prints durations: 999.6 reads as 1s rather than 1000ms. */ @@ -45,50 +52,72 @@ const HIDDEN_METRICS = new Set([ ]); const METRIC_LABELS: Record = { - output_tok_per_s: 'TPS', - per_user_tok_per_s: 'TPS/user', + output_tok_per_s: 'tok/s', + per_user_tok_per_s: 'tok/s/user', total_input_tokens: 'Input tokens', total_output_tokens: 'Output tokens', ttft_ms: 'TTFT', tpot_ms: 'TPOT', }; +/** The tab a metric is shown under. A metric not listed here gets a tab of its own. */ +const METRIC_GROUPS: Record = { + output_tok_per_s: 'tok/s', + per_user_tok_per_s: 'tok/s', + ttft_ms: 'TTFT', + tpot_ms: 'TPOT', +}; + const TOKEN_COUNT_METRICS = new Set(['total_input_tokens', 'total_output_tokens']); -export type BenchmarkMetric = { label: string; value: string }; +export type BenchmarkChart = { + group: string; + title: string; + formatValue: (value: number) => string; + data: { x: string; y: number }[]; +}; -const formatMetricValue = (key: string, value: number): string => { - if (TOKEN_COUNT_METRICS.has(key)) return formatTokenCount(value); - if (key.endsWith('_ms')) return formatDurationMs(value); - return round(value); +const getMetricFormatter = (key: string): ((value: number) => string) => { + if (TOKEN_COUNT_METRICS.has(key)) return formatTokenCount; + if (key.endsWith('_ms')) return formatDurationMs; + return round; }; +export const sortResults = (results: IPresetResult[]): IPresetResult[] => + [...results].sort((a, b) => a.concurrency - b.concurrency); + /** - * The metrics worth showing, flattened: a metric measured as a distribution - * becomes one entry per statistic. + * One chart per metric worth showing, each with a value per concurrency. A + * metric measured as a distribution becomes one chart per statistic. A + * concurrency without a benchmark has no point. */ -export const getBenchmarkMetrics = (metrics: HashMap): BenchmarkMetric[] => { - const entries: BenchmarkMetric[] = []; - - Object.entries(metrics ?? {}).forEach(([key, value]) => { - if (HIDDEN_METRICS.has(key)) return; - const label = METRIC_LABELS[key] ?? key; - - if (typeof value === 'number') { - entries.push({ label, value: formatMetricValue(key, value) }); - return; - } - - if (value && typeof value === 'object') { - Object.entries(value as HashMap).forEach(([statistic, statisticValue]) => { - if (typeof statisticValue !== 'number') return; - entries.push({ - label: `${label} ${statistic}`, - value: formatMetricValue(key, statisticValue), - }); - }); +export const getBenchmarkCharts = (results: IPresetResult[]): BenchmarkChart[] => { + const charts = new Map(); + + const addPoint = (title: string, key: string, concurrency: number, value: unknown) => { + if (typeof value !== 'number') return; + if (!charts.has(title)) { + const group = METRIC_GROUPS[key] ?? METRIC_LABELS[key] ?? key; + charts.set(title, { group, title, formatValue: getMetricFormatter(key), data: [] }); } + charts.get(title)?.data.push({ x: String(concurrency), y: value }); + }; + + sortResults(results).forEach(({ concurrency, benchmark }) => { + Object.entries(benchmark?.metrics ?? {}).forEach(([key, value]) => { + if (HIDDEN_METRICS.has(key)) return; + const label = METRIC_LABELS[key] ?? key; + + if (value && typeof value === 'object') { + Object.entries(value as HashMap).forEach(([statistic, statisticValue]) => + addPoint(`${label} ${statistic}`, key, concurrency, statisticValue), + ); + return; + } + + addPoint(label, key, concurrency, value); + }); }); - return entries; + return Array.from(charts.values()); }; diff --git a/frontend/src/locale/en.json b/frontend/src/locale/en.json index 78f95503ea..a94944125b 100644 --- a/frontend/src/locale/en.json +++ b/frontend/src/locale/en.json @@ -911,6 +911,8 @@ "no_cli_description": "To use dstack, install the CLI on your local machine.", "constraints": "Constraints", "concurrency": "Concurrency", + "metric": "Metric", + "no_benchmark_data": "No data", "dataset": "Dataset", "input_tokens": "Input tokens", "output_tokens": "Output tokens", diff --git a/frontend/src/pages/Presets/Details/Benchmark/index.tsx b/frontend/src/pages/Presets/Details/Benchmark/index.tsx index fd0e18648c..31ffe81c93 100644 --- a/frontend/src/pages/Presets/Details/Benchmark/index.tsx +++ b/frontend/src/pages/Presets/Details/Benchmark/index.tsx @@ -1,10 +1,10 @@ -import React, { FC } from 'react'; +import React, { FC, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useParams } from 'react-router-dom'; -import { Box, ColumnLayout, Container, Header, Loader } from 'components'; +import { BarChart, Box, Container, Header, Loader, SelectCSD, Tabs } from 'components'; -import { formatTokenCount, getBenchmarkMetrics } from 'libs/presets'; +import { BenchmarkChart, formatTokenCount, getBenchmarkCharts, sortResults } from 'libs/presets'; import { useGetPresetQuery } from 'services/preset'; export const PresetBenchmark: FC = () => { @@ -12,6 +12,9 @@ export const PresetBenchmark: FC = () => { const params = useParams(); const paramProjectName = params.projectName ?? ''; const paramPresetId = params.presetId ?? ''; + const [activeGroup, setActiveGroup] = useState(null); + // Each tab remembers the metric last picked in it. + const [metricByGroup, setMetricByGroup] = useState>({}); const { data, isLoading } = useGetPresetQuery({ project_name: paramProjectName, @@ -25,26 +28,68 @@ export const PresetBenchmark: FC = () => { ); - const metrics = getBenchmarkMetrics(data.spec.preset.benchmark.metrics as HashMap); + const results = sortResults(data.spec.preset.result); + const concurrencies = results.map(({ concurrency }) => String(concurrency)); + + // Context length is not a measured metric, so it gets a tab of its own. + const contextLengthChart: BenchmarkChart = { + group: t('presets.context_length'), + title: t('presets.context_length'), + formatValue: formatTokenCount, + data: results + .filter(({ context_length }) => typeof context_length === 'number') + .map(({ concurrency, context_length }) => ({ x: String(concurrency), y: context_length as number })), + }; + + const charts = [...getBenchmarkCharts(results), contextLengthChart]; + const groups = Array.from(new Set(charts.map(({ group }) => group))); + const group = activeGroup ?? groups[0]; + const groupCharts = charts.filter((chart) => chart.group === group); + const chart = groupCharts.find(({ title }) => title === metricByGroup[group]) ?? groupCharts[0]; + + // Only the open tab's chart is drawn, so the others carry no content. + const tabs = groups.map((id) => ({ + id, + label: id, + content: id === group && ( + {t('presets.no_benchmark_data')}} + /> + ), + })); return ( {t('presets.benchmark')}}> - -
- {t('presets.context_length')} -
{formatTokenCount(data.spec.preset.context_length)}
-
-
- {t('presets.concurrency')} -
{String((data.spec.preset.benchmark.workload as HashMap)?.concurrency)}
-
- {metrics.map(({ label, value }) => ( -
- {label} -
{value}
-
- ))} -
+ setActiveGroup(detail.activeTabId)} + // A tab with a single metric has nothing to pick. + actions={ + groupCharts.length > 1 && ( + ({ label: title, value: title }))} + onChange={({ detail }) => + setMetricByGroup((current) => ({ + ...current, + [group]: detail.selectedOption.value ?? chart.title, + })) + } + ariaLabel={t('presets.metric')} + /> + ) + } + />
); }; diff --git a/frontend/src/pages/Presets/Details/Constraints/index.tsx b/frontend/src/pages/Presets/Details/Constraints/index.tsx index 75450fea72..b329a81883 100644 --- a/frontend/src/pages/Presets/Details/Constraints/index.tsx +++ b/frontend/src/pages/Presets/Details/Constraints/index.tsx @@ -4,7 +4,7 @@ import { useParams } from 'react-router-dom'; import { Box, ColumnLayout, Container, Header, Loader } from 'components'; -import { formatTokenCount } from 'libs/presets'; +import { formatTokenCount, formatTokenRange } from 'libs/presets'; import { useGetPresetQuery } from 'services/preset'; export const PresetConstraints: FC = () => { @@ -27,7 +27,12 @@ export const PresetConstraints: FC = () => { // The conditions the benchmark holds for: the workload it measured and the // context the service was verified to serve. - const workload = (data.spec.preset.benchmark.workload ?? {}) as HashMap; + // The dataset and shared prefix are the same across concurrencies, so the + // first benchmarked result stands for all of them. The measured token + // averages can differ, so those show as ranges. + const results = data.spec.preset.result; + const workloads = results.flatMap(({ benchmark }) => (benchmark ? [benchmark.workload as HashMap] : [])); + const workload = workloads[0] ?? {}; const dataset = workload.dataset as string | undefined; const inputTokens = workload.input_tokens as number; const sharedPrefix = (workload.shared_prefix_tokens as number) ?? 0; @@ -43,11 +48,11 @@ export const PresetConstraints: FC = () => { )}
{t('presets.input_tokens')} -
{formatTokenCount(inputTokens)}
+
{formatTokenRange(workloads.map((w) => w.input_tokens as number))}
{t('presets.output_tokens')} -
{formatTokenCount(workload.output_tokens as number)}
+
{formatTokenRange(workloads.map((w) => w.output_tokens as number))}
{sharedPrefix > 0 && (
diff --git a/frontend/src/types/preset.d.ts b/frontend/src/types/preset.d.ts index c7063d59ed..a7bb1e76f2 100644 --- a/frontend/src/types/preset.d.ts +++ b/frontend/src/types/preset.d.ts @@ -21,13 +21,18 @@ declare interface IPresetVerificationReplicaGroup { replicas: HashMap[]; } +declare interface IPresetResult { + concurrency: number; + context_length?: number | null; + benchmark?: IPresetBenchmark | null; +} + declare interface IPresetSpec { preset: { base: string; repo: string; - context_length: number; service: HashMap; - benchmark: IPresetBenchmark; + result: IPresetResult[]; verified_on: IPresetVerificationReplicaGroup[]; }; file_archives: { id: string; path: string }[]; From 7a3cc9724d3fb57c20df9464bbec257d5145b9cf Mon Sep 17 00:00:00 2001 From: Bihan Rana Date: Tue, 24 Nov 2026 15:43:32 +0545 Subject: [PATCH 2/2] Update Support Concurrency Sweep in Web UI --- frontend/src/libs/presets.ts | 79 +--------------- .../Presets/Details/Benchmark/helpers.ts | 93 +++++++++++++++++++ .../pages/Presets/Details/Benchmark/index.tsx | 22 +---- .../Details/Benchmark/useBenchmarkCharts.ts | 19 ++++ 4 files changed, 117 insertions(+), 96 deletions(-) create mode 100644 frontend/src/pages/Presets/Details/Benchmark/helpers.ts create mode 100644 frontend/src/pages/Presets/Details/Benchmark/useBenchmarkCharts.ts diff --git a/frontend/src/libs/presets.ts b/frontend/src/libs/presets.ts index a3c8ab3028..9bc3d7a8ca 100644 --- a/frontend/src/libs/presets.ts +++ b/frontend/src/libs/presets.ts @@ -38,86 +38,11 @@ const formatDurationMs = (value: number): string => (value < 999.5 ? `${round(va const round = (value: number): string => String(Math.round(value * 100) / 100); -/** - * Request counts, wall time, and token totals say nothing about how the preset - * performs: the totals are the workload multiplied by the request count, which - * the constraints already state. - */ -const HIDDEN_METRICS = new Set([ - 'successful_requests', - 'failed_requests', - 'duration_seconds', - 'total_input_tokens', - 'total_output_tokens', -]); - -const METRIC_LABELS: Record = { - output_tok_per_s: 'tok/s', - per_user_tok_per_s: 'tok/s/user', - total_input_tokens: 'Input tokens', - total_output_tokens: 'Output tokens', - ttft_ms: 'TTFT', - tpot_ms: 'TPOT', -}; - -/** The tab a metric is shown under. A metric not listed here gets a tab of its own. */ -const METRIC_GROUPS: Record = { - output_tok_per_s: 'tok/s', - per_user_tok_per_s: 'tok/s', - ttft_ms: 'TTFT', - tpot_ms: 'TPOT', -}; - const TOKEN_COUNT_METRICS = new Set(['total_input_tokens', 'total_output_tokens']); -export type BenchmarkChart = { - group: string; - title: string; - formatValue: (value: number) => string; - data: { x: string; y: number }[]; -}; - -const getMetricFormatter = (key: string): ((value: number) => string) => { +/** How a benchmark metric's values are printed, picked by the metric's name. */ +export const getMetricFormatter = (key: string): ((value: number) => string) => { if (TOKEN_COUNT_METRICS.has(key)) return formatTokenCount; if (key.endsWith('_ms')) return formatDurationMs; return round; }; - -export const sortResults = (results: IPresetResult[]): IPresetResult[] => - [...results].sort((a, b) => a.concurrency - b.concurrency); - -/** - * One chart per metric worth showing, each with a value per concurrency. A - * metric measured as a distribution becomes one chart per statistic. A - * concurrency without a benchmark has no point. - */ -export const getBenchmarkCharts = (results: IPresetResult[]): BenchmarkChart[] => { - const charts = new Map(); - - const addPoint = (title: string, key: string, concurrency: number, value: unknown) => { - if (typeof value !== 'number') return; - if (!charts.has(title)) { - const group = METRIC_GROUPS[key] ?? METRIC_LABELS[key] ?? key; - charts.set(title, { group, title, formatValue: getMetricFormatter(key), data: [] }); - } - charts.get(title)?.data.push({ x: String(concurrency), y: value }); - }; - - sortResults(results).forEach(({ concurrency, benchmark }) => { - Object.entries(benchmark?.metrics ?? {}).forEach(([key, value]) => { - if (HIDDEN_METRICS.has(key)) return; - const label = METRIC_LABELS[key] ?? key; - - if (value && typeof value === 'object') { - Object.entries(value as HashMap).forEach(([statistic, statisticValue]) => - addPoint(`${label} ${statistic}`, key, concurrency, statisticValue), - ); - return; - } - - addPoint(label, key, concurrency, value); - }); - }); - - return Array.from(charts.values()); -}; diff --git a/frontend/src/pages/Presets/Details/Benchmark/helpers.ts b/frontend/src/pages/Presets/Details/Benchmark/helpers.ts new file mode 100644 index 0000000000..7523033dc9 --- /dev/null +++ b/frontend/src/pages/Presets/Details/Benchmark/helpers.ts @@ -0,0 +1,93 @@ +import { formatTokenCount, getMetricFormatter } from 'libs/presets'; + +export type BenchmarkChart = { + group: string; + title: string; + formatValue: (value: number) => string; + data: { x: string; y: number }[]; +}; + +/** + * Request counts, wall time, and token totals say nothing about how the preset + * performs: the totals are the workload multiplied by the request count, which + * the constraints already state. + */ +const HIDDEN_METRICS = new Set([ + 'successful_requests', + 'failed_requests', + 'duration_seconds', + 'total_input_tokens', + 'total_output_tokens', +]); + +const METRIC_LABELS: Record = { + output_tok_per_s: 'tok/s', + per_user_tok_per_s: 'tok/s/user', + total_input_tokens: 'Input tokens', + total_output_tokens: 'Output tokens', + ttft_ms: 'TTFT', + tpot_ms: 'TPOT', +}; + +/** The tab a metric is shown under. A metric not listed here gets a tab of its own. */ +const METRIC_GROUPS: Record = { + output_tok_per_s: 'tok/s', + per_user_tok_per_s: 'tok/s', + ttft_ms: 'TTFT', + tpot_ms: 'TPOT', +}; + +/** The x axis: every concurrency of the sweep, in ascending order. */ +export const getConcurrencies = (results: IPresetResult[]): string[] => + sortResults(results).map(({ concurrency }) => String(concurrency)); + +/** + * One chart per metric worth showing, each with a value per concurrency, and + * one for the context length. A metric measured as a distribution becomes one + * chart per statistic. A concurrency without a benchmark has no point. + */ +export const getBenchmarkCharts = (results: IPresetResult[], contextLengthLabel: string): BenchmarkChart[] => { + const charts = new Map(); + const sorted = sortResults(results); + + const addPoint = (title: string, key: string, concurrency: number, value: unknown) => { + if (typeof value !== 'number') return; + if (!charts.has(title)) { + const group = METRIC_GROUPS[key] ?? METRIC_LABELS[key] ?? key; + charts.set(title, { group, title, formatValue: getMetricFormatter(key), data: [] }); + } + charts.get(title)?.data.push({ x: String(concurrency), y: value }); + }; + + sorted.forEach(({ concurrency, benchmark }) => { + Object.entries(benchmark?.metrics ?? {}).forEach(([key, value]) => { + if (HIDDEN_METRICS.has(key)) return; + const label = METRIC_LABELS[key] ?? key; + + if (value && typeof value === 'object') { + Object.entries(value as HashMap).forEach(([statistic, statisticValue]) => + addPoint(`${label} ${statistic}`, key, concurrency, statisticValue), + ); + return; + } + + addPoint(label, key, concurrency, value); + }); + }); + + const contextLengthChart: BenchmarkChart = { + group: contextLengthLabel, + title: contextLengthLabel, + formatValue: formatTokenCount, + data: sorted.flatMap(({ concurrency, context_length }) => + typeof context_length === 'number' ? [{ x: String(concurrency), y: context_length }] : [], + ), + }; + + return [...Array.from(charts.values()), contextLengthChart]; +}; + +/** The tabs, in the order their first chart appears. */ +export const getChartGroups = (charts: BenchmarkChart[]): string[] => Array.from(new Set(charts.map(({ group }) => group))); + +const sortResults = (results: IPresetResult[]): IPresetResult[] => [...results].sort((a, b) => a.concurrency - b.concurrency); diff --git a/frontend/src/pages/Presets/Details/Benchmark/index.tsx b/frontend/src/pages/Presets/Details/Benchmark/index.tsx index 31ffe81c93..a22ee23f52 100644 --- a/frontend/src/pages/Presets/Details/Benchmark/index.tsx +++ b/frontend/src/pages/Presets/Details/Benchmark/index.tsx @@ -4,8 +4,7 @@ import { useParams } from 'react-router-dom'; import { BarChart, Box, Container, Header, Loader, SelectCSD, Tabs } from 'components'; -import { BenchmarkChart, formatTokenCount, getBenchmarkCharts, sortResults } from 'libs/presets'; -import { useGetPresetQuery } from 'services/preset'; +import { useBenchmarkCharts } from './useBenchmarkCharts'; export const PresetBenchmark: FC = () => { const { t } = useTranslation(); @@ -16,33 +15,18 @@ export const PresetBenchmark: FC = () => { // Each tab remembers the metric last picked in it. const [metricByGroup, setMetricByGroup] = useState>({}); - const { data, isLoading } = useGetPresetQuery({ + const { isLoading, concurrencies, charts, groups } = useBenchmarkCharts({ project_name: paramProjectName, id: paramPresetId, }); - if (isLoading || !data) + if (isLoading) return ( ); - const results = sortResults(data.spec.preset.result); - const concurrencies = results.map(({ concurrency }) => String(concurrency)); - - // Context length is not a measured metric, so it gets a tab of its own. - const contextLengthChart: BenchmarkChart = { - group: t('presets.context_length'), - title: t('presets.context_length'), - formatValue: formatTokenCount, - data: results - .filter(({ context_length }) => typeof context_length === 'number') - .map(({ concurrency, context_length }) => ({ x: String(concurrency), y: context_length as number })), - }; - - const charts = [...getBenchmarkCharts(results), contextLengthChart]; - const groups = Array.from(new Set(charts.map(({ group }) => group))); const group = activeGroup ?? groups[0]; const groupCharts = charts.filter((chart) => chart.group === group); const chart = groupCharts.find(({ title }) => title === metricByGroup[group]) ?? groupCharts[0]; diff --git a/frontend/src/pages/Presets/Details/Benchmark/useBenchmarkCharts.ts b/frontend/src/pages/Presets/Details/Benchmark/useBenchmarkCharts.ts new file mode 100644 index 0000000000..25b3dfcabc --- /dev/null +++ b/frontend/src/pages/Presets/Details/Benchmark/useBenchmarkCharts.ts @@ -0,0 +1,19 @@ +import { useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; + +import { useGetPresetQuery } from 'services/preset'; + +import { getBenchmarkCharts, getChartGroups, getConcurrencies } from './helpers'; + +export const useBenchmarkCharts = (params: { project_name: string; id: string }) => { + const { t } = useTranslation(); + const { data, isLoading } = useGetPresetQuery(params); + + const concurrencies = useMemo(() => getConcurrencies(data?.spec.preset.result ?? []), [data]); + + const charts = useMemo(() => getBenchmarkCharts(data?.spec.preset.result ?? [], t('presets.context_length')), [data, t]); + + const groups = useMemo(() => getChartGroups(charts), [charts]); + + return { isLoading: isLoading || !data, concurrencies, charts, groups }; +};