Skip to content

Commit

Permalink
Add loss conversion from uint16 to float in ProgressBar class (#39231)
Browse files Browse the repository at this point in the history
* Add loss conversion from uint16 to float in progressbar class

* Fix test coverage

* Actually fix coverage

* Fix format error
  • Loading branch information
piotrekobi authored Feb 21, 2022
1 parent a863b32 commit 740cfa9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions python/paddle/hapi/progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import sys
import time
import numpy as np
import struct
from collections import namedtuple

__all__ = []
Expand Down Expand Up @@ -79,6 +80,20 @@ def start(self):
def update(self, current_num, values={}):
now = time.time()

def convert_uint16_to_float(in_list):
in_list = np.asarray(in_list)
out = np.vectorize(
lambda x: struct.unpack('<f', struct.pack('<I', x << 16))[0],
otypes=[np.float32])(in_list.flat)
return np.reshape(out, in_list.shape)

for i, (k, val) in enumerate(values):
if k == "loss":
val = val if isinstance(val, list) or isinstance(
val, np.ndarray) else [val]
if isinstance(val[0], np.uint16):
values[i] = ("loss", list(convert_uint16_to_float(val)))

if current_num:
time_per_unit = (now - self._start) / current_num
else:
Expand Down
1 change: 1 addition & 0 deletions python/paddle/tests/test_progressbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def prog_bar(self, num, epoch, width, verbose=1):
progbar.update(1, [['loss', 1e-4]])
progbar.update(1, [['loss', np.array([1.])]])
progbar.update(1, [['loss', np.array([1e-4])]])
progbar.update(1, [['loss', np.array([1]).astype(np.uint16)]])
progbar.start()

progbar.update(0, values)
Expand Down

0 comments on commit 740cfa9

Please sign in to comment.