Skip to content

Commit

Permalink
[Fix] Support update np.ScalarType data in message_hub (#898)
Browse files Browse the repository at this point in the history
* Clean the commit history

* Update message_hub.py

---------

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
  • Loading branch information
HAOCHENYE and zhouzaida committed Feb 1, 2023
1 parent 5753cd9 commit 6dc1d70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 6 additions & 4 deletions mmengine/logging/message_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,19 @@ def get_info(self, key: str) -> Any:
return self._runtime_info[key]

def _get_valid_value(
self, value: Union['torch.Tensor', np.ndarray, int, float]) \
-> Union[int, float]:
self,
value: Union['torch.Tensor', np.ndarray, np.number, int, float],
) -> Union[int, float]:
"""Convert value to python built-in type.
Args:
value (torch.Tensor or np.ndarray or int or float): value of log.
value (torch.Tensor or np.ndarray or np.number or int or float):
value of log.
Returns:
float or int: python built-in type value.
"""
if isinstance(value, np.ndarray):
if isinstance(value, (np.ndarray, np.number)):
assert value.size == 1
value = value.item()
elif isinstance(value, (int, float)):
Expand Down
12 changes: 8 additions & 4 deletions tests/test_logging/test_message_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ def test_init(self):

def test_update_scalar(self):
message_hub = MessageHub.get_instance('mmengine')
# test create target `HistoryBuffer` by name
# Update scalar with int.
message_hub.update_scalar('name', 1)
log_buffer = message_hub.log_scalars['name']
assert (log_buffer._log_history == np.array([1])).all()
# test update target `HistoryBuffer` by name
message_hub.update_scalar('name', 1)

# Update scalar with np.ndarray.
message_hub.update_scalar('name', np.array(1))
assert (log_buffer._log_history == np.array([1, 1])).all()
# unmatched string will raise a key error

# Update scalar with np.int
message_hub.update_scalar('name', np.int32(1))
assert (log_buffer._log_history == np.array([1, 1, 1])).all()

def test_update_info(self):
message_hub = MessageHub.get_instance('mmengine')
Expand Down

0 comments on commit 6dc1d70

Please sign in to comment.