Update ModuleDict doc about order
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/17717
Differential Revision: D14346557
Pulled By: ezyang
fbshipit-source-id: 2484c7d8105f9aa8bce5567d1fa2d4f587cc9cc2
diff --git a/torch/nn/modules/container.py b/torch/nn/modules/container.py
index 18a3a5a..e8f870c 100644
--- a/torch/nn/modules/container.py
+++ b/torch/nn/modules/container.py
@@ -207,8 +207,21 @@
class ModuleDict(Module):
r"""Holds submodules in a dictionary.
- ModuleDict can be indexed like a regular Python dictionary, but modules it
- contains are properly registered, and will be visible by all Module methods.
+ :class:`~torch.nn.ModuleDict` can be indexed like a regular Python dictionary,
+ but modules it contains are properly registered, and will be visible by all
+ :class:`~torch.nn.Module` methods.
+
+ :class:`~torch.nn.ModuleDict` is an **ordered** dictionary that respects
+
+ * the order of insertion, and
+
+ * in :meth:`~torch.nn.ModuleDict.update`, the order of the merged ``OrderedDict``
+ or another :class:`~torch.nn.ModuleDict` (the argument to :meth:`~torch.nn.ModuleDict.update`).
+
+ Note that :meth:`~torch.nn.ModuleDict.update` with other unordered mapping
+ types (e.g., Python's plain ``dict``) doesn't not preserve order of the
+ merged mapping.
+
Arguments:
modules (iterable, optional): a mapping (dictionary) of (string: module)
@@ -301,7 +314,7 @@
type(modules).__name__)
if isinstance(modules, container_abcs.Mapping):
- if isinstance(modules, OrderedDict):
+ if isinstance(modules, (OrderedDict, ModuleDict)):
for key, module in modules.items():
self[key] = module
else: