Skip to content

Changes some of the CPU Math implemenation from our current version to use the new TensorPrimitives package. #6875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
11a3a50
using tensor primitives
michaelgsharp Oct 12, 2023
3270d4c
added missing files
michaelgsharp Oct 12, 2023
ab1516a
some with indexes changed
michaelgsharp Nov 1, 2023
dbf574e
Initial swap for TensorPrimitives done
michaelgsharp Nov 2, 2023
549a8b4
Rebased and cleaned code
michaelgsharp Nov 2, 2023
cf545b8
more minor cleanup
michaelgsharp Nov 2, 2023
036f91b
added system.numerics.tensors version to props
michaelgsharp Nov 2, 2023
6629ec7
build fixes
michaelgsharp Nov 2, 2023
e07b34d
added net6 again
michaelgsharp Nov 2, 2023
c3c37c9
updates from PR comments
michaelgsharp Nov 8, 2023
0524ac1
fixed sumabsu
michaelgsharp Nov 8, 2023
699a54f
fixed baseline tests
michaelgsharp Nov 9, 2023
486b7ea
test fixes
michaelgsharp Nov 9, 2023
2623b60
fixed test failure for kmeans
michaelgsharp Nov 9, 2023
d7d8d62
changed decimal comparison
michaelgsharp Nov 14, 2023
6277b0c
updated more baselines
michaelgsharp Nov 14, 2023
dc62198
Test fixes.
michaelgsharp Nov 14, 2023
fbdc84d
template update
michaelgsharp Nov 14, 2023
35160fb
Test Fixes.
michaelgsharp Nov 14, 2023
8ed17e4
fixed performance test csproj
michaelgsharp Nov 14, 2023
d69e6e3
added baselines for linux arm/64
michaelgsharp Nov 14, 2023
1af712b
fixed linux arm baselines
michaelgsharp Nov 14, 2023
47fec68
fixed arm baselines
michaelgsharp Nov 14, 2023
6d33274
removed extra files
michaelgsharp Nov 14, 2023
21ca19b
Merge branch 'tensor-math2' of https://github.com/michaelgsharp/machi…
michaelgsharp Nov 14, 2023
c0c53fb
arm32 baselines updated
michaelgsharp Nov 15, 2023
54e876a
fixed arm baselines
michaelgsharp Nov 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .vsts-dotnet-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ jobs:
_configuration: Debug
_config_short: DFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
Release_Build:
_configuration: Release
_config_short: RFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
innerLoop: true
vsTestConfiguration: "/Framework:.NETCoreApp,Version=v4.0"
pool:
Expand Down
3 changes: 2 additions & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
Expand All @@ -13,6 +13,7 @@
<add key="dotnet5-roslyn" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
<add key="mlnet-daily" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/MachineLearning/nuget/v3/index.json" />
<add key="mlnet-assets" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/machinelearning-assets/nuget/v3/index.json" />
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources>
<clear />
Expand Down
4 changes: 2 additions & 2 deletions build/.night-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ jobs:
_configuration: Debug
_config_short: DFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
Release_Build:
_configuration: Release
_config_short: RFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
nightlyBuild: true
pool:
vmImage: windows-2019
Expand Down
4 changes: 2 additions & 2 deletions build/.outer-loop-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ jobs:
_configuration: Debug
_config_short: DFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
Release_Build:
_configuration: Release
_config_short: RFX
_includeBenchmarkData: false
_targetFramework: net461
_targetFramework: net462
pool:
vmImage: windows-2019

Expand Down
8 changes: 8 additions & 0 deletions build/ci/job-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ jobs:
contents: |
artifacts/TestResults/**
targetFolder: $(Build.ArtifactStagingDirectory)
- task: CopyFiles@2
displayName: Stage baseline files
condition: not(succeeded())
inputs:
sourceFolder: $(Build.SourcesDirectory)
contents: |
artifacts/bin/**/TestOutput/**
targetFolder: $(Build.ArtifactStagingDirectory)
- task: CopyFiles@2
displayName: Stage process dump and pdb if any
condition: not(succeeded())
Expand Down
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<SystemConfigurationConfigurationManagerVersion>6.0.1</SystemConfigurationConfigurationManagerVersion>
<SystemIOFileSystemAccessControl>4.5.0</SystemIOFileSystemAccessControl>
<SystemMemoryVersion>4.5.5</SystemMemoryVersion>
<SystemNumericsTensorsVersion>8.0.0-rtm.23523.3</SystemNumericsTensorsVersion>
<SystemReflectionEmitLightweightVersion>4.3.0</SystemReflectionEmitLightweightVersion>
<SystemReflectionEmitVersion>4.3.0</SystemReflectionEmitVersion>
<SystemRuntimeCompilerServicesUnsafeVersion>6.0.0</SystemRuntimeCompilerServicesUnsafeVersion>
Expand Down
4 changes: 2 additions & 2 deletions eng/helix.proj
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@
<ItemGroup>
<HelixWorkItem Include="%(ProjectsWithTargetFramework.Filename)">
<PayloadDirectory>$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\</PayloadDirectory>
<PayloadDirectory Condition="%(ProjectsWithTargetFramework.TargetFrameworks) == 'net461'">$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\win-x64</PayloadDirectory>
<PayloadDirectory Condition="%(ProjectsWithTargetFramework.TargetFrameworks) == 'net462'">$(BUILD_SOURCESDIRECTORY)\artifacts\bin\%(ProjectsWithTargetFramework.Filename)\$(BuildConfig)\%(ProjectsWithTargetFramework.TargetFrameworks)\win-x64</PayloadDirectory>
<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>
<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>
<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>
<Timeout>01:00:00</Timeout>
<Timeout Condition="$(HelixTargetQueues.ToLowerInvariant().Contains('osx'))">00:30:00</Timeout>
<Timeout Condition="'$(WorkItemTimeout)' != ''">$(WorkItemTimeout)</Timeout>
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.ML.CpuMath/AvxIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using Microsoft.ML.Internal.CpuMath.Core;
#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters. Such names may become reserved for the language.
using nuint = System.UInt64;
#pragma warning restore CS8981 // The type name only contains lower-cased ascii characters. Such names may become reserved for the language.

namespace Microsoft.ML.Internal.CpuMath
{
Expand Down
197 changes: 197 additions & 0 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;
using System.Numerics.Tensors;
using Microsoft.ML.Internal.CpuMath.Core;

namespace Microsoft.ML.Internal.CpuMath
{
[BestFriend]
internal static partial class CpuMathUtils
{
/// <summary>
/// Adds a value to a destination.
/// </summary>
/// <param name="value">The value to add.</param>
/// <param name="destination">The destination to add the value to.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(float value, Span<float> destination)
{
Contracts.AssertNonEmpty(destination);
TensorPrimitives.Add(destination, value, destination);
}

/// <summary>
/// Scales a value to a destination.
/// </summary>
/// <param name="value">The value to add.</param>
/// <param name="destination">The destination to add the value to.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Scale(float value, Span<float> destination)
{
Contracts.AssertNonEmpty(destination);
TensorPrimitives.Multiply(destination, value, destination);
}

/// <summary>
/// Scales a values by a source to a destination.
/// destination = value * source
/// </summary>
/// <param name="value">The value to scale by.</param>
/// <param name="source">The source values.</param>
/// <param name="destination">The destination.</param>
/// <param name="count">The count of items.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Scale(float value, ReadOnlySpan<float> source, Span<float> destination, int count)
{
Contracts.AssertNonEmpty(source);
Contracts.AssertNonEmpty(destination);
Contracts.Assert(count > 0);
Contracts.Assert(count <= source.Length);
Contracts.Assert(count <= destination.Length);

TensorPrimitives.Multiply(source.Slice(0, count), value, destination);
}

/// <summary>
/// Add to the destination from the source by scale.
/// </summary>
/// <param name="scale">The scale to add by.</param>
/// <param name="source">The source values.</param>
/// <param name="destination">The destination values.</param>
/// <param name="count">The count of items.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddScale(float scale, ReadOnlySpan<float> source, Span<float> destination, int count)
{
Contracts.AssertNonEmpty(source);
Contracts.AssertNonEmpty(destination);
Contracts.Assert(count > 0);
Contracts.Assert(count <= source.Length);
Contracts.Assert(count <= destination.Length);

TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), destination);
}

/// <summary>
/// Add to the destination by scale and source into a new result.
/// </summary>
/// <param name="scale">The scale to add by.</param>
/// <param name="source">The source values.</param>
/// <param name="destination">The destination values.</param>
/// <param name="result">A new collection of values to be returned.</param>
/// <param name="count">The count of items.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddScaleCopy(float scale, ReadOnlySpan<float> source, ReadOnlySpan<float> destination, Span<float> result, int count)
{
Contracts.AssertNonEmpty(source);
Contracts.AssertNonEmpty(destination);
Contracts.AssertNonEmpty(result);
Contracts.Assert(count > 0);
Contracts.Assert(count <= source.Length);
Contracts.Assert(count <= destination.Length);
Contracts.Assert(count <= result.Length);

TensorPrimitives.MultiplyAdd(source.Slice(0, count), scale, destination.Slice(0, count), result.Slice(0, count));
}

/// <summary>
/// Add from a source to a destination.
/// </summary>
/// <param name="source">The source values.</param>
/// <param name="destination">The destination values.</param>
/// <param name="count">The count of items.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(ReadOnlySpan<float> source, Span<float> destination, int count)
{
Contracts.AssertNonEmpty(source);
Contracts.AssertNonEmpty(destination);
Contracts.Assert(count > 0);
Contracts.Assert(count <= source.Length);
Contracts.Assert(count <= destination.Length);

TensorPrimitives.Add(source.Slice(0, count), destination.Slice(0, count), destination.Slice(0, count));
}

/// <summary>
/// Multiply each element with left and right elements.
/// </summary>
/// <param name="left">The left element.</param>
/// <param name="right">The right element.</param>
/// <param name="destination">The destination values.</param>
/// <param name="count">The count of items.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MulElementWise(ReadOnlySpan<float> left, ReadOnlySpan<float> right, Span<float> destination, int count)
{
Contracts.AssertNonEmpty(left);
Contracts.AssertNonEmpty(right);
Contracts.AssertNonEmpty(destination);
Contracts.Assert(count > 0);
Contracts.Assert(count <= left.Length);
Contracts.Assert(count <= right.Length);
Contracts.Assert(count <= destination.Length);

TensorPrimitives.Multiply(left.Slice(0, count), right.Slice(0, count), destination.Slice(0, count));
}

/// <summary>
/// Sum the values in the source.
/// </summary>
/// <param name="source">The source values.</param>
/// <returns>The sum of all items in <paramref name="source"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Sum(ReadOnlySpan<float> source)
{
Contracts.AssertNonEmpty(source);

return TensorPrimitives.Sum(source);
}

/// <summary>
/// Sum the squares of each item in the source.
/// </summary>
/// <param name="source">The source values.</param>
/// <returns>The sum of the squares of all items in <paramref name="source"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SumSq(ReadOnlySpan<float> source)
{
Contracts.AssertNonEmpty(source);

return TensorPrimitives.SumOfSquares(source);
}

/// <summary>
/// Sum the absolute value of each item in the source.
/// </summary>
/// <param name="source">The source values.</param>
/// <returns>The sum of all absolute value of the items in <paramref name="source"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SumAbs(ReadOnlySpan<float> source)
{
Contracts.AssertNonEmpty(source);

return TensorPrimitives.SumOfMagnitudes(source);
}

/// <summary>
/// Returns the dot product of each item in the left and right spans.
/// </summary>
/// <param name="left">The left span.</param>
/// <param name="right">The right span.</param>
/// <param name="count">The count of items.</param>
/// <returns>The dot product.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DotProductDense(ReadOnlySpan<float> left, ReadOnlySpan<float> right, int count)
{
Contracts.AssertNonEmpty(left);
Contracts.AssertNonEmpty(right);
Contracts.Assert(count > 0);
Contracts.Assert(left.Length >= count);
Contracts.Assert(right.Length >= count);

return TensorPrimitives.Dot(left.Slice(0, count), right.Slice(0, count));
}
}
}
Loading