Merge pull request #455 from microsoft/user/numform/adapter_selection

Add Adapter Selection Sample
This commit is contained in:
Numfor Tiapo 2022-01-25 11:06:19 -08:00 коммит произвёл GitHub
Родитель 4fa07c93eb 4518c7fc93
Коммит 67c6e732c1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
14 изменённых файлов: 318 добавлений и 4 удалений

Просмотреть файл

@ -36,7 +36,7 @@
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.props" />
<PropertyGroup>
<ProjectGuid>dea7791f-55cf-4ed5-bc99-3870997b1242</ProjectGuid>
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
<AssetTargetFallback>net5.0-windows$(TargetPlatformVersion);$(AssetTargetFallback)</AssetTargetFallback>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>

Просмотреть файл

@ -37,6 +37,9 @@ namespace WinMLSamplesGallery
case "EncryptedModel":
SampleFrame.Navigate(typeof(Samples.EncryptedModel));
break;
case "AdapterSelection":
SampleFrame.Navigate(typeof(Samples.AdapterSelection));
break;
}
if (sampleMetadata.Docs.Count > 0)
DocsHeader.Visibility = Visibility.Visible;

Просмотреть файл

@ -71,6 +71,17 @@
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/EncryptedModel/EncryptedModel.xaml.cs",
"Docs": [],
"IsRecentlyAdded": true
},
{
"Title": "Adapter Selection",
"DescriptionShort": "Select different adapters based on your power and performance needs.",
"Description": "The sample showcases how to use Windows ML with different adapters that have tradeoffs between power and performance.",
"Icon": "\uE155",
"Tag": "AdapterSelection",
"XAMLGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/AdapterSelection/AdapterSelection.xaml",
"CSharpGithubLink": "https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/AdapterSelection/AdapterSelection.xaml.cs",
"Docs": [],
"IsRecentlyAdded": true
}
]
}

Просмотреть файл

@ -0,0 +1,51 @@
<Page
x:Class="WinMLSamplesGallery.Samples.AdapterSelection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local_controls="using:WinMLSamplesGallery.Controls"
xmlns:local_samples="using:WinMLSamplesGallery.Samples"
mc:Ignorable="d"
FontFamily="Arial">
<Page.Resources>
<x:String x:Key="SourceGitHubURL">
https://github.com/microsoft/Windows-Machine-Learning/blob/master/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/AdapterList.cpp
</x:String>
</Page.Resources>
<StackPanel Margin="0,20,0,0">
<Border BorderBrush="Gray" BorderThickness="2" CornerRadius="5">
<ListView x:Name="AdapterListView" SelectedIndex="0" SelectionChanged="ChangeAdapter">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="0,0,0,1" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Border>
<ComboBox x:Name="CodeSnippetComboBox" SelectedIndex="0" BorderBrush="#dbdbdb" Header="" Width="70" FontSize="18" Margin="0,20,0,0"
Typography.Capitals="AllSmallCaps" Typography.StylisticSet4="True"
HorizontalAlignment="Right">
<x:String>C#</x:String>
</ComboBox>
<Grid x:Name="CodeSnippet" Background="#F6F6F6" BorderBrush="#dbdbdb" BorderThickness="1" CornerRadius="5" Padding="15,20,15,22">
<TextBlock x:Name="TestName" FontSize="14" FontFamily="Consolas">
<Run Foreground="#3562FF">var </Run>
<Run Foreground="#1684B2">device</Run>
<Run> = </Run>
<Run Foreground="#3562FF">new </Run>
<Run Foreground="#1F9D00">LearningModelDevice</Run><Run>(</Run><Run Foreground="#005c11">
LearningModelDeviceKind.</Run><Run x:Name="selectedDeviceKind" Foreground="#1684B2"></Run><Run>);</Run>
</TextBlock>
</Grid>
<TextBlock x:Name="ViewSourCodeText" Visibility="Collapsed" Margin="0,40,0,0" FontWeight="Bold" TextWrapping="Wrap">
For information on how to use a custom adapter with Windows ML
<Hyperlink TextDecorations="None" NavigateUri="{StaticResource SourceGitHubURL}" ToolTipService.ToolTip="{StaticResource SourceGitHubURL}">
view source code.
</Hyperlink>
</TextBlock>
</StackPanel>
</Page>

Просмотреть файл

@ -0,0 +1,104 @@
using Microsoft.AI.MachineLearning;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Storage;
using WinMLSamplesGalleryNative;
namespace WinMLSamplesGallery.Samples
{
public sealed partial class AdapterSelection : Page
{
List<string> adapter_options;
LearningModelDevice device;
public AdapterSelection()
{
this.InitializeComponent();
adapter_options = new List<string> {
"Cpu",
"DirectX",
"DirectXHighPerformance",
"DirectXMinPower"
};
device = new LearningModelDevice(LearningModelDeviceKind.Cpu);
selectedDeviceKind.Text = "Cpu";
var adapters_arr = WinMLSamplesGalleryNative.AdapterList.GetAdapters();
var adapters = RemoveMicrosoftBasicRenderDriver(adapters_arr);
adapter_options.AddRange(adapters);
AdapterListView.ItemsSource = adapter_options;
}
private void ChangeAdapter(object sender, RoutedEventArgs e)
{
var device_kind_str = adapter_options[AdapterListView.SelectedIndex];
if (AdapterListView.SelectedIndex < 4)
{
device = new LearningModelDevice(
GetLearningModelDeviceKind(device_kind_str));
toggleCodeSnippet(true);
}
else
{
device = WinMLSamplesGalleryNative.AdapterList.CreateLearningModelDeviceFromAdapter(device_kind_str);
toggleCodeSnippet(false);
}
}
private LearningModelDeviceKind GetLearningModelDeviceKind(string device_kind_str)
{
selectedDeviceKind.Text = device_kind_str;
if (device_kind_str == "CPU")
{
return LearningModelDeviceKind.Cpu;
}
else if (device_kind_str == "DirectX")
{
return LearningModelDeviceKind.DirectX;
}
else if (device_kind_str == "DirectXHighPerformance")
{
return LearningModelDeviceKind.DirectXHighPerformance;
}
else
{
return LearningModelDeviceKind.DirectXMinPower;
}
}
private void toggleCodeSnippet(bool show)
{
if (show)
{
CodeSnippet.Visibility = Visibility.Visible;
CodeSnippetComboBox.Visibility = Visibility.Visible;
ViewSourCodeText.Visibility = Visibility.Collapsed;
} else
{
CodeSnippet.Visibility = Visibility.Collapsed;
CodeSnippetComboBox.Visibility = Visibility.Collapsed;
ViewSourCodeText.Visibility = Visibility.Visible;
}
}
private List<string> RemoveMicrosoftBasicRenderDriver(string[] adapters_arr)
{
List<string> adapters = new List<string>(adapters_arr);
for (int i = 0; i < adapters.Count; i++)
{
if(adapters[i] == "Microsoft Basic Render Driver")
{
adapters.RemoveAt(i);
break;
}
}
return adapters;
}
}
}

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 916 KiB

Просмотреть файл

@ -0,0 +1,24 @@
# Windows ML Samples Gallery: Adapter Selection
Select different adapters based on your power and performance needs.
The sample showcases how to use Windows ML with different adapters that have tradeoffs between power and performance.
<img src="Docs/AdapterSelectionScreenshot.png" width="650"/>
- [Getting Started](#getting-started)
- [Feedback](#feedback)
- [External Links](#external-links)
## Getting Started
- Check out the [source](https://github.com/microsoft/Windows-Machine-Learning/blob/master/adapter_selection/Samples/WinMLSamplesGallery/WinMLSamplesGallery/Samples/AdapterSelection/AdapterSelection.xaml.cs).
- Learn how to [get a list of adapters using DXCore](https://github.com/microsoft/Windows-Machine-Learning/blob/master/adapter_selection/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/AdapterList.cpp#L12) to your session.
- Learn how to [create learning model devices from adapters](https://github.com/microsoft/Windows-Machine-Learning/blob/master/adapter_selection/Samples/WinMLSamplesGallery/WinMLSamplesGalleryNative/AdapterList.cpp#L41).
## Feedback
Please file an issue [here](https://github.com/microsoft/Windows-Machine-Learning/issues/new) if you encounter any issues with the Windows ML Samples Gallery or wish to request a new sample.
## External Links
- [Windows ML Library (WinML)](https://docs.microsoft.com/en-us/windows/ai/windows-ml/)
- [DirectML](https://github.com/microsoft/directml)
- [ONNX Model Zoo](https://github.com/onnx/models)
- [Windows UI Library (WinUI)](https://docs.microsoft.com/en-us/windows/apps/winui/)

Просмотреть файл

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows10.0.18362.0</TargetFramework>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
<RootNamespace>WinMLSamplesGallery</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
@ -160,6 +160,7 @@
<ItemGroup>
<Folder Include="LargeModels\" />
<Folder Include="Samples\AdapterSelection\Docs\" />
</ItemGroup>
<Target Name="DownloadContentFiles" BeforeTargets="BeforeBuild">

Просмотреть файл

@ -2,7 +2,7 @@
<PropertyGroup>
<ProjectName>WinMLSamplesGalleryNative.Interop</ProjectName>
<TargetFramework>net5.0-windows10.0.18362.0</TargetFramework>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x86;x64;arm64;</Platforms>

Просмотреть файл

@ -0,0 +1,83 @@
#include "pch.h"
#include "AdapterList.h"
#include "AdapterList.g.cpp"
#include <initguid.h>
#include <dxcore.h>
#include "d3d12.h"
#include "winrt/Microsoft.AI.MachineLearning.h"
#include "Microsoft.AI.MachineLearning.Native.h"
namespace winrt::WinMLSamplesGalleryNative::implementation
{
winrt::com_ptr<IDXCoreAdapterList> GetDXCoreAdapterList() {
// Create an adapter factory
winrt::com_ptr<IDXCoreAdapterFactory> adapterFactory;
winrt::check_hresult(::DXCoreCreateAdapterFactory(adapterFactory.put()));
// Retrieve an adapter list
winrt::com_ptr<IDXCoreAdapterList> d3D12CoreComputeAdapters;
GUID attributes[]{ DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE };
winrt::check_hresult(
adapterFactory->CreateAdapterList(_countof(attributes),
attributes,
d3D12CoreComputeAdapters.put()));
return d3D12CoreComputeAdapters;
}
winrt::com_array<hstring> AdapterList::GetAdapters() {
winrt::com_ptr<IDXCoreAdapterList> d3D12CoreComputeAdapters = GetDXCoreAdapterList();
// Get the adapter descriptions from the adapters in the list
const uint32_t count{ d3D12CoreComputeAdapters->GetAdapterCount() };
com_array<hstring> driver_descriptions = com_array<hstring>(count);
for (uint32_t i = 0; i < count; ++i)
{
winrt::com_ptr<IDXCoreAdapter> candidateAdapter;
winrt::check_hresult(
d3D12CoreComputeAdapters->GetAdapter(i, candidateAdapter.put()));
CHAR description[128];
candidateAdapter->GetProperty(DXCoreAdapterProperty::DriverDescription, sizeof(description), description);
driver_descriptions[i] = to_hstring(description);
}
return driver_descriptions;
}
winrt::Microsoft::AI::MachineLearning::LearningModelDevice AdapterList::CreateLearningModelDeviceFromAdapter(winrt::hstring description) {
winrt::com_ptr<IDXCoreAdapterList> d3D12CoreComputeAdapters = GetDXCoreAdapterList();
// Find the adapter in the list with the matching description
const uint32_t count{ d3D12CoreComputeAdapters->GetAdapterCount() };
for (uint32_t i = 0; i < count; ++i)
{
winrt::com_ptr<IDXCoreAdapter> candidateAdapter;
winrt::check_hresult(
d3D12CoreComputeAdapters->GetAdapter(i, candidateAdapter.put()));
CHAR driver_description[128];
candidateAdapter->GetProperty(DXCoreAdapterProperty::DriverDescription, sizeof(driver_description), driver_description);
hstring hstr_driver_description = to_hstring(driver_description);
if (description == hstr_driver_description) {
// create D3D12Device
com_ptr<IUnknown> spIUnknownAdapter;
candidateAdapter->QueryInterface(IID_IUnknown, spIUnknownAdapter.put_void());
com_ptr<ID3D12Device> spD3D12Device;
D3D12CreateDevice(spIUnknownAdapter.get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), spD3D12Device.put_void());
// create D3D12 command queue from device
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
com_ptr<ID3D12CommandQueue> spCommandQueue;
spD3D12Device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(spCommandQueue.put()));
// create LearningModelDevice from command queue
com_ptr<ILearningModelDeviceFactoryNative> dFactory =
get_activation_factory<winrt::Microsoft::AI::MachineLearning::LearningModelDevice, ILearningModelDeviceFactoryNative>();
com_ptr<::IUnknown> spLearningDevice;
dFactory->CreateFromD3D12CommandQueue(spCommandQueue.get(), spLearningDevice.put());
return spLearningDevice.as<winrt::Microsoft::AI::MachineLearning::LearningModelDevice>();
}
}
}
}

Просмотреть файл

@ -0,0 +1,19 @@
#pragma once
#include "AdapterList.g.h"
namespace winrt::WinMLSamplesGalleryNative::implementation
{
struct AdapterList : AdapterListT<AdapterList>
{
AdapterList() = default;
static winrt::com_array<hstring> GetAdapters();
static winrt::Microsoft::AI::MachineLearning::LearningModelDevice CreateLearningModelDeviceFromAdapter(winrt::hstring description);
};
}
namespace winrt::WinMLSamplesGalleryNative::factory_implementation
{
struct AdapterList : AdapterListT<AdapterList, implementation::AdapterList>
{
};
}

Просмотреть файл

@ -16,4 +16,11 @@ namespace WinMLSamplesGalleryNative
{
static Microsoft.AI.MachineLearning.LearningModel LoadEncryptedResource(String key);
}
[default_interface]
runtimeclass AdapterList
{
static String[] GetAdapters();
static Microsoft.AI.MachineLearning.LearningModelDevice CreateLearningModelDeviceFromAdapter(String description);
}
}

Просмотреть файл

@ -14,7 +14,7 @@
<DefaultLanguage>en-US</DefaultLanguage>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
<ApplicationTypeRevision>10.0</ApplicationTypeRevision>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.19041.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
@ -142,6 +142,7 @@
</ResourceCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AdapterList.h" />
<ClInclude Include="EncryptedModels.h" />
<ClInclude Include="OpenCVImage.h" />
<ClInclude Include="pch.h" />
@ -149,6 +150,7 @@
<ClInclude Include="WeakBuffer.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="AdapterList.cpp" />
<ClCompile Include="EncryptedModels.cpp" />
<ClCompile Include="OpenCVImage.cpp" />
<ClCompile Include="pch.cpp">

Просмотреть файл

@ -9,6 +9,9 @@
<ClCompile Include="OpenCVImage.cpp">
<Filter>OpenCVInterop</Filter>
</ClCompile>
<ClCompile Include="AdapterList.cpp">
<Filter>AdapterList</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
@ -24,6 +27,9 @@
<ClInclude Include="WeakBuffer.h">
<Filter>AbiHelpers</Filter>
</ClInclude>
<ClInclude Include="AdapterList.h">
<Filter>AdapterList</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Resource.rc" />
@ -51,5 +57,8 @@
<Filter Include="AbiHelpers">
<UniqueIdentifier>{ed4f3871-4514-4b93-9ace-1315cad5491e}</UniqueIdentifier>
</Filter>
<Filter Include="AdapterList">
<UniqueIdentifier>{9df6a233-c2c6-4062-b738-31f7ba631acd}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>