utils.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import collections
  2. from itertools import repeat
  3. from typing import List, Dict, Any
  4. __all__ = ['consume_prefix_in_state_dict_if_present']
  5. def _ntuple(n, name="parse"):
  6. def parse(x):
  7. if isinstance(x, collections.abc.Iterable):
  8. return tuple(x)
  9. return tuple(repeat(x, n))
  10. parse.__name__ = name
  11. return parse
  12. _single = _ntuple(1, "_single")
  13. _pair = _ntuple(2, "_pair")
  14. _triple = _ntuple(3, "_triple")
  15. _quadruple = _ntuple(4, "_quadruple")
  16. def _reverse_repeat_tuple(t, n):
  17. r"""Reverse the order of `t` and repeat each element for `n` times.
  18. This can be used to translate padding arg used by Conv and Pooling modules
  19. to the ones used by `F.pad`.
  20. """
  21. return tuple(x for x in reversed(t) for _ in range(n))
  22. def _list_with_default(out_size: List[int], defaults: List[int]) -> List[int]:
  23. if isinstance(out_size, int):
  24. return out_size
  25. if len(defaults) <= len(out_size):
  26. raise ValueError(
  27. "Input dimension should be at least {}".format(len(out_size) + 1)
  28. )
  29. return [
  30. v if v is not None else d for v, d in zip(out_size, defaults[-len(out_size) :])
  31. ]
  32. def consume_prefix_in_state_dict_if_present(
  33. state_dict: Dict[str, Any], prefix: str
  34. ) -> None:
  35. r"""Strip the prefix in state_dict in place, if any.
  36. ..note::
  37. Given a `state_dict` from a DP/DDP model, a local model can load it by applying
  38. `consume_prefix_in_state_dict_if_present(state_dict, "module.")` before calling
  39. :meth:`torch.nn.Module.load_state_dict`.
  40. Args:
  41. state_dict (OrderedDict): a state-dict to be loaded to the model.
  42. prefix (str): prefix.
  43. """
  44. keys = sorted(state_dict.keys())
  45. for key in keys:
  46. if key.startswith(prefix):
  47. newkey = key[len(prefix) :]
  48. state_dict[newkey] = state_dict.pop(key)
  49. # also strip the prefix in metadata if any.
  50. if "_metadata" in state_dict:
  51. metadata = state_dict["_metadata"]
  52. for key in list(metadata.keys()):
  53. # for the metadata dict, the key can be:
  54. # '': for the DDP module, which we want to remove.
  55. # 'module': for the actual model.
  56. # 'module.xx.xx': for the rest.
  57. if len(key) == 0:
  58. continue
  59. newkey = key[len(prefix) :]
  60. metadata[newkey] = metadata.pop(key)