Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

MXNet Python numerical scalar is truncated when we set the locale #18079

Description

@stu1130

Description

deepjavalibrary/djl#49 indicates that MXNet backend didn't handle the locale right. When we use the operator that can input the scalar, we use string as medium to pass the scalar to backend. In C++ engine, we use std::stod to convert the string back to double https://github.com/apache/incubator-mxnet/blob/7a59239a260920dfde7c6bac410679c57a868cc0/src/operator/tensor/elemwise_binary_scalar_op.h#L365
The std::stod converts the string based on locale, but it doesn't get the locale from environment variable. So it is still use us by default. This is not the case in Java, you can see scalar was truncated which leads to wrong result.

You can reproduce the issue with the following command:

# install the de_DE.UTF-8 locale on the system
# 0.7 in de_DE.UTF-8 is represented as 0,7
# start the python
>>> import mxnet as mx
>>> from mxnet import npx
>>> import locale
>>> a = mx.np.array(1.3)
>>> a + 0.7
2.0 # right result as expected
>>> locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
>>>  a + 0.7
1.3 # wrong result because 0.7 was truncated to 0.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions