Skip to content

Add basic error handling logic #23

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

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Test/TorchSharp/TorchSharp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using TorchSharp.JIT;
using TorchSharp.NN;
using TorchSharp.Tensor;
Expand Down Expand Up @@ -164,7 +166,7 @@ public void TestSparse()
Assert.IsTrue(sparse.IsSparse);
Assert.IsFalse(i.IsSparse);
Assert.IsFalse(v.IsSparse);
CollectionAssert.AreEqual(sparse.Indeces.Data<long>().ToArray(), new long[] { 0, 1, 1, 2, 0, 2 });
CollectionAssert.AreEqual(sparse.Indices.Data<long>().ToArray(), new long[] { 0, 1, 1, 2, 0, 2 });
CollectionAssert.AreEqual(sparse.Values.Data<float>().ToArray(), new float[] { 3, 4, 5 });
}
}
Expand Down Expand Up @@ -461,13 +463,24 @@ public void TestPoissonNLLLoss2()
}
}

# if DEBUG
[TestMethod]
public void TestErrorHandling()
{
using (TorchTensor input = FloatTensor.From(new float[] { 0.5f, 1.5f}))
using (TorchTensor target = FloatTensor.From(new float[] { 1f, 2f, 3f }))
{
Assert.ThrowsException<ExternalException>(() => NN.LossFunction.PoissonNLL()(input, target));
}
}
#endif

[TestMethod]
public void TestZeroGrad()
{
var lin1 = NN.Module.Linear(1000, 100);
var lin2 = NN.Module.Linear(100, 10);
var seq = NN.Module.Sequential(lin1, NN.Module.Relu(), lin2);

seq.ZeroGrad();
}

Expand Down
9 changes: 7 additions & 2 deletions TorchSharp/NN/LossFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ public static Loss NLL(TorchTensor? weigths = null, Reduction reduction = Reduct
}

[DllImport("libTorchSharp")]
extern static IntPtr THSNN_lossPoissonNLL(IntPtr srct, IntPtr trgt, bool logInput, bool full, float eps, long reduction);
extern static IntPtr THSNN_loss_poisson_nll(IntPtr srct, IntPtr trgt, bool logInput, bool full, float eps, long reduction);

public static Loss PoissonNLL(bool logInput = true, bool full = false, float eps = 1e-8f, Reduction reduction = Reduction.Mean)
{
return (TorchTensor src, TorchTensor target) => new TorchTensor(THSNN_lossPoissonNLL(src.Handle, target.Handle, logInput, full, eps, (long)reduction));
return (TorchTensor src, TorchTensor target) =>
{
var tptr = THSNN_loss_poisson_nll(src.Handle, target.Handle, logInput, full, eps, (long)reduction);
Torch.AssertNoErrors();
return new TorchTensor(tptr);
};
}
}

Expand Down
4 changes: 3 additions & 1 deletion TorchSharp/Tensor/TorchTensorTyped.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,9 @@ static public TorchTensor Random(long[] size, string device = "cpu", bool requir
{
fixed (long* psizes = size)
{
return new TorchTensor (THSTensor_rand ((IntPtr)psizes, size.Length, (sbyte)ATenScalarMapping.Float, device, requiresGrad));
var tptr = THSTensor_rand((IntPtr)psizes, size.Length, (sbyte)ATenScalarMapping.Float, device, requiresGrad);
Torch.AssertNoErrors();
return new TorchTensor (tptr);
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion TorchSharp/Torch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TorchSharp
{
using Debug = System.Diagnostics.Debug;

public static class Torch
{
[DllImport("libTorchSharp")]
Expand All @@ -19,5 +23,19 @@ public static bool IsCudaAvailable()
{
return THSTorch_isCudaAvailable();
}

[DllImport("libTorchSharp")]
extern static IntPtr THSTorch_get_and_reset_last_err();

[Conditional("DEBUG")]
internal static void AssertNoErrors()
{
var error = THSTorch_get_and_reset_last_err();

if (error != IntPtr.Zero)
{
throw new ExternalException(Marshal.PtrToStringAnsi(error));
}
}
}
}