Skip to content

Arm build changes #5789

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 16 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changes from PR comments. Removal of FastTreeNative flag.
  • Loading branch information
michaelgsharp committed May 17, 2021
commit 07e1fe02f73b1ffc2a088c7c591c145c0436f36d
7 changes: 6 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
<StrongNameKeyId>Open</StrongNameKeyId>
</PropertyGroup>

<ItemGroup>
<TargetsArm Include="LdaNative" />
<TargetsArm Include="MatrixFactorizationNative" />
</ItemGroup>

<PropertyGroup>
<DefineConstants Condition="'$(TargetPlatform)' == 'arm64'">$(DefineConstants);ARM64</DefineConstants>
<DefineConstants Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'">$(DefineConstants);ARM64</DefineConstants>
</PropertyGroup>

<PropertyGroup>
Expand Down
10 changes: 9 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
</NativeAssemblyReference>
</ItemGroup>

<Copy SourceFiles = "@(NativeAssemblyReference->'%(FullAssemblyPath)')"
<PropertyGroup>
<ShouldCopyx64>false</ShouldCopyx64>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the value of these properties. What other target architectures do we have beside x86, x64, arm64, or arm?

These properties appear to only be used in the below Copy, and as far as I can tell, that condition is always true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see now, this is to support copying LdaNative and MatrixFactorizationNative assemblies on arm, but not the other native files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I figured we could also use this same pattern when we get to blazer WASM. Though I guess we can't really copy any native files there... so maybe this was a bit overkill.

<ShouldCopyx64 Condition="'$(TargetArchitecture)' == 'x86' Or '$(TargetArchitecture)' == 'x64'">true</ShouldCopyx64>
<ShouldCopyArm>false</ShouldCopyArm>
<ShouldCopyArm Condition="('$(TargetArchitecture)' == 'arm64' Or '$(TargetArchitecture)' == 'arm') And %(NativeAssemblyReference.Item) == %(TargetsArm.Identity)">true</ShouldCopyArm>
</PropertyGroup>

<Copy Condition="$(ShouldCopyx64) or $(ShouldCopyArm)"
SourceFiles = "@(NativeAssemblyReference->'%(FullAssemblyPath)')"
DestinationFolder="$(OutDir)"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"
Retries="$(CopyRetryCount)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</ProjectReference>

<NativeAssemblyReference Include="MatrixFactorizationNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="FastTreeNative" />
<NativeAssemblyReference Include="FastTreeNative" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="$(TensorFlowVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
<ProjectReference Include="..\..\..\src\Microsoft.ML.StandardTrainers\Microsoft.ML.StandardTrainers.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.ML.Transforms\Microsoft.ML.Transforms.csproj" />

<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="CpuMathNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="FastTreeNative" />
<NativeAssemblyReference Include="CpuMathNative" />
<NativeAssemblyReference Include="FastTreeNative" />
<NativeAssemblyReference Include="MatrixFactorizationNative" />
<NativeAssemblyReference Include="LdaNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="SymSgdNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="MklProxyNative" />
<NativeAssemblyReference Include="SymSgdNative" />
<NativeAssemblyReference Include="MklProxyNative" />

</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions docs/samples/Microsoft.ML.Samples/Microsoft.ML.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<ProjectReference Include="..\..\..\src\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.ML.Data\Microsoft.ML.Data.csproj" />

<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="CpuMathNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="FastTreeNative" />
<NativeAssemblyReference Include="CpuMathNative" />
<NativeAssemblyReference Include="FastTreeNative" />
<NativeAssemblyReference Include="MatrixFactorizationNative" />
<NativeAssemblyReference Include="LdaNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="SymSgdNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="MklProxyNative" />
<NativeAssemblyReference Include="SymSgdNative" />
<NativeAssemblyReference Include="MklProxyNative" />
<None Include="Dynamic\Trainers\BinaryClassification\FastForest.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Console/Microsoft.ML.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
<ProjectReference Include="..\Microsoft.ML.TensorFlow\Microsoft.ML.TensorFlow.csproj" />
<ProjectReference Include="..\Microsoft.ML.Transforms\Microsoft.ML.Transforms.csproj" />

<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="FastTreeNative" />
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="CpuMathNative" />
<NativeAssemblyReference Include="FastTreeNative" />
<NativeAssemblyReference Include="CpuMathNative" />
<NativeAssemblyReference Include="MatrixFactorizationNative" />
<NativeAssemblyReference Include="LdaNative" />
<!-- TODO: once we fix the 4 intel MKL methods, SymSgdNative will need to go back in. -->
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="SymSgdNative"/>
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm'" Include="MklImports"/>
<NativeAssemblyReference Include="SymSgdNative"/>
<NativeAssemblyReference Include="MklImports"/>
<NativeAssemblyReference Condition="'$(TargetArchitecture)' != 'arm64' And '$(TargetArchitecture)' != 'arm' And '$(OS)' == 'Windows_NT'" Include="libiomp5md"/>
</ItemGroup>

Expand Down
44 changes: 28 additions & 16 deletions src/Microsoft.ML.FastTree/Dataset/DenseIntArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ internal abstract class DenseIntArray : IntArray, IIntArrayForwardIndexer
{
public override IntArrayType Type { get { return IntArrayType.Dense; } }

protected DenseIntArray(int length)
protected DenseIntArray(int length, PerformSumup sumupHandler = null)
{
Contracts.Assert(length >= 0);
Length = length;
SumupHandler = sumupHandler;
}

public override int Length { get; }
Expand Down Expand Up @@ -70,7 +71,6 @@ public override IntArray[] Split(int[][] assignment)
return newArrays;
}

#if USE_FASTTREENATIVE
internal const string NativePath = "FastTreeNative";
[DllImport(NativePath), SuppressUnmanagedCodeSecurity]
private static extern unsafe int C_Sumup_float(
Expand Down Expand Up @@ -111,8 +111,6 @@ protected static unsafe void SumupCPlusPlusDense(SumupInputData input, FeatureHi
}
}

#endif

public override IIntArrayForwardIndexer GetIndexer()
{
return this;
Expand Down Expand Up @@ -391,18 +389,21 @@ public Dense8BitIntArray(int len)
: base(len)
{
_data = new byte[len];
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense8BitIntArray(byte[] buffer, ref int position)
: base(buffer.ToInt(ref position))
{
_data = buffer.ToByteArray(ref position);
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense8BitIntArray(int len, IEnumerable<int> values)
: base(len)
{
_data = values.Select(i => (byte)i).ToArray(len);
SetupSumupHandler(SumupNative, base.Sumup);
}

/// <summary>
Expand Down Expand Up @@ -445,8 +446,7 @@ public override unsafe int this[int index]
}
}

#if USE_FASTTREENATIVE
public override void Sumup(SumupInputData input, FeatureHistogram histogram)
private void SumupNative(SumupInputData input, FeatureHistogram histogram)
{
unsafe
{
Expand All @@ -456,7 +456,8 @@ public override void Sumup(SumupInputData input, FeatureHistogram histogram)
}
}
}
#endif

public override void Sumup(SumupInputData input, FeatureHistogram histogram) => SumupHandler(input, histogram);
}

/// <summary>
Expand All @@ -475,12 +476,14 @@ public Dense4BitIntArray(int len)
: base(len)
{
_data = new byte[(len + 1) / 2]; // Even length = half the bytes. Odd length = half the bytes+0.5.
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense4BitIntArray(int len, IEnumerable<int> values)
: base(len)
{
_data = new byte[(len + 1) / 2];
SetupSumupHandler(SumupNative, base.Sumup);

int currentIndex = 0;
bool upper = true;
Expand All @@ -505,6 +508,7 @@ public Dense4BitIntArray(byte[] buffer, ref int position)
: base(buffer.ToInt(ref position))
{
_data = buffer.ToByteArray(ref position);
SetupSumupHandler(SumupNative, base.Sumup);
}

/// <summary>
Expand Down Expand Up @@ -565,8 +569,7 @@ public override unsafe int this[int index]
}
}

#if USE_FASTTREENATIVE
public override void Sumup(SumupInputData input, FeatureHistogram histogram)
public void SumupNative(SumupInputData input, FeatureHistogram histogram)
{
unsafe
{
Expand All @@ -576,7 +579,8 @@ public override void Sumup(SumupInputData input, FeatureHistogram histogram)
}
}
}
#endif

public override void Sumup(SumupInputData input, FeatureHistogram histogram) => SumupHandler(input, histogram);
}

/// <summary>
Expand All @@ -592,18 +596,21 @@ public Dense16BitIntArray(int len)
: base(len)
{
_data = new ushort[len];
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense16BitIntArray(int len, IEnumerable<int> values)
: base(len)
{
_data = values.Select(i => (ushort)i).ToArray(len);
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense16BitIntArray(byte[] buffer, ref int position)
: base(buffer.ToInt(ref position))
{
_data = buffer.ToUShortArray(ref position);
SetupSumupHandler(SumupNative, base.Sumup);
}

public override unsafe void Callback(Action<IntPtr> callback)
Expand Down Expand Up @@ -648,8 +655,8 @@ public override unsafe int this[int index]
_data[index] = (ushort)value;
}
}
#if USE_FASTTREENATIVE
public override void Sumup(SumupInputData input, FeatureHistogram histogram)

public void SumupNative(SumupInputData input, FeatureHistogram histogram)
{
unsafe
{
Expand All @@ -660,7 +667,9 @@ public override void Sumup(SumupInputData input, FeatureHistogram histogram)
}
}
}
#endif

public override void Sumup(SumupInputData input, FeatureHistogram histogram) => SumupHandler(input, histogram);

}

/// <summary>
Expand All @@ -676,18 +685,21 @@ public Dense32BitIntArray(int len)
: base(len)
{
_data = new int[len];
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense32BitIntArray(int len, IEnumerable<int> values)
: base(len)
{
_data = values.ToArray(len);
SetupSumupHandler(SumupNative, base.Sumup);
}

public Dense32BitIntArray(byte[] buffer, ref int position)
: base(buffer.ToInt(ref position))
{
_data = buffer.ToIntArray(ref position);
SetupSumupHandler(SumupNative, base.Sumup);
}

public override unsafe void Callback(Action<IntPtr> callback)
Expand Down Expand Up @@ -733,8 +745,7 @@ public override int this[int index]
}
}

#if USE_FASTTREENATIVE
public override void Sumup(SumupInputData input, FeatureHistogram histogram)
public void SumupNative(SumupInputData input, FeatureHistogram histogram)
{
unsafe
{
Expand All @@ -745,6 +756,7 @@ public override void Sumup(SumupInputData input, FeatureHistogram histogram)
}
}
}
#endif

public override void Sumup(SumupInputData input, FeatureHistogram histogram) => SumupHandler(input, histogram);
}
}
47 changes: 46 additions & 1 deletion src/Microsoft.ML.FastTree/Dataset/IntArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.ML.Runtime;

namespace Microsoft.ML.Trainers.FastTree
Expand Down Expand Up @@ -32,6 +34,38 @@ internal abstract class IntArray : IEnumerable<int>
/// </summary>
public abstract int Length { get; }

// We can test if the dll is present by calling into a non-existant method.
// If we get an EntryPointNotFoundException then the dll exists and we can use the native code.
// If we get a DllNotFoundException then the dll doesn't exist and we should use the managed fallbacks.
private const string NativePath = "FastTreeNative";
[DllImport(NativePath), SuppressUnmanagedCodeSecurity]
private static extern void NonExistantMethod();

/// <summary>
/// Lazy bool that checks if the FastTreeNative dll exists so we know if we should use the native code
/// or the managed fallbacks.
/// </summary>
public static Lazy<bool> UseFastTreeNative = new(() =>
{
try
{
NonExistantMethod();
}
// If the entry point isn't found then the dll exists and we should use the managed code.
catch(EntryPointNotFoundException)
{
return true;
}
// If any other error is returned we should use the managed fallbacks.
catch
{
return false;
}

// We will never hit this but we need to have it so every path has a return value. Defaulting to managed fallbacks.
return false;
});

/// <summary>
/// Returns the number of bytes written by the member ToByteArray()
/// </summary>
Expand Down Expand Up @@ -198,6 +232,17 @@ public static IntArray New(byte[] buffer, ref int position)
/// <returns>An indexer into the array</returns>
public abstract IIntArrayForwardIndexer GetIndexer();

// Used in the child classes so we can set either the native or managed Sumup method one time and then
// never have to check again.
protected delegate void PerformSumup(SumupInputData input, FeatureHistogram histogram);

// Handler so the child classes don't have to redefine it. If they don't have different logic for native vs managed
// code then they don't need to use this.
protected PerformSumup SumupHandler { get; set; }

// Helper to setup the SumupHandler for the derived classes that need it.
protected void SetupSumupHandler(PerformSumup native, PerformSumup managed) => SumupHandler = UseFastTreeNative.Value ? native : managed;

public virtual void Sumup(SumupInputData input, FeatureHistogram histogram)
{
Contracts.Assert((input.Weights == null) == (histogram.SumWeightsByBin == null));
Expand Down Expand Up @@ -313,7 +358,7 @@ public IntArray Compress(uint[] workarray = null)
int bits = SegmentIntArray.BitsForValue((uint)maxval);
if (bits <= 21)
{
SegmentIntArray.SegmentFindOptimalPath(workarray, Length,
SegmentIntArray.SegmentFindOptimalPath.Value(workarray, Length,
bits, out segBits, out segTransitions);
}
}
Expand Down
Loading