Skip to content

Auto-load model and run inference on WinUI app launch#622

Open
yeelam-gordon wants to merge 1 commit intomainfrom
fix/61791037-winui-startup-demo
Open

Auto-load model and run inference on WinUI app launch#622
yeelam-gordon wants to merge 1 commit intomainfrom
fix/61791037-winui-startup-demo

Conversation

@yeelam-gordon
Copy link
Copy Markdown
Contributor

Instead of requiring the user to manually select an execution provider and click Load / Reload Model, the WinUI WindowsML sample now automatically loads the model with the default EP and runs inference on startup.

If auto-load fails (e.g., no GPU available for DML), the error is shown and the user can still manually select a different EP and retry.

Changes

  • Samples/WindowsML/cs-winui/MainWindow.xaml.cs — After EP initialization, automatically calls LoadModelAsync() and LoadAndRunDemoAsync() with graceful error handling fallback.

Testing

  • Built and deployed on a Hyper-V VM (no GPU) — app auto-runs with CPU EP successfully
  • Manual reload button still works for switching EPs

Instead of requiring the user to manually select an EP and click
'Load / Reload Model', the app now automatically loads the model
with the default execution provider and runs inference on startup.
If auto-load fails (e.g. no GPU for DML), the error is shown and
the user can still manually select a different EP and retry.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the WindowsML WinUI sample startup flow to automatically load the model using the initially selected execution provider and immediately run the demo inference, while keeping the manual “Load / Reload Model” path available as a fallback if startup auto-load fails.

Changes:

  • Replace the initial instruction text with an auto-load startup status message.
  • Automatically call LoadModelAsync() and (if successful) LoadAndRunDemoAsync() during initialization.
  • Add a startup auto-load failure message prompting the user to manually select an EP and retry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


providerInfo.AppendLine("========================================");
providerInfo.AppendLine("Select an execution provider (and device if required) then click 'Load / Reload Model'.");
providerInfo.AppendLine("Auto-loading model with default execution provider...");
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The status text says "default execution provider", but the code path uses whatever EP is currently selected in EpCombo (which is set to the first EP name after sorting in PopulateEpCombo). This may not be the actual/default EP and can mislead users. Consider changing the message to reflect the actual selected EP (e.g., include EpCombo.SelectedItem) or use wording like "Auto-loading model with selected execution provider".

Suggested change
providerInfo.AppendLine("Auto-loading model with default execution provider...");
var selectedExecutionProvider = EpCombo.SelectedItem?.ToString();
providerInfo.AppendLine(
string.IsNullOrWhiteSpace(selectedExecutionProvider)
? "Auto-loading model with selected execution provider..."
: $"Auto-loading model with selected execution provider: {selectedExecutionProvider}...");

Copilot uses AI. Check for mistakes.
Comment on lines +84 to +88
// Auto-load model and run inference on startup
try
{
await LoadModelAsync();
if (_session != null)
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

Auto-load now calls LoadModelAsync() immediately after InitializeAndRunDemoAsync() already initialized providers. Since LoadModelAsync() calls ModelManager.InitializeExecutionProvidersAsync(...) again, startup will run provider initialization twice (including EnsureReadyAsync/registration loops), which can add noticeable launch latency. Consider making provider initialization idempotent/cached (e.g., track a "providers initialized" flag) or remove the redundant initialization call from one of the paths.

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +97
ResultsText.Text = providerInfo.ToString()
+ $"\nAuto-load failed: {ex.Message}"
+ "\nSelect an execution provider and click 'Load / Reload Model' to retry.";
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

This builds providerInfo with StringBuilder.AppendLine() (which uses Windows newlines) but then appends additional lines using hard-coded "\n". Mixing newline conventions can lead to inconsistent rendering in the UI. Prefer using Environment.NewLine consistently, or append these lines to the same StringBuilder via AppendLine() before assigning to ResultsText.Text.

Suggested change
ResultsText.Text = providerInfo.ToString()
+ $"\nAuto-load failed: {ex.Message}"
+ "\nSelect an execution provider and click 'Load / Reload Model' to retry.";
var failureInfo = new StringBuilder(providerInfo.ToString());
failureInfo.AppendLine($"Auto-load failed: {ex.Message}");
failureInfo.AppendLine("Select an execution provider and click 'Load / Reload Model' to retry.");
ResultsText.Text = failureInfo.ToString();

Copilot uses AI. Check for mistakes.
@yeelam-gordon yeelam-gordon requested a review from timkur April 14, 2026 23:00
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.

3 participants