Пример #1
0
        /// <summary>
        /// Frees memory buffers, caches and handles allocated in or to the provider.
        /// Does not unload the provider itself, it is still usable afterwards.
        /// </summary>
        public virtual void FreeResources()
        {
            Kernel kernel = Interlocked.Exchange(ref _kernel, null);
            if (kernel != null)
            {
                SafeNativeMethods.x_fft_free(ref kernel.Handle);
            }

            MklProvider.FreeResources();
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            MklProvider.Load(minRevision: 4);
            MklProvider.ConfigurePrecision(_consistency, _precision, _accuracy);

            int linearAlgebra = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMajor);

            // we only support exactly one major version, since major version changes imply a breaking change.
            if (linearAlgebra != 2)
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting linear algebra v2 but provider implements v{0}.", linearAlgebra));
            }
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available. If not, fall back to alternatives like the managed provider
        /// </summary>
        public void InitializeVerify()
        {
            MklProvider.Load(minRevision: 11);

            // we only support exactly one major version, since major version changes imply a breaking change.
            int fftMajor = SafeNativeMethods.query_capability((int)ProviderCapability.FourierTransformMajor);
            int fftMinor = SafeNativeMethods.query_capability((int)ProviderCapability.FourierTransformMinor);

            if (!(fftMajor == 1 && fftMinor >= 0))
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting fourier transform v1 but provider implements v{0}.", fftMajor));
            }
        }
Пример #4
0
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public void InitializeVerify()
        {
            int revision = MklProvider.Load(_hintPath);

            if (revision < MinimumCompatibleRevision)
            {
                throw new NotSupportedException($"MKL Native Provider revision r{revision} is too old. Consider upgrading to a newer version. Revision r{MinimumCompatibleRevision} and newer are supported.");
            }

            sparseSolverMajor = SafeNativeMethods.query_capability((int)ProviderCapability.SparseSolverMajor);
            sparseSolverMinor = SafeNativeMethods.query_capability((int)ProviderCapability.SparseSolverMinor);
            if (!(sparseSolverMajor == 1 && sparseSolverMinor >= 0))
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting sparse solver v1 but provider implements v{0}.", sparseSolverMajor));
            }
        }
Пример #5
0
        /// <summary>
        /// Initialize and verify that the provided is indeed available. If not, fall back to alternatives like the managed provider
        /// </summary>
        public void InitializeVerify()
        {
            int revision = MklProvider.Load(hintPath: _hintPath);
            if (revision < MinimumCompatibleRevision)
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider revision r{revision} is too old. Consider upgrading to a newer version. Revision r{MinimumCompatibleRevision} and newer are supported."));
            }

            // we only support exactly one major version, since major version changes imply a breaking change.
            int fftMajor = SafeNativeMethods.query_capability((int) ProviderCapability.FourierTransformMajor);
            int fftMinor = SafeNativeMethods.query_capability((int) ProviderCapability.FourierTransformMinor);
            if (!(fftMajor == 1 && fftMinor >= 0))
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider not compatible. Expecting Fourier transform v1 but provider implements v{fftMajor}."));
            }
        }
Пример #6
0
        public void Mnist([DefaultValue(true)] bool gui)
        {
            const int batchSize = 128;
            const int hSize     = 20;

            MklProvider.TryUseMkl(true, ConsoleProgressWriter.Instance);

            string dataDir = Path.Combine(Path.GetTempPath(), "Retia_datasets", "MNIST");

            DownloadDataset(dataDir);

            Console.WriteLine("Loading training set");
            var trainSet = LoadTrainingSet(dataDir);

            trainSet.BatchSize = batchSize;

            var network = new LayeredNet <float>(batchSize, 1,
                                                 new AffineLayer <float>(trainSet.InputSize, hSize, AffineActivation.Sigmoid),
                                                 new LinearLayer <float>(hSize, trainSet.TargetSize),
                                                 new SoftMaxLayer <float>(trainSet.TargetSize));
            var optimizer = new AdamOptimizer <float>();

            network.Optimizer = optimizer;

            var trainer = new OptimizingTrainer <float>(network, optimizer, trainSet,
                                                        new OptimizingTrainerOptions(1)
            {
                ErrorFilterSize = 100,
                MaxEpoch        = 1,
                ProgressWriter  = ConsoleProgressWriter.Instance,
                ReportProgress  = new EachIteration(100),
                ReportMesages   = true
            }, new OptimizingSession("MNIST"));

            RetiaGui retiaGui;

            if (gui)
            {
                retiaGui = new RetiaGui();
                retiaGui.RunAsync(() => new TrainingWindow(new TypedTrainingModel <float>(trainer)));
            }

            var runner = ConsoleRunner.Create(trainer, network);

            runner.Run();
        }
        public void InitializeVerify()
        {
            int revision = MklProvider.Load(_hintPath, _consistency, _precision, _accuracy);

            if (revision < MinimumCompatibleRevision)
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider revision r{revision} is too old. Consider upgrading to a newer version. Revision r{MinimumCompatibleRevision} and newer are supported."));
            }

            _linearAlgebraMajor   = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMajor);
            _linearAlgebraMinor   = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMinor);
            _vectorFunctionsMajor = SafeNativeMethods.query_capability((int)ProviderCapability.VectorFunctionsMajor);
            _vectorFunctionsMinor = SafeNativeMethods.query_capability((int)ProviderCapability.VectorFunctionsMinor);

            // we only support exactly one major version, since major version changes imply a breaking change.
            if (_linearAlgebraMajor != 2)
            {
                throw new NotSupportedException(FormattableString.Invariant($"MKL Native Provider not compatible. Expecting linear algebra v2 but provider implements v{_linearAlgebraMajor}."));
            }
        }
Пример #8
0
        public void Xor()
        {
            MklProvider.TryUseMkl(true, ConsoleProgressWriter.Instance);

            var optimizer = new RMSPropOptimizer <float>(1e-3f);
            var net       = new LayeredNet <float>(1, 1, new AffineLayer <float>(2, 3, AffineActivation.Tanh), new AffineLayer <float>(3, 1, AffineActivation.Tanh)
            {
                ErrorFunction = new MeanSquareError <float>()
            })
            {
                Optimizer = optimizer
            };

            var trainer = new OptimizingTrainer <float>(net, optimizer, new XorDataset(true), new OptimizingTrainerOptions(1)
            {
                ErrorFilterSize    = 0,
                ReportProgress     = new EachIteration(1),
                ReportMesages      = true,
                ProgressWriter     = ConsoleProgressWriter.Instance,
                LearningRateScaler = new ProportionalLearningRateScaler(new EachIteration(1), 9e-5f)
            }, new OptimizingSession("XOR"));

            var runner = ConsoleRunner.Create(trainer, net);

            trainer.TrainReport += (sender, args) =>
            {
                if (args.Errors.Last().RawError < 1e-7f)
                {
                    runner.Stop();
                    Console.WriteLine("Finished training.");
                }
            };

            var gui = new RetiaGui();

            gui.RunAsync(() => new TrainingWindow(new TypedTrainingModel <float>(trainer)));

            runner.Run();
        }
Пример #9
0
        public void Learn(
            [Aliases("b"), Required]        string batchesPath,
            [Aliases("c")]                          string configPath,
            [Aliases("r"), DefaultValue(0.0002f)]  float learningRate,
            [DefaultValue(false)]       bool gpu,
            [DefaultValue(true)]       bool gui)
        {
            MklProvider.TryUseMkl(true, ConsoleProgressWriter.Instance);

            Console.WriteLine($"Loading test set from {batchesPath}");
            _dataProvider.Load(batchesPath);

            var optimizer = new RMSPropOptimizer <float>(learningRate, 0.95f, 0.0f, 0.9f);
            LayeredNet <float> network;

            if (string.IsNullOrEmpty(configPath))
            {
                network           = CreateNetwork(_dataProvider.TrainingSet.InputSize, 128, _dataProvider.TrainingSet.TargetSize);
                network.Optimizer = optimizer;
            }
            else
            {
                network           = CreateNetwork(configPath);
                network.Optimizer = optimizer;
            }
            network.ResetOptimizer();

            if (gpu)
            {
                Console.WriteLine("Brace yourself for GPU!");
                network.UseGpu();
            }

            var trainOptions = new OptimizingTrainerOptions(SEQ_LEN)
            {
                ErrorFilterSize = 100,
                ReportMesages   = true,
                MaxEpoch        = 1000,
                ProgressWriter  = ConsoleProgressWriter.Instance,
                ReportProgress  = new EachIteration(10)
            };

            trainOptions.LearningRateScaler = new ProportionalLearningRateScaler(new ActionSchedule(1, PeriodType.Iteration), 9.9e-5f);

            var session = new OptimizingSession(Path.GetFileNameWithoutExtension(batchesPath));
            var trainer = new OptimizingTrainer <float>(network, optimizer, _dataProvider.TrainingSet, trainOptions, session);

            RetiaGui retiaGui;
            TypedTrainingModel <float> model = null;

            if (gui)
            {
                retiaGui = new RetiaGui();
                retiaGui.RunAsync(() =>
                {
                    model = new TypedTrainingModel <float>(trainer);
                    return(new TrainingWindow(model));
                });
            }

            var epochWatch = new Stopwatch();

            trainer.EpochReached += sess =>
            {
                epochWatch.Stop();
                Console.WriteLine($"Trained epoch in {epochWatch.Elapsed.TotalSeconds} s.");

                // Showcasing plot export
                if (model != null)
                {
                    using (var stream = new MemoryStream())
                    {
                        model.ExportErrorPlot(stream, 600, 400);

                        stream.Seek(0, SeekOrigin.Begin);

                        session.AddFileToReport("ErrorPlots\\plot.png", stream);
                    }
                }

                epochWatch.Restart();
            };
            trainer.PeriodicActions.Add(new UserAction(new ActionSchedule(100, PeriodType.Iteration), () =>
            {
                if (gpu)
                {
                    network.TransferStateToHost();
                }

                string text = TestRNN(network.Clone(1, SEQ_LEN), 500, _dataProvider.Vocab);
                Console.WriteLine(text);
                session.AddFileToReport("Generated\\text.txt", text);

                trainOptions.ProgressWriter.ItemComplete();
            }));

            var runner = ConsoleRunner.Create(trainer, network);

            epochWatch.Start();
            runner.Run();
        }
Пример #10
0
 /// <summary>
 /// Frees memory buffers, caches and handles allocated in or to the provider.
 /// Does not unload the provider itself, it is still usable afterwards.
 /// </summary>
 public override void FreeResources()
 {
     MklProvider.FreeResources();
 }
 /// <summary>
 /// Frees the memory allocated to the MKL memory pool on the current thread.
 /// </summary>
 public void ThreadFreeBuffers()
 {
     MklProvider.ThreadFreeBuffers();
 }
Пример #12
0
 /// <summary>
 /// Try to find out whether the provider is available, at least in principle.
 /// Verification may still fail if available, but it will certainly fail if unavailable.
 /// </summary>
 public override bool IsAvailable()
 {
     return(MklProvider.IsAvailable(minRevision: 4, hintPath: _hintPath));
 }
Пример #13
0
 /// <summary>
 /// Try to find out whether the provider is available, at least in principle.
 /// Verification may still fail if available, but it will certainly fail if unavailable.
 /// </summary>
 public override bool IsAvailable()
 {
     return(MklProvider.IsAvailable(minRevision: 4));
 }
Пример #14
0
 /// <summary>
 /// Try to find out whether the provider is available, at least in principle.
 /// Verification may still fail if available, but it will certainly fail if unavailable.
 /// </summary>
 public bool IsAvailable()
 {
     return(MklProvider.IsAvailable(minRevision: 11, hintPath: _hintPath));
 }
Пример #15
0
 /// <summary>
 /// Frees memory buffers, caches and handles allocated in or to the provider.
 /// Does not unload the provider itself, it is still usable afterwards.
 /// </summary>
 public void FreeResources()
 {
     MklProvider.FreeResources();
 }
 /// <summary>
 /// Enable gathering of peak memory statistics of the MKL memory pool.
 /// </summary>
 public void EnablePeakMemoryStatistics()
 {
     MklProvider.EnablePeakMemoryStatistics();
 }
 /// <summary>
 /// Retrieves information about the MKL memory pool.
 /// </summary>
 /// <param name="allocatedBuffers">On output, returns the number of memory buffers allocated.</param>
 /// <returns>Returns the number of bytes allocated to all memory buffers.</returns>
 public long MemoryStatistics(out int allocatedBuffers)
 {
     return(MklProvider.MemoryStatistics(out allocatedBuffers));
 }
 /// <summary>
 /// Disable the MKL memory pool. May impact performance.
 /// </summary>
 public void DisableMemoryPool()
 {
     MklProvider.DisableMemoryPool();
 }
Пример #19
0
 /// <summary>
 /// Frees the memory allocated to the MKL memory pool.
 /// </summary>
 public void FreeBuffers()
 {
     MklProvider.FreeBuffers();
 }
Пример #20
0
 /// <summary>
 /// Try to find out whether the provider is available, at least in principle.
 /// Verification may still fail if available, but it will certainly fail if unavailable.
 /// </summary>
 public bool IsAvailable()
 {
     return(MklProvider.IsAvailable(hintPath: _hintPath));
 }
 /// <summary>
 /// Disable gathering of peak memory statistics of the MKL memory pool.
 /// </summary>
 public void DisablePeakMemoryStatistics()
 {
     MklProvider.DisablePeakMemoryStatistics();
 }
 /// <summary>
 /// Try to find out whether the provider is available, at least in principle.
 /// Verification may still fail if available, but it will certainly fail if unavailable.
 /// </summary>
 public bool IsAvailable()
 {
     return(MklProvider.IsAvailable(minRevision: 11));
 }
Пример #23
0
 public override string ToString()
 {
     return(MklProvider.Describe());
 }
 /// <summary>
 /// Measures peak memory usage of the MKL memory pool.
 /// </summary>
 /// <param name="reset">Whether the usage counter should be reset.</param>
 /// <returns>The peak number of bytes allocated to all memory buffers.</returns>
 public long PeakMemoryStatistics(bool reset = true)
 {
     return(MklProvider.PeakMemoryStatistics(reset));
 }