Skip to content

Commit

Permalink
Introduced common FP16 capability. (#1074)
Browse files Browse the repository at this point in the history
* Added support for FP16 capabilities on all accelerators.
* Adapted tests to pay attention to FP16 capabilities.
* Adapted CPUDevice to support shared CPUCapabilities.
  • Loading branch information
m4rs-mt authored Sep 1, 2023
1 parent 220af93 commit f75ecb4
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 37 deletions.
30 changes: 23 additions & 7 deletions Src/ILGPU.Tests/CompareFloatOperations.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: CompareFloatOperations.tt/CompareFloatOperations.cs
Expand Down Expand Up @@ -59,6 +59,7 @@ namespace ILGPU.Tests

<# foreach (var (operationName, infix) in operationConfigurations) { #>
<# foreach (var type in FloatTypes) { #>
<# bool isFP16 = type == FloatTypes[0]; #>
<# var floatRanges = GetFloatRanges(type, true, true, true, false); #>
<# var baseName = "_" + operationName + "_" + type.Name; #>
<# var testName = "CompareOperation" + baseName; #>
Expand All @@ -83,30 +84,38 @@ namespace ILGPU.Tests
<# } #>
};

<# if (isFP16) { #>
[SkippableTheory]
<# } else { #>
[Theory]
<# } #>
[MemberData(nameof(<#= testName #>Data))]
[KernelMethod(nameof(<#= kernelName #>))]
public void <#= testName #>(
<#= type.Type #> left,
<#= type.Type #> right)
{
const int length = 32;
using var a = Accelerator.Allocate1D<<#= type.Type #>>(length);
using var b = Accelerator.Allocate1D<<#= type.Type #>>(length);
using var c = Accelerator.Allocate1D<int>(length);
<# if (isFP16) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
const int Length = 32;
using var a = Accelerator.Allocate1D<<#= type.Type #>>(Length);
using var b = Accelerator.Allocate1D<<#= type.Type #>>(Length);
using var c = Accelerator.Allocate1D<int>(Length);
Initialize(a.View, left);
Initialize(b.View, right);
Execute(length, a.View, b.View, c.View);
Execute(Length, a.View, b.View, c.View);

var result = left <#= infix #> right ? 1 : 0;
var reference = Enumerable.Repeat(result, length).ToArray();
var reference = Enumerable.Repeat(result, Length).ToArray();
Verify(c.View, reference);
}

<# } #>
<# } #>

<# foreach (var type in FloatTypes) { #>
<# bool isFP16 = type == FloatTypes[0]; #>
<# foreach (var (limitName, limitFormat) in floatLimits) { #>
<# var baseName = "_" + limitName + "_" + type.Name; #>
<# var testName = "Constant" + baseName; #>
Expand All @@ -124,10 +133,17 @@ namespace ILGPU.Tests
output[index] = input[index] == <#= testValue #> ? 1 : 0;
}

<# if (isFP16) { #>
[SkippableFact]
<# } else { #>
[Fact]
<# } #>
[KernelMethod(nameof(<#= kernelName #>))]
public void <#= testName #>()
{
<# if (isFP16) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
var inputArray = new [] { <#= type.FormatNumber("0.0") #>, <#= testValue #> };
var expected = inputArray.Select(
x => x == <#= testValue #> ? 1 : 0).ToArray();
Expand Down
21 changes: 14 additions & 7 deletions Src/ILGPU.Tests/ConvertFloatOperations.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: ConvertFloatOperations.tt/ConvertFloatOperations.cs
Expand Down Expand Up @@ -34,7 +34,7 @@ namespace ILGPU.Tests

<# foreach (var type in FloatTypes) { #>
<# foreach (var targetType in types) { #>
<# var floatRanges = GetFloatRanges(type, true); #>
<# bool isFP16 = type == FloatTypes[0] || targetType == FloatTypes[0]; #>
<# var baseName = "_" + type.Name + "_" + targetType.Name; #>
<# var testName = "ConvertOperation" + baseName; #>
<# var kernelName = "ConvertOperationKernel" + baseName; #>
Expand All @@ -46,7 +46,11 @@ namespace ILGPU.Tests
b[index] = (<#= targetType.Type #>)a[index];
}

<# if (isFP16) { #>
[SkippableTheory]
<# } else { #>
[Theory]
<# } #>
[InlineData(0.0f)]
[InlineData(1.0f)]
[InlineData((float)Math.PI)]
Expand All @@ -56,15 +60,18 @@ namespace ILGPU.Tests
[KernelMethod(nameof(<#= kernelName #>))]
public void <#= testName #>(float value)
{
<# if (isFP16) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
var convertedValue = (<#= type.Type #>)value;
const int length = 32;
using var a = Accelerator.Allocate1D<<#= type.Type #>>(length);
using var b = Accelerator.Allocate1D<<#= targetType.Type #>>(length);
const int Length = 32;
using var a = Accelerator.Allocate1D<<#= type.Type #>>(Length);
using var b = Accelerator.Allocate1D<<#= targetType.Type #>>(Length);
Initialize(a.View, convertedValue);
Execute(length, a.View, b.View);
Execute(Length, a.View, b.View);

var result = (<#= targetType.Type #>)convertedValue;
var reference = Enumerable.Repeat(result, length).ToArray();
var reference = Enumerable.Repeat(result, Length).ToArray();
Verify(b.View, reference);
}

Expand Down
20 changes: 17 additions & 3 deletions Src/ILGPU.Tests/ConvertIntOperations.Generated.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: ConvertIntOperations.Generated.tt/ConvertIntOperations.Generated.cs
Expand Down Expand Up @@ -30,6 +30,7 @@ namespace ILGPU.Tests
{
<# foreach (var type in IntTypes) { #>
<# foreach (var targetType in types) { #>
<# bool isFP16 = type == FloatTypes[0] || targetType == FloatTypes[0]; #>
<# var baseName = "_" + type.Name + "_" + targetType.Name; #>
<# var testName = "ConvertOperation" + baseName; #>
<# var kernelName = "ConvertOperationKernel" + baseName; #>
Expand All @@ -41,9 +42,11 @@ namespace ILGPU.Tests
b[index] = (<#= targetType.Type #>)a[index];
}

[Theory]
<# // Half conversions of these values is implementation specific in these cases #>
<# if (targetType != FloatTypes[0]) { #>
<# if (isFP16) { #>
[SkippableTheory]
<# } else { #>
[Theory]
[InlineData(<#= type.Type #>.MaxValue)]
[InlineData(<#= type.Type #>.MinValue)]
[InlineData(<#= type.Type #>.MinValue + 1)]
Expand All @@ -54,6 +57,9 @@ namespace ILGPU.Tests
[KernelMethod(nameof(<#= kernelName #>))]
public void <#= testName #>(<#= type.Type #> value)
{
<# if (isFP16) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
const int length = 32;
using var a = Accelerator.Allocate1D<<#= type.Type #>>(length);
using var b = Accelerator.Allocate1D<<#= targetType.Type #>>(length);
Expand All @@ -75,10 +81,18 @@ namespace ILGPU.Tests
}
}

<# if (targetType != FloatTypes[0]) { #>
[Fact]
<# } else { #>
[SkippableFact]
<# } #>
[KernelMethod(nameof(<#= kernelName #>_MaxValue))]
public void <#= testName #>_MaxValue()
{
<# if (targetType == FloatTypes[0]) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>

const int length = 32;
using var output = Accelerator.Allocate1D<<#= targetType.Type #>>(length);
Execute(length, output.View);
Expand Down
11 changes: 6 additions & 5 deletions Src/ILGPU.Tests/FixedBuffers.tt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ namespace ILGPU.Tests
ref FixedBufferStruct<#= type.Name #> value,
<#= type.Type #> scalarValue)
{
for (int i = 0; i < Length; ++i)
value.Data[i] += scalarValue;
<# for (int i = 0; i < FixedBufferLength; ++i) { #>
value.Data[<#= i #>] += scalarValue;
<# } #>
}

internal static void FixedBuffer<#= type.Name #>Kernel(
Expand Down Expand Up @@ -265,7 +266,7 @@ namespace ILGPU.Tests
Accelerator.Allocate1D<MultiFixedBufferStruct<#= type.Name #>>(1);

Execute(Length, buffer1.View, buffer2.View, buffer3.View, fixedBuffers.View);

<#= type.Type #> scalarValue = 2;
var expected =
new[]
Expand All @@ -274,7 +275,7 @@ namespace ILGPU.Tests
};
Verify(fixedBuffers.View, expected);
}

internal static readonly MultiFixedBufferStruct<#= type.Name #>
Local<#= type.Name #>Struct =
new MultiFixedBufferStruct<#= type.Name #>((<#= type.Type #>)2);
Expand All @@ -289,7 +290,7 @@ namespace ILGPU.Tests
data2[index] = Local<#= type.Name #>Struct.B[index];
data3[index] = Local<#= type.Name #>Struct.C[index];
}

[Fact]
[KernelMethod(nameof(GetStaticMultiFixedBuffer<#= type.Name #>Kernel))]
public void GetStaticMultiFixedBuffer<#= type.Name #>()
Expand Down
24 changes: 24 additions & 0 deletions Src/ILGPU.Tests/Generic/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,18 @@ public void Serialize(IXunitSerializationInfo info)
for (int i = 0; i < Length; ++i)
info.AddValue(nameof(Data) + i, Data[i]);
}

public override string ToString()
{
string result = string.Empty;
for (int i = 0; i < Length; ++i)
{
result += Data[i];
if (i + 1 < Length)
result += ", ";
}
return result;
}
}

public unsafe struct LongFixedBufferStruct : IXunitSerializable
Expand All @@ -497,6 +509,18 @@ public void Serialize(IXunitSerializationInfo info)
for (int i = 0; i < Length; ++i)
info.AddValue(nameof(Data) + i, Data[i]);
}

public override string ToString()
{
string result = string.Empty;
for (int i = 0; i < Length; ++i)
{
result += Data[i];
if (i + 1 < Length)
result += ", ";
}
return result;
}
}

#endregion
Expand Down
7 changes: 5 additions & 2 deletions Src/ILGPU.Tests/GridOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: GridOperations.cs
Expand Down Expand Up @@ -37,7 +37,7 @@ internal static void GridDimensionKernel(
Debug.Assert(Grid.IdxZ < Grid.DimZ);
}

[Theory]
[SkippableTheory]
[InlineData(1, 0, 0)]
[InlineData(0, 1, 0)]
[InlineData(0, 0, 1)]
Expand All @@ -54,6 +54,9 @@ public void GridDimension(int xMask, int yMask, int zMask)
Math.Max(i * zMask, 1)),
Index3D.One);

Skip.If(extent.GridDim.Y > Accelerator.MaxGridSize.Y);
Skip.If(extent.GridDim.Z > Accelerator.MaxGridSize.Z);

Execute(extent, buffer.View);

var expected = new int[]
Expand Down
2 changes: 1 addition & 1 deletion Src/ILGPU.Tests/GroupOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: GroupOperations.cs
Expand Down
12 changes: 9 additions & 3 deletions Src/ILGPU.Tests/ReinterpretCasts.tt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: ReinterpretCasts.tt/ReinterpretCasts.cs
Expand Down Expand Up @@ -41,10 +41,13 @@ namespace ILGPU.Tests
data[index] = Interop.FloatAsInt(value);
}

[Fact]
[SkippableFact]
[KernelMethod(nameof(<#= floatType.Name #>AsIntKernel))]
public void <#= floatType.Name #>AsInt()
{
<# if (floatType == FloatTypes[0]) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
var floatValue = (<#= floatType.Type #>)2.0;
using var data = Accelerator.Allocate1D<<#= intType.Type #>>(Length);
Execute(Length, data.View, floatValue);
Expand All @@ -63,10 +66,13 @@ namespace ILGPU.Tests
data[index] = Interop.IntAsFloat(value);
}

[Fact]
[SkippableFact]
[KernelMethod(nameof(IntAs<#= floatType.Name #>Kernel))]
public void IntAs<#= floatType.Name #>()
{
<# if (floatType == FloatTypes[0]) { #>
Skip.If(!Accelerator.Capabilities.Float16);
<# } #>
var intValue = Interop.FloatAsInt((<#= floatType.Type #>)2.0);
using var data = Accelerator.Allocate1D<<#= floatType.Type #>>(Length);
Execute(Length, data.View, intValue);
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/SharedMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal static void DynamicSharedMemoryKernel(
data[idx] = value + DynamicSharedMemoryNested();
}

[Theory]
[SkippableTheory]
[InlineData(1)]
[InlineData(10)]
[InlineData(32)]
Expand Down Expand Up @@ -386,4 +386,4 @@ public void MultiDimensionalSharedMemory3DDenseZY()
Verify(buffer.View, expected);
}
}
}
}
4 changes: 3 additions & 1 deletion Src/ILGPU.Tests/UnaryFloatOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static void IsPredicateF16Kernel(
data[index + 4] = Half.IsNaN(value) ? 1 : 0;
}

[Theory]
[SkippableTheory]
[MemberData(nameof(HalfData))]
[KernelMethod(nameof(IsPredicateF16Kernel))]
public void IsPredicateF16(
Expand All @@ -56,6 +56,8 @@ public void IsPredicateF16(
bool isPositiveInfinity,
bool isNegativeInfinity)
{
Skip.If(!Accelerator.Capabilities.Float16);

using var buffer = Accelerator.Allocate1D<int>(5);
Execute<Index1D>(1, buffer.View, value);

Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Tests/WarpOperations.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ---------------------------------------------------------------------------------------
// ILGPU
// Copyright (c) 2021-2022 ILGPU Project
// Copyright (c) 2021-2023 ILGPU Project
// www.ilgpu.net
//
// File: WarpOperations.cs
Expand Down Expand Up @@ -286,7 +286,7 @@ internal static void DivergentWarpBarrierKernel(
data[index] = sharedMemory[Warp.WarpIdx] * Warp.WarpSize + value;
}

[Theory]
[SkippableTheory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU/Runtime/CPU/CPUDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ private CPUDevice(
MaxSharedMemoryPerGroup = int.MaxValue;
MaxConstantMemory = int.MaxValue;
NumThreads = MaxNumThreads;
Capabilities = new CPUCapabilityContext();
}

/// <summary>
Expand Down
Loading

0 comments on commit f75ecb4

Please sign in to comment.