From 6716edf2322b341f6619d3771a9322c9bad379aa Mon Sep 17 00:00:00 2001 From: LaiyuanGong <30686426+HisiFish@users.noreply.github.com> Date: Wed, 11 Dec 2019 19:47:50 -0600 Subject: [PATCH] [NODE][Serialization]fix serialization precision loss in float (#4503) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix serialization precision loss in float When we want to serialize a tvm.tensor object(like pickle), we will get a precision loss cause by std::to_string()。 For example, a2.value will be 0.0 while a.value=0.00000001 in the following: import tvm import pickle a = tvm.const(0.00000001, 'float32') a2 = pickle.loads(pickle.dumps(a)) * remove line end spaces --- src/node/serialization.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node/serialization.cc b/src/node/serialization.cc index d270e72d3958..cb310eb2cda9 100644 --- a/src/node/serialization.cc +++ b/src/node/serialization.cc @@ -167,7 +167,11 @@ class JSONAttrGetter : public AttrVisitor { ReflectionVTable* reflection_ = ReflectionVTable::Global(); void Visit(const char* key, double* value) final { - node_->attrs[key] = std::to_string(*value); + std::ostringstream s; + // Type have approximately 16 decimal digits + s.precision(16); + s << (*value); + node_->attrs[key] = s.str(); } void Visit(const char* key, int64_t* value) final { node_->attrs[key] = std::to_string(*value);