arrayfire / arrayfire-dotnet

.NET wrapper for ArrayFire
BSD 3-Clause "New" or "Revised" License
78 stars 21 forks source link

dot issue #23

Closed Oceania2018 closed 5 years ago

Oceania2018 commented 5 years ago

Whe I run the HelloWorld and try to do dot;

            Device.SetBackend(Backend.DEFAULT);
            Device.PrintInfo();

            var arr1 = Data.RandNormal<float>(3, 3);
            var arr2 = Data.RandNormal<float>(3, 3);
            var arr3 = arr1 + arr2;
            var arr4 = Matrix.Multiply(arr1, arr2);
            var arr5 = Arith.Sin(arr1) + Arith.Cos(arr2);
            var arr6 = Vector.Dot(arr1, arr2);

image

9prady9 commented 5 years ago

Uploading the sample input and output shown by @Oceania2018 on our slack conversation here.

image image(1)

The type of batching you want to do dot product can be done using matmul. The arrayfire function named dot product only handles the case of dot(vector_array, vector_array). Having said that, like I pointed at the beginning of this paragraph, batched dot product can be carried out using matrix multiplication.

Oceania2018 commented 5 years ago

When I pass int, it raises error. If I change to float or double, it works in matmul.

9prady9 commented 5 years ago

Matmul is supported for floating point types: float, double and their complex counterparts. Hence, error is returned by C API.

Oceania2018 commented 5 years ago

the matmul return the result is not correct. It should be [[7, 11], [4, 7]]

The numpy result: image

The arrayfire result: image

9prady9 commented 5 years ago

What is the result of matmul from arrayfire ?

Oceania2018 commented 5 years ago

[[9, 8], [5, 5]]

9prady9 commented 5 years ago

The image you uploaded shows np.dot result as [[9, 8], [5, 5]].

In any case, irrespective of inputs using matrix multiplication is a standard way to obtain bunch of dot products quickly. Please note that ArrayFire expects data to be in column major format, may be that is the reason behind different output.