Skip to content

Commit a1f17bc

Browse files
authored
Bug Bash - EP+Performance compile paths and fixes (#634)
* Added comment to print ready state after ensurereadyasync * Bug Bash - removing ep_policy option DISABLE * Bug 3 Adding Ep+Performance specific compile paths * Removing unused variable * reverting DISABLE option change
1 parent 9f03325 commit a1f17bc

9 files changed

Lines changed: 141 additions & 13 deletions

File tree

Samples/WindowsML/Shared/cpp/ArgumentParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ namespace Shared
191191
<< L" --download Download required packages\n"
192192
<< L" --use_model_catalog Use the model catalog for model selection\n"
193193
<< L" --model <path> Path to the input ONNX model (default: SqueezeNet.onnx in executable directory)\n"
194-
<< L" --compiled_output <path> Path for compiled output model (default: SqueezeNet_ctx.onnx)\n"
194+
<< L" --compiled_output <path> Path for compiled output model (default: auto-generated with device info)\n"
195195
<< L" --image_path <path> Path to the input image (default: sample kitten image)\n"
196196
<< L"\n"
197197
<< L"Exactly one of --ep_policy or --ep_name must be specified.\n"

Samples/WindowsML/Shared/cpp/ModelManager.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE.md in the repo root for license information.
33
#include "ModelManager.h"
44
#include <iostream>
@@ -253,5 +253,56 @@ namespace Shared
253253
return ModelVariant::Default;
254254
}
255255

256+
std::filesystem::path ModelManager::GenerateCompiledModelPath(
257+
const std::filesystem::path& modelPath,
258+
const std::filesystem::path& executableFolder,
259+
const CommandLineOptions& options)
260+
{
261+
// If user explicitly specified --compiled_output, use it as-is
262+
if (!options.output_path.empty())
263+
{
264+
return std::filesystem::path(options.output_path);
265+
}
266+
267+
std::wstring baseName = modelPath.stem().wstring();
268+
std::wstring suffix;
269+
270+
if (options.ep_policy.has_value())
271+
{
272+
std::string policyStr = ArgumentParser::ToString(options.ep_policy.value());
273+
suffix = L"_" + std::wstring(policyStr.begin(), policyStr.end());
274+
}
275+
else if (!options.ep_name.empty())
276+
{
277+
suffix = L"_" + options.ep_name;
278+
279+
// Try to determine device type
280+
std::wstring deviceType;
281+
if (options.device_type.has_value())
282+
{
283+
deviceType = options.device_type.value();
284+
}
285+
286+
if (!deviceType.empty())
287+
{
288+
suffix += L"_" + deviceType;
289+
}
290+
}
291+
292+
if (options.perf_mode == PerformanceMode::MaxPerformance)
293+
{
294+
suffix += L"_MaxPerformance";
295+
}
296+
else if (options.perf_mode == PerformanceMode::MaxEfficiency)
297+
{
298+
suffix += L"_MaxEfficiency";
299+
}
300+
301+
std::wstring fileName = baseName + L"_ctx" + suffix + L".onnx";
302+
auto result = executableFolder / fileName;
303+
std::wcout << L"Compiled model path: " << result.wstring() << std::endl;
304+
return result;
305+
}
306+
256307
} // namespace Shared
257308
} // namespace WindowsML

Samples/WindowsML/Shared/cpp/ModelManager.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ namespace Shared
5454
std::wstring& outputModelPath,
5555
std::wstring& outputImagePath);
5656

57+
/// <summary>
58+
/// Generate a device-specific compiled model path.
59+
/// Encodes EP policy/name, device type, and performance mode so compiled models
60+
/// for different device configurations don't collide.
61+
/// </summary>
62+
static std::filesystem::path GenerateCompiledModelPath(
63+
const std::filesystem::path& modelPath,
64+
const std::filesystem::path& executableFolder,
65+
const CommandLineOptions& options);
66+
5767
/// <summary>
5868
/// Get model path for specified variant
5969
/// </summary>

Samples/WindowsML/Shared/cs/ArgumentParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class Options
3838
public bool Download { get; set; } = false;
3939
public bool UseModelCatalog { get; set; } = false;
4040
public string ModelPath { get; set; } = string.Empty;
41-
public string OutputPath { get; set; } = "SqueezeNet_ctx.onnx";
41+
public string OutputPath { get; set; } = string.Empty;
4242
public string ImagePath { get; set; } = string.Empty;
4343
public ModelVariant Variant { get; set; } = ModelVariant.Default;
4444
public PerformanceMode PerfMode { get; set; } = PerformanceMode.Default;
@@ -221,7 +221,7 @@ public static void PrintHelp()
221221
Console.WriteLine(" --download Download required packages");
222222
Console.WriteLine(" --use_model_catalog Use the model catalog for model discovery");
223223
Console.WriteLine(" --model <path> Path to input ONNX model (default: SqueezeNet.onnx)");
224-
Console.WriteLine(" --compiled_output <path> Path for compiled output model (default: SqueezeNet_ctx.onnx)");
224+
Console.WriteLine(" --compiled_output <path> Path for compiled output model (default: auto-generated with device info)");
225225
Console.WriteLine(" --image_path <path> Path to the input image (default: sample kitten image)");
226226
Console.WriteLine(" --help, -h Display this help message");
227227
Console.WriteLine();

Samples/WindowsML/Shared/cs/ExecutionProviderManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public static async Task InitializeProvidersAsync(bool allowDownload = false)
4242
if (allowDownload || readyState != ExecutionProviderReadyState.NotPresent)
4343
{
4444
await provider.EnsureReadyAsync();
45+
Console.WriteLine($" Updated Ready state: {provider.ReadyState}");
4546
}
4647

4748
provider.TryRegister();

Samples/WindowsML/Shared/cs/ModelManager.cs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,60 @@ public static string GetModelVariantPath(string executableFolder, ModelVariant v
110110
return modelPath;
111111
}
112112

113+
/// <summary>
114+
/// Generate a device-specific compiled model path.
115+
/// Encodes EP policy/name, device type, and performance mode into the filename
116+
/// so that compiled models for different device configurations don't collide.
117+
/// </summary>
118+
public static string GenerateCompiledModelPath(string modelPath, string executableFolder, Options options)
119+
{
120+
// If user explicitly specified --compiled_output, use it as-is
121+
if (!string.IsNullOrEmpty(options.OutputPath))
122+
{
123+
return options.OutputPath.Contains(Path.DirectorySeparatorChar) ?
124+
options.OutputPath : Path.Combine(executableFolder, options.OutputPath);
125+
}
126+
127+
string baseName = Path.GetFileNameWithoutExtension(modelPath);
128+
string suffix = BuildDeviceSuffix(options);
129+
130+
string fileName = $"{baseName}_ctx{suffix}.onnx";
131+
Console.WriteLine($"Compiled model path: {Path.Combine(executableFolder, fileName)}");
132+
return Path.Combine(executableFolder, fileName);
133+
}
134+
135+
/// <summary>
136+
/// Build a device-identifying suffix for the compiled model filename.
137+
/// </summary>
138+
private static string BuildDeviceSuffix(Options options)
139+
{
140+
var parts = new List<string>();
141+
142+
if (options.EpPolicy.HasValue)
143+
{
144+
parts.Add(options.EpPolicy.Value.ToString());
145+
}
146+
else if (!string.IsNullOrEmpty(options.EpName))
147+
{
148+
parts.Add(options.EpName);
149+
150+
// Try to determine device type
151+
string? deviceType = options.DeviceType;
152+
153+
if (!string.IsNullOrEmpty(deviceType))
154+
{
155+
parts.Add(deviceType);
156+
}
157+
}
158+
159+
if (options.PerfMode != PerformanceMode.Default)
160+
{
161+
parts.Add(options.PerfMode.ToString());
162+
}
163+
164+
return parts.Count > 0 ? "_" + string.Join("_", parts) : "";
165+
}
166+
113167
/// <summary>
114168
/// Resolve model paths with intelligent variant selection
115169
/// </summary>
@@ -192,8 +246,7 @@ public static string GetModelVariantPath(string executableFolder, ModelVariant v
192246
modelPath = GetModelVariantPath(executableFolder, variant);
193247
}
194248

195-
string compiledModelPath = options.OutputPath.Contains(Path.DirectorySeparatorChar) ?
196-
options.OutputPath : Path.Combine(executableFolder, options.OutputPath);
249+
string compiledModelPath = GenerateCompiledModelPath(modelPath, executableFolder, options);
197250

198251
if (!File.Exists(labelsPath))
199252
{
@@ -225,8 +278,7 @@ public static (string modelPath, string compiledModelPath, string labelsPath) Re
225278
modelPath = GetModelVariantPath(executableFolder, options.Variant);
226279
}
227280

228-
string compiledModelPath = options.OutputPath.Contains(Path.DirectorySeparatorChar) ?
229-
options.OutputPath : Path.Combine(executableFolder, options.OutputPath);
281+
string compiledModelPath = GenerateCompiledModelPath(modelPath, executableFolder, options);
230282

231283
string labelsPath = Path.Combine(executableFolder, "SqueezeNet.Labels.txt");
232284

Samples/WindowsML/cpp/CppConsoleDesktop/CppConsoleDesktop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ IAsyncAction RunInferenceAsync(const CommandLineOptions& options)
110110
std::vector<std::string> labels = ModelManager::LoadLabels(labelsPath);
111111

112112
std::filesystem::path outputPath =
113-
options.output_path.empty() ? executableFolder / L"SqueezeNet_ctx.onnx" : std::filesystem::path(options.output_path);
113+
ModelManager::GenerateCompiledModelPath(modelPath, executableFolder, options);
114114

115115
std::filesystem::path imagePath =
116116
options.image_path.empty() ? executableFolder / L"image.png" : std::filesystem::path(options.image_path);

Samples/WindowsML/cpp/CppConsoleDesktop/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Options:
1515
--compile Compile the model
1616
--download Download required packages
1717
--model <path> Path to input ONNX model (default: SqueezeNet.onnx in executable directory)
18-
--compiled_output <path> Path for compiled output model (default: SqueezeNet_ctx.onnx)
18+
--compiled_output <path> Path for compiled output model (default: auto-generated with device info)
1919
--image_path <path> Path to the input image (default: sample kitten image)
2020
```
2121

@@ -68,7 +68,14 @@ for (const auto& [ep_name, devices] : ep_device_map)
6868
6969
### 2. Model Compilation
7070
71-
The sample shows how to compile an ONNX model for optimized execution:
71+
The sample shows how to compile an ONNX model for optimized execution. Compiled model filenames
72+
are automatically generated with device-specific identifiers to prevent collisions:
73+
74+
- Policy mode: `SqueezeNet_ctx_PREFER_GPU.onnx`
75+
- Explicit EP: `SqueezeNet_ctx_DML_GPU.onnx`
76+
- With perf mode: `SqueezeNet_ctx_PREFER_NPU_MaxPerformance.onnx`
77+
78+
Use `--compiled_output` to override with a custom path.
7279
7380
```cpp
7481
#include <win_onnxruntime_cxx_api.h>

Samples/WindowsML/cs/CSharpConsoleDesktop/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Options:
1515
--compile Compile the model
1616
--download Download required packages
1717
--model <path> Path to input ONNX model (default: SqueezeNet.onnx in executable directory)
18-
--compiled_output <path> Path for compiled output model (default: SqueezeNet_ctx.onnx)
18+
--compiled_output <path> Path for compiled output model (default: auto-generated with device info)
1919
--image_path <path> Path to the input image (default: sample kitten image)
2020
```
2121

@@ -80,7 +80,14 @@ foreach (KeyValuePair<string, List<OrtEpDevice>> epGroup in epDeviceMap)
8080

8181
### 2. Model Compilation
8282

83-
The sample shows how to compile an ONNX model for optimized execution:
83+
The sample shows how to compile an ONNX model for optimized execution. Compiled model filenames
84+
are automatically generated with device-specific identifiers to prevent collisions:
85+
86+
- Policy mode: `SqueezeNet_ctx_PREFER_GPU.onnx`
87+
- Explicit EP: `SqueezeNet_ctx_DML_GPU.onnx`
88+
- With perf mode: `SqueezeNet_ctx_PREFER_NPU_MaxPerformance.onnx`
89+
90+
Use `--compiled_output` to override with a custom path.
8491

8592
```csharp
8693
// Create compilation options from session options

0 commit comments

Comments
 (0)