Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.AccessViolationException at TorchSharp.PInvoke.LibTorchSharp.THSGenerator_manual_seed #1038

Open
cb7754 opened this issue Jun 23, 2024 · 0 comments

Comments

@cb7754
Copy link

cb7754 commented Jun 23, 2024

I was trying to reproduce the example of ML.Net: machinelearning-samples/samples/csharp/getting-started/MLNET2/SentenceSimilarity 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.NasBertTrainer2+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.ValueGetter1<System.ReadOnlyMemory1<Char>>, Microsoft.ML.ValueGetter1<System.ReadOnlyMemory1<Char>>, Microsoft.ML.ValueGetter1, System.Collections.Generic.List1<Tensor> ByRef, System.Collections.Generic.List1 ByRef)
at Microsoft.ML.TorchSharp.NasBert.NasBertTrainer2+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.NasBertTrainer2[[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.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<float>(actualColumnName)
					.Select(x => (double)x);
	var predicted =
			predictions.GetColumn<float>(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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant