Skip to content

Commit 870cf7e

Browse files
authored
Make CpuMath not depending on ML.Core again (#1724)
* Make CpuMath not depending on ML.Core again * Modify BestFriend attributes and their analyzer * Update src/Microsoft.ML.Core/Properties/PublicKey.cs * Rename PRIVATE_CONTRACTS compiler flag to CPUMATH_INFRASTRUCTURE * Fix comments
1 parent f92c90d commit 870cf7e

20 files changed

+82
-25
lines changed

src/Microsoft.ML.Core/BestFriendAttribute.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44

55
using System;
66

7+
#if CPUMATH_INFRASTRUCTURE
8+
// CpuMath has its own BestFriend and WantsToBeBestFriends attributes for making itself a standalone module
9+
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
10+
#else
11+
// This namespace contains the BestFriend and WantsToBeBestFriends attributes generally used in ML.NET project settings
712
namespace Microsoft.ML
13+
#endif
814
{
915
/// <summary>
1016
/// Intended to be applied to types and members marked as internal to indicate that friend access of this

src/Microsoft.ML.Core/Microsoft.ML.Core.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
<RootNamespace>Microsoft.ML</RootNamespace>
99
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<Folder Include="Properties\" />
13-
</ItemGroup>
14-
1511
<ItemGroup>
1612
<PackageReference Include="System.Collections.Immutable" Version="$(SystemCollectionsImmutableVersion)" />
1713
<PackageReference Include="System.ComponentModel.Composition" Version="$(SystemComponentModelCompositionVersion)" />

src/Microsoft.ML.Core/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,3 @@
4040
[assembly: InternalsVisibleTo(assemblyName: "Microsoft.ML.Transforms" + PublicKey.Value)]
4141

4242
[assembly: WantsToBeBestFriends]
43-
44-
namespace Microsoft.ML
45-
{
46-
[BestFriend]
47-
internal static class PublicKey
48-
{
49-
public const string Value = ", PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb";
50-
public const string TestValue = ", PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4";
51-
}
52-
}

src/Microsoft.ML.Core/PublicKey.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// See the LICENSE file in the project root for more information.
4+
5+
#if !CPUMATH_INFRASTRUCTURE
6+
// This namespace contains the PublicKey generally used in ML.NET project settings
7+
namespace Microsoft.ML
8+
#else
9+
// CpuMath module has its own PublicKey for isolating itself from Microsoft.ML.Core
10+
// Note that CpuMath uses its own BestFriend defined in Microsoft.ML.Runtime.Internal.CpuMath.Core.
11+
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
12+
#endif
13+
{
14+
[BestFriend]
15+
internal static class PublicKey
16+
{
17+
public const string Value = ", PublicKey=00240000048000009400000006020000002400005253413100040000010001004b86c4cb78549b34bab61a3b1800e23bfeb5b3ec390074041536a7e3cbd97f5f04cf0f857155a8928eaa29ebfd11cfbbad3ba70efea7bda3226c6a8d370a4cd303f714486b6ebc225985a638471e6ef571cc92a4613c00b8fa65d61ccee0cbe5f36330c9a01f4183559f1bef24cc2917c6d913e3a541333a1d05d9bed22b38cb";
18+
public const string TestValue = ", PublicKey=002400000480000094000000060200000024000052534131000400000100010015c01ae1f50e8cc09ba9eac9147cf8fd9fce2cfe9f8dce4f7301c4132ca9fb50ce8cbf1df4dc18dd4d210e4345c744ecb3365ed327efdbc52603faa5e21daa11234c8c4a73e51f03bf192544581ebe107adee3a34928e39d04e524a9ce729d5090bfd7dad9d10c722c0def9ccc08ff0a03790e48bcd1f9b6c476063e1966a1c4";
19+
}
20+
}

src/Microsoft.ML.Core/Utilities/Contracts.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
using System.IO;
1717
using System.Threading;
1818

19+
#if CPUMATH_INFRASTRUCTURE
20+
namespace Microsoft.ML.Runtime.Internal.CpuMath.Core
21+
#else
1922
namespace Microsoft.ML.Runtime
23+
#endif
2024
{
2125
using Conditional = System.Diagnostics.ConditionalAttribute;
2226
using Debug = System.Diagnostics.Debug;
@@ -27,7 +31,11 @@ namespace Microsoft.ML.Runtime
2731
/// totally replace the exception, etc. It is not legal to return null from
2832
/// Process (unless null was passed in, which really shouldn't happen).
2933
/// </summary>
34+
#if CPUMATH_INFRASTRUCTURE
35+
internal interface IExceptionContext
36+
#else
3037
public interface IExceptionContext
38+
#endif
3139
{
3240
TException Process<TException>(TException ex)
3341
where TException : Exception;
@@ -38,6 +46,15 @@ TException Process<TException>(TException ex)
3846
string ContextDescription { get; }
3947
}
4048

49+
#if CPUMATH_INFRASTRUCTURE
50+
[Flags]
51+
internal enum MessageSensitivity
52+
{
53+
None = 0,
54+
Unknown = ~None
55+
}
56+
#endif
57+
4158
[BestFriend]
4259
internal static partial class Contracts
4360
{
@@ -137,6 +154,7 @@ public static MessageSensitivity Sensitivity(this Exception ex)
137154
return (ex.Data[SensitivityKey] as MessageSensitivity?) ?? MessageSensitivity.Unknown;
138155
}
139156

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

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

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

733754
/// <summary>
734755
/// This documents that the parameter can legally be null.

src/Microsoft.ML.CpuMath/AlignedArray.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67

78
namespace Microsoft.ML.Runtime.Internal.CpuMath

src/Microsoft.ML.CpuMath/AlignedMatrix.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Float = System.Single;
66

7+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
78
using System;
89
using System.Collections;
910
using System.Collections.Generic;

src/Microsoft.ML.CpuMath/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System.Runtime.CompilerServices;
6-
using Microsoft.ML;
77

88
[assembly: InternalsVisibleTo("Microsoft.ML.CpuMath.UnitTests.netstandard" + PublicKey.TestValue)]
99
[assembly: InternalsVisibleTo("Microsoft.ML.CpuMath.UnitTests.netcoreapp" + PublicKey.TestValue)]

src/Microsoft.ML.CpuMath/CpuAligenedMathUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
6+
57
namespace Microsoft.ML.Runtime.Internal.CpuMath
68
{
79
[BestFriend]

src/Microsoft.ML.CpuMath/CpuMathUtils.netcoreapp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System.Runtime.CompilerServices;
67
using System.Runtime.Intrinsics.X86;
78
using System;

src/Microsoft.ML.CpuMath/CpuMathUtils.netstandard.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67
using System.Runtime.CompilerServices;
78

src/Microsoft.ML.CpuMath/EigenUtils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67
using Float = System.Single;
78

src/Microsoft.ML.CpuMath/ICpuBuffer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67
using System.Collections.Generic;
78
using Float = System.Single;

src/Microsoft.ML.CpuMath/IntUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
66
using System.Runtime.InteropServices;
77
using System.Runtime.CompilerServices;
88
using System.Security;

src/Microsoft.ML.CpuMath/Microsoft.ML.CpuMath.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
<TargetFrameworks Condition="'$(UseIntrinsics)' == 'true'">netstandard2.0;netcoreapp3.0</TargetFrameworks>
66
<IncludeInPackage>Microsoft.ML.CpuMath</IncludeInPackage>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<DefineConstants>$(DefineConstants);CPUMATH_INFRASTRUCTURE</DefineConstants>
89
<LangVersion>7.3</LangVersion>
910
</PropertyGroup>
1011

1112
<ItemGroup>
12-
<Folder Include="Properties\" />
13+
<Compile Include="..\Microsoft.ML.Core\Utilities\Contracts.cs" />
14+
<Compile Include="..\Microsoft.ML.Core\BestFriendAttribute.cs" />
15+
<Compile Include="..\Microsoft.ML.Core\PublicKey.cs" />
16+
17+
<!-- Workaround https://github.com/dotnet/project-system/issues/935 -->
18+
<None Include="**/*.cs" />
1319
</ItemGroup>
1420

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

27-
<ItemGroup>
28-
<ProjectReference Include="..\..\src\Microsoft.ML.Core\Microsoft.ML.Core.csproj" />
29-
</ItemGroup>
30-
3133
</Project>

src/Microsoft.ML.CpuMath/ProbabilityFunctions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67

78
namespace Microsoft.ML.Runtime.Internal.CpuMath

src/Microsoft.ML.CpuMath/Sse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System;
67
using System.Runtime.InteropServices;
78

src/Microsoft.ML.CpuMath/Thunk.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.ML.Runtime.Internal.CpuMath.Core;
56
using System.Runtime.InteropServices;
67
using System.Security;
78

tools-local/Microsoft.ML.InternalCodeAnalyzer/BestFriendAnalyzer.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public override void Initialize(AnalysisContext context)
4141
context.RegisterSemanticModelAction(Analyze);
4242
}
4343

44-
private void Analyze(SemanticModelAnalysisContext context)
44+
private void AnalyzeCore(SemanticModelAnalysisContext context, string attributeName, string assemblyAttributeName)
4545
{
4646
var model = context.SemanticModel;
4747
var comp = model.Compilation;
4848

4949
// Get the symbols of the key types we are analyzing. If we can't find either
5050
// of them there is no point in going further.
51-
var bestFriendAttributeType = comp.GetTypeByMetadataName(AttributeName);
51+
var bestFriendAttributeType = comp.GetTypeByMetadataName(attributeName);
5252
if (bestFriendAttributeType == null)
5353
return;
54-
var wantsToBeBestFriendsAttributeType = comp.GetTypeByMetadataName(AssemblyAttributeName);
54+
var wantsToBeBestFriendsAttributeType = comp.GetTypeByMetadataName(assemblyAttributeName);
5555
if (wantsToBeBestFriendsAttributeType == null)
5656
return;
5757

@@ -89,6 +89,9 @@ private void Analyze(SemanticModelAnalysisContext context)
8989
// further.
9090
if (!assemblyHasAttrMap.TryGetValue(symbolAssembly, out bool assemblyWantsBestFriends))
9191
{
92+
// It's the first of seeing the assembly containing symbol. A key-value pair is added into assemblyHasAttrMap to
93+
// indicate if that assembly includes an attribute WantsToBeBestFriends. If an assembly has WantsToBeBestFriends then
94+
// its associated value would be true.
9295
assemblyWantsBestFriends = symbolAssembly.GetAttributes().Any(a => a.AttributeClass == wantsToBeBestFriendsAttributeType);
9396
assemblyHasAttrMap[symbolAssembly] = assemblyWantsBestFriends;
9497
}
@@ -104,5 +107,11 @@ private void Analyze(SemanticModelAnalysisContext context)
104107
context.ReportDiagnostic(diagnostic);
105108
}
106109
}
110+
111+
private void Analyze(SemanticModelAnalysisContext context)
112+
{
113+
AnalyzeCore(context, "Microsoft.ML.BestFriendAttribute", "Microsoft.ML.WantsToBeBestFriendsAttribute");
114+
AnalyzeCore(context, "Microsoft.ML.Runtime.Internal.CpuMath.Core.BestFriendAttribute", "Microsoft.ML.Runtime.Internal.CpuMath.Core.WantsToBeBestFriendsAttribute");
115+
}
107116
}
108117
}

tools-local/Microsoft.ML.InternalCodeAnalyzer/ContractsCheckAnalyzer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
196196
var containingSymbolName = methodSymbol.ContainingSymbol.ToString();
197197
// The "internal" version is one used by some projects that want to benefit from Contracts,
198198
// but for some reason cannot reference MLCore.
199+
// Contract functions defined Microsoft.ML.Runtime.Internal.CpuMath.Core are introduced for breaking the dependencies
200+
// from CpuMath project to Microsoft.ML.Core.
199201
if (containingSymbolName != "Microsoft.ML.Runtime.Contracts" &&
200-
containingSymbolName != "Microsoft.ML.Runtime.Internal.Contracts")
202+
containingSymbolName != "Microsoft.ML.Runtime.Internal.CpuMath.Core.Contracts")
201203
{
202204
return;
203205
}

0 commit comments

Comments
 (0)