Skip to content

Commit

Permalink
[NODE][Serialization]fix serialization precision loss in float (apach…
Browse files Browse the repository at this point in the history
…e#4503)

* 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
  • Loading branch information
HisiFish authored and Xingyu Zhou committed Dec 13, 2019
1 parent d35feb3 commit 6716edf
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/node/serialization.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <double> 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);
Expand Down

0 comments on commit 6716edf

Please sign in to comment.