Skip to content

refactor: Merge cli_utils.classify_model_input into utils.model_input (#1078)#1079

Merged
xieofxie merged 8 commits into
mainfrom
hualxie/merge-classify-model-input
Jul 10, 2026
Merged

refactor: Merge cli_utils.classify_model_input into utils.model_input (#1078)#1079
xieofxie merged 8 commits into
mainfrom
hualxie/merge-classify-model-input

Conversation

@xieofxie

@xieofxie xieofxie commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1078.

The CLI carried two parallel -m/--model classifiers:

  • utils/cli.py — enum ModelInputKind, rich ModelInput (folder metadata), and a classify_model_input that raises click.UsageError on bad input. Used by the command modules.
  • utils/model_input.py — string-literal kinds, leaner ModelInput, a pure classify_model_input (returns invalid, no raising) plus resolve_model_input (Hub download). Used by the library layer (auto.py, engine.py) and inspect's hub check.

inspect even ran both on the same value. This merges them into a single, canonical classifier.

Changes

  • utils/model_input.py is now the single classifier + resolver:
    • ModelInputKind is a str-based Enum, so both kind is ModelInputKind.ONNX_FILE and kind == "hub_onnx" keep working from one type.
    • ModelInput gains folder metadata (is_hf_folder, folder_has_onnx, is_winml_cli_folder) and an error message.
    • classify_model_input is pure (no I/O, never raises): it folds in hub_onnx detection, folder metadata, and the strict path / HF-id validation with friendly messages, returning kind=INVALID + error for bad input.
  • utils/cli.py drops its duplicate enum/dataclass/classifier and re-exports the canonical ones. is_onnx_file_path / normalize_model_arg are unchanged.
  • Command callers (build, config, export, inspect, perf) now raise click.UsageError on kind=INVALID using the classifier's error message. inspect collapses its double-classify into a single call and reports a missing local .onnx as ONNX file not found.

Notes on reconciled semantics

  • A non-.onnx path-shaped value with ≥2 / segments (e.g. org/repo/path) is now INVALID ("path does not exist") rather than being forwarded to the Hub as a doomed hf_id. Library pass-through (resolve_model_input(x).local_path or x) is unaffected for these inputs.
  • The classifier stays existence-independent for the .onnx kind; inspect performs the existence check to surface the friendly "ONNX file not found" message.

All tests/unit/utils and tests/unit/commands suites pass (1140 tests).

The CLI carried two parallel model-input classifiers: a rich, raising one
in utils/cli.py (enum kind + folder metadata) and a pure one in
utils/model_input.py (string kind + hub_onnx detection + resolver).
inspect even ran both on the same value.

Unify them into a single pure classifier in utils/model_input.py:

- ModelInputKind is now a str-based Enum so "kind is ModelInputKind.X" and
  "kind == 'hub_onnx'" both keep working from one type.
- ModelInput gains folder metadata (is_hf_folder, folder_has_onnx,
  is_winml_cli_folder) and an "error" message; it never raises.
- classify_model_input folds in hub_onnx detection, folder metadata, and
  the strict path/HF-id validation with friendly messages.
- cli.py drops its duplicate enum/dataclass/classifier and re-exports the
  canonical ones; is_onnx_file_path/normalize_model_arg unchanged.
- Command callers (build/config/export/inspect/perf) raise
  click.UsageError on kind=INVALID using the error message; inspect
  collapses its double-classify into one call and reports a missing local
  .onnx as "ONNX file not found".
- Tests updated to the pure contract and reconciled semantics.
@xieofxie
xieofxie requested a review from a team as a code owner July 9, 2026 07:49
Use redundant-alias imports so mypy strict (no_implicit_reexport) treats
ModelInput, ModelInputKind, and classify_model_input as re-exported.
Comment thread src/winml/modelkit/utils/cli.py Fixed
@xieofxie xieofxie changed the title Merge cli_utils.classify_model_input into utils.model_input (#1078) refactor: Merge cli_utils.classify_model_input into utils.model_input (#1078) Jul 9, 2026

@DingmaomaoBJTU DingmaomaoBJTU left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving — the classifier consolidation is a clear improvement and the core logic checks out (pure classify_model_input, str-based ModelInputKind, and the resolve_model_input(x).local_path or x library pass-through is genuinely unaffected by the org/repo/path -> INVALID change). Left a few inline notes as non-blocking follow-ups: the INVALID-guard ordering in build/perf/config (path-shaped or -c-less invalid inputs bypass the friendly guard), an inspect DRY nit, and some computed-but-unused folder-metadata fields.

Comment thread src/winml/modelkit/commands/build.py Outdated
Comment thread src/winml/modelkit/commands/inspect.py Outdated
Comment thread src/winml/modelkit/commands/perf.py
Comment thread src/winml/modelkit/utils/model_input.py Outdated
Comment thread tests/unit/utils/test_cli.py Outdated

@DingmaomaoBJTU DingmaomaoBJTU left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed after the fixes in 928f2195e (+ the main merge and export-test hardening in 23167ab8a). Every thread from the previous pass is properly addressed and CI is green — approving.

Verified:

  • build.py (was blocking): classify + INVALID guard moved up right after normalize_model_arg, so INVALID inputs are rejected before both the -c and config-less routing; the late re-classification is gone and routing reuses the up-front model_input. The config-less #553 regression is closed, and the two new tests (test_build_configless_missing_onnx_raises, test_build_configless_invalid_id_raises) exercise exactly that path.
  • perf.py: path-shaped missing .onnx now gets the friendly "ONNX file not found" via an existence re-check (Path is imported at module level); comment updated to match the existence-agnostic classifier.
  • inspect.py: ONNX_FILE/HUB_ONNX collapse is behavior-preserving (existing file still falls through to "not yet supported"; missing file still raises "not found").
  • model_input.py: is_hf_folder/folder_has_onnx/is_winml_cli_folder dropped cleanly — no dangling references in src/ or tests/.
  • tests: classifier coverage consolidated into test_model_input.py; all CI shards (commands/analyze/models/optim/remaining), lint, and CodeQL pass.

@xieofxie
xieofxie merged commit f102649 into main Jul 10, 2026
8 checks passed
@xieofxie
xieofxie deleted the hualxie/merge-classify-model-input branch July 10, 2026 09:15
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.

refactor: merge cli_utils.classify_model_input and src/winml/modelkit/utils/model_input.py

3 participants