Skip to content

Bug in dot for Native Engine #40

@Lundez

Description

@Lundez

Hi,

Using dot on the following two matrices returns the wrong response when the NativeLinAlg is applied.

I figured the reason: .transpose() using strides. This is not sent into the NativeLinAlg.dot function via JNI.
If I do mk.linalg.dot(X.transpose().deepCopy(), y) I get the expected result.
Running mk.linalg.dot(X.deepCopy().transpose(), y) gives unexpected result, as an example.

Not sure about the best fix, unless the native functions allows strides. Doing deep-copies is rather expensive, so perhaps fall-back to the JvmLinAlg if strides are modified from default? As the JvmLinAlg works out-of-the-box 😄

P.S.
What makes me even more interested is that this actually works when it's the RHS being transposed! Only LHS transpose impact the end-result.

val yy = mk.ndarray(floatArrayOf(-.5f, -.5f,-.5f,.5f,.5f,.5f,.5f), 1, 7)
        val xx = mk.ndarray(mk[mk[1.8472979, 1.8472979, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[1.8472979, 1.8472979, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[1.5596159, 0.0, 1.5596159, 1.5596159, 0.0, 0.0, 0.0],
                mk[1.8472979, 0.0, 1.8472979, 0.0, 0.0, 0.0, 0.0],
                mk[1.8472979, 0.0, 1.8472979, 0.0, 0.0, 0.0, 0.0],
                mk[1.5596159, 1.5596159, 1.5596159, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 2.2527628, 0.0, 0.0, 0.0, 0.0, 0.0],
                mk[0.0, 0.0, 0.0, 1.3364723, 1.3364723, 1.3364723, 1.3364723],
                mk[0.0, 0.0, 0.0, 1.3364723, 1.3364723, 1.3364723, 1.3364723],
                mk[0.0, 0.0, 0.0, 1.3364723, 1.3364723, 1.3364723, 1.3364723]])
        println(xx.asType<Float>().transpose().deepCopy().transpose().dot(yy.transpose())) // Simply remove the transpose and look at usual result

Extra: It might make sense to do a check like you do for some methods to validate if using native is worth it. For this small matrix JvmLinAlg is more than twice as fast (I guess it's because you need to copy memory for JNI, right?).

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions