Repository: pytorch
Issue: When using libtorch v1.10.2, calling at::slow_conv_dilated3d directly returns wrong results on cpu backend
Labels: module: cpp,module: convolution,triaged

### 🐛 Describe the bug

When using libtorch v1.10.2, calling at::slow_conv_dilated3d directly returns wrong results
~~~c++
#include <c10/util/Optional.h>
#include <torch/torch.h>
#include <tuple>

int main()
{
  auto device = torch::kCPU;
  std::vector<int64_t> input_size = {2, 2, 2, 2};
  std::vector<int64_t> kernel_size = {2, 2, 2};
  std::vector<int64_t> weight_size = {3,2, 2, 2, 2};

  at::Tensor input = torch::tensor(
      {{{{-0.6171, 1.9573}, {-0.9776, -0.4388}},
        {{0.0830 ,0.3295}, {1.1376 ,1.4564}}},
       {{{-0.7016, 1.2533}, {-0.2551, 0.3261}},
        {{1.3227, -0.1871}, {-1.0956, -1.0137}}}},device);

  at::Tensor weight = torch::tensor(
      {{{{{-0.4548, - 0.1107}, {0.0223, - 1.2321}},
         {{1.1203, 1.7528}, {1.7692, - 0.9271}}},
        {{{1.3980, - 0.7515}, {1.2593, 1.0403}},
         {{0.3530, - 0.7153}, {1.7454 ,0.9942}}}},
       {{{{-0.7186, - 0.2611}, {0.6274 ,0.6565}},
         {{-1.4249, - 0.6673}, {-0.7796, - 1.1311}}},
        {{{0.3173, - 0.1052}, {-0.3449, 0.4073}},
         {{-0.7267, 0.0879}, {2.5294, 0.0152}}}},
       {{{{-1.3843, 1.6123}, {0.2952 ,- 0.7957}},
         {{-0.1807, 0.3354}, {0.8913 ,- 0.3995}}},
        {{{1.0847 ,0.5221}, {-0.3060 ,- 0.7522}},
         {{0.0249 ,- 0.5351}, {1.1408, 0.6337}}}}},device);

  at::Tensor bias_opt = torch::tensor({-0.8054,-1.1183,0.1813},device);

  std::vector<int64_t> stride_size = {1, 1, 1};
  std::vector<int64_t> dilation_size = {1, 1, 1};
  std::vector<int64_t> pad_size = {1, 1, 1};
  at::Tensor output_ref = at::slow_conv_dilated3d(
      input,
      weight,
      kernel_size,
      bias_opt,
      stride_size,
      pad_size,
      dilation_size);
  std::cout<<"\noutput_ref\n"<<output_ref;
}
~~~

CmakeLists.txt
~~~
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(test)
SET(CMAKE_BUILD_TYPE "Release") 
find_package(Torch REQUIRED)
 
add_executable(cuda_abs testForNaiveDilatedConv.cpp)
target_link_libraries(cuda_abs "${TORCH_LIBRARIES}")
set_property(TARGET cuda_abs PROPERTY CXX_STANDARD 14)
~~~

compile commands
~~~
cmake -DCMAKE_PREFIX_PATH=<your path>/libtorch/share/cmake  ../
~~~


output
~~~
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/cuda_abs.dir/testForNaiveDilatedConv.cpp.o
[100%] Linking CXX executable test
[100%] Built target cuda_abs
$ ./test

output_ref
(1,1,.,.) = 
 -0.9308 -3.6903  4.8450
 -0.7325 -0.6539  1.6226
 -2.3365 -2.9930 -1.1819

(2,1,.,.) = 
  6.8734e-01 -3.4884e+00  1.6442e+00
 -2.0979e+28  8.1139e-01  5.8812e+37
  6.2993e-01 -2.0979e+28  3.8827e-01

(3,1,.,.) = 
  1.8436e+25  1.0901e+27 -2.0931e+28
  3.9735e-01 -7.0053e-01 -3.4156e-01
 -1.9138e-01 -1.5137e-01  8.7411e-02

(1,2,.,.) = 
  0.1502 -1.1594  0.7600
 -2.5282 -3.4266  1.2549
  1.9593  2.9388  0.8109

(2,2,.,.) = 
  5.8812e+37  4.5556e+00  5.8812e+37
 -1.7533e+00 -7.7393e+00 -5.4296e+00
 -5.7334e-01 -1.1839e+00 -9.1978e-01

(3,2,.,.) = 
  1.8238e+00  4.8299e+37  2.7452e-01
 -2.2202e+00  2.5651e+00 -1.5019e+00
 -7.4157e-01 -2.0979e+28  6.7274e-01

(1,3,.,.) = 
  1.4550  1.2482 -0.0470
 -3.3633 -2.1065 -1.4742
  0.8787 -1.2672 -1.8982

(2,3,.,.) = 
  5.9323e-01 -2.6401e-01  2.7126e-01
 -2.0979e+28  1.9285e+00  9.6723e-01
 -1.8177e-01  4.7473e+27  3.0881e+29

(3,3,.,.) = 
  5.8812e+37 -5.0169e-01 -2.0979e+28
  7.4332e-01  5.8812e+37  8.1047e-02
  5.8812e+37 -9.4428e-01 -3.1157e+00
~~~

expected output 
~~~
output
(1,1,.,.) = 
 -0.9308 -3.6906  4.8449
 -0.7325 -0.6538  1.6225
 -2.3366 -2.9932 -1.1820

(2,1,.,.) = 
  0.6874 -3.4885  1.6442
  1.4521  0.8113 -2.5327
  0.6300  1.8999  0.3883

(3,1,.,.) = 
 -0.1981 -1.3382  3.1744
  0.3973 -0.7006 -0.3416
 -0.1914 -0.1514  0.0874

(1,2,.,.) = 
  0.1502 -1.1594  0.7598
 -2.5281 -3.4267  1.2547
  1.9594  2.9389  0.8110

(2,2,.,.) = 
 -0.7647  4.5557  0.0656
 -1.7534 -7.7397 -5.4296
 -0.5733 -1.1839 -0.9197

(3,2,.,.) = 
  1.8238 -1.1348  0.2746
 -2.2202  2.5653 -1.5019
 -0.7416  1.3375  0.6729

(1,3,.,.) = 
  1.4550  1.2483 -0.0470
 -3.3633 -2.1065 -1.4742
  0.8787 -1.2673 -1.8982

(2,3,.,.) = 
  0.5932 -0.2640  0.2712
  0.1397  1.9286  0.9672
 -0.1817 -1.4388 -1.3682

(3,3,.,.) = 
 -1.0610 -0.5016  0.1545
  0.7435  2.0281  0.0811
  1.2621 -0.9443 -3.1156
~~~


### Versions

not python
is a c++ issue

output:
~~~
Collecting environment information...
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (GCC) 9.4.0
Clang version: Could not collect
CMake version: version 3.15.5
Libc version: glibc-2.17

Python version: 3.7.8 (default, Jun 10 2021, 01:08:33)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] (64-bit runtime)
Python platform: Linux-5.4.0-110-generic-x86_64-with-debian-bullseye-sid
Is CUDA available: N/A
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3060
Nvidia driver version: 515.43.04
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

Versions of relevant libraries:
[pip3] numpy==1.21.2
[conda] Could not collect
~~~

cc @jbschlosser