From 95d6d4dad865ce0b4968e2fdffab1c091b52724b Mon Sep 17 00:00:00 2001 From: alinpahontu2912 Date: Mon, 16 Feb 2026 11:46:35 +0100 Subject: [PATCH 1/3] Fix HFFT tests to use complex input tensors PyTorch's fft.hfft/hfft2/hfftn require complex input tensors. The tests were passing real-valued tensors (Float32/Float64), which causes 'NYI' errors in newer PyTorch versions. Fixed by using complex64/complex128 input types and ihfft for inverse verification. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/TorchSharpTest/TestTorchTensor.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/TorchSharpTest/TestTorchTensor.cs b/test/TorchSharpTest/TestTorchTensor.cs index bd75ecbb2..29763dc65 100644 --- a/test/TorchSharpTest/TestTorchTensor.cs +++ b/test/TorchSharpTest/TestTorchTensor.cs @@ -6942,12 +6942,12 @@ public void Float64FFT() [TestOf(nameof(fft.hfft))] public void Float32HFFT() { - var input = torch.arange(4); + var input = torch.arange(4, complex64); var output = fft.hfft(input); Assert.Equal(6, output.shape[0]); Assert.Equal(ScalarType.Float32, output.dtype); - var inverted = fft.ifft(output); + var inverted = fft.ihfft(output); Assert.Equal(ScalarType.ComplexFloat32, inverted.dtype); } @@ -6955,12 +6955,12 @@ public void Float32HFFT() [TestOf(nameof(fft.hfft))] public void Float64HFFT() { - var input = torch.arange(4, float64); + var input = torch.arange(4, complex128); var output = fft.hfft(input); Assert.Equal(6, output.shape[0]); Assert.Equal(ScalarType.Float64, output.dtype); - var inverted = fft.ifft(output); + var inverted = fft.ihfft(output); Assert.Equal(ScalarType.ComplexFloat64, inverted.dtype); } @@ -7203,10 +7203,10 @@ public void Float64RFFTN() [TestOf(nameof(fft.hfft2))] public void Float32HFFT2() { - var input = torch.rand(new long[] { 5, 5, 5, 5 }); + var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex64); var output = fft.hfft2(input); Assert.Equal(new long[] { 5, 5, 5, 8 }, output.shape); - Assert.Equal(input.dtype, output.dtype); + Assert.Equal(ScalarType.Float32, output.dtype); var inverted = fft.ihfft2(output); Assert.Equal(new long[] { 5, 5, 5, 5 }, inverted.shape); @@ -7217,10 +7217,10 @@ public void Float32HFFT2() [TestOf(nameof(fft.hfft2))] public void Float64HFFT2() { - var input = torch.rand(new long[] { 5, 5, 5, 5 }, float64); + var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex128); var output = fft.hfft2(input); Assert.Equal(new long[] { 5, 5, 5, 8 }, output.shape); - Assert.Equal(input.dtype, output.dtype); + Assert.Equal(ScalarType.Float64, output.dtype); var inverted = fft.ihfft2(output); Assert.Equal(new long[] { 5, 5, 5, 5 }, inverted.shape); @@ -7231,10 +7231,10 @@ public void Float64HFFT2() [TestOf(nameof(fft.hfft2))] public void Float32HFFTN() { - var input = torch.rand(new long[] { 5, 5, 5, 5 }); + var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex64); var output = fft.hfft2(input); Assert.Equal(new long[] { 5, 5, 5, 8 }, output.shape); - Assert.Equal(input.dtype, output.dtype); + Assert.Equal(ScalarType.Float32, output.dtype); var inverted = fft.ihfft2(output); Assert.Equal(new long[] { 5, 5, 5, 5 }, inverted.shape); @@ -7249,10 +7249,10 @@ public void Float64HFFTN() // TODO: Something in this test makes if fail on Windows / Release and MacOS / Release - var input = torch.rand(new long[] { 5, 5, 5, 5 }, float64); + var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex128); var output = fft.hfftn(input); Assert.Equal(new long[] { 5, 5, 5, 8 }, output.shape); - Assert.Equal(input.dtype, output.dtype); + Assert.Equal(ScalarType.Float64, output.dtype); var inverted = fft.ihfftn(output); Assert.Equal(new long[] { 5, 5, 5, 5 }, inverted.shape); From ff3c8d9138d52f29f8f419f5f1fb00797abb4d77 Mon Sep 17 00:00:00 2001 From: alinpahontu2912 Date: Tue, 17 Feb 2026 11:36:52 +0100 Subject: [PATCH 2/3] Fix Float32HFFTN test to call hfftn instead of hfft2 The test method was incorrectly calling fft.hfft2() instead of fft.hfftn(), and using ihfft2() instead of ihfftn() for verification. Updated the TestOf attribute to match the correct function being tested. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/TorchSharpTest/TestTorchTensor.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/TorchSharpTest/TestTorchTensor.cs b/test/TorchSharpTest/TestTorchTensor.cs index 29763dc65..f7a8e1fb2 100644 --- a/test/TorchSharpTest/TestTorchTensor.cs +++ b/test/TorchSharpTest/TestTorchTensor.cs @@ -7228,15 +7228,15 @@ public void Float64HFFT2() } [Fact] - [TestOf(nameof(fft.hfft2))] + [TestOf(nameof(fft.hfftn))] public void Float32HFFTN() { var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex64); - var output = fft.hfft2(input); + var output = fft.hfftn(input); Assert.Equal(new long[] { 5, 5, 5, 8 }, output.shape); Assert.Equal(ScalarType.Float32, output.dtype); - var inverted = fft.ihfft2(output); + var inverted = fft.ihfftn(output); Assert.Equal(new long[] { 5, 5, 5, 5 }, inverted.shape); Assert.Equal(ScalarType.ComplexFloat32, inverted.dtype); } From f3bab3e3de18593537eb561a5ef282f17dc51178 Mon Sep 17 00:00:00 2001 From: alinpahontu2912 Date: Thu, 19 Feb 2026 13:37:00 +0100 Subject: [PATCH 3/3] Address PR comments: Remove redundant tests and rename others Removed Float32HFFT and Float64HFFT as they were duplicates of ComplexFloat versions. Renamed Float32HFFT2/N and Float64HFFT2/N to ComplexFloat... to reflect input type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- test/TorchSharpTest/TestTorchTensor.cs | 34 +++----------------------- 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/test/TorchSharpTest/TestTorchTensor.cs b/test/TorchSharpTest/TestTorchTensor.cs index f7a8e1fb2..f019b4ccb 100644 --- a/test/TorchSharpTest/TestTorchTensor.cs +++ b/test/TorchSharpTest/TestTorchTensor.cs @@ -6938,32 +6938,6 @@ public void Float64FFT() Assert.Equal(ScalarType.ComplexFloat64, inverted.dtype); } - [Fact] - [TestOf(nameof(fft.hfft))] - public void Float32HFFT() - { - var input = torch.arange(4, complex64); - var output = fft.hfft(input); - Assert.Equal(6, output.shape[0]); - Assert.Equal(ScalarType.Float32, output.dtype); - - var inverted = fft.ihfft(output); - Assert.Equal(ScalarType.ComplexFloat32, inverted.dtype); - } - - [Fact] - [TestOf(nameof(fft.hfft))] - public void Float64HFFT() - { - var input = torch.arange(4, complex128); - var output = fft.hfft(input); - Assert.Equal(6, output.shape[0]); - Assert.Equal(ScalarType.Float64, output.dtype); - - var inverted = fft.ihfft(output); - Assert.Equal(ScalarType.ComplexFloat64, inverted.dtype); - } - [Fact] [TestOf(nameof(fft.rfft))] public void Float32RFFT() @@ -7201,7 +7175,7 @@ public void Float64RFFTN() [Fact] [TestOf(nameof(fft.hfft2))] - public void Float32HFFT2() + public void ComplexFloat32HFFT2() { var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex64); var output = fft.hfft2(input); @@ -7215,7 +7189,7 @@ public void Float32HFFT2() [Fact] [TestOf(nameof(fft.hfft2))] - public void Float64HFFT2() + public void ComplexFloat64HFFT2() { var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex128); var output = fft.hfft2(input); @@ -7229,7 +7203,7 @@ public void Float64HFFT2() [Fact] [TestOf(nameof(fft.hfftn))] - public void Float32HFFTN() + public void ComplexFloat32HFFTN() { var input = torch.rand(new long[] { 5, 5, 5, 5 }, complex64); var output = fft.hfftn(input); @@ -7243,7 +7217,7 @@ public void Float32HFFTN() [Fact(Skip = "Fails on all Release builds.")] [TestOf(nameof(fft.hfftn))] - public void Float64HFFTN() + public void ComplexFloat64HFFTN() { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {