Skip to content
Prev Previous commit
Next Next commit
Clarify benchmark input preparation stages
  • Loading branch information
csadorf committed Nov 10, 2026
commit fa414eceee72dcd89b6960c7842f056b78d62990
70 changes: 25 additions & 45 deletions python/cuml/cuml/benchmark/harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,6 @@ class _PreparedCase:
fit_args: tuple[Any, ...] | None


def _prepare_input_groups(
backend: Backend,
*groups: tuple[Any, ...],
runtime: Any = None,
) -> tuple[tuple[Any, ...], ...]:
"""Prepare inputs in one backend call, preserving their logical groups."""
prepared = iter(
backend.prepare_inputs(
tuple(value for group in groups for value in group), runtime
)
)
return tuple(tuple(next(prepared) for _ in group) for group in groups)


def _prepare_case(
backend: Backend,
suite: Suite,
Expand All @@ -149,54 +135,48 @@ def _prepare_case(
) -> _PreparedCase:
"""Partition host data, then convert and materialize only required inputs."""
estimator_class = backend.load_estimator(spec)
provider = suite.provider_spec

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes two sources for the backend in _prepare_case. Conversion goes through suite.provider_spec.backend, while load_estimator and prepare_inputs use the backend argument. In production these are the same object (see _benchmark_case), but nothing enforces it.

The new tests have to patch the provider's backend and swap the entry in PROVIDERS to keep them consistent. Could _prepare_case take the backend from provider.backend (or receive the provider instead of the backend) so there is only one?


# Generate and partition host data.
X, y = generate_data(case)
is_fit = case.lifecycle == "fit"
if not is_fit:
if is_fit:
measured = {"X": X, "y": y}
else:
split = case.training_rows
train = {"X": X[:split], "y": y[:split]}
measured = {"X": X[split:], "y": y[split:]}
else:
train = measured = {"X": X, "y": y}

representations = {
"X": "csr"
# Convert only selected inputs, applying provider-specific requirements.
x_representation = (
"csr"
if case.input_format == "csr"
else "dataframe"
if case.dataset == "categorical"
else "array",
"y": "array",
}
provider = suite.provider_spec
operation_data = train if is_fit else measured
operation_args = tuple(
provider.convert_data(
operation_data[name],
estimator=case.estimator,
role=name,
representation=representations[name],
)
for name in case.input_selection
else "array"
)
fit_args = (
()
if is_fit
else tuple(

def convert(inputs, selection):
return tuple(
provider.convert_data(
train[name],
inputs[name],
estimator=case.estimator,
role=name,
representation=representations[name],
representation=x_representation if name == "X" else "array",
)
for name in case.fit_input_selection
for name in selection
)
)
operation_args, fit_args = _prepare_input_groups(
backend, operation_args, fit_args, runtime=client
)

operation_args = convert(measured, case.input_selection)
fit_args = () if is_fit else convert(train, case.fit_input_selection)

# Materialize both groups together, then restore their boundary.
prepared = backend.prepare_inputs(operation_args + fit_args, client)
boundary = len(operation_args)
return _PreparedCase(
estimator_class=estimator_class,
operation_args=operation_args,
fit_args=None if is_fit else fit_args,
operation_args=prepared[:boundary],
fit_args=None if is_fit else prepared[boundary:],
)


Expand Down
50 changes: 23 additions & 27 deletions python/cuml/tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,15 @@ def convert_data(self, value, *, estimator, role, representation):
return value + 1

provider = get_provider("scikit-learn")
preparation_calls = []
client = object()

def prepare_inputs(inputs, runtime):
assert runtime is client
preparation_calls.append(inputs)
return tuple(value + 10 for value in inputs)

monkeypatch.setattr(provider.backend, "prepare_inputs", prepare_inputs)
monkeypatch.setitem(
PROVIDERS,
"scikit-learn",
Expand All @@ -3161,19 +3170,28 @@ def convert_data(self, value, *, estimator, role, representation):
provider.backend, "load_estimator", lambda spec: object
)
prepared = harness._prepare_case(
provider.backend, _suite(case), case, provider.estimator_spec("PCA")
)
provider.backend,
_suite(case),
case,
provider.estimator_spec("PCA"),
client,
)
assert prepared.estimator_class is object
assert len(preparation_calls) == 1
assert len(preparation_calls[0]) == (3 if inference else 1)
for actual, (_, original) in zip(preparation_calls[0], calls, strict=True):
np.testing.assert_array_equal(actual, original + 1)
assert [role for role, _ in calls] == (
["X", "X", "y"] if inference else ["X"]
)
measured_X = X[32:] if inference else X
np.testing.assert_array_equal(calls[0][1], measured_X)
np.testing.assert_array_equal(prepared.operation_args[0], measured_X + 1)
np.testing.assert_array_equal(prepared.operation_args[0], measured_X + 11)
if inference:
np.testing.assert_array_equal(calls[1][1], X[:32])
np.testing.assert_array_equal(calls[2][1], y[:32])
np.testing.assert_array_equal(prepared.fit_args[0], X[:32] + 1)
np.testing.assert_array_equal(prepared.fit_args[1], y[:32] + 1)
np.testing.assert_array_equal(prepared.fit_args[0], X[:32] + 11)
np.testing.assert_array_equal(prepared.fit_args[1], y[:32] + 11)
else:
assert prepared.fit_args is None

Expand Down Expand Up @@ -3214,28 +3232,6 @@ def convert(value, *, representation):
assert calls == [(value, representation)]


@pytest.mark.parametrize(
"groups", [(), ((),), ((1, 2), (), (3,)), ((), (1,), ())]
)
def test_prepare_input_groups_preserves_structure(groups):
calls = []
runtime = object()

class Backend:
def prepare_inputs(self, inputs, client):
assert client is runtime
calls.append(inputs)
return tuple(value * 10 for value in inputs)

prepared = harness._prepare_input_groups(
Backend(), *groups, runtime=runtime
)
assert calls == [tuple(value for group in groups for value in group)]
assert prepared == tuple(
tuple(value * 10 for value in group) for group in groups
)


@pytest.mark.parametrize("shape", [(64, 8), (64,)])
def test_native_conversion_preserves_shape_and_values(shape):
host = np.ones(shape)
Expand Down
Loading