Skip to content

Commit

Permalink
[MetalPerformanceShaders] Neural Networks Update to Xcode 11 (xamarin…
Browse files Browse the repository at this point in the history
…#8844)

* [MetalPerformanceShaders] Neural Networks Update to Xcode 11

This includes updates from PRs xamarin#6932, xamarin#6935 and xamarin#7461

It adds new functionality to the neural network components.
This is still not the complete API for 11.3

* Update src/metalperformanceshaders.cs

Co-Authored-By: Rolf Bjarne Kvinge <[email protected]>

* Make changes requested by @rolfbjarne

* Fix binding of default random distribution creation

The parameterless function creates new
default distributions and is not a property.
It is a counterpart of CreateUniform.

* Expose public APIs for MPSCnnConvolutionTransposeNode

These APIs are public and documented at: https://developer.apple.com/documentation/metalperformanceshaders/mpscnnconvolutiontransposenode/2942641-initwithsource?language=objc

I have also tested that they work.

* Reintroduce compat API.

* Fix acronym casing.

* Fix introspection tests.

* Fix xtro.

* One last xtro issue.

* Fix more xtro.

* Another introspection fix.

Co-authored-By: Frank A. Krueger <[email protected]>
Co-authored-by: Rolf Bjarne Kvinge <[email protected]>

* Apply suggestions from code review

Co-authored-by: Rolf Bjarne Kvinge <[email protected]>

* Apply feedback

* Please the typo guardians

Co-authored-by: Frank A. Krueger <[email protected]>
Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
  • Loading branch information
3 people committed Jul 26, 2020
1 parent 2a81b70 commit 75349d9
Show file tree
Hide file tree
Showing 19 changed files with 781 additions and 582 deletions.
119 changes: 114 additions & 5 deletions src/MetalPerformanceShaders/MPSDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace MetalPerformanceShaders {

[iOS (9,0)][Mac (10, 13)]
[iOS (9,0)][Mac (10, 13)][Introduced (PlatformName.MacCatalyst, 13, 0)]
[Native] // NSUInteger
[Flags] // NS_OPTIONS
public enum MPSKernelOptions : ulong {
Expand All @@ -29,7 +29,7 @@ public enum MPSKernelOptions : ulong {
#endif
}

[iOS (9,0)][Mac (10, 13)]
[iOS (9,0)][Mac (10, 13)][Introduced (PlatformName.MacCatalyst, 13, 0)]
[Native] // NSUInteger
public enum MPSImageEdgeMode : ulong {
Zero,
Expand All @@ -50,7 +50,7 @@ public enum MPSAlphaType : ulong {
Premultiplied = 2,
}

[iOS (10,0)][TV (10,0)][Mac (10, 13)]
[iOS (10,0)][TV (10,0)][Mac (10, 13)][Introduced (PlatformName.MacCatalyst, 13, 0)]
public enum MPSDataType : uint { // uint32_t
Invalid = 0,

Expand All @@ -61,6 +61,7 @@ public enum MPSDataType : uint { // uint32_t
SignedBit = 0x20000000,
Int8 = SignedBit | 8,
Int16 = SignedBit | 16,
Int32 = SignedBit | 32,

UInt8 = 8,
UInt16 = 16,
Expand All @@ -74,14 +75,30 @@ public enum MPSDataType : uint { // uint32_t
Unorm8 = NormalizedBit | 8,
}

[iOS (10,0)][TV (10,0)][Mac (10, 13)]
[Flags]
[Native]
public enum MPSAliasingStrategy : ulong
{
Default = 0x0,
DontCare = Default,
ShallAlias = 1uL << 0,
ShallNotAlias = 1uL << 1,
AliasingReserved = ShallAlias | ShallNotAlias,
PreferTemporaryMemory = 1uL << 2,
PreferNonTemporaryMemory = 1uL << 3,
}

[iOS (10,0)][TV (10,0)][Mac (10,13)][Introduced (PlatformName.MacCatalyst, 13, 0)]
[Native]
public enum MPSImageFeatureChannelFormat : ulong {
Invalid = 0,
Unorm8 = 1,
Unorm16 = 2,
Float16 = 3,
Float32 = 4,
[iOS (13,0), TV (13,0), Mac (10,15)]
Reserved0 = 5,

//[iOS (12,0), TV (12,0), Mac (10,14)]
//Count, // must always be last, and because of this it will cause breaking changes.
}
Expand Down Expand Up @@ -110,6 +127,12 @@ public struct MPSSize {
public double Depth;
}

// uses NSUInteger
public struct MPSDimensionSlice {
public nuint Start;
public nuint Length;
}

[Mac (10, 13)]
public struct MPSRegion {
public MPSOrigin Origin;
Expand Down Expand Up @@ -156,6 +179,7 @@ public struct MPSImageHistogramInfo {
public Vector4 MaxPixelValue;
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (11, 0), Mac (10, 13), iOS (11, 0)]
public enum MPSMatrixDecompositionStatus {
Success = 0,
Expand All @@ -164,6 +188,15 @@ public enum MPSMatrixDecompositionStatus {
NonPositiveDefinite = -3,
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[Flags]
[Native]
public enum MPSMatrixRandomDistribution : ulong
{
Default = 0x1,
Uniform = 0x2,
}

// MPSTypes.h
// FIXME: public delegate IMTLTexture MPSCopyAllocator (MPSKernel filter, IMTLCommandBuffer commandBuffer, IMTLTexture sourceTexture);
public delegate NSObject MPSCopyAllocator (MPSKernel filter, NSObject commandBuffer, NSObject sourceTexture);
Expand Down Expand Up @@ -246,7 +279,7 @@ public enum MPSNNPaddingMethod : ulong {
ExcludeEdges = (1 << 15),
}

[TV (11, 0), Mac (10, 13), iOS (11, 0)]
[TV (11, 0), Mac (10, 13), iOS (11, 0)][Introduced (PlatformName.MacCatalyst, 13, 0)]
[Native]
public enum MPSDataLayout : ulong {
HeightPerWidthPerFeatureChannels = 0,
Expand Down Expand Up @@ -308,6 +341,7 @@ public MTLTextureUsage TextureUsage {
}
#endif
}
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (11, 3), iOS (11, 3), Mac (10, 13, 4)]
[Native]
public enum MPSStateResourceType : ulong {
Expand Down Expand Up @@ -422,6 +456,7 @@ public enum MPSNNComparisonType : ulong {
GreaterOrEqual,
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (11, 3), Mac (10, 13, 4), iOS (11, 3)]
public enum MPSCnnLossType : uint {
MeanAbsoluteError = 0,
Expand All @@ -437,6 +472,7 @@ public enum MPSCnnLossType : uint {
//Count, // must always be last, and because of this it will cause breaking changes.
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (11, 3), Mac (10, 13, 4), iOS (11, 3)]
public enum MPSCnnReductionType {
None = 0,
Expand Down Expand Up @@ -516,4 +552,77 @@ public enum MPSRnnMatrixId : ulong {
GruOutputGateBiasTerms,
//Count, // must always be last, and because of this it will cause breaking changes.
}

public static class MPSConstants
{
public const uint FunctionConstantIndex = 127;
public const uint BatchSizeIndex = 126;
public const uint UserConstantIndex = 125;
public const uint NDArrayConstantIndex = 124;
// Maximum number of textures depends on the platform
// MaxTextures = 128 or 32,
}

public enum MPSCustomKernelIndex : uint
{
DestIndex = 0,
Src0Index = 0,
Src1Index = 1,
Src2Index = 2,
Src3Index = 3,
Src4Index = 4,
UserDataIndex = 30,
}

[StructLayout (LayoutKind.Sequential)]
public struct MPSMatrixOffset
{
public uint RowOffset;
public uint ColumnOffset;
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (11,3), Mac (10,13,4), iOS (11,3)]
public enum MPSImageType : uint
{
Type2d = 0,
Type2dArray = 1,
Array2d = 2,
Array2dArray = 3,

ArrayMask = 1,
BatchMask = 2,
TypeMask = 3,
NoAlpha = 4,
TexelFormatMask = 56,
TexelFormatShift = 3,
TexelFormatStandard = 0u << (int)TexelFormatShift,
TexelFormatUnorm8 = 1u << (int)TexelFormatShift,
TexelFormatFloat16 = 2u << (int)TexelFormatShift,
TexelFormatBFloat16 = 3u << (int)TexelFormatShift,
BitCount = 6,
Mask = (1u << (int)BitCount) - 1,
Type2dNoAlpha = Type2d | NoAlpha,
Type2dArrayNoAlpha = Type2dArray | NoAlpha,
Array2dNoAlpha = Type2d | BatchMask | NoAlpha,
Array2dArrayNoAlpha = Type2dArray | BatchMask | NoAlpha,

DestTextureType = (MPSConstants.FunctionConstantIndex >> (int)(0*BitCount)) & Mask,
Src0TextureType = (MPSConstants.FunctionConstantIndex >> (int)(0*BitCount)) & Mask,
Src1TextureType = (MPSConstants.FunctionConstantIndex >> (int)(1*BitCount)) & Mask,
Src2TextureType = (MPSConstants.FunctionConstantIndex >> (int)(2*BitCount)) & Mask,
Src3TextureType = (MPSConstants.FunctionConstantIndex >> (int)(3*BitCount)) & Mask,
Src4TextureType = (MPSConstants.FunctionConstantIndex >> (int)(4*BitCount)) & Mask,
}

[Flags]
[Native]
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (12,2), Mac (10,14,4), iOS (12,2)]
public enum MPSDeviceOptions : ulong
{
Default = 0x0,
LowPower = 0x1,
SkipRemovable = 0x2,
}
}
8 changes: 4 additions & 4 deletions src/MetalPerformanceShaders/MPSImageBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Metal;

namespace MetalPerformanceShaders {
[iOS (11,3), TV (11,3), Mac (10,13,4)]
[iOS (11,3), TV (11,3), Mac (10,13,4)][Introduced (PlatformName.MacCatalyst, 13, 0)]
public static partial class MPSImageBatch {

[DllImport (Constants.MetalPerformanceShadersLibrary)]
Expand Down Expand Up @@ -58,11 +58,11 @@ public static nuint GetResourceSize (NSArray<MPSImage> imageBatch)
}

// TODO: Disabled due to 'MPSImageBatchIterate' is not in the native library rdar://47282304.
//[iOS (12,0), TV (12,0), Mac (10,14)]
//[iOS (12,0), TV (12,0), Mac (10,14)][Introduced (PlatformName.MacCatalyst, 13, 0)]
//[DllImport (Constants.MetalPerformanceShadersLibrary)]
//static extern nint MPSImageBatchIterate (IntPtr batch, IntPtr iterator);

//[iOS (12,0), TV (12,0), Mac (10,14)]
//[iOS (12,0), TV (12,0), Mac (10,14)][Introduced (PlatformName.MacCatalyst, 13, 0)]
//public delegate nint MPSImageBatchIterator (MPSImage image, nuint index);

//[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
Expand All @@ -85,7 +85,7 @@ public static nuint GetResourceSize (NSArray<MPSImage> imageBatch)
// }
//}

//[iOS (12,0), TV (12,0), Mac (10,14)]
//[iOS (12,0), TV (12,0), Mac (10,14)][Introduced (PlatformName.MacCatalyst, 13, 0)]
//[BindingImpl (BindingImplOptions.Optimizable)]
//public static nint Iterate (NSArray<MPSImage> imageBatch, MPSImageBatchIterator iterator)
//{
Expand Down
23 changes: 23 additions & 0 deletions src/MetalPerformanceShaders/MPSKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public static bool Supports (IMTLDevice device)
return MPSSupportsMTLDevice (device == null ? IntPtr.Zero : device.Handle);
}

[DllImport (Constants.MetalPerformanceShadersLibrary)]
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (13,0), Mac (10,15), iOS (13,0)]
static extern /* id<MTLDevice> _Nullable */ IntPtr MPSGetPreferredDevice (nuint options);

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (13,0), Mac (10,15), iOS (13,0)]
public static IMTLDevice GetPreferredDevice (MPSDeviceOptions options)
{
var h = MPSGetPreferredDevice ((nuint)(ulong) options);
return Runtime.GetINativeObject<IMTLDevice> (h, false);
}

internal unsafe static float [] GetTransform (IntPtr transform)
{
var t = (float*) transform;
Expand Down Expand Up @@ -57,6 +70,16 @@ public unsafe static MTLRegion RectNoClip {
}

#if !COREBUILD
public partial class MPSImage {

[iOS (13,0), TV (12,0), Mac (10,15)][Introduced (PlatformName.MacCatalyst, 13, 0)]
[DllImport (Constants.MetalPerformanceShadersLibrary)]
static extern MPSImageType MPSGetImageType (IntPtr image);

[iOS (13,0), TV (12,0), Mac (10,15)][Introduced (PlatformName.MacCatalyst, 13, 0)]
public MPSImageType ImageType => MPSGetImageType (Handle);
}

public partial class MPSImageDilate {

[DesignatedInitializer]
Expand Down
52 changes: 52 additions & 0 deletions src/MetalPerformanceShaders/MPSNDArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using Metal;
using Foundation;

namespace MetalPerformanceShaders {
public partial class MPSNDArray {

public void ExportData (IMTLCommandBuffer cmdBuf, IMTLBuffer buffer, MPSDataType sourceDataType, nuint offset)
{
ExportData (cmdBuf, buffer, sourceDataType, offset, IntPtr.Zero);
}
public unsafe void ExportData (IMTLCommandBuffer cmdBuf, IMTLBuffer buffer, MPSDataType sourceDataType, nuint offset, nint[] rowStrides)
{
fixed (nint* p = rowStrides) {
ExportData (cmdBuf, buffer, sourceDataType, offset, (IntPtr)p);
}
}

public void ImportData (IMTLCommandBuffer cmdBuf, IMTLBuffer buffer, MPSDataType sourceDataType, nuint offset)
{
ImportData (cmdBuf, buffer, sourceDataType, offset, IntPtr.Zero);
}
public unsafe void ImportData (IMTLCommandBuffer cmdBuf, IMTLBuffer buffer, MPSDataType sourceDataType, nuint offset, nint[] rowStrides)
{
fixed (nint* p = rowStrides) {
ImportData (cmdBuf, buffer, sourceDataType, offset, (IntPtr)p);
}
}

public void WriteBytes (IntPtr buffer)
{
WriteBytes (buffer, IntPtr.Zero);
}
public unsafe void WriteBytes (IntPtr buffer, nint[] strideBytesPerDimension)
{
fixed (nint* p = strideBytesPerDimension) {
WriteBytes (buffer, (IntPtr)p);
}
}

public void ReadBytes (IntPtr buffer)
{
ReadBytes (buffer, IntPtr.Zero);
}
public unsafe void ReadBytes (IntPtr buffer, nint[] strideBytesPerDimension)
{
fixed (nint* p = strideBytesPerDimension) {
ReadBytes (buffer, (IntPtr)p);
}
}
}
}
18 changes: 18 additions & 0 deletions src/MetalPerformanceShaders/MPSNNGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Runtime.InteropServices;
using CoreGraphics;
using Foundation;
using Metal;
using ObjCRuntime;

namespace MetalPerformanceShaders {
public partial class MPSNNGraph {
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[TV (13,0), Mac (10,15), iOS (13,0)]
public unsafe static MPSNNGraph Create (IMTLDevice device, MPSNNImageNode[] resultImages, bool[] resultsAreNeeded)
{
fixed (void *resultsAreNeededHandle = resultsAreNeeded)
return Create (device, resultImages, (IntPtr) resultsAreNeededHandle);
}
}
}
3 changes: 3 additions & 0 deletions src/MetalPerformanceShaders/MPSStateBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Metal;

namespace MetalPerformanceShaders {
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[iOS (11,3), TV (11,3), Mac (10,13,4)]
public static partial class MPSStateBatch {

Expand Down Expand Up @@ -43,11 +44,13 @@ public static void Synchronize (NSArray<MPSState> stateBatch, IMTLCommandBuffer
MPSStateBatchSynchronize (stateBatch.Handle, commandBuffer.Handle);
}

[Introduced (PlatformName.MacCatalyst, 13, 0)]
[iOS (12,0), TV (12,0), Mac (10,14)]
[DllImport (Constants.MetalPerformanceShadersLibrary)]
static extern nuint MPSStateBatchResourceSize (IntPtr batch);

// Using 'NSArray<MPSState>' instead of `MPSState[]` because array 'Handle' matters.
[Introduced (PlatformName.MacCatalyst, 13, 0)]
[iOS (12,0), TV (12,0), Mac (10,14)]
public static nuint GetResourceSize (NSArray<MPSState> stateBatch)
{
Expand Down
3 changes: 0 additions & 3 deletions src/ObjCRuntime/Dlfcn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ static public class OpenGLES
static public class AudioToolbox {
static public readonly IntPtr Handle = Dlfcn.dlopen (Constants.AudioToolboxLibrary, 0);
}
static public class MetalPerformanceShaders {
static public readonly IntPtr Handle = Dlfcn.dlopen (Constants.MetalPerformanceShadersLibrary, 0);
}
#endif
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,8 @@ METALPERFORMANCESHADERS_SOURCES = \
MetalPerformanceShaders/MPSCnnNeuron.cs \
MetalPerformanceShaders/MPSCnnKernel.cs \
MetalPerformanceShaders/MPSMatrixDescriptor.cs \
MetalPerformanceShaders/MPSNDArray.cs \
MetalPerformanceShaders/MPSNNGraph.cs \
MetalPerformanceShaders/MPSImageHistogram.cs \
MetalPerformanceShaders/MPSStateBatch.cs \
MetalPerformanceShaders/MPSStateResourceList.cs \
Expand Down
Loading

0 comments on commit 75349d9

Please sign in to comment.