Skip to content

Fixed array-based overloads of max_pool{123}d. #1090

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
Sep 8, 2023
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
1 change: 1 addition & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __Bug Fixes__:
#1041 Running example code got error in Windows 10<br/>
#1064 Inplace operators create an alias<br/>
#1084 Module.zero_grad() does not work<br/>
#1089 max_pool2d overload creates tensor with incorrect shape<br/>

## NuGet Version 0.100.4

Expand Down
4 changes: 2 additions & 2 deletions src/TorchSharp/NN/Pooling/MaxPool1D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static Tensor max_pool1d(Tensor input, long kernelSize, long? stride = nu
long? padding = null, long? dilation = null, bool ceil_mode = false)
{
var kernelSizes = new long[] { kernelSize };
var strides = new long[] { stride ?? 1 };
var strides = new long[] { stride ?? kernelSize };
var paddings = new long[] { padding ?? 0 };
var dilations = new long[] { dilation ?? 1 };
unsafe {
Expand Down Expand Up @@ -121,7 +121,7 @@ public static (Tensor output, Tensor indices) max_pool1d_with_indices(Tensor inp
long? padding = null, long? dilation = null, bool ceil_mode = false)
{
var kernelSizes = new long[] { kernelSize };
var strides = new long[] { stride ?? 1 };
var strides = new long[] { stride ?? kernelSize };
var paddings = new long[] { padding ?? 0 };
var dilations = new long[] { dilation ?? 1 };
IntPtr[] ptrArray;
Expand Down
4 changes: 2 additions & 2 deletions src/TorchSharp/NN/Pooling/MaxPool2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static partial class functional
public static Tensor max_pool2d(Tensor input, long[] kernelSize, long[] strides = null,
long[] padding = null, long[] dilation = null, bool ceil_mode = false)
{
strides = strides ?? kernelSize.Select(x => 1L).ToArray();
strides = strides ?? kernelSize;
padding = padding ?? kernelSize.Select(x => 0L).ToArray();
dilation = dilation ?? kernelSize.Select(x => 1L).ToArray();
unsafe {
Expand Down Expand Up @@ -232,7 +232,7 @@ public static unsafe Tensor max_pool2d(Tensor input, (long, long) kernelSize, (l
public static (Tensor output, Tensor indices) max_pool2d_with_indices(Tensor input, long[] kernelSize, long[] strides = null,
long[] padding = null, long[] dilation = null, bool ceil_mode = false)
{
strides = strides ?? kernelSize.Select(x => 1L).ToArray();
strides = strides ?? kernelSize;
padding = padding ?? kernelSize.Select(x => 0L).ToArray();
dilation = dilation ?? kernelSize.Select(x => 1L).ToArray();
IntPtr[] ptrArray;
Expand Down
4 changes: 2 additions & 2 deletions src/TorchSharp/NN/Pooling/MaxPool3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static partial class functional
public static Tensor max_pool3d(Tensor input, long[] kernelSize, long[] strides = null,
long[] padding = null, long[] dilation = null, bool ceil_mode = false)
{
strides = strides ?? kernelSize.Select(x => 1L).ToArray();
strides = strides ?? kernelSize;
padding = padding ?? kernelSize.Select(x => 0L).ToArray();
dilation = dilation ?? kernelSize.Select(x => 1L).ToArray();
unsafe {
Expand Down Expand Up @@ -145,7 +145,7 @@ public static Tensor max_pool3d(Tensor input, long[] kernelSize, long[] strides
public static (Tensor output, Tensor indices) max_pool3d_with_indices(Tensor input, long[] kernelSize, long[] strides = null,
long[] padding = null, long[] dilation = null, bool ceil_mode = false)
{
strides = strides ?? kernelSize.Select(x => 1L).ToArray();
strides = strides ?? kernelSize;
padding = padding ?? kernelSize.Select(x => 0L).ToArray();
dilation = dilation ?? kernelSize.Select(x => 1L).ToArray();
IntPtr[] ptrArray;
Expand Down
25 changes: 25 additions & 0 deletions test/TorchSharpTest/TestTorchTensorBugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,5 +1155,30 @@ static void Validate1057()
}
}
}

[Fact]
public void Validate1089_2d()
{
var t = torch.zeros(1, 6, 28, 28);
var expectedShape = new long[] { 1, 6, 14, 14 };

Assert.Multiple(
() => Assert.Equal(expectedShape, functional.max_pool2d(t, 2).shape),
() => Assert.Equal(expectedShape, functional.max_pool2d(t, ( 2, 2 )).shape),
() => Assert.Equal(expectedShape, functional.max_pool2d(t, new long[] { 2, 2 }).shape)
);

Assert.Equal(expectedShape, functional.max_pool2d_with_indices(t, new long[] { 2, 2 }).output.shape);
}

[Fact]
public void Validate1089_3d()
{
var t = torch.zeros(new long[] { 1, 6, 28, 28, 28 });
var expectedShape = new long[] { 1, 6, 14, 14, 14 };

Assert.Equal(expectedShape, functional.max_pool3d(t, new long[] { 2, 2, 2 }).shape);
Assert.Equal(expectedShape, functional.max_pool3d_with_indices(t, new long[] { 2, 2, 2 }).output.shape);
}
}
}