Skip to content

Make CpuMath not depending on ML.Core again #1724

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 12 commits into from
Nov 27, 2018
6 changes: 6 additions & 0 deletions src/Microsoft.ML.Core/BestFriendAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

using System;

#if CPUMATH_INFRASTRUCTURE
// CpuMath has its own BestFriend and WantsToBeBestFriends attributes for making itself a standalone module
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
#else
// This namespace contains the BestFriend and WantsToBeBestFriends attributes generally used in ML.NET project settings
namespace Microsoft.ML
#endif
{
/// <summary>
/// Intended to be applied to types and members marked as internal to indicate that friend access of this
Expand Down
4 changes: 0 additions & 4 deletions src/Microsoft.ML.Core/Microsoft.ML.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
<RootNamespace>Microsoft.ML</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutableVersion)" />
<PackageReference Include="System.ComponentModel.Composition" Version="$(SystemComponentModelCompositionVersion)" />
Expand Down
10 changes: 0 additions & 10 deletions src/Microsoft.ML.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,3 @@
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Transforms" + PublicKey.Value)]

[assembly: WantsToBeBestFriends]

namespace Microsoft.ML
{
[BestFriend]
internal static class PublicKey
{
public const string Value = ", PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb";
public const string TestValue = ", PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4";
}
}
20 changes: 20 additions & 0 deletions src/Microsoft.ML.Core/PublicKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

#if !CPUMATH_INFRASTRUCTURE
// This namespace contains the PublicKey generally used in ML.NET project settings
namespace Microsoft.ML
#else
// CpuMath module has its own PublicKey for isolating itself from Microsoft.ML.Core
// Note that CpuMath uses its own BestFriend defined in Microsoft.ML.Runtime.Internal.CpuMath.Core.
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
#endif
{
[BestFriend]
internal static class PublicKey
{
public const string Value = ", PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb";
public const string TestValue = ", PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4";
}
}
21 changes: 21 additions & 0 deletions src/Microsoft.ML.Core/Utilities/Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
using System.IO;
using System.Threading;

#if CPUMATH_INFRASTRUCTURE
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
#else
namespace Microsoft.ML.Runtime
#endif
{
using Conditional = System.Diagnostics.ConditionalAttribute;
using Debug = System.Diagnostics.Debug;
Expand All @@ -27,7 +31,11 @@ namespace Microsoft.ML.Runtime
/// totally replace the exception, etc. It is not legal to return null from
/// Process (unless null was passed in, which really shouldn't happen).
/// </summary>
#if CPUMATH_INFRASTRUCTURE
internal interface IExceptionContext
#else
public interface IExceptionContext
#endif
{
TException Process<TException>(TException ex)
where TException : Exception;
Expand All @@ -38,6 +46,15 @@ TException Process<TException>(TException ex)
string ContextDescription { get; }
}

#if CPUMATH_INFRASTRUCTURE
[Flags]
internal enum MessageSensitivity
{
None = 0,
Unknown = ~None
}
#endif

[BestFriend]
internal static partial class Contracts
{
Expand Down Expand Up @@ -137,6 +154,7 @@ public static MessageSensitivity Sensitivity(this Exception ex)
return (ex.Data[SensitivityKey] as MessageSensitivity?) ?? MessageSensitivity.Unknown;
}

#if !CPUMATH_INFRASTRUCTURE
/// <summary>
/// This is an internal convenience implementation of an exception context to make marking
/// exceptions with a specific sensitivity flag a bit less onorous. The alternative to a scheme
Expand Down Expand Up @@ -213,6 +231,7 @@ public static IExceptionContext UserSensitive(this IExceptionContext ctx)
/// </summary>
public static IExceptionContext SchemaSensitive(this IExceptionContext ctx)
=> new SensitiveExceptionContext(ctx, MessageSensitivity.Schema);
#endif

/// <summary>
/// Sets the assert handler to the given function, returning the previous handler.
Expand Down Expand Up @@ -721,6 +740,7 @@ public static void CheckIO(this IExceptionContext ctx, bool f, string msg)
throw ExceptIO(ctx, msg);
}

#if !CPUMATH_INFRASTRUCTURE
/// <summary>
/// Check state of the host and throw exception if host marked to stop all exection.
/// </summary>
Expand All @@ -729,6 +749,7 @@ public static void CheckAlive(this IHostEnvironment env)
if (env.IsCancelled)
throw Process(new OperationCanceledException("Operation was cancelled."), env);
}
#endif

/// <summary>
/// This documents that the parameter can legally be null.
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/AlignedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;

namespace Microsoft.ML.Runtime.Internal.CpuMath
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/AlignedMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Float = System.Single;

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.CpuMath/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System.Runtime.CompilerServices;
using Microsoft.ML;

[assembly: InternalsVisibleTo("Microsoft.ML.CpuMath.UnitTests.netstandard" + PublicKey.TestValue)]
[assembly: InternalsVisibleTo("Microsoft.ML.CpuMath.UnitTests.netcoreapp" + PublicKey.TestValue)]
Expand Down
2 changes: 2 additions & 0 deletions src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;

namespace Microsoft.ML.Runtime.Internal.CpuMath
{
[BestFriend]
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics.X86;
using System;
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/CpuMathUtils.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;
using System.Runtime.CompilerServices;

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/EigenUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;
using Float = System.Single;

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/ICpuBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;
using System.Collections.Generic;
using Float = System.Single;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.CpuMath/IntUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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 Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Security;
Expand Down
12 changes: 7 additions & 5 deletions src/Microsoft.ML.CpuMath/Microsoft.ML.CpuMath.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
<TargetFrameworks Condition="'$(UseIntrinsics)' == 'true'">netstandard2.0;netcoreapp3.0</TargetFrameworks>
<IncludeInPackage>Microsoft.ML.CpuMath</IncludeInPackage>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);CPUMATH_INFRASTRUCTURE</DefineConstants>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup>
<Folder Include="Properties\" />
<Compile Include="..\Microsoft.ML.Core\Utilities\Contracts.cs" />
<Compile Include="..\Microsoft.ML.Core\BestFriendAttribute.cs" />
<Compile Include="..\Microsoft.ML.Core\PublicKey.cs" />

<!-- Workaround https://github.com/dotnet/project-system/issues/935 -->
<None Include="**/*.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
Expand All @@ -24,8 +30,4 @@
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/ProbabilityFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;

namespace Microsoft.ML.Runtime.Internal.CpuMath
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/Sse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System;
using System.Runtime.InteropServices;

Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.ML.CpuMath/Thunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.ML.Runtime.Internal.CpuMath.Core;
using System.Runtime.InteropServices;
using System.Security;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public override void Initialize(AnalysisContext context)
context.RegisterSemanticModelAction(Analyze);
}

private void Analyze(SemanticModelAnalysisContext context)
private void AnalyzeCore(SemanticModelAnalysisContext context, string attributeName, string assemblyAttributeName)
{
var model = context.SemanticModel;
var comp = model.Compilation;

// Get the symbols of the key types we are analyzing. If we can't find either
// of them there is no point in going further.
var bestFriendAttributeType = comp.GetTypeByMetadataName(AttributeName);
var bestFriendAttributeType = comp.GetTypeByMetadataName(attributeName);
if (bestFriendAttributeType == null)
return;
var wantsToBeBestFriendsAttributeType = comp.GetTypeByMetadataName(AssemblyAttributeName);
var wantsToBeBestFriendsAttributeType = comp.GetTypeByMetadataName(assemblyAttributeName);
if (wantsToBeBestFriendsAttributeType == null)
return;

Expand Down Expand Up @@ -89,6 +89,9 @@ private void Analyze(SemanticModelAnalysisContext context)
// further.
if (!assemblyHasAttrMap.TryGetValue(symbolAssembly, out bool assemblyWantsBestFriends))
{
// It's the first of seeing the assembly containing symbol. A key-value pair is added into assemblyHasAttrMap to
// indicate if that assembly includes an attribute WantsToBeBestFriends. If an assembly has WantsToBeBestFriends then
// its associated value would be true.
assemblyWantsBestFriends = symbolAssembly.GetAttributes().Any(a => a.AttributeClass == wantsToBeBestFriendsAttributeType);
assemblyHasAttrMap[symbolAssembly] = assemblyWantsBestFriends;
}
Expand All @@ -104,5 +107,11 @@ private void Analyze(SemanticModelAnalysisContext context)
context.ReportDiagnostic(diagnostic);
}
}

private void Analyze(SemanticModelAnalysisContext context)
{
AnalyzeCore(context, "Microsoft.ML.BestFriendAttribute", "Microsoft.ML.WantsToBeBestFriendsAttribute");
AnalyzeCore(context, "Microsoft.ML.Runtime.Internal.CpuMath.Core.BestFriendAttribute", "Microsoft.ML.Runtime.Internal.CpuMath.Core.WantsToBeBestFriendsAttribute");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
var containingSymbolName = methodSymbol.ContainingSymbol.ToString();
// The "internal" version is one used by some projects that want to benefit from Contracts,
// but for some reason cannot reference MLCore.
// Contract functions defined Microsoft.ML.Runtime.Internal.CpuMath.Core are introduced for breaking the dependencies
// from CpuMath project to Microsoft.ML.Core.
if (containingSymbolName != "Microsoft.ML.Runtime.Contracts" &&
containingSymbolName != "Microsoft.ML.Runtime.Internal.Contracts")
containingSymbolName != "Microsoft.ML.Runtime.Internal.CpuMath.Core.Contracts")
{
return;
}
Expand Down