Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit e70c90f

Browse files
committed
Add logging to DeviceService
1 parent a2f7b53 commit e70c90f

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

OnnxStack.UI/Dialogs/UpdateModelDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146

147147
<!--Override Defaults-->
148148
<UniformGrid Grid.Column="0" Columns="4" Visibility="{Binding IsOverrideEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,5,0,10">
149-
<StackPanel Margin="0,0,4,0">
149+
<StackPanel Margin="0,0,4,0">
150150
<TextBlock Text="Device" />
151151
<userControls:DevicePickerControl
152152
UISettings="{Binding UISettings, ElementName=UI}"

OnnxStack.UI/Models/DeviceInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
namespace OnnxStack.UI.Models
22
{
3-
public record DeviceInfo(string Name, int DeviceId, int VRAM);
3+
public class DeviceInfo
4+
{
5+
public DeviceInfo(string name, int deviceId, int vram)
6+
{
7+
Name = name;
8+
DeviceId = deviceId;
9+
VRAM = vram;
10+
}
11+
12+
public string Name { get; set; }
13+
public int DeviceId { get; set; }
14+
public int VRAM { get; set; }
15+
}
416
}

OnnxStack.UI/Services/DeviceService.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using OnnxStack.UI.Models;
1+
using Microsoft.Extensions.Logging;
2+
using OnnxStack.UI.Models;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -9,39 +10,64 @@ namespace OnnxStack.UI.Services
910
{
1011
public class DeviceService : IDeviceService
1112
{
13+
private readonly ILogger<DeviceService> _logger;
1214
private IReadOnlyList<DeviceInfo> _devices;
1315

14-
public DeviceService()
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="DeviceService"/> class.
18+
/// </summary>
19+
/// <param name="logger">The logger.</param>
20+
public DeviceService(ILogger<DeviceService> logger)
1521
{
22+
_logger = logger;
1623
_devices = GetDevices();
1724
}
1825

26+
/// <summary>
27+
/// Gets the devices.
28+
/// </summary>
1929
public IReadOnlyList<DeviceInfo> Devices => _devices;
2030

21-
private static IReadOnlyList<DeviceInfo> GetDevices()
31+
32+
/// <summary>
33+
/// Gets the devices.
34+
/// </summary>
35+
/// <returns></returns>
36+
private IReadOnlyList<DeviceInfo> GetDevices()
2237
{
23-
var devices = new List<DeviceInfo> { new DeviceInfo("CPU", -1, 0) };
38+
_logger.LogInformation("[GetDevices] - Query Devices...");
39+
var devices = new List<DeviceInfo> { new DeviceInfo("CPU", 0, 0) };
2440

2541
try
2642
{
2743
var adapters = new AdapterInfo[10];
2844
AdapterInterop.GetAdapters(adapters);
2945
devices.AddRange(adapters
30-
.Where(x => x.DeviceId > 0)
46+
.Where(x => x.DedicatedVideoMemory > 0)
3147
.Select(GetDeviceInfo)
3248
.ToList());
49+
devices.ForEach(x => _logger.LogInformation($"[GetDevices] - Found Device: {x.Name}, DeviceId: {x.DeviceId}"));
3350
}
3451
catch (Exception ex)
3552
{
3653
devices.Add(new DeviceInfo("GPU0", 0, 0));
3754
devices.Add(new DeviceInfo("GPU1", 1, 0));
55+
_logger.LogError($"[GetDevices] - Failed to query devices, {ex.Message}");
3856
}
57+
58+
_logger.LogInformation($"[GetDevices] - Query devices complete, Devices Found: {devices.Count}");
3959
return devices;
4060
}
4161

62+
63+
/// <summary>
64+
/// Gets the device information.
65+
/// </summary>
66+
/// <param name="adapter">The adapter.</param>
67+
/// <returns></returns>
4268
private static DeviceInfo GetDeviceInfo(AdapterInfo adapter)
4369
{
44-
string description = string.Empty;
70+
string description;
4571
unsafe
4672
{
4773
description = new string(adapter.Description);

OnnxStack.UI/UserControls/DevicePickerControl.xaml.cs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public partial class DevicePickerControl : UserControl, INotifyPropertyChanged
1818
private readonly IDeviceService _deviceService;
1919
private DeviceInfo _selectedDevice;
2020

21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="DevicePickerControl"/> class.
23+
/// </summary>
2124
public DevicePickerControl()
2225
{
2326
if (!DesignerProperties.GetIsInDesignMode(this))
@@ -27,26 +30,30 @@ public DevicePickerControl()
2730
}
2831

2932
public static readonly DependencyProperty UISettingsProperty =
30-
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
31-
{
32-
if (c is DevicePickerControl control)
33-
control.OnSettingsChanged();
34-
}));
33+
DependencyProperty.Register("UISettings", typeof(OnnxStackUIConfig), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
34+
{
35+
if (c is DevicePickerControl control)
36+
control.OnSettingsChanged();
37+
}));
3538

3639
public static readonly DependencyProperty ExecutionProviderProperty =
37-
DependencyProperty.Register("ExecutionProvider", typeof(ExecutionProvider), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
40+
DependencyProperty.Register("ExecutionProvider", typeof(ExecutionProvider?), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
3841
{
3942
if (c is DevicePickerControl control)
4043
control.OnSettingsChanged();
4144
}));
4245

4346
public static readonly DependencyProperty DeviceIdProperty =
44-
DependencyProperty.Register("DeviceId", typeof(int), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
47+
DependencyProperty.Register("DeviceId", typeof(int?), typeof(DevicePickerControl), new PropertyMetadata((c, e) =>
4548
{
4649
if (c is DevicePickerControl control)
4750
control.OnSettingsChanged();
4851
}));
4952

53+
54+
/// <summary>
55+
/// Gets or sets the UI settings.
56+
/// </summary>
5057
public OnnxStackUIConfig UISettings
5158
{
5259
get { return (OnnxStackUIConfig)GetValue(UISettingsProperty); }
@@ -56,45 +63,60 @@ public OnnxStackUIConfig UISettings
5663
/// <summary>
5764
/// Gets or sets the ExecutionProvider.
5865
/// </summary>
59-
public ExecutionProvider ExecutionProvider
66+
public ExecutionProvider? ExecutionProvider
6067
{
61-
get { return (ExecutionProvider)GetValue(ExecutionProviderProperty); }
68+
get { return (ExecutionProvider?)GetValue(ExecutionProviderProperty); }
6269
set { SetValue(ExecutionProviderProperty, value); }
6370
}
6471

6572
/// <summary>
6673
/// Gets or sets the DeviceId.
6774
/// </summary>
68-
public int DeviceId
75+
public int? DeviceId
6976
{
70-
get { return (int)GetValue(DeviceIdProperty); }
77+
get { return (int?)GetValue(DeviceIdProperty); }
7178
set { SetValue(DeviceIdProperty, value); }
7279
}
7380

74-
81+
/// <summary>
82+
/// Gets the devices.
83+
/// </summary>
7584
public IReadOnlyList<DeviceInfo> Devices => _deviceService.Devices;
7685

7786

78-
87+
/// <summary>
88+
/// Gets or sets the selected device.
89+
/// </summary>
7990
public DeviceInfo SelectedDevice
8091
{
8192
get { return _selectedDevice; }
8293
set { _selectedDevice = value; OnSelectedDeviceChanged(); }
8394
}
8495

96+
97+
/// <summary>
98+
/// Called when UISettings changed.
99+
/// </summary>
85100
private void OnSettingsChanged()
86101
{
87-
SelectedDevice = ExecutionProvider == ExecutionProvider.Cpu
102+
SelectedDevice = ExecutionProvider == Core.Config.ExecutionProvider.Cpu
88103
? Devices.FirstOrDefault()
89-
: Devices.FirstOrDefault(x => x.DeviceId == DeviceId);
104+
: Devices.FirstOrDefault(x => x.Name != "CPU" && x.DeviceId == DeviceId);
90105
}
91106

107+
108+
/// <summary>
109+
/// Called when SelectedDevice changed.
110+
/// </summary>
92111
private void OnSelectedDeviceChanged()
93112
{
113+
if (_selectedDevice is null)
114+
return;
115+
94116
if (_selectedDevice.Name == "CPU")
95117
{
96118
DeviceId = 0;
97-
ExecutionProvider = ExecutionProvider.Cpu;
119+
ExecutionProvider = Core.Config.ExecutionProvider.Cpu;
98120
}
99121
else
100122
{
@@ -114,6 +136,4 @@ public void NotifyPropertyChanged([CallerMemberName] string property = "")
114136
}
115137
#endregion
116138
}
117-
118-
119139
}

0 commit comments

Comments
 (0)