Skip to content

Instantly share code, notes, and snippets.

@chenkelmann
Last active April 9, 2020 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenkelmann/2bfa9627d79a9aaab34a46227d81aea5 to your computer and use it in GitHub Desktop.
Save chenkelmann/2bfa9627d79a9aaab34a46227d81aea5 to your computer and use it in GitHub Desktop.
Simple Example showing DJL Locale issues
import ai.djl.mxnet.engine.MxNDArray;
import ai.djl.mxnet.engine.MxNDManager;
import ai.djl.mxnet.engine.MxOpParams;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDManager;
// Tested using the following dependencies:
// compile "ai.djl:api:0.4.0"
// compile "ai.djl.mxnet:mxnet-engine:0.4.0"
// compile "ai.djl.mxnet:mxnet-native-auto:1.6.0"
// The error occurs on a Linux machine if LC_NUMERIC=de_DE.UTF-8 (Tested on Linux Mint 19.3)
// If LC_NUMERIC is set to en_US.UTF-8 everything works as expected.
public class DJLLocaleTest {
public static void main(final String[] args) {
final NDManager manager = NDManager.newBaseManager();
//This is the bug: The double gets turned into the String "0.7" which seems to be parsed into a double again
//by the MxNet engine. I think the engine uses the German number locale and turns the string "0.7" into a 0, as it
//stops parsing at the first illegal char, the ".". (This is just a guess)
//prints:
//ND: () gpu(0) float64
//1.3
System.out.println(manager.create(1.3).add(0.7));
//This works as expected, as creating an NDArray as argument circumvents the String parsing.
//prints
//ND: (1) gpu(0) float64
//[2.]
System.out.println(manager.create(1.3).add(manager.create(new double[]{0.7})));
//We can reproduce the error by passing the string into the engine manually:
//ND: () gpu(0) float64
//1.3
System.out.println(addWithStringParam((MxNDArray) manager.create(1.3), (MxNDManager)manager, "0.7"));
// If we pass a number string with a German number format, it also works fine.
//prints:
//ND: () gpu(0) float64
//2.
System.out.println(addWithStringParam((MxNDArray) manager.create(1.3), (MxNDManager)manager, "0,7"));
}
/**
* This is copy-pasted and modified from MxNDArray.
* It executes the addition in the same way as MxNDArray.add(Number) does internally.
*/
public static NDArray addWithStringParam(final MxNDArray array, final MxNDManager manager, final String param) {
MxOpParams params = new MxOpParams();
params.add("scalar", param);
return manager.invoke("_npi_add_scalar", array, params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment