MessageHub¶
- class mmengine.logging.MessageHub(name, log_scalars=None, runtime_info=None, resumed_keys=None)[源代码]¶
Message hub for component interaction. MessageHub is created and accessed in the same way as ManagerMixin.
MessageHub
will record log information and runtime information. The log information refers to the learning rate, loss, etc. of the model during training phase, which will be stored asHistoryBuffer
. The runtime information refers to the iter times, meta information of runner etc., which will be overwritten by next update.- 参数
name (str) – Name of message hub used to get corresponding instance globally.
log_scalars (dict, optional) – Each key-value pair in the dictionary is the name of the log information such as “loss”, “lr”, “metric” and their corresponding values. The type of value must be HistoryBuffer. Defaults to None.
runtime_info (dict, optional) – Each key-value pair in the dictionary is the name of the runtime information and their corresponding values. Defaults to None.
resumed_keys (dict, optional) – Each key-value pair in the dictionary decides whether the key in
_log_scalars
and_runtime_info
will be serialized.
备注
Key in
_resumed_keys
belongs to_log_scalars
or_runtime_info
. The corresponding value cannot be set repeatedly.实际案例
>>> # create empty `MessageHub`. >>> message_hub1 = MessageHub('name') >>> log_scalars = dict(loss=HistoryBuffer()) >>> runtime_info = dict(task='task') >>> resumed_keys = dict(loss=True) >>> # create `MessageHub` from data. >>> message_hub2 = MessageHub( >>> name='name', >>> log_scalars=log_scalars, >>> runtime_info=runtime_info, >>> resumed_keys=resumed_keys)
- classmethod get_current_instance()[源代码]¶
Get latest created
MessageHub
instance.MessageHub
can callget_current_instance()
before any instance has been created, and return a message hub with the instance name “mmengine”.- 返回
Empty
MessageHub
instance.- 返回类型
- get_info(key, default=None)[源代码]¶
Get runtime information by key. If the key does not exist, this method will return default information.
- 参数
key (str) – Key of runtime information.
default (Any, optional) – The default returned value for the given key.
- 返回
A copy of corresponding runtime information if the key exists.
- 返回类型
Any
- get_scalar(key)[源代码]¶
Get
HistoryBuffer
instance by key.备注
Considering the large memory footprint of history buffers in the post-training,
get_scalar()
will not return a reference of history buffer rather than a copy.- 参数
key (str) – Key of
HistoryBuffer
.- 返回
Corresponding
HistoryBuffer
instance if the key exists.- 返回类型
- load_state_dict(state_dict)[源代码]¶
Loads log scalars, runtime information and resumed keys from
state_dict
ormessage_hub
.If
state_dict
is a dictionary returned bystate_dict()
, it will only make copies of data which should be resumed from the sourcemessage_hub
.If
state_dict
is amessage_hub
instance, it will make copies of all data from the source message_hub. We suggest to load data fromdict
rather than aMessageHub
instance.- 参数
state_dict (dict or MessageHub) – A dictionary contains key
log_scalars
runtime_info
andresumed_keys
, or a MessageHub instance.- 返回类型
None
- property log_scalars: collections.OrderedDict¶
Get all
HistoryBuffer
instances.备注
Considering the large memory footprint of history buffers in the post-training,
get_scalar()
will return a reference of history buffer rather than a copy.- 返回
All
HistoryBuffer
instances.- 返回类型
OrderedDict
- pop_info(key, default=None)[源代码]¶
Remove runtime information by key. If the key does not exist, this method will return the default value.
- 参数
key (str) – Key of runtime information.
default (Any, optional) – The default returned value for the given key.
- 返回
The runtime information if the key exists.
- 返回类型
Any
- property runtime_info: collections.OrderedDict¶
Get all runtime information.
- 返回
A copy of all runtime information.
- 返回类型
OrderedDict
- state_dict()[源代码]¶
Returns a dictionary containing log scalars, runtime information and resumed keys, which should be resumed.
The returned
state_dict
can be loaded byload_state_dict()
.- 返回
A dictionary contains
log_scalars
,runtime_info
andresumed_keys
.- 返回类型
- update_info(key, value, resumed=True)[源代码]¶
Update runtime information.
The key corresponding runtime information will be overwritten each time calling
update_info
.备注
The
resumed
argument needs to be consistent for the samekey
.实际案例
>>> message_hub = MessageHub(name='name') >>> message_hub.update_info('iter', 100)
- update_info_dict(info_dict, resumed=True)[源代码]¶
Update runtime information with dictionary.
The key corresponding runtime information will be overwritten each time calling
update_info
.备注
The
resumed
argument needs to be consistent for the sameinfo_dict
.实际案例
>>> message_hub = MessageHub(name='name') >>> message_hub.update_info({'iter': 100})
- update_scalar(key, value, count=1, resumed=True)[源代码]¶
Update :attr:_log_scalars.
Update
HistoryBuffer
in_log_scalars
. If corresponding keyHistoryBuffer
has been created,value
andcount
is the argument ofHistoryBuffer.update
, Otherwise,update_scalar
will create anHistoryBuffer
with value and count via the constructor ofHistoryBuffer
.实际案例
>>> message_hub = MessageHub(name='name') >>> # create loss `HistoryBuffer` with value=1, count=1 >>> message_hub.update_scalar('loss', 1) >>> # update loss `HistoryBuffer` with value >>> message_hub.update_scalar('loss', 3) >>> message_hub.update_scalar('loss', 3, resumed=False) AssertionError: loss used to be true, but got false now. resumed keys cannot be modified repeatedly'
备注
The
resumed
argument needs to be consistent for the samekey
.- 参数
key (str) – Key of
HistoryBuffer
.value (torch.Tensor or np.ndarray or int or float) – Value of log.
count (torch.Tensor or np.ndarray or int or float) – Accumulation times of log, defaults to 1. count will be used in smooth statistics.
resumed (str) – Whether the corresponding
HistoryBuffer
could be resumed. Defaults to True.
- 返回类型
None
- update_scalars(log_dict, resumed=True)[源代码]¶
Update
_log_scalars
with a dict.update_scalars
iterates through each pair of log_dict key-value, and callsupdate_scalar
. If type of value is dict, the value should bedict(value=xxx) or dict(value=xxx, count=xxx)
. Item inlog_dict
has the same resume option.备注
The
resumed
argument needs to be consistent for the samelog_dict
.- 参数
- 返回类型
None
实际案例
>>> message_hub = MessageHub.get_instance('mmengine') >>> log_dict = dict(a=1, b=2, c=3) >>> message_hub.update_scalars(log_dict) >>> # The default count of `a`, `b` and `c` is 1. >>> log_dict = dict(a=1, b=2, c=dict(value=1, count=2)) >>> message_hub.update_scalars(log_dict) >>> # The count of `c` is 2.