dotnet / dotnet/machinelearning-samples

System.AccessViolationException at TorchSharp.PInvoke.LibTorchSharp.THSGenerator_manual_seed

Open
#1,038 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
PowerShell
Stars
4.7k
Forks
2.7k
Avg merge
2d 22h
Merged PRs (30d)
1

Description

I was trying to reproduce the example of ML.Net: [machinelearning-samples/samples/csharp/getting-started/MLNET2/SentenceSimilarity][1] and I got the following error :

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at TorchSharp.PInvoke.LibTorchSharp.THSGenerator_manual_seed(Int64)
--------------------------------
at TorchSharp.torch+random.manual_seed(Int64)
at Microsoft.ML.TorchSharp.NasBert.NasBertTrainer`2+TrainerBase[[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrainStep(Microsoft.ML.DataViewRowCursor, Microsoft.ML.ValueGetter`1>, Microsoft.ML.ValueGetter`1>, Microsoft.ML.ValueGetter`1, System.Collections.Generic.List`1 ByRef, System.Collections.Generic.List`1 ByRef)
at Microsoft.ML.TorchSharp.NasBert.NasBertTrainer`2+TrainerBase[[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Train(Microsoft.ML.IDataView)
at Microsoft.ML.TorchSharp.NasBert.NasBertTrainer`2[[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Single, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Fit(Microsoft.ML.IDataView)
at Microsoft.ML.Data.EstimatorChain`1[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Fit(Microsoft.ML.IDataView)
at Program.$(System.String[])

in this line:

```var model = pipeline.Fit(dataSplit.TrainSet);```

the complete code is the following:

```using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.TorchSharp;
using MathNet.Numerics.Statistics;
using Microsoft.ML.Transforms;

// Initialize MLContext
var ctx = new MLContext();

// (Optional) Use GPU
ctx.GpuDeviceId = 0;
ctx.FallbackToCpu = false;

// Log training output
ctx.Log += (o, e) => {
if (e.Source.Contains("NasBertTrainer"))
Console.WriteLine(e.Message);
};

// Load data into IDataView
var columns = new[]
{
new TextLoader.Column("search_term",DataKind.String,3),
new TextLoader.Column("relevance",DataKind.Single,4),
new TextLoader.Column("product_description",DataKind.String,5)
};

var loaderOptions = new TextLoader.Options()
{
Columns = columns,
HasHeader = true,
Separators = new[] { ',' },
MaxRows = 1000 // Dataset has 75k rows. Only load 1k for quicker training
};

var dataPath = Path.GetFullPath(@"C:\Dropbox\Documents\Visual Studio 2019\source\repos\Machine Learning & AI\home-depot-sentence-similarity.csv");
var textLoader = ctx.Data.CreateTextLoader(loaderOptions);
var data = textLoader.Load(dataPath);

// Split data into 80% training, 20% testing
var dataSplit = ctx.Data.TrainTestSplit(data, testFraction: 0.2);

// Define pipeline
var pipeline =
ctx.Transforms.ReplaceMissingValues("relevance", replacementMode: MissingValueReplacingEstimator.ReplacementMode.Mean)
.Append(ctx.Regression.Trainers.SentenceSimilarity(labelColumnName: "relevance", sentence1ColumnName: "search_term", sentence2ColumnName: "product_description"));

// Train the model
var model = pipeline.Fit(dataSplit.TrainSet); // THIS IS THE LINE THAT PRODUCED THE ERROR

// Use the model to make predictions on the test dataset
var predictions = model.Transform(dataSplit.TestSet);

// Evaluate the model
Evaluate(predictions, "relevance", "Score");

// Save the model
ctx.Model.Save(model, data.Schema, "model.zip");

void Evaluate(IDataView predictions, string actualColumnName, string predictedColumnName)
{
var actual =
predictions.GetColumn(actualColumnName)
.Select(x => (double)x);
var predicted =
predictions.GetColumn(predictedColumnName)
.Select(x => (double)x);
var corr = Correlation.Pearson(actual, predicted);
Console.WriteLine($"Pearson Correlation: {corr}");
}
```

I used the same version of the packages that were used in the example:

MathNet.Numeric.Signed (5.00)

Microsoft.ML (2.0.0)

Microsoft.ML.TorchSharp (0.20.0)

TorchSharp-cuda-windows (0.98.3)

but the error persist.
Any idea?

[1]: https://github.com/dotnet/machinelearning-samples/blob/main/samples/csharp/getting-started/MLNET2/SentenceSimilarity/Program.cs

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.