diff --git a/src/TorchSharp/NN/ParameterList.cs b/src/TorchSharp/NN/ParameterList.cs index 400eacd5b..e6e3fd0eb 100644 --- a/src/TorchSharp/NN/ParameterList.cs +++ b/src/TorchSharp/NN/ParameterList.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using static TorchSharp.torch.nn; +#nullable enable namespace TorchSharp { using System.Linq; @@ -43,7 +44,7 @@ public override bool has_parameter(string target) return int.TryParse(target, out int idx) && idx > -1 && idx < _list.Count && _list[idx] is not null; } - public override Parameter get_parameter(string target) + public override Parameter? get_parameter(string target) { if (int.TryParse(target, out int idx) && idx > -1 && idx < _list.Count) { return _list[idx]; @@ -51,7 +52,7 @@ public override Parameter get_parameter(string target) return null; } - protected override void _toEpilog(torch.ScalarType? dtype, torch.Device device, bool non_blocking) + protected override void _toEpilog(torch.ScalarType? dtype, torch.Device? device, bool non_blocking) { for (int i = 0; i < _list.Count; i++) { diff --git a/src/TorchSharp/NN/Sequential.cs b/src/TorchSharp/NN/Sequential.cs index 711be65d1..7ddc14fa7 100644 --- a/src/TorchSharp/NN/Sequential.cs +++ b/src/TorchSharp/NN/Sequential.cs @@ -6,6 +6,7 @@ using static TorchSharp.torch; +#nullable enable namespace TorchSharp { using System.Runtime.CompilerServices; @@ -22,7 +23,7 @@ namespace Modules /// This allows `Sequential.forward` to be implemented directly in this class, and to do so efficiently. /// For scenarios where customization via derivation is useful, use one of the type-parameterized versions of /// Sequential and implement the `forward` method in the derived class, which do not assume that the submodules - /// have a uniform Module base class. + /// have a uniform Module base class. /// public class Sequential : torch.nn.Module { diff --git a/src/TorchSharp/NN/Utils/PackedSequence.cs b/src/TorchSharp/NN/Utils/PackedSequence.cs index 4ee06fbdf..8bbd6155e 100644 --- a/src/TorchSharp/NN/Utils/PackedSequence.cs +++ b/src/TorchSharp/NN/Utils/PackedSequence.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { public static partial class torch @@ -20,7 +21,7 @@ public static partial class rnn /// public sealed class PackedSequence : IDisposable { - internal DisposeScope OwningDisposeScope { + internal DisposeScope? OwningDisposeScope { get => mOwningDisposeScope; set { mOwningDisposeScope = value; @@ -81,7 +82,7 @@ protected override bool ReleaseHandle() /// internal bool IsInvalid => handle.IsInvalid; private HType handle; - private DisposeScope mOwningDisposeScope; + private DisposeScope? mOwningDisposeScope; internal PackedSequence(HType handle) { @@ -137,7 +138,7 @@ public PackedSequence MoveToOtherDisposeScope(PackedSequence other) return MoveToOtherDisposeScope(other.OwningDisposeScope); } - public PackedSequence MoveToOtherDisposeScope(DisposeScope other) + public PackedSequence MoveToOtherDisposeScope(DisposeScope? other) { if (OwningDisposeScope == null && other != null) { other.Attach(this); diff --git a/src/TorchSharp/Scalar.cs b/src/TorchSharp/Scalar.cs index cf95ac47f..cfe92cd98 100644 --- a/src/TorchSharp/Scalar.cs +++ b/src/TorchSharp/Scalar.cs @@ -2,6 +2,7 @@ using System; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { /// diff --git a/src/TorchSharp/Special.cs b/src/TorchSharp/Special.cs index 59b98e91b..1b568376e 100644 --- a/src/TorchSharp/Special.cs +++ b/src/TorchSharp/Special.cs @@ -2,6 +2,7 @@ using System; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { public static partial class torch @@ -14,7 +15,7 @@ public static class special /// Input tensor /// Optional output tensor, will be modified if present. /// - public static Tensor airy_ai(Tensor input, Tensor @out = null) + public static Tensor airy_ai(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_airy_ai(input.Handle) : @@ -29,7 +30,7 @@ public static Tensor airy_ai(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor bessel_j0(Tensor input, Tensor @out = null) + public static Tensor bessel_j0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_bessel_j0(input.Handle) : @@ -44,7 +45,7 @@ public static Tensor bessel_j0(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor bessel_j1(Tensor input, Tensor @out = null) + public static Tensor bessel_j1(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_bessel_j1(input.Handle) : @@ -59,7 +60,7 @@ public static Tensor bessel_j1(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor bessel_y0(Tensor input, Tensor @out = null) + public static Tensor bessel_y0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_bessel_y0(input.Handle) : @@ -74,7 +75,7 @@ public static Tensor bessel_y0(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor bessel_y1(Tensor input, Tensor @out = null) + public static Tensor bessel_y1(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_bessel_y1(input.Handle) : @@ -89,7 +90,7 @@ public static Tensor bessel_y1(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor modified_bessel_i0(Tensor input, Tensor @out = null) + public static Tensor modified_bessel_i0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_modified_bessel_i0(input.Handle) : @@ -104,7 +105,7 @@ public static Tensor modified_bessel_i0(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor modified_bessel_i1(Tensor input, Tensor @out = null) + public static Tensor modified_bessel_i1(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_modified_bessel_i1(input.Handle) : @@ -119,7 +120,7 @@ public static Tensor modified_bessel_i1(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor modified_bessel_k0(Tensor input, Tensor @out = null) + public static Tensor modified_bessel_k0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_modified_bessel_k0(input.Handle) : @@ -134,7 +135,7 @@ public static Tensor modified_bessel_k0(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor modified_bessel_k1(Tensor input, Tensor @out = null) + public static Tensor modified_bessel_k1(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_modified_bessel_k1(input.Handle) : @@ -149,7 +150,7 @@ public static Tensor modified_bessel_k1(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor scaled_modified_bessel_k0(Tensor input, Tensor @out = null) + public static Tensor scaled_modified_bessel_k0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_scaled_modified_bessel_k0(input.Handle) : @@ -164,7 +165,7 @@ public static Tensor scaled_modified_bessel_k0(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor scaled_modified_bessel_k1(Tensor input, Tensor @out = null) + public static Tensor scaled_modified_bessel_k1(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_scaled_modified_bessel_k1(input.Handle) : @@ -179,7 +180,7 @@ public static Tensor scaled_modified_bessel_k1(Tensor input, Tensor @out = null) /// /// The input tensor /// An optional output tensor, which will be modified if present. - public static Tensor spherical_bessel_j0(Tensor input, Tensor @out = null) + public static Tensor spherical_bessel_j0(Tensor input, Tensor? @out = null) { var res = @out is null ? THSSpecial_spherical_bessel_j0(input.Handle) : @@ -197,7 +198,7 @@ public static Tensor spherical_bessel_j0(Tensor input, Tensor @out = null) /// The input tensor. /// n /// An optional output tensor. - public static Tensor chebyshev_polynomial_t(Tensor x, Tensor n, Tensor @out = null) + public static Tensor chebyshev_polynomial_t(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_chebyshev_polynomial_t(x.Handle, n.Handle) : @@ -216,7 +217,7 @@ public static Tensor chebyshev_polynomial_t(Tensor x, Tensor n, Tensor @out = nu /// The input tensor. /// n /// An optional output tensor. - public static Tensor chebyshev_polynomial_u(Tensor x, Tensor n, Tensor @out = null) + public static Tensor chebyshev_polynomial_u(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_chebyshev_polynomial_u(x.Handle, n.Handle) : @@ -234,7 +235,7 @@ public static Tensor chebyshev_polynomial_u(Tensor x, Tensor n, Tensor @out = nu /// The input tensor. /// n /// An optional output tensor. - public static Tensor chebyshev_polynomial_v(Tensor x, Tensor n, Tensor @out = null) + public static Tensor chebyshev_polynomial_v(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_chebyshev_polynomial_v(x.Handle, n.Handle) : @@ -252,7 +253,7 @@ public static Tensor chebyshev_polynomial_v(Tensor x, Tensor n, Tensor @out = nu /// The input tensor. /// n /// An optional output tensor. - public static Tensor chebyshev_polynomial_w(Tensor x, Tensor n, Tensor @out = null) + public static Tensor chebyshev_polynomial_w(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_chebyshev_polynomial_w(x.Handle, n.Handle) : @@ -270,7 +271,7 @@ public static Tensor chebyshev_polynomial_w(Tensor x, Tensor n, Tensor @out = nu /// The input tensor. /// n /// An optional output tensor. - public static Tensor shifted_chebyshev_polynomial_t(Tensor x, Tensor n, Tensor @out = null) + public static Tensor shifted_chebyshev_polynomial_t(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_shifted_chebyshev_polynomial_t(x.Handle, n.Handle) : @@ -289,7 +290,7 @@ public static Tensor shifted_chebyshev_polynomial_t(Tensor x, Tensor n, Tensor @ /// The input tensor. /// n /// An optional output tensor. - public static Tensor shifted_chebyshev_polynomial_u(Tensor x, Tensor n, Tensor @out = null) + public static Tensor shifted_chebyshev_polynomial_u(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_shifted_chebyshev_polynomial_u(x.Handle, n.Handle) : @@ -307,7 +308,7 @@ public static Tensor shifted_chebyshev_polynomial_u(Tensor x, Tensor n, Tensor @ /// The input tensor. /// n /// An optional output tensor. - public static Tensor shifted_chebyshev_polynomial_v(Tensor x, Tensor n, Tensor @out = null) + public static Tensor shifted_chebyshev_polynomial_v(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_shifted_chebyshev_polynomial_v(x.Handle, n.Handle) : @@ -325,7 +326,7 @@ public static Tensor shifted_chebyshev_polynomial_v(Tensor x, Tensor n, Tensor @ /// The input tensor. /// n /// An optional output tensor. - public static Tensor shifted_chebyshev_polynomial_w(Tensor x, Tensor n, Tensor @out = null) + public static Tensor shifted_chebyshev_polynomial_w(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_shifted_chebyshev_polynomial_w(x.Handle, n.Handle) : @@ -343,7 +344,7 @@ public static Tensor shifted_chebyshev_polynomial_w(Tensor x, Tensor n, Tensor @ /// The input tensor. /// n /// An optional output tensor. - public static Tensor hermite_polynomial_h(Tensor x, Tensor n, Tensor @out = null) + public static Tensor hermite_polynomial_h(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_hermite_polynomial_h(x.Handle, n.Handle) : @@ -361,7 +362,7 @@ public static Tensor hermite_polynomial_h(Tensor x, Tensor n, Tensor @out = null /// The input tensor. /// n /// An optional output tensor. - public static Tensor hermite_polynomial_he(Tensor x, Tensor n, Tensor @out = null) + public static Tensor hermite_polynomial_he(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_hermite_polynomial_he(x.Handle, n.Handle) : @@ -373,14 +374,14 @@ public static Tensor hermite_polynomial_he(Tensor x, Tensor n, Tensor @out = nul /// /// Laguerre polynomials - /// + /// /// See: https://en.wikipedia.org/wiki/Laguerre_polynomials /// /// The input tensor. /// n /// An optional output tensor. /// - public static Tensor laguerre_polynomial_l(Tensor x, Tensor n, Tensor @out = null) + public static Tensor laguerre_polynomial_l(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_laguerre_polynomial_l(x.Handle, n.Handle) : @@ -392,13 +393,13 @@ public static Tensor laguerre_polynomial_l(Tensor x, Tensor n, Tensor @out = nul /// /// Legendre polynomials - /// + /// /// https://en.wikipedia.org/wiki/Legendre_polynomials /// /// The input tensor. /// n /// An optional output tensor. - public static Tensor legendre_polynomial_p(Tensor x, Tensor n, Tensor @out = null) + public static Tensor legendre_polynomial_p(Tensor x, Tensor n, Tensor? @out =null) { var res = @out is null ? THSSpecial_legendre_polynomial_p(x.Handle, n.Handle) : diff --git a/src/TorchSharp/Tensor/Factories/Tensor.Factories.cs b/src/TorchSharp/Tensor/Factories/Tensor.Factories.cs index fc306d3b0..b306c0cd7 100644 --- a/src/TorchSharp/Tensor/Factories/Tensor.Factories.cs +++ b/src/TorchSharp/Tensor/Factories/Tensor.Factories.cs @@ -8,6 +8,7 @@ using TorchSharp.Utils; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { public static partial class torch @@ -134,7 +135,7 @@ public static Tensor normal(double mean, double std, ReadOnlySpan size, Sc /// A constructed tensor with elements of `dtype` /// private static Tensor _tensor_generic(Array rawArray, ReadOnlySpan dimensions, sbyte origType, ScalarType? dtype, Device? device, bool requires_grad, bool clone = true, string[]? names = null) - { + { { // Validate the sizes before handing over storage to native code... var prod = 1L; @@ -189,10 +190,10 @@ private static Tensor _tensor_generic(Array rawArray, ReadOnlySpan dimensi } private static Tensor _tensor_generic(Memory rawArray, ReadOnlySpan dimensions, sbyte origType, ScalarType? dtype, Device? device, bool requires_grad, bool clone = true, string[]? names = null) - { - if (clone) - { - return _tensor_generic(rawArray.ToArray(), dimensions, origType, dtype, device, requires_grad, false, names); + { + if (clone) + { + return _tensor_generic(rawArray.ToArray(), dimensions, origType, dtype, device, requires_grad, false, names); } { @@ -215,7 +216,7 @@ private static Tensor _tensor_generic(Memory rawArray, ReadOnlySpan var dataHandle = rawArray.Pin(); var dataArrayAddr = (IntPtr)dataHandle.Pointer; - + TorchSharp.PInvoke.GCHandleDeleter deleter = null!; deleter = new TorchSharp.PInvoke.GCHandleDeleter((IntPtr ptr) => { dataHandle.Dispose(); @@ -225,7 +226,7 @@ private static Tensor _tensor_generic(Memory rawArray, ReadOnlySpan void *ptr = null; IntPtr iPtr = (IntPtr)ptr; - + fixed (long* shape = dimensions) { var handle = THSTensor_new(dataArrayAddr, deleter, (IntPtr)shape, dimensions.Length, origType, (sbyte)dtype.Value, (int)device.type, device.index, requires_grad); @@ -585,7 +586,7 @@ public static Tensor Load(System.IO.BinaryReader reader) var tensor = torch.empty(loadedShape, dtype: type); tensor.ReadBytesFromStream(reader.BaseStream); - + return tensor; } diff --git a/src/TorchSharp/Tensor/torch.OtherOperations.cs b/src/TorchSharp/Tensor/torch.OtherOperations.cs index fb4568b5c..cd7920dfa 100644 --- a/src/TorchSharp/Tensor/torch.OtherOperations.cs +++ b/src/TorchSharp/Tensor/torch.OtherOperations.cs @@ -6,6 +6,7 @@ using TorchSharp.PInvoke; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { // https://pytorch.org/docs/stable/torch#other-operations diff --git a/src/TorchSharp/Tensor/torch.Utilities.cs b/src/TorchSharp/Tensor/torch.Utilities.cs index 42745a786..460a42e67 100644 --- a/src/TorchSharp/Tensor/torch.Utilities.cs +++ b/src/TorchSharp/Tensor/torch.Utilities.cs @@ -4,6 +4,7 @@ using System.Diagnostics.Contracts; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { // https://pytorch.org/docs/stable/torch#utilities diff --git a/src/TorchSharp/Torch.cs b/src/TorchSharp/Torch.cs index 6f1731511..eba7e044b 100644 --- a/src/TorchSharp/Torch.cs +++ b/src/TorchSharp/Torch.cs @@ -13,6 +13,7 @@ using TorchSharp.PInvoke; using static TorchSharp.PInvoke.NativeMethods; +#nullable enable namespace TorchSharp { public static partial class torch