-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
11a3a50
using tensor primitives
michaelgsharp 3270d4c
added missing files
michaelgsharp ab1516a
some with indexes changed
michaelgsharp dbf574e
Initial swap for TensorPrimitives done
michaelgsharp 549a8b4
Rebased and cleaned code
michaelgsharp cf545b8
more minor cleanup
michaelgsharp 036f91b
added system.numerics.tensors version to props
michaelgsharp 6629ec7
build fixes
michaelgsharp e07b34d
added net6 again
michaelgsharp c3c37c9
updates from PR comments
michaelgsharp 0524ac1
fixed sumabsu
michaelgsharp 699a54f
fixed baseline tests
michaelgsharp 486b7ea
test fixes
michaelgsharp 2623b60
fixed test failure for kmeans
michaelgsharp d7d8d62
changed decimal comparison
michaelgsharp 6277b0c
updated more baselines
michaelgsharp dc62198
Test fixes.
michaelgsharp fbdc84d
template update
michaelgsharp 35160fb
Test Fixes.
michaelgsharp 8ed17e4
fixed performance test csproj
michaelgsharp d69e6e3
added baselines for linux arm/64
michaelgsharp 1af712b
fixed linux arm baselines
michaelgsharp 47fec68
fixed arm baselines
michaelgsharp 6d33274
removed extra files
michaelgsharp 21ca19b
Merge branch 'tensor-math2' of https://github.com/michaelgsharp/machi…
michaelgsharp c0c53fb
arm32 baselines updated
michaelgsharp 54e876a
fixed arm baselines
michaelgsharp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.