arrayfire / arrayfire-rust

Rust wrapper for ArrayFire
BSD 3-Clause "New" or "Revised" License
810 stars 58 forks source link

[BUG] Impossible to use `convolve2_nn` #323

Closed c0dearm closed 2 years ago

c0dearm commented 2 years ago

Description

I am using the code from the main branch of this very same repo, because I needed this fix, which is not yet released: https://github.com/arrayfire/arrayfire-rust/pull/316

Apparently, this fix is breaking completely the convolve2_nn function, or maybe I am doing something stupid.

This is the code I am trying to run:

fn test_conv2d() {
        let x = &arrayfire::constant!(0.5; 4,4,3,16);
        let k = &arrayfire::constant!(1.0; 2,2,3,5);
        let z = arrayfire::convolve2_nn(
            x,
            k,
            arrayfire::dim4!(1, 1, 1, 1),
            arrayfire::dim4!(0, 0, 0, 0),
            arrayfire::dim4!(1, 1, 1, 1),
        );
    }

As you can see it is a convolution with stride 1, 0 padding and 1 dilation.

The following panic happens when trying to execute it:

In function af_convolve2_nn
In file src/api/c/convolve.cpp:347
Invalid argument at index 3
Expected: stride_dims > 0 && stride_dims <= 2
 0# af_convolve2_nn in /lib/libafcpu.so.3
 1# af_convolve2_nn in /lib/libaf.so.3
 2# 0x000056274C38882E in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 3# 0x000056274C38B00A in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 4# 0x000056274C371BFA in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 5# 0x000056274C37C51E in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 6# 0x000056274C3C8363 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 7# 0x000056274C3C714A in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 8# 0x000056274C3C6310 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
 9# 0x000056274C3C4B78 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
10# 0x000056274C3BF511 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
11# 0x000056274C3A864C in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
12# 0x000056274C3BD517 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
13# 0x000056274C3BE481 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
14# 0x000056274C3866D3 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
15# 0x000056274C37C76B in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
16# 0x000056274C39250E in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
17# 0x000056274C369FF1 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
18# 0x000056274C3EA68E in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
19# 0x000056274C369FC0 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
20# 0x000056274C3866FC in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f
21# 0x00007FC70D91AD90 in /lib/x86_64-linux-gnu/libc.so.6
22# __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6
23# 0x000056274C362595 in /home/aitor/Documents/mushin/target/debug/deps/mushin-6c092483ec26be2f

Looks to me that somehow the stride dimensions are not being passed correctly to the C API.

System Information

ArrayFire v3.8.2 (CPU, 64-bit Linux, build 5752f2dc)

Checklist

c0dearm commented 2 years ago

Okay, I know what happens.

The C API expects between 1 and 2 stride dimensions. But since the latest change, it is always fixed to 4: https://github.com/arrayfire/arrayfire-rust/pull/316

I will submit a Pull Request fixing the issue by setting the constant to 2 instead. According to the Arrayfire docs, only 2 dimensions are expected and the others are ignored.

c0dearm commented 2 years ago

Fixed here: https://github.com/arrayfire/arrayfire-rust/pull/324