Skip to content
Snippets Groups Projects
Unverified Commit 6dc1d708 authored by Mashiro's avatar Mashiro Committed by GitHub
Browse files

[Fix] Support update np.ScalarType data in message_hub (#898)


* Clean the commit history

* Update message_hub.py

---------

Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>
parent 5753cd98
No related branches found
No related tags found
No related merge requests found
......@@ -314,17 +314,19 @@ class MessageHub(ManagerMixin):
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)):
......
......@@ -34,14 +34,18 @@ class TestMessageHub:
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')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment