Skip to content

Commit 4b7c754

Browse files
michaelgsharptannergoodingViktorHoferstephentoubtmds
authored
[release/8.0] Numerics and Tensors backport (#92245)
* added Bcl.Numerics * Adding a naive implementation of various primitive tensor operations (#91228) * Adding a naive implementation of various primitive tensor operations * Adding tests covering the new tensor primitives APIs * Adding tensor primitives APIs to the ref assembly * Allow .NET Framework to build/run * Sync TFMs between ref and src, csproj simplication and clean-up * Apply suggestions from code review Co-authored-by: Viktor Hofer <[email protected]> * Don't use var * Fix the S.N.Tensors readme and remove the file marking it as non-shipping --------- Co-authored-by: Viktor Hofer <[email protected]> Co-authored-by: Michael Sharp <[email protected]> * Start vectorizing TensorPrimitives (#91596) * Start vectorizing TensorPrimitives Just does two functions to establish the files into which the rest of the implementations can be moved. * 6 more naive methods for Tensor Primitives. (#92142) * 6 more naive methods * updates from pr comments * Add remaining set of TensorPrimitives APIs for .NET 8 (#92154) * Add remaining set of TensorPrimitives APIs for .NET 8 Adds non-vectorized implementations of: - Max - Min - MaxMagnitude - MinMagnitude - IndexOfMax - IndexOfMin - IndexOfMaxMagnitude - ConvertToHalf (only on .NET Core) - ConvertToSingle (only on .NET Core) - IndexOfMinMagnitude Adds vectorized implementations of: - Sum - SumOfSquares - SumOfMagnitudes - Product - ProductOfSums - ProductOfDifferences Also includes the helpers that'll make it trivial to vectorize Dot. Beyond vectorizing the non-vectorized ones, the vectorized implementations should be improved further, including: - Handling alignment better - Vectorizing the remainder that doesn't fit in a vector rather than falling back to scalar * Cleanup after previous PR, vectorize CosineSimilarity/Dot/L2Normalize/Distance, add tests * Address PR feedback, and fix a few other issues * Fix TensorPrimitives.CosineSimilarity to use vectorized implementations (#92204) * Fixed duplicated code from merge. * New Microsoft.BCL.Numerics package (#91074) * bcl numberics library added * bcl done * added explicit 2.1 target * Minor doc updates * Apply suggestions from code review Co-authored-by: Viktor Hofer <[email protected]> * fixes from PR comments * minor csproj fixes * fixed ref target frameworks * minor ref csproj updates * minor csproj updates --------- Co-authored-by: Viktor Hofer <[email protected]> * Microsoft.Bcl.Numerics.Tests: fix restore failure when DotNetBuildFromSource. (#91402) * Microsoft.Bcl.Numerics.Tests: fix restore failure when DotNetBuildFromSource. * Use NetCoreAppCurrent. * Try fix CI test failures. --------- Co-authored-by: Tanner Gooding <[email protected]> Co-authored-by: Viktor Hofer <[email protected]> Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Tom Deseyn <[email protected]>
1 parent e00e6aa commit 4b7c754

Some content is hidden

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

46 files changed

+6091
-23389
lines changed

src/coreclr/jit/utils.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -2734,7 +2734,7 @@ float FloatingPointUtils::maximumNumber(float x, float y)
27342734
//
27352735
// It propagates NaN inputs back to the caller and
27362736
// otherwise returns the lesser of the inputs. It
2737-
// treats +0 as lesser than -0 as per the specification.
2737+
// treats +0 as greater than -0 as per the specification.
27382738
//
27392739
// Arguments:
27402740
// val1 - left operand
@@ -2763,7 +2763,7 @@ double FloatingPointUtils::minimum(double val1, double val2)
27632763
//
27642764
// It propagates NaN inputs back to the caller and
27652765
// otherwise returns the input with a lesser magnitude.
2766-
// It treats +0 as lesser than -0 as per the specification.
2766+
// It treats +0 as greater than -0 as per the specification.
27672767
//
27682768
// Arguments:
27692769
// x - left operand
@@ -2856,7 +2856,7 @@ double FloatingPointUtils::minimumNumber(double x, double y)
28562856
//
28572857
// It propagates NaN inputs back to the caller and
28582858
// otherwise returns the lesser of the inputs. It
2859-
// treats +0 as lesser than -0 as per the specification.
2859+
// treats +0 as greater than -0 as per the specification.
28602860
//
28612861
// Arguments:
28622862
// val1 - left operand
@@ -2885,7 +2885,7 @@ float FloatingPointUtils::minimum(float val1, float val2)
28852885
//
28862886
// It propagates NaN inputs back to the caller and
28872887
// otherwise returns the input with a lesser magnitude.
2888-
// It treats +0 as lesser than -0 as per the specification.
2888+
// It treats +0 as greater than -0 as per the specification.
28892889
//
28902890
// Arguments:
28912891
// x - left operand

src/libraries/Common/tests/TestUtilities/System/Buffers/BoundedMemory.Unix.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public UnixImplementation(int elementCount)
3030

3131
public override bool IsReadonly => false;
3232

33+
public override int Length => _elementCount;
34+
3335
public override Memory<T> Memory => _memoryManager.Memory;
3436

3537
public override Span<T> Span
@@ -83,10 +85,7 @@ protected override void Dispose(bool disposing)
8385
// no-op; the handle will be disposed separately
8486
}
8587

86-
public override Span<T> GetSpan()
87-
{
88-
throw new NotImplementedException();
89-
}
88+
public override Span<T> GetSpan() => _impl.Span;
9089

9190
public override MemoryHandle Pin(int elementIndex)
9291
{

src/libraries/Common/tests/TestUtilities/System/Buffers/BoundedMemory.Windows.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ internal WindowsImplementation(VirtualAllocHandle handle, int byteOffsetIntoHand
8181

8282
public override bool IsReadonly => (Protection != VirtualAllocProtection.PAGE_READWRITE);
8383

84+
public override int Length => _elementCount;
85+
8486
internal VirtualAllocProtection Protection
8587
{
8688
get
@@ -189,10 +191,7 @@ protected override void Dispose(bool disposing)
189191
// no-op; the handle will be disposed separately
190192
}
191193

192-
public override Span<T> GetSpan()
193-
{
194-
throw new NotImplementedException();
195-
}
194+
public override Span<T> GetSpan() => _impl.Span;
196195

197196
public override MemoryHandle Pin(int elementIndex)
198197
{

src/libraries/Common/tests/TestUtilities/System/Buffers/BoundedMemory.cs

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public abstract class BoundedMemory<T> : IDisposable where T : unmanaged
1414
/// </summary>
1515
public abstract bool IsReadonly { get; }
1616

17+
/// <summary>Gets the length of the <see cref="BoundedMemory{T}"/> instance.</summary>
18+
public abstract int Length { get; }
19+
1720
/// <summary>
1821
/// Gets the <see cref="Memory{Byte}"/> which represents this native memory.
1922
/// This <see cref="BoundedMemory{T}"/> instance must be kept alive while working with the <see cref="Memory{Byte}"/>.
@@ -44,5 +47,23 @@ public abstract class BoundedMemory<T> : IDisposable where T : unmanaged
4447
/// OS does not support marking the memory block as read+write.
4548
/// </summary>
4649
public abstract void MakeWriteable();
50+
51+
/// <summary>
52+
/// Gets the <see cref="Span{Byte}"/> which represents this native memory.
53+
/// This <see cref="BoundedMemory{T}"/> instance must be kept alive while working with the <see cref="Span{Byte}"/>.
54+
/// </summary>
55+
public static implicit operator Span<T>(BoundedMemory<T> boundedMemory) => boundedMemory.Span;
56+
57+
/// <summary>
58+
/// Gets the <see cref="ReadOnlySpan{Byte}"/> which represents this native memory.
59+
/// This <see cref="BoundedMemory{T}"/> instance must be kept alive while working with the <see cref="ReadOnlySpan{Byte}"/>.
60+
/// </summary>
61+
public static implicit operator ReadOnlySpan<T>(BoundedMemory<T> boundedMemory) => boundedMemory.Span;
62+
63+
/// <summary>
64+
/// Gets a reference to the element at the specified index.
65+
/// This <see cref="BoundedMemory{T}"/> instance must be kept alive while working with the reference.
66+
/// </summary>
67+
public ref T this[int index] => ref Span[index];
4768
}
4869
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUtilities", "..\Common\tests\TestUtilities\TestUtilities.csproj", "{CAEE0409-CCC3-4EA6-AB54-177FD305D42D}"
3+
EndProject
4+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.Numerics", "ref\Microsoft.Bcl.Numerics.csproj", "{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE}"
5+
EndProject
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.Numerics", "src\Microsoft.Bcl.Numerics.csproj", "{4D4BED71-8904-4A74-88CD-63D002CCACD0}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bcl.Numerics.Tests", "tests\Microsoft.Bcl.Numerics.Tests.csproj", "{51D9518A-464D-4257-9567-3BDCFF24F3EE}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComInterfaceGenerator", "..\System.Runtime.InteropServices\gen\ComInterfaceGenerator\ComInterfaceGenerator.csproj", "{E30F71EB-6C3B-4052-84F7-36EAA178A45E}"
11+
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryImportGenerator", "..\System.Runtime.InteropServices\gen\LibraryImportGenerator\LibraryImportGenerator.csproj", "{0AE44453-273B-4F0E-9901-A87891A73C1B}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Interop.SourceGeneration", "..\System.Runtime.InteropServices\gen\Microsoft.Interop.SourceGeneration\Microsoft.Interop.SourceGeneration.csproj", "{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB}"
15+
EndProject
16+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{6614EF7F-23FC-4809-AFF5-1ADBF1B6422C}"
17+
EndProject
18+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{111B1B5B-A004-4C05-9A8C-E0931DADA5FB}"
19+
EndProject
20+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{85204CF5-0C88-4BBB-9E70-D8CCED82ED3D}"
21+
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gen", "gen", "{D6A9108E-553B-445E-A037-FA4F3140A279}"
23+
EndProject
24+
Global
25+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
26+
Debug|Any CPU = Debug|Any CPU
27+
Release|Any CPU = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
30+
{CAEE0409-CCC3-4EA6-AB54-177FD305D42D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{CAEE0409-CCC3-4EA6-AB54-177FD305D42D}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{CAEE0409-CCC3-4EA6-AB54-177FD305D42D}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{CAEE0409-CCC3-4EA6-AB54-177FD305D42D}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{4D4BED71-8904-4A74-88CD-63D002CCACD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{4D4BED71-8904-4A74-88CD-63D002CCACD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{4D4BED71-8904-4A74-88CD-63D002CCACD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{4D4BED71-8904-4A74-88CD-63D002CCACD0}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{51D9518A-464D-4257-9567-3BDCFF24F3EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{51D9518A-464D-4257-9567-3BDCFF24F3EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{51D9518A-464D-4257-9567-3BDCFF24F3EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{51D9518A-464D-4257-9567-3BDCFF24F3EE}.Release|Any CPU.Build.0 = Release|Any CPU
46+
{E30F71EB-6C3B-4052-84F7-36EAA178A45E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{E30F71EB-6C3B-4052-84F7-36EAA178A45E}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{E30F71EB-6C3B-4052-84F7-36EAA178A45E}.Release|Any CPU.ActiveCfg = Release|Any CPU
49+
{E30F71EB-6C3B-4052-84F7-36EAA178A45E}.Release|Any CPU.Build.0 = Release|Any CPU
50+
{0AE44453-273B-4F0E-9901-A87891A73C1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{0AE44453-273B-4F0E-9901-A87891A73C1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{0AE44453-273B-4F0E-9901-A87891A73C1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{0AE44453-273B-4F0E-9901-A87891A73C1B}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
57+
{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB}.Release|Any CPU.Build.0 = Release|Any CPU
58+
EndGlobalSection
59+
GlobalSection(SolutionProperties) = preSolution
60+
HideSolutionNode = FALSE
61+
EndGlobalSection
62+
GlobalSection(NestedProjects) = preSolution
63+
{CAEE0409-CCC3-4EA6-AB54-177FD305D42D} = {6614EF7F-23FC-4809-AFF5-1ADBF1B6422C}
64+
{51D9518A-464D-4257-9567-3BDCFF24F3EE} = {6614EF7F-23FC-4809-AFF5-1ADBF1B6422C}
65+
{73E7C25C-AEBC-4F4F-B8D1-0CC49D5B92DE} = {111B1B5B-A004-4C05-9A8C-E0931DADA5FB}
66+
{4D4BED71-8904-4A74-88CD-63D002CCACD0} = {85204CF5-0C88-4BBB-9E70-D8CCED82ED3D}
67+
{E30F71EB-6C3B-4052-84F7-36EAA178A45E} = {D6A9108E-553B-445E-A037-FA4F3140A279}
68+
{0AE44453-273B-4F0E-9901-A87891A73C1B} = {D6A9108E-553B-445E-A037-FA4F3140A279}
69+
{D0F1936C-CF7C-4448-9F90-B9DEABE89EBB} = {D6A9108E-553B-445E-A037-FA4F3140A279}
70+
EndGlobalSection
71+
GlobalSection(ExtensibilityGlobals) = postSolution
72+
SolutionGuid = {A835CEDB-E9E2-49EE-8499-BD7FDD984E53}
73+
EndGlobalSection
74+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
4+
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.MathF))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// ------------------------------------------------------------------------------
4+
// Changes to this file must follow the https://aka.ms/api-review process.
5+
// ------------------------------------------------------------------------------
6+
7+
namespace System
8+
{
9+
public static partial class MathF
10+
{
11+
public const float E = 2.7182817f;
12+
public const float PI = 3.1415927f;
13+
public static float Abs(float x) { throw null; }
14+
public static float Acos(float x) { throw null; }
15+
public static float Asin(float x) { throw null; }
16+
public static float Atan(float x) { throw null; }
17+
public static float Atan2(float y, float x) { throw null; }
18+
public static float Ceiling(float x) { throw null; }
19+
public static float Cos(float x) { throw null; }
20+
public static float Cosh(float x) { throw null; }
21+
public static float Exp(float x) { throw null; }
22+
public static float Floor(float x) { throw null; }
23+
public static float IEEERemainder(float x, float y) { throw null; }
24+
public static float Log(float x) { throw null; }
25+
public static float Log(float x, float y) { throw null; }
26+
public static float Log10(float x) { throw null; }
27+
public static float Max(float x, float y) { throw null; }
28+
public static float Min(float x, float y) { throw null; }
29+
public static float Pow(float x, float y) { throw null; }
30+
public static float Round(float x) { throw null; }
31+
public static float Round(float x, int digits) { throw null; }
32+
public static float Round(float x, int digits, System.MidpointRounding mode) { throw null; }
33+
public static float Round(float x, System.MidpointRounding mode) { throw null; }
34+
public static int Sign(float x) { throw null; }
35+
public static float Sin(float x) { throw null; }
36+
public static float Sinh(float x) { throw null; }
37+
public static float Sqrt(float x) { throw null; }
38+
public static float Tan(float x) { throw null; }
39+
public static float Tanh(float x) { throw null; }
40+
public static float Truncate(float x) { throw null; }
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);netstandard2.1</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="Microsoft.Bcl.Numerics.cs" Condition="'$(TargetFramework)' != 'netstandard2.1'" />
9+
<Compile Include="Microsoft.Bcl.Numerics.Forwards.cs" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum);netstandard2.1</TargetFrameworks>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<IsPackable>true</IsPackable>
7+
8+
<!-- This assembly should never be placed inbox as it is only for downlevel compatibility. -->
9+
<PackageDescription>Provides the System.MathF for .NET Standard 2.0</PackageDescription>
10+
<!-- Disabling baseline validation since this is a brand new package.
11+
Once this package has shipped a stable version, the following line
12+
should be removed in order to re-enable validation. -->
13+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
14+
</PropertyGroup>
15+
16+
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
17+
<Compile Include="System\MathF.cs" />
18+
</ItemGroup>
19+
20+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
21+
<Compile Include="System\Microsoft.Bcl.Numerics.Forwards.cs" />
22+
</ItemGroup>
23+
24+
</Project>

src/libraries/Microsoft.Bcl.Numerics/src/PACKAGE.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ As of .NET Core 2.0 and .NET Standard 2.1, the C# language has support for math
1010

1111
## How to Use
1212

13-
<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package -->
14-
1513
```C#
1614
using System;
1715

0 commit comments

Comments
 (0)