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
Hi,
Using
doton the following two matrices returns the wrong response when theNativeLinAlgis applied.I figured the reason:
.transpose()using strides. This is not sent into theNativeLinAlg.dotfunction 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
JvmLinAlgif strides are modified from default? As theJvmLinAlgworks 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.
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
JvmLinAlgis more than twice as fast (I guess it's because you need to copy memory for JNI, right?).