merged the latest csharp folder changes from VSTS repo @313681f9

This commit is contained in:
Shah Asaduzzaman (ASAD) 2018-11-27 19:01:28 -08:00
parent d7d43bc13f
commit 29f9e89226
6 changed files with 268 additions and 9 deletions

View file

@ -18,28 +18,39 @@ CMake creates a target to this project
<Message Importance="High" Text="Restoring NuGet packages for CSharp projects..." />
<MSBuild Projects="src\Microsoft.ML.OnnxRuntime\Microsoft.ML.OnnxRuntime.csproj"
Targets="Restore"
Properties="RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
Properties="Platform=AnyCPU;RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
/>
<MSBuild Projects="sample\Microsoft.ML.OnnxRuntime.InferenceSample\Microsoft.ML.OnnxRuntime.InferenceSample.csproj"
Targets="Restore"
Properties="RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
Properties="Platform=AnyCPU;RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
/>
<MSBuild Projects="test\Microsoft.ML.OnnxRuntime.Tests\Microsoft.ML.OnnxRuntime.Tests.csproj"
Targets="Restore"
Properties="RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
/>
<MSBuild Projects="tools\Microsoft.ML.OnnxRuntime.PerfTool\Microsoft.ML.OnnxRuntime.PerfTool.csproj"
Targets="Restore"
Properties="Platform=AnyCPU;RestoreConfigFile=$(MSBuildThisFileDirectory)\NuGet.CSharp.config;MSBuildWarningsAsMessages=NU1503;RestoreIgnoreFailedSource=true"
/>
</Target>
<Target Name="Build">
<Message Importance="High" Text="Building CSharp projects..." />
<MSBuild Projects="src\Microsoft.ML.OnnxRuntime\Microsoft.ML.OnnxRuntime.csproj"
Targets="Build" />
Targets="ObtainPackageVersion;Build"
Properties="Platform=AnyCPU"/>
<MSBuild Projects="sample\Microsoft.ML.OnnxRuntime.InferenceSample\Microsoft.ML.OnnxRuntime.InferenceSample.csproj"
Targets="Build" />
Targets="Build"
Properties="Platform=AnyCPU"
/>
<MSBuild Projects="test\Microsoft.ML.OnnxRuntime.Tests\Microsoft.ML.OnnxRuntime.Tests.csproj"
Targets="Build" />
Targets="Build"
/>
<MSBuild Projects="tools\Microsoft.ML.OnnxRuntime.PerfTool\Microsoft.ML.OnnxRuntime.PerfTool.csproj"
Targets="Build"
Properties="Platform=AnyCPU"
/>
</Target>
<Target Name="RunTest" AfterTargets="Build">
@ -47,11 +58,11 @@ CMake creates a target to this project
<Exec Command="dotnet test test\Microsoft.ML.OnnxRuntime.Tests\Microsoft.ML.OnnxRuntime.Tests.csproj -c $(Configuration) --no-build" />
</Target>
<Target Name="CreatePackage" AfterTargets="RunTest">
<Target Name="CreatePackage">
<Message Importance="High" Text="Bundling NuGet package ..." />
<MSBuild Projects="src\Microsoft.ML.OnnxRuntime\Microsoft.ML.OnnxRuntime.csproj"
Targets="ObtainPackageVersion;Pack"
Properties="NoBuild=true"
Properties="NoBuild=true;Platform=AnyCPU"
/>
</Target>

View file

@ -9,7 +9,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.OnnxRuntime.In
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.OnnxRuntime.Tests", "test\Microsoft.ML.OnnxRuntime.Tests\Microsoft.ML.OnnxRuntime.Tests.csproj", "{50173D13-DF29-42E7-A30B-8B12D36C77B1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.OnnxRuntime.PerfTool", "tools\Microsoft.ML.OnnxRuntime.PerfTool\Microsoft.ML.OnnxRuntime.PerfTool.csproj", "{310506FD-6E78-4D62-989B-25D69A85E8CF}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
@ -27,6 +32,10 @@ Global
{50173D13-DF29-42E7-A30B-8B12D36C77B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50173D13-DF29-42E7-A30B-8B12D36C77B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50173D13-DF29-42E7-A30B-8B12D36C77B1}.Release|Any CPU.Build.0 = Release|Any CPU
{310506FD-6E78-4D62-989B-25D69A85E8CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{310506FD-6E78-4D62-989B-25D69A85E8CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{310506FD-6E78-4D62-989B-25D69A85E8CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{310506FD-6E78-4D62-989B-25D69A85E8CF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

@ -30,18 +30,21 @@
<None Include="$(NativeBuildOutputDir)\onnxruntime.dll"
PackagePath="\runtimes\win10-x64\native"
Pack="true"
CopyToOutputDirectory="Always"
Visible="false"
/>
<None Include="$(NativeBuildOutputDir)\onnxruntime.pdb"
Condition="Exists('$(NativeBuildOutputDir)\onnxruntime.pdb')"
PackagePath="\runtimes\win10-x64\native"
Pack="true"
CopyToOutputDirectory="Always"
Visible="false"
/>
<None Include="$(NativeBuildOutputDir)\mkldnn.dll"
Condition="Exists('$(NativeBuildOutputDir)\mkldnn.dll')"
PackagePath="\runtimes\win10-x64\native"
Pack="true"
CopyToOutputDirectory="Always"
Visible="false"
/>
@ -58,7 +61,7 @@
/>
</ItemGroup>
<Target Name="ObtainPackageVersion" BeforeTargets="Pack">
<Target Name="ObtainPackageVersion" BeforeTargets="Build;Pack">
<ReadLinesFromFile File="$(OnnxRuntimeCsharpRoot)\..\VERSION_NUMBER">
<Output TaskParameter="Lines" ItemName="MajorVersionNumber"/>
</ReadLinesFromFile>
@ -69,6 +72,7 @@
<PropertyGroup>
<RepositoryCommit>$(GitCommitHash)</RepositoryCommit>
<PackageVersion>@(MajorVersionNumber)</PackageVersion>
<Version>$(PackageVersion)</Version>
<PackageVersion Condition="'$(IsReleaseBuild)'==''">$(PackageVersion)-dev-$(GitCommitHash)</PackageVersion>
</PropertyGroup>
<Message Importance="High" Text="PackageVersion=$(PackageVersion)" />

View file

@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OnnxRuntimeCsharpRoot>..\..</OnnxRuntimeCsharpRoot>
<buildDirectory Condition="'$(buildDirectory)'==''">$(OnnxRuntimeCsharpRoot)\..\build\Windows</buildDirectory>
<NativeBuildOutputDir>$(buildDirectory)\$(Configuration)\$(Configuration)</NativeBuildOutputDir>
<SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<None Include="$(NativeBuildOutputDir)\onnxruntime.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(NativeBuildOutputDir)\onnxruntime.pdb" Condition="Exists('$(NativeBuildOutputDir)\onnxruntime.pdb')">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(NativeBuildOutputDir)\mkldnn.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
<None Include="$(OnnxRuntimeCSharpRoot)\testdata\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Visible>false</Visible>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(OnnxRuntimeCSharpRoot)\src\Microsoft.ML.OnnxRuntime\Microsoft.ML.OnnxRuntime.csproj" />
<PackageReference Include="Microsoft.ML.Scoring" Version="1.1.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,139 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.ML.OnnxRuntime;
using System.Numerics.Tensors;
using System.Diagnostics;
namespace Microsoft.ML.OnnxRuntime.PerfTool
{
public enum TimingPoint
{
Start = 0,
ModelLoaded = 1,
InputLoaded = 2,
RunComplete = 3,
TotalCount = 4
}
class Program
{
public static void Main(string[] args)
{
/*
args[0] = model-file-name
args[1] = input-file-name
args[2] = iteration count
*/
if (args.Length < 3)
{
PrintUsage();
Environment.Exit(1);
}
string modelPath = args[0];
string inputPath = args[1];
int iteration = Int32.Parse(args[2]);
Console.WriteLine("Running model {0} in OnnxRuntime with input {1} for {2} times", modelPath, inputPath, iteration);
DateTime[] timestamps = new DateTime[(int)TimingPoint.TotalCount];
RunModelOnnxRuntime(modelPath, inputPath, iteration, timestamps);
PrintReport(timestamps, iteration);
Console.WriteLine("Done");
Console.WriteLine("Running model {0} in Sonoma with input {1} for {2} times", modelPath, inputPath, iteration);
RunModelOnnxRuntime(modelPath, inputPath, iteration, timestamps);
PrintReport(timestamps, iteration);
Console.WriteLine("Done");
}
public static float[] LoadTensorFromFile(string filename)
{
var tensorData = new List<float>();
// read data from file
using (var inputFile = new System.IO.StreamReader(filename))
{
inputFile.ReadLine(); //skip the input name
string[] dataStr = inputFile.ReadLine().Split(new char[] { ',', '[', ']' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < dataStr.Length; i++)
{
tensorData.Add(Single.Parse(dataStr[i]));
}
}
return tensorData.ToArray();
}
static void RunModelOnnxRuntime(string modelPath, string inputPath, int iteration, DateTime[] timestamps)
{
if (timestamps.Length != (int)TimingPoint.TotalCount)
{
throw new ArgumentException("Timestamps array must have "+(int)TimingPoint.TotalCount+" size");
}
timestamps[(int)TimingPoint.Start] = DateTime.Now;
using (var session = new InferenceSession(modelPath))
{
timestamps[(int)TimingPoint.ModelLoaded] = DateTime.Now;
var inputMeta = session.InputMetadata;
var container = new List<NamedOnnxValue>();
foreach (var name in inputMeta.Keys)
{
float[] rawData = LoadTensorFromFile(inputPath);
var tensor = new DenseTensor<float>(rawData, inputMeta[name].Dimensions);
container.Add(NamedOnnxValue.CreateFromTensor<float>(name, tensor));
}
timestamps[(int)TimingPoint.InputLoaded] = DateTime.Now;
// Run the inference
for (int i=0; i < iteration; i++)
{
var results = session.Run(container); // results is an IReadOnlyList<NamedOnnxValue> container
Debug.Assert(results != null);
Debug.Assert(results.Count == 1);
//results = null;
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
timestamps[(int)TimingPoint.RunComplete] = DateTime.Now;
}
}
static void PrintUsage()
{
Console.WriteLine("Usage:\n"
+"dotnet Microsoft.ML.OnnxRuntime.PerfTool <onnx-model-path> <input-file-path> <iteration-count>"
);
}
static void PrintReport(DateTime[] timestamps, int iterations)
{
Console.WriteLine("Model Load Time = " + (timestamps[(int)TimingPoint.ModelLoaded] - timestamps[(int)TimingPoint.Start]).TotalMilliseconds);
Console.WriteLine("Input Load Time = " + (timestamps[(int)TimingPoint.InputLoaded] - timestamps[(int)TimingPoint.ModelLoaded]).TotalMilliseconds);
double totalRuntime = (timestamps[(int)TimingPoint.RunComplete] - timestamps[(int)TimingPoint.InputLoaded]).TotalMilliseconds;
double perIterationTime = totalRuntime / iterations;
Console.WriteLine("Total Run time for {0} iterations = {1}", iterations, totalRuntime);
Console.WriteLine("Per iteration time = {0}", perIterationTime);
}
}
}

View file

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ML.Scoring;
using System.Diagnostics;
namespace Microsoft.ML.OnnxRuntime.PerfTool
{
public class SonomaRunner
{
public static void RunModelSonoma(string modelPath, string inputPath, int iteration, DateTime[] timestamps)
{
if (timestamps.Length != (int)TimingPoint.TotalCount)
{
throw new ArgumentException("Timestamps array must have " + (int)TimingPoint.TotalCount + " size");
}
timestamps[(int)TimingPoint.Start] = DateTime.Now;
var modelName = "lotusrt_squeezenet";
using (var modelManager = new ModelManager(modelPath, true))
{
modelManager.InitOnnxModel(modelName, int.MaxValue);
timestamps[(int)TimingPoint.ModelLoaded] = DateTime.Now;
Tensor[] inputs = new Tensor[1];
var inputShape = new long[] { 1, 3, 224, 224 }; // hardcoded values
float[] inputData0 = Program.LoadTensorFromFile(inputPath);
inputs[0] = Tensor.Create(inputData0, inputShape);
string[] inputNames = new string[] {"data_0"};
string[] outputNames = new string[] { "softmaxout_1" };
timestamps[(int)TimingPoint.InputLoaded] = DateTime.Now;
for (int i = 0; i < iteration; i++)
{
var outputs = modelManager.RunModel(
modelName,
int.MaxValue,
inputNames,
inputs,
outputNames
);
Debug.Assert(outputs != null);
Debug.Assert(outputs.Length == 1);
}
timestamps[(int)TimingPoint.RunComplete] = DateTime.Now;
}
}
}
}