utils.py 2.5 KB

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