Skip to content

Commit

Permalink
Add unit tests for tensors
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Dec 11, 2023
1 parent c63e5dc commit 98a9355
Show file tree
Hide file tree
Showing 21 changed files with 727 additions and 28 deletions.
16 changes: 16 additions & 0 deletions NetFabric.Numerics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetFabric.Numerics.Geodesy.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFabric.Numerics.Tensors", "src\NetFabric.Numerics.Tensors\NetFabric.Numerics.Tensors.csproj", "{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EC0C1351-970E-48A9-BDA3-3E5E2B095402}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFabric.Numerics.Tensors.UnitTests", "src\NetFabric.Numerics.Tensors.UnitTests\NetFabric.Numerics.Tensors.UnitTests.csproj", "{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetFabric.Numerics.Tensors.Benchmarks", "src\NetFabric.Numerics.Tensors.Benchmarks\NetFabric.Numerics.Tensors.Benchmarks.csproj", "{A8467A84-F9DD-4B6C-91BB-CF39029331A4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -96,6 +102,14 @@ Global
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D2EE0A5-32A2-4019-AD55-7E9B70B76D67}.Release|Any CPU.Build.0 = Release|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5}.Release|Any CPU.Build.0 = Release|Any CPU
{A8467A84-F9DD-4B6C-91BB-CF39029331A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8467A84-F9DD-4B6C-91BB-CF39029331A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8467A84-F9DD-4B6C-91BB-CF39029331A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8467A84-F9DD-4B6C-91BB-CF39029331A4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -104,6 +118,8 @@ Global
{A04C4E36-5F33-43F8-A7EE-15D81C7F8A58} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{BA0837F2-730C-4537-B08B-5FFD2D91AF30} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{83BDB68A-4F4B-4D7D-A7C7-55CC9F03657B} = {762517CA-3F22-4DC6-955C-4F4FC69EB670}
{31E6CCB0-E04A-4947-BF74-E8E70D5594E5} = {EC0C1351-970E-48A9-BDA3-3E5E2B095402}
{A8467A84-F9DD-4B6C-91BB-CF39029331A4} = {EC0C1351-970E-48A9-BDA3-3E5E2B095402}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AACC30AE-BE57-461F-817B-2AE0F001A33F}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BenchmarkDotNet.Attributes;

namespace NetFabric.Numerics.Rectangular2D.Benchmarks;
namespace NetFabric.Numerics.Tensors.Benchmarks;

public class TensorAddBenchmarks
public class AddBenchmarks
{
short[]? arrayShort;
int[]? arrayInt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BenchmarkDotNet.Attributes;

namespace NetFabric.Numerics.Rectangular2D.Benchmarks;
namespace NetFabric.Numerics.Tensors.Benchmarks;

public class TensorAddValueBenchmarks
public class AddValueBenchmarks
{
short[]? arrayShort;
int[]? arrayInt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<RootNamespace>NetFabric.Numerics.Tensors.Benchmarks</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetFabric.Numerics.Tensors\NetFabric.Numerics.Tensors.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions src/NetFabric.Numerics.Tensors.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using System.Runtime.Intrinsics;

var config = DefaultConfig.Instance
.WithSummaryStyle(SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend))
.HideColumns(Column.EnvironmentVariables, Column.RatioSD, Column.Error)
// .AddDiagnoser(new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig
// (exportGithubMarkdown: true, printInstructionAddresses: false)))
.AddJob(Job.Default.WithId("Scalar")
.WithEnvironmentVariable("DOTNET_EnableHWIntrinsic", "0")
.AsBaseline());

if (Vector128.IsHardwareAccelerated)
{
config = config
.AddJob(Job.Default.WithId("Vector128")
.WithEnvironmentVariable("DOTNET_EnableAVX2", "0")
.WithEnvironmentVariable("DOTNET_EnableAVX512F", "0"));
}
if (Vector256.IsHardwareAccelerated)
{
config = config
.AddJob(Job.Default.WithId("Vector256")
.WithEnvironmentVariable("DOTNET_EnableAVX512F", "0"));
}
if (Vector512.IsHardwareAccelerated)
{
config = config
.AddJob(Job.Default.WithId("Vector512"));
}

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using BenchmarkDotNet.Attributes;

namespace NetFabric.Numerics.Rectangular2D.Benchmarks;
namespace NetFabric.Numerics.Tensors.Benchmarks;

public class TensorSumBenchmarks
public class SumBenchmarks
{
short[]? arrayShort;
int[]? arrayInt;
Expand Down
100 changes: 100 additions & 0 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddMultiplyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddMultiplyTests
{
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Short_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (short)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (short)(value + 2)).ToArray();
var result = new short[count];
var expected = Enumerable.Range(0, count).Select(value => (short)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<short>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).ToArray();
var y = Enumerable.Range(0, count).Select(value => value + 1).ToArray();
var z = Enumerable.Range(0, count).Select(value => value + 2).ToArray();
var result = new int[count];
var expected = Enumerable.Range(0, count).Select(value => (value + value + 1) * (value + 2)).ToArray();

// act
Tensor.AddMultiply<int>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (long)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (long)(value + 2)).ToArray();
var result = new long[count];
var expected = Enumerable.Range(0, count).Select(value => (long)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<long>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (float)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (float)(value + 2)).ToArray();
var result = new float[count];
var expected = Enumerable.Range(0, count).Select(value => (float)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<float>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void AddMultiply_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (double)(value + 1)).ToArray();
var z = Enumerable.Range(0, count).Select(value => (double)(value + 2)).ToArray();
var result = new double[count];
var expected = Enumerable.Range(0, count).Select(value => (double)((value + value + 1) * (value + 2))).ToArray();

// act
Tensor.AddMultiply<double>(x, y, z, result);

// assert
result.Should().Equal(expected);
}

}
95 changes: 95 additions & 0 deletions src/NetFabric.Numerics.Tensors.UnitTests/AddTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Linq;

namespace NetFabric.Numerics.Tensors.UnitTests;

public class AddTests
{
public static TheoryData<int> AddData
=> new() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 };

[Theory]
[MemberData(nameof(AddData))]
public void Add_Short_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (short)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (short)(value + 1)).ToArray();
var result = new short[count];
var expected = Enumerable.Range(0, count).Select(value => (short)(value + value + 1)).ToArray();

// act
Tensor.Add<short>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Int_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).ToArray();
var y = Enumerable.Range(0, count).Select(value => value + 1).ToArray();
var result = new int[count];
var expected = Enumerable.Range(0, count).Select(value => value + value + 1).ToArray();

// act
Tensor.Add<int>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Long_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (long)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (long)(value + 1)).ToArray();
var result = new long[count];
var expected = Enumerable.Range(0, count).Select(value => (long)(value + value + 1)).ToArray();

// act
Tensor.Add<long>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Float_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (float)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (float)(value + 1)).ToArray();
var result = new float[count];
var expected = Enumerable.Range(0, count).Select(value => (float)(value + value + 1)).ToArray();

// act
Tensor.Add<float>(x, y, result);

// assert
result.Should().Equal(expected);
}

[Theory]
[MemberData(nameof(AddData))]
public void Add_Double_Should_Succeed(int count)
{
// arrange
var x = Enumerable.Range(0, count).Select(value => (double)value).ToArray();
var y = Enumerable.Range(0, count).Select(value => (double)(value + 1)).ToArray();
var result = new double[count];
var expected = Enumerable.Range(0, count).Select(value => (double)(value + value + 1)).ToArray();

// act
Tensor.Add<double>(x, y, result);

// assert
result.Should().Equal(expected);
}

}
Loading

0 comments on commit 98a9355

Please sign in to comment.