SlimMPSCnnFullyConnected (IMTLDevice device, MPSCnnConvolutionDescriptor convolutionDescriptor, IntPtr kernelWeights, IntPtr biasTerms, MPSCnnConvolutionFlags flags)
			: base (NSObjectFlag.Empty)
		{
			var sel = "initWithDevice:convolutionDescriptor:kernelWeights:biasTerms:flags:";
			var selector = Selector.GetHandle (sel);
			InitializeHandle (IntPtr_objc_msgSend_IntPtr_IntPtr_IntPtr_IntPtr_UInt64 (Handle, selector, device.Handle, convolutionDescriptor.Handle, kernelWeights, biasTerms, (ulong)flags), sel);
		}
        public static SlimMPSCnnFullyConnected Create(uint kernelWidth, uint kernelHeight,
                                                      uint inputFeatureChannels, uint outputFeatureChannels,
                                                      MPSCnnNeuron neuronFilter, IMTLDevice device,
                                                      string kernelParamsBinaryName,
                                                      uint destinationFeatureChannelOffset = 0)
        {
            // get the url to this layer's weights and bias
            var wtPath = NSBundle.MainBundle.PathForResource($"weights_{kernelParamsBinaryName}", "dat");
            var bsPath = NSBundle.MainBundle.PathForResource($"bias_{kernelParamsBinaryName}", "dat");

            unsafe
            {
                using (var mmfW = MemoryMappedFile.CreateFromFile(wtPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read))
                    using (var mmfB = MemoryMappedFile.CreateFromFile(bsPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read))
                        using (var wView = mmfW.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
                            using (var bView = mmfB.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read)) {
                                byte *w = null;
                                wView.SafeMemoryMappedViewHandle.AcquirePointer(ref w);

                                byte *b = null;
                                bView.SafeMemoryMappedViewHandle.AcquirePointer(ref b);

                                var convDesc = MPSCnnConvolutionDescriptor.GetConvolutionDescriptor(
                                    kernelWidth, kernelHeight,
                                    inputFeatureChannels, outputFeatureChannels,
                                    neuronFilter);

                                return(new SlimMPSCnnFullyConnected(device, convDesc, (IntPtr)w, (IntPtr)b, MPSCnnConvolutionFlags.None)
                                {
                                    DestinationFeatureChannelOffset = destinationFeatureChannelOffset
                                });
                            }
            }
        }
예제 #3
0
        unsafe SlimMPSCnnConvolution(IMTLDevice device, MPSCnnConvolutionDescriptor convolutionDescriptor, IntPtr kernelWeights, IntPtr biasTerms, MPSCnnConvolutionFlags flags)
            : base(NSObjectFlag.Empty)
        {
            const string sel = "initWithDevice:convolutionDescriptor:kernelWeights:biasTerms:flags:";

            InitializeHandle(IntPtr_objc_msgSend_IntPtr_IntPtr_IntPtr_IntPtr_UInt64(Handle, Selector.GetHandle(sel), device.Handle, convolutionDescriptor.Handle, kernelWeights, biasTerms, (ulong)flags), sel);
        }
예제 #4
0
        /// <summary>
        /// Initializes a fully connected kernel.
        /// Returns: A valid SlimMPSCnnConvolution object or null, if failure.
        /// </summary>
        /// <param name="kernelWidth">Kernel Width</param>
        /// <param name="kernelHeight">Kernel Height</param>
        /// <param name="inputFeatureChannels">Number feature channels in input of this layer</param>
        /// <param name="outputFeatureChannels">Number feature channels from output of this layer</param>
        /// <param name="neuronFilter">A neuronFilter to add at the end as activation (could be null)</param>
        /// <param name="device">The IMTLDevice on which this SlimMPSCnnConvolution filter will be used</param>
        /// <param name="kernelParamsBinaryName">name of the layer to fetch kernelParameters by adding a prefix "weights_" or "bias_"</param>
        /// <param name="padding">Bool value whether to use padding or not</param>
        /// <param name="strideX">Stride of the filter</param>
        /// <param name="strideY">Stride of the filter</param>
        /// <param name="destinationFeatureChannelOffset">FeatureChannel no. in the destination MPSImage to start writing from, helps with concat operations</param>
        /// <param name="groupNum">if grouping is used, default value is 1 meaning no groups</param>
        public static SlimMPSCnnConvolution Create(uint kernelWidth, uint kernelHeight,
                                                   uint inputFeatureChannels, uint outputFeatureChannels,
                                                   MPSCnnNeuron neuronFilter, IMTLDevice device,
                                                   string kernelParamsBinaryName, bool padding,
                                                   uint strideX, uint strideY,
                                                   uint destinationFeatureChannelOffset, uint groupNum)
        {
            // get the url to this layer's weights and bias
            var wtPath = NSBundle.MainBundle.PathForResource($"weights_{kernelParamsBinaryName}", "dat");
            var bsPath = NSBundle.MainBundle.PathForResource($"bias_{kernelParamsBinaryName}", "dat");

            // create appropriate convolution descriptor with appropriate stride
            var convDesc = MPSCnnConvolutionDescriptor.GetConvolutionDescriptor(
                kernelWidth, kernelHeight,
                inputFeatureChannels, outputFeatureChannels,
                neuronFilter);

            convDesc.StrideInPixelsX = strideX;
            convDesc.StrideInPixelsY = strideY;

            if (groupNum <= 0)
            {
                throw new ArgumentException("Group size can't be less than 1");
            }
            convDesc.Groups = groupNum;

            unsafe
            {
                using (var mmfW = MemoryMappedFile.CreateFromFile(wtPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read))
                    using (var mmfB = MemoryMappedFile.CreateFromFile(bsPath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read))
                        using (var wView = mmfW.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
                            using (var bView = mmfB.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read)) {
                                byte *w = null;
                                wView.SafeMemoryMappedViewHandle.AcquirePointer(ref w);

                                byte *b = null;
                                bView.SafeMemoryMappedViewHandle.AcquirePointer(ref b);

                                return(new SlimMPSCnnConvolution(device, convDesc, (IntPtr)w, (IntPtr)b, MPSCnnConvolutionFlags.None)
                                {
                                    DestinationFeatureChannelOffset = destinationFeatureChannelOffset,
                                    padding = padding
                                });
                            }
            }
        }
        public ConvolutionWeights(CompileOptions options, int inChannels, int outChannels, int kernelSize, int stride, bool bias, string label, int seed)
        {
            this.options = options.ExecutionOptions;

            descriptor = MPSCnnConvolutionDescriptor.CreateCnnConvolutionDescriptor(
                (System.nuint)kernelSize, (System.nuint)kernelSize,
                (System.nuint)inChannels,
                (System.nuint)outChannels);
            descriptor.StrideInPixelsX = (nuint)stride;
            descriptor.StrideInPixelsY = (nuint)stride;
            this.bias  = bias;
            this.label = string.IsNullOrEmpty(label) ? Guid.NewGuid().ToString() : label;

            var lenWeights = inChannels * kernelSize * kernelSize * outChannels;

            var vDescWeights = VectorDescriptor(lenWeights);

            weightVectors = new OptimizableVector(options.Device, vDescWeights, 0.0f);

            var vDescBiases = VectorDescriptor(outChannels);

            biasVectors = new OptimizableVector(options.Device, vDescBiases, 0.1f);

            RandomizeWeights((nuint)seed);

            convWtsAndBias  = new MPSCnnConvolutionWeightsAndBiasesState(weightVectors.Value.Data, biasVectors.Value.Data);
            momentumVectors = NSArray <MPSVector> .FromNSObjects(weightVectors.Momentum, biasVectors.Momentum);

            velocityVectors = NSArray <MPSVector> .FromNSObjects(weightVectors.Velocity, biasVectors.Velocity);

            var odesc = new MPSNNOptimizerDescriptor(options.LearningRate, 1.0f, MPSNNRegularizationType.None, 1.0f);

            updater = new MPSNNOptimizerAdam(
                options.Device,
                beta1: 0.9f, beta2: 0.999f, epsilon: 1e-8f,
                timeStep: 0,
                optimizerDescriptor: odesc);
        }