Skip to content

Commit d2cf997

Browse files
Changes some of the CPU Math implemenation from our current version to use the new TensorPrimitives package. (#6875)
* using tensor primitives * added missing files * some with indexes changed * Initial swap for TensorPrimitives done * Rebased and cleaned code * more minor cleanup * added system.numerics.tensors version to props * build fixes * added net6 again * updates from PR comments * fixed sumabsu * fixed baseline tests * test fixes * fixed test failure for kmeans * changed decimal comparison * updated more baselines * Test fixes. * template update * Test Fixes. * fixed performance test csproj * added baselines for linux arm/64 * fixed linux arm baselines * fixed arm baselines * removed extra files * arm32 baselines updated * fixed arm baselines
1 parent d8ad1e6 commit d2cf997

File tree

523 files changed

+134266
-18398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

523 files changed

+134266
-18398
lines changed

.vsts-dotnet-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ jobs:
149149
_configuration: Debug
150150
_config_short: DFX
151151
_includeBenchmarkData: false
152-
_targetFramework: net461
152+
_targetFramework: net462
153153
Release_Build:
154154
_configuration: Release
155155
_config_short: RFX
156156
_includeBenchmarkData: false
157-
_targetFramework: net461
157+
_targetFramework: net462
158158
innerLoop: true
159159
vsTestConfiguration: "/Framework:.NETCoreApp,Version=v4.0"
160160
pool:

NuGet.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<solution>
44
<add key="disableSourceControlIntegration" value="true" />
@@ -13,6 +13,7 @@
1313
<add key="dotnet5-roslyn" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
1414
<add key="mlnet-daily" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/MachineLearning/nuget/v3/index.json" />
1515
<add key="mlnet-assets" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/machinelearning-assets/nuget/v3/index.json" />
16+
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
1617
</packageSources>
1718
<disabledPackageSources>
1819
<clear />

build/.night-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ jobs:
9999
_configuration: Debug
100100
_config_short: DFX
101101
_includeBenchmarkData: false
102-
_targetFramework: net461
102+
_targetFramework: net462
103103
Release_Build:
104104
_configuration: Release
105105
_config_short: RFX
106106
_includeBenchmarkData: false
107-
_targetFramework: net461
107+
_targetFramework: net462
108108
nightlyBuild: true
109109
pool:
110110
vmImage: windows-2019

build/.outer-loop-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ jobs:
9393
_configuration: Debug
9494
_config_short: DFX
9595
_includeBenchmarkData: false
96-
_targetFramework: net461
96+
_targetFramework: net462
9797
Release_Build:
9898
_configuration: Release
9999
_config_short: RFX
100100
_includeBenchmarkData: false
101-
_targetFramework: net461
101+
_targetFramework: net462
102102
pool:
103103
vmImage: windows-2019
104104

build/ci/job-template.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ jobs:
201201
contents: |
202202
artifacts/TestResults/**
203203
targetFolder: $(Build.ArtifactStagingDirectory)
204+
- task: CopyFiles@2
205+
displayName: Stage baseline files
206+
condition: not(succeeded())
207+
inputs:
208+
sourceFolder: $(Build.SourcesDirectory)
209+
contents: |
210+
artifacts/bin/**/TestOutput/**
211+
targetFolder: $(Build.ArtifactStagingDirectory)
204212
- task: CopyFiles@2
205213
displayName: Stage process dump and pdb if any
206214
condition: not(succeeded())

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<SystemConfigurationConfigurationManagerVersion>6.0.1</SystemConfigurationConfigurationManagerVersion>
2525
<SystemIOFileSystemAccessControl>4.5.0</SystemIOFileSystemAccessControl>
2626
<SystemMemoryVersion>4.5.5</SystemMemoryVersion>
27+
<SystemNumericsTensorsVersion>8.0.0-rtm.23523.3</SystemNumericsTensorsVersion>
2728
<SystemReflectionEmitLightweightVersion>4.3.0</SystemReflectionEmitLightweightVersion>
2829
<SystemReflectionEmitVersion>4.3.0</SystemReflectionEmitVersion>
2930
<SystemRuntimeCompilerServicesUnsafeVersion>6.0.0</SystemRuntimeCompilerServicesUnsafeVersion>

eng/helix.proj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@
147147
<ItemGroup>
148148
<HelixWorkItem Include="%(ProjectsWithTargetFramework.Filename)">
149149
<PayloadDirectory>$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\</PayloadDirectory>
150-
<PayloadDirectory Condition="%(ProjectsWithTargetFramework.TargetFrameworks) == 'net461'">$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\win-x64</PayloadDirectory>
150+
<PayloadDirectory Condition="%(ProjectsWithTargetFramework.TargetFrameworks) == 'net462'">$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\win-x64</PayloadDirectory>
151151
<Command>dotnet exec --roll-forward Major --runtimeconfig %(ProjectsWithTargetFramework.Filename).runtimeconfig.json --depsfile %(ProjectsWithTargetFramework.Filename).deps.json $(HelixCorrelationPayloadPath)/xunit-runner/tools/netcoreapp2.0/xunit.console.dll %(ProjectsWithTargetFramework.Filename).dll -notrait Category=SkipInCI -xml testResults.xml</Command>
152-
<Command Condition="$(TestTargetFramework.EndsWith('net461')) And %(ProjectsWithTargetFramework.TargetFrameworks) != 'net6.0'">$(HelixCorrelationPayloadPath)/xunit-runner/tools/net461/xunit.console.exe %(ProjectsWithTargetFramework.Filename).dll -notrait Category=SkipInCI -xml testResults.xml</Command>
152+
<Command Condition="$(TestTargetFramework.EndsWith('net462')) And %(ProjectsWithTargetFramework.TargetFrameworks) != 'net6.0'">$(HelixCorrelationPayloadPath)/xunit-runner/tools/net462/xunit.console.exe %(ProjectsWithTargetFramework.Filename).dll -notrait Category=SkipInCI -xml testResults.xml</Command>
153153
<Timeout>01:00:00</Timeout>
154154
<Timeout Condition="$(HelixTargetQueues.ToLowerInvariant().Contains('osx'))">00:30:00</Timeout>
155155
<Timeout Condition="'$(WorkItemTimeout)' != ''">$(WorkItemTimeout)</Timeout>

src/Microsoft.ML.CpuMath/AvxIntrinsics.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
using System.Runtime.Intrinsics;
1616
using System.Runtime.Intrinsics.X86;
1717
using Microsoft.ML.Internal.CpuMath.Core;
18+
#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
1819
using nuint = System.UInt64;
20+
#pragma warning restore CS8981 // The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
1921

2022
namespace Microsoft.ML.Internal.CpuMath
2123
{
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using System.Numerics.Tensors;
8+
using Microsoft.ML.Internal.CpuMath.Core;
9+
10+
namespace Microsoft.ML.Internal.CpuMath
11+
{
12+
[BestFriend]
13+
internal static partial class CpuMathUtils
14+
{
15+
/// <summary>
16+
/// Adds a value to a destination.
17+
/// </summary>
18+
/// <param name="value">The value to add.</param>
19+
/// <param name="destination">The destination to add the value to.</param>
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static void Add(float value, Span<float> destination)
22+
{
23+
Contracts.AssertNonEmpty(destination);
24+
TensorPrimitives.Add(destination, value, destination);
25+
}
26+
27+
/// <summary>
28+
/// Scales a value to a destination.
29+
/// </summary>
30+
/// <param name="value">The value to add.</param>
31+
/// <param name="destination">The destination to add the value to.</param>
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public static void Scale(float value, Span<float> destination)
34+
{
35+
Contracts.AssertNonEmpty(destination);
36+
TensorPrimitives.Multiply(destination, value, destination);
37+
}
38+
39+
/// <summary>
40+
/// Scales a values by a source to a destination.
41+
/// destination = value * source
42+
/// </summary>
43+
/// <param name="value">The value to scale by.</param>
44+
/// <param name="source">The source values.</param>
45+
/// <param name="destination">The destination.</param>
46+
/// <param name="count">The count of items.</param>
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
public static void Scale(float value, ReadOnlySpan<float> source, Span<float> destination, int count)
49+
{
50+
Contracts.AssertNonEmpty(source);
51+
Contracts.AssertNonEmpty(destination);
52+
Contracts.Assert(count > 0);
53+
Contracts.Assert(count <= source.Length);
54+
Contracts.Assert(count <= destination.Length);
55+
56+
TensorPrimitives.Multiply(source.Slice(0, count), value, destination);
57+
}
58+
59+
/// <summary>
60+
/// Add to the destination from the source by scale.
61+
/// </summary>
62+
/// <param name="scale">The scale to add by.</param>
63+
/// <param name="source">The source values.</param>
64+
/// <param name="destination">The destination values.</param>
65+
/// <param name="count">The count of items.</param>
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
67+
public static void AddScale(float scale, ReadOnlySpan<float> source, Span<float> destination, int count)
68+
{
69+
Contracts.AssertNonEmpty(source);
70+
Contracts.AssertNonEmpty(destination);
71+
Contracts.Assert(count > 0);
72+
Contracts.Assert(count <= source.Length);
73+
Contracts.Assert(count <= destination.Length);
74+
75+
TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), destination);
76+
}
77+
78+
/// <summary>
79+
/// Add to the destination by scale and source into a new result.
80+
/// </summary>
81+
/// <param name="scale">The scale to add by.</param>
82+
/// <param name="source">The source values.</param>
83+
/// <param name="destination">The destination values.</param>
84+
/// <param name="result">A new collection of values to be returned.</param>
85+
/// <param name="count">The count of items.</param>
86+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
87+
public static void AddScaleCopy(float scale, ReadOnlySpan<float> source, ReadOnlySpan<float> destination, Span<float> result, int count)
88+
{
89+
Contracts.AssertNonEmpty(source);
90+
Contracts.AssertNonEmpty(destination);
91+
Contracts.AssertNonEmpty(result);
92+
Contracts.Assert(count > 0);
93+
Contracts.Assert(count <= source.Length);
94+
Contracts.Assert(count <= destination.Length);
95+
Contracts.Assert(count <= result.Length);
96+
97+
TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), result.Slice(0, count));
98+
}
99+
100+
/// <summary>
101+
/// Add from a source to a destination.
102+
/// </summary>
103+
/// <param name="source">The source values.</param>
104+
/// <param name="destination">The destination values.</param>
105+
/// <param name="count">The count of items.</param>
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107+
public static void Add(ReadOnlySpan<float> source, Span<float> destination, int count)
108+
{
109+
Contracts.AssertNonEmpty(source);
110+
Contracts.AssertNonEmpty(destination);
111+
Contracts.Assert(count > 0);
112+
Contracts.Assert(count <= source.Length);
113+
Contracts.Assert(count <= destination.Length);
114+
115+
TensorPrimitives.Add(source.Slice(0, count), destination.Slice(0, count), destination.Slice(0, count));
116+
}
117+
118+
/// <summary>
119+
/// Multiply each element with left and right elements.
120+
/// </summary>
121+
/// <param name="left">The left element.</param>
122+
/// <param name="right">The right element.</param>
123+
/// <param name="destination">The destination values.</param>
124+
/// <param name="count">The count of items.</param>
125+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
126+
public static void MulElementWise(ReadOnlySpan<float> left, ReadOnlySpan<float> right, Span<float> destination, int count)
127+
{
128+
Contracts.AssertNonEmpty(left);
129+
Contracts.AssertNonEmpty(right);
130+
Contracts.AssertNonEmpty(destination);
131+
Contracts.Assert(count > 0);
132+
Contracts.Assert(count <= left.Length);
133+
Contracts.Assert(count <= right.Length);
134+
Contracts.Assert(count <= destination.Length);
135+
136+
TensorPrimitives.Multiply(left.Slice(0, count), right.Slice(0, count), destination.Slice(0, count));
137+
}
138+
139+
/// <summary>
140+
/// Sum the values in the source.
141+
/// </summary>
142+
/// <param name="source">The source values.</param>
143+
/// <returns>The sum of all items in <paramref name="source"/>.</returns>
144+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
145+
public static float Sum(ReadOnlySpan<float> source)
146+
{
147+
Contracts.AssertNonEmpty(source);
148+
149+
return TensorPrimitives.Sum(source);
150+
}
151+
152+
/// <summary>
153+
/// Sum the squares of each item in the source.
154+
/// </summary>
155+
/// <param name="source">The source values.</param>
156+
/// <returns>The sum of the squares of all items in <paramref name="source"/>.</returns>
157+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158+
public static float SumSq(ReadOnlySpan<float> source)
159+
{
160+
Contracts.AssertNonEmpty(source);
161+
162+
return TensorPrimitives.SumOfSquares(source);
163+
}
164+
165+
/// <summary>
166+
/// Sum the absolute value of each item in the source.
167+
/// </summary>
168+
/// <param name="source">The source values.</param>
169+
/// <returns>The sum of all absolute value of the items in <paramref name="source"/>.</returns>
170+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
171+
public static float SumAbs(ReadOnlySpan<float> source)
172+
{
173+
Contracts.AssertNonEmpty(source);
174+
175+
return TensorPrimitives.SumOfMagnitudes(source);
176+
}
177+
178+
/// <summary>
179+
/// Returns the dot product of each item in the left and right spans.
180+
/// </summary>
181+
/// <param name="left">The left span.</param>
182+
/// <param name="right">The right span.</param>
183+
/// <param name="count">The count of items.</param>
184+
/// <returns>The dot product.</returns>
185+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
186+
public static float DotProductDense(ReadOnlySpan<float> left, ReadOnlySpan<float> right, int count)
187+
{
188+
Contracts.AssertNonEmpty(left);
189+
Contracts.AssertNonEmpty(right);
190+
Contracts.Assert(count > 0);
191+
Contracts.Assert(left.Length >= count);
192+
Contracts.Assert(right.Length >= count);
193+
194+
return TensorPrimitives.Dot(left.Slice(0, count), right.Slice(0, count));
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)