__init__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from .modules import * # noqa: F403
  2. from .parameter import (
  3. Parameter as Parameter,
  4. UninitializedParameter as UninitializedParameter,
  5. UninitializedBuffer as UninitializedBuffer,
  6. )
  7. from .parallel import DataParallel as DataParallel
  8. from . import init
  9. from . import functional
  10. from . import utils
  11. def factory_kwargs(kwargs):
  12. r"""
  13. Given kwargs, returns a canonicalized dict of factory kwargs that can be directly passed
  14. to factory functions like torch.empty, or errors if unrecognized kwargs are present.
  15. This function makes it simple to write code like this::
  16. class MyModule(nn.Module):
  17. def __init__(self, **kwargs):
  18. factory_kwargs = torch.nn.factory_kwargs(kwargs)
  19. self.weight = Parameter(torch.empty(10, **factory_kwargs))
  20. Why should you use this function instead of just passing `kwargs` along directly?
  21. 1. This function does error validation, so if there are unexpected kwargs we will
  22. immediately report an error, instead of deferring it to the factory call
  23. 2. This function supports a special `factory_kwargs` argument, which can be used to
  24. explicitly specify a kwarg to be used for factory functions, in the event one of the
  25. factory kwargs conflicts with an already existing argument in the signature (e.g.
  26. in the signature ``def f(dtype, **kwargs)``, you can specify ``dtype`` for factory
  27. functions, as distinct from the dtype argument, by saying
  28. ``f(dtype1, factory_kwargs={"dtype": dtype2})``)
  29. """
  30. if kwargs is None:
  31. return {}
  32. simple_keys = {"device", "dtype", "memory_format"}
  33. expected_keys = simple_keys | {"factory_kwargs"}
  34. if not kwargs.keys() <= expected_keys:
  35. raise TypeError(f"unexpected kwargs {kwargs.keys() - expected_keys}")
  36. # guarantee no input kwargs is untouched
  37. r = dict(kwargs.get("factory_kwargs", {}))
  38. for k in simple_keys:
  39. if k in kwargs:
  40. if k in r:
  41. raise TypeError(f"{k} specified twice, in **kwargs and in factory_kwargs")
  42. r[k] = kwargs[k]
  43. return r