Skip to content

fix(serve): validate --device via clap possible values (exit 2) - #304

Open
r0x0r wants to merge 1 commit into
mainfrom
fix/serve-device-clap-possible-values
Open

fix(serve): validate --device via clap possible values (exit 2)#304
r0x0r wants to merge 1 commit into
mainfrom
fix/serve-device-clap-possible-values

Conversation

@r0x0r

@r0x0r r0x0r commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

rocm serve --device bogus previously rejected the invalid value via application
logic (exit code 1, message "unsupported device policy") instead of clap's
possible values validation (exit code 2). Every other enum-style argument in
the CLI — --engine, config set-*, engines install, etc. — uses clap
possible values, which produces exit code 2 and lists the valid choices.

This models --device as a clap ValueEnum (DevicePolicyArg) so invalid input
is rejected consistently and the valid choices are listed in the error.

Changes

  • Add DevicePolicyArg (gpu_required, gpu_preferred, cpu_only) and change
    serve --device from Option<String> to Option<DevicePolicyArg>.
  • The historical aliases (auto/gpugpu_required, cpucpu_only) stay
    accepted for backward compatibility but are hidden from the advertised list.
  • The intentional cpu_only app-level rejection (exit 1 with a GPU-required
    message) is preserved — only the generic "unsupported device policy" exit-1
    path is replaced by clap's exit-2 usage error.
  • Reworked the device possible-values sync test to read clap's structural
    possible values instead of a hand-written doc string, mirroring the existing
    --engine test.

Behavior

Before:

$ rocm serve qwen --device bogus
Error: unsupported device policy: bogus      # exit 1

After:

$ rocm serve qwen --device bogus
error: invalid value 'bogus' for '--device <DEVICE>'
  [possible values: gpu_required, gpu_preferred, cpu_only]   # exit 2

Tests

  • serve_rejects_unknown_device_policy_as_usage_error — asserts invalid
    --device is a clap InvalidValue usage error (fails before the fix, passes
    after).
  • serve_accepts_device_policy_values_and_aliases — asserts every value and
    legacy alias still parses to the expected variant.
  • serve_device_help_lists_match_device_policy_names — reworked to verify clap's
    structural possible values stay in sync with DevicePolicy.

cargo test -p rocm --bin rocm, cargo clippy -p rocm --all-targets -- -D warnings, and cargo fmt all pass. No new e2e scenario is added: the change is
a CLI usage-validation/exit-code correction with no new runtime engine behavior,
and it is fully covered by the argument-parsing unit tests above.

Model `rocm serve --device` as a clap `ValueEnum` (`DevicePolicyArg`) so an
invalid value is rejected by clap's usage validation (exit code 2) with the
valid choices listed, instead of parsing as a free-form string and failing
later in application logic with a generic "unsupported device policy" error
(exit code 1).

This makes `--device` consistent with every other enum-style argument in the
CLI (`--engine`, config set-*, engines install, etc.), all of which use clap
possible values. The historical aliases (`auto`/`gpu` for gpu_required, `cpu`
for cpu_only) stay accepted for backward compatibility but are hidden from the
advertised list, and the intentional cpu_only app-level rejection (exit 1 with
a GPU-required message) is preserved.

Adds regression tests asserting invalid `--device` is a clap InvalidValue usage
error and that every value and alias still parses, and reworks the device
possible-values sync test to read clap's structural possible values rather than
a hand-written doc string.

Signed-off-by: Roman Sirokov <roman.sirokov@amd.com>
@r0x0r
r0x0r requested a review from a team as a code owner August 24, 2026 09:13
@r0x0r
r0x0r requested a review from volen-silo August 24, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant