Auto-load model and run inference on WinUI app launch#622
Auto-load model and run inference on WinUI app launch#622yeelam-gordon wants to merge 1 commit intomainfrom
Conversation
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>
There was a problem hiding this comment.
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..."); |
There was a problem hiding this comment.
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".
| 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}..."); |
| // Auto-load model and run inference on startup | ||
| try | ||
| { | ||
| await LoadModelAsync(); | ||
| if (_session != null) |
There was a problem hiding this comment.
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.
| ResultsText.Text = providerInfo.ToString() | ||
| + $"\nAuto-load failed: {ex.Message}" | ||
| + "\nSelect an execution provider and click 'Load / Reload Model' to retry."; |
There was a problem hiding this comment.
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.
| 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(); |
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
Testing