refactor: Merge cli_utils.classify_model_input into utils.model_input (#1078)#1079
Conversation
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.
Use redundant-alias imports so mypy strict (no_implicit_reexport) treats ModelInput, ModelInputKind, and classify_model_input as re-exported.
DingmaomaoBJTU
left a comment
There was a problem hiding this comment.
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.
…-check, collapse inspect branch, drop dead folder metadata, move classify tests
DingmaomaoBJTU
left a comment
There was a problem hiding this comment.
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-cand config-less routing; the late re-classification is gone and routing reuses the up-frontmodel_input. The config-less#553regression 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
.onnxnow gets the friendly "ONNX file not found" via an existence re-check (Pathis imported at module level); comment updated to match the existence-agnostic classifier. - inspect.py:
ONNX_FILE/HUB_ONNXcollapse 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_folderdropped cleanly — no dangling references insrc/ortests/. - tests: classifier coverage consolidated into
test_model_input.py; all CI shards (commands/analyze/models/optim/remaining), lint, and CodeQL pass.
Summary
Fixes #1078.
The CLI carried two parallel
-m/--modelclassifiers:utils/cli.py— enumModelInputKind, richModelInput(folder metadata), and aclassify_model_inputthat raisesclick.UsageErroron bad input. Used by the command modules.utils/model_input.py— string-literal kinds, leanerModelInput, a pureclassify_model_input(returnsinvalid, no raising) plusresolve_model_input(Hub download). Used by the library layer (auto.py,engine.py) and inspect's hub check.inspecteven ran both on the same value. This merges them into a single, canonical classifier.Changes
utils/model_input.pyis now the single classifier + resolver:ModelInputKindis astr-basedEnum, so bothkind is ModelInputKind.ONNX_FILEandkind == "hub_onnx"keep working from one type.ModelInputgains folder metadata (is_hf_folder,folder_has_onnx,is_winml_cli_folder) and anerrormessage.classify_model_inputis pure (no I/O, never raises): it folds inhub_onnxdetection, folder metadata, and the strict path / HF-id validation with friendly messages, returningkind=INVALID+errorfor bad input.utils/cli.pydrops its duplicate enum/dataclass/classifier and re-exports the canonical ones.is_onnx_file_path/normalize_model_argare unchanged.build,config,export,inspect,perf) now raiseclick.UsageErroronkind=INVALIDusing the classifier'serrormessage.inspectcollapses its double-classify into a single call and reports a missing local.onnxasONNX file not found.Notes on reconciled semantics
.onnxpath-shaped value with ≥2/segments (e.g.org/repo/path) is nowINVALID("path does not exist") rather than being forwarded to the Hub as a doomedhf_id. Library pass-through (resolve_model_input(x).local_path or x) is unaffected for these inputs..onnxkind;inspectperforms the existence check to surface the friendly "ONNX file not found" message.All
tests/unit/utilsandtests/unit/commandssuites pass (1140 tests).