Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of inf float value #5912

Merged
merged 1 commit into from Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/node/serialization.cc
Expand Up @@ -352,9 +352,15 @@ class JSONAttrSetter : public AttrVisitor {
template <typename T>
void ParseValue(const char* key, T* value) const {
std::istringstream is(GetValue(key));
is >> *value;
if (is.fail()) {
LOG(FATAL) << "Wrong value format for field " << key;
if (is.str() == "inf") {
*value = std::numeric_limits<T>::infinity();
} else if (is.str() == "-inf") {
*value = -std::numeric_limits<T>::infinity();
} else {
is >> *value;
if (is.fail()) {
LOG(FATAL) << "Wrong value format for field " << key;
}
}
}
void Visit(const char* key, double* value) final { ParseValue(key, value); }
Expand Down
11 changes: 11 additions & 0 deletions tests/python/unittest/test_node_reflection.py
Expand Up @@ -28,6 +28,16 @@ def test_const_saveload_json():
zz = tvm.ir.load_json(json_str)
tvm.ir.assert_structural_equal(zz, z, map_free_vars=True)

def _test_infinity_value(value, dtype):
x = tvm.tir.const(value, dtype)
json_str = tvm.ir.save_json(x)
tvm.ir.assert_structural_equal(x, tvm.ir.load_json(json_str))

def test_infinity_value():
_test_infinity_value(float("inf"), 'float64')
_test_infinity_value(float("-inf"), 'float64')
_test_infinity_value(float("inf"), 'float32')
_test_infinity_value(float("-inf"), 'float32')

def test_make_smap():
# save load json
Expand Down Expand Up @@ -145,3 +155,4 @@ def test_dict():
test_make_sum()
test_pass_config()
test_dict()
test_infinity_value()