Skip to content

feat(compile): add CGIR to CG Output IR compiler mode - #1467

Open
fangyangci wants to merge 4 commits into
mainfrom
fangyangci/cgc-input-output-ir-compile
Open

fangyangci wants to merge 4 commits into
mainfrom
fangyangci/cgc-input-output-ir-compile

Conversation

@fangyangci

@fangyangci fangyangci commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a CGIR to device-targeted CG Output IR compiler mode to winml compile, while preserving the existing ONNX-to-EPContext path.

winml compile `
  -m input.mlir `
  --compiler cgc `
  --device gpu `
  -o output.mlir

--compiler cgc is optional for a single .mlir or .mlirbc input; the CLI infers the CGC compiler from those suffixes. It remains available to select the compiler explicitly or for CGIR files with another suffix.

  • Route persistent CGIR compilation through the session-backed WinMLRuntimeSession.compile_to_file() API.
  • Share Runtime compiler/options ownership with the existing ONNX-to-CGC session path.
  • Select a GPU through DXCore, with explicit --device-luid binding when needed, without requiring an ORT execution provider.
  • Call the Runtime target's ModelCompiler.compile_to_file() with CompiledModelForm.DEVICE_TARGETED.
  • Reject incompatible EP/config/embedding options instead of ignoring them.
  • Publish the compiled artifact, Runtime resource companions, validated I/O metadata, and a hash-bound compile receipt with overwrite protection and rollback.
  • Remove stale destination metadata when an overwrite no longer produces metadata, while preserving rollback safety.
  • Update compile command documentation and CGC onboarding examples.

Output and validation semantics

The output is MLIR bytecode, including when the filename uses .mlir; .mlirbc is also accepted. This change does not add a text-dump option.

Default validation reloads the output, builds a Runtime pipeline, and compares I/O schemas through the existing stage-schema compatibility helper. Dynamic dimensions reported as either 0 or UINT64_MAX are normalized before comparison. Validation does not run inference, certify numerical accuracy, or execute a benchmark. --no-validate skips that reload/build check, not compilation.

The new mode currently supports GPU only. Existing ort, ort_session, and qairt compilation behavior remains unchanged. A compatible preview Windows ML Runtime is required.

Verification

  • 188 directly related unit tests passed across compile CLI/compiler, Runtime session, CGC export, and Foundry coverage.
  • mypy -p winml.modelkit passed for all 464 source files.
  • Changed-file Ruff checks passed; GitHub lint, test, and CodeQL checks passed.
  • The adjacent commands/compiler/session/CGC-export suites completed with 3108 passed and 7 skipped; two unrelated local hardware/environment tests failed because OpenVINO was already registered and NPU PDH counters were unavailable.
  • 3 native GPU end-to-end cases previously passed: generated text CGIR, generated bytecode CGIR, and a local FP32 ASG model's exported CGIR.
  • Native verification used Python 3.11 x64, windowsml 2.8.75.dev0, onnxruntime-windowsml 1.30.0.202609102321, and NVIDIA GeForce RTX 5070.

No model weights, benchmark reports/results, or local runtime packages are included in this PR.

Compile existing CGC Input IR through the Windows ML Runtime with explicit input-format, target and compiler selectors. Preserve EPContext defaults and add GPU binding, atomic artifact publication, validation, tests and documentation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fangyangci
fangyangci requested a review from a team as a code owner October 10, 2026 10:11
Comment thread tests/e2e/test_compile_cgc_ir_e2e.py Fixed

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.

I found two validation-path regressions that can reject otherwise valid compiled artifacts; details are inline.

Comment thread src/winml/modelkit/compiler/runtime.py Outdated
Comment thread src/winml/modelkit/compiler/runtime.py Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fangyangci fangyangci changed the title feat(compile): add explicit CGC Input IR to Output IR mode feat(compile): add explicit CGIR to CG Output IR mode Oct 12, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@fangyangci fangyangci changed the title feat(compile): add explicit CGIR to CG Output IR mode feat(compile): add CGIR to CG Output IR compiler mode Oct 12, 2026

@zhenchaoni Zhenchao Ni (zhenchaoni) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Requesting changes primarily because CGC compilation needs to use WinMLRuntimeSession, consistent with the existing WinMLSession / WinMLQairtSession convention. The three inline comments cover that architectural requirement, stale metadata on overwrite, and the compiler type-narrowing failure.

ONNX input support for --compiler cgc is not a requirement for this PR and is not a blocker.

Please also keep this change incremental: reconsider whether the additional Foundry pre-parse, text/regex classification, and hash receipt are necessary for exposing Runtime compilation. Preserve necessary resource-companion handling and overwrite safety, but avoid a parallel Runtime compilation implementation. The exporter metadata format change from "cgc-input-ir" to "cgir" appears unrelated to this feature and should preferably be left out unless a direct dependency requires it.

Comment thread src/winml/modelkit/compiler/runtime.py Outdated
Comment thread src/winml/modelkit/compiler/runtime.py Outdated
Comment thread src/winml/modelkit/commands/compile.py
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

4 participants