init.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # mypy: allow-untyped-defs
  2. import inspect
  3. import torch
  4. def skip_init(module_cls, *args, **kwargs):
  5. r"""
  6. Given a module class object and args / kwargs, instantiate the module without initializing parameters / buffers.
  7. This can be useful if initialization is slow or if custom initialization will
  8. be performed, making the default initialization unnecessary. There are some caveats to this, due to
  9. the way this function is implemented:
  10. 1. The module must accept a `device` arg in its constructor that is passed to any parameters
  11. or buffers created during construction.
  12. 2. The module must not perform any computation on parameters in its constructor except
  13. initialization (i.e. functions from :mod:`torch.nn.init`).
  14. If these conditions are satisfied, the module can be instantiated with parameter / buffer values
  15. uninitialized, as if having been created using :func:`torch.empty`.
  16. Args:
  17. module_cls: Class object; should be a subclass of :class:`torch.nn.Module`
  18. args: args to pass to the module's constructor
  19. kwargs: kwargs to pass to the module's constructor
  20. Returns:
  21. Instantiated module with uninitialized parameters / buffers
  22. Example::
  23. >>> # xdoctest: +IGNORE_WANT("non-deterministic")
  24. >>> import torch
  25. >>> m = torch.nn.utils.skip_init(torch.nn.Linear, 5, 1)
  26. >>> m.weight
  27. Parameter containing:
  28. tensor([[0.0000e+00, 1.5846e+29, 7.8307e+00, 2.5250e-29, 1.1210e-44]],
  29. requires_grad=True)
  30. >>> m2 = torch.nn.utils.skip_init(torch.nn.Linear, in_features=6, out_features=1)
  31. >>> m2.weight
  32. Parameter containing:
  33. tensor([[-1.4677e+24, 4.5915e-41, 1.4013e-45, 0.0000e+00, -1.4677e+24,
  34. 4.5915e-41]], requires_grad=True)
  35. """
  36. if not issubclass(module_cls, torch.nn.Module):
  37. raise RuntimeError(f'Expected a Module; got {module_cls}')
  38. if 'device' not in inspect.signature(module_cls).parameters:
  39. raise RuntimeError('Module must support a \'device\' arg to skip initialization')
  40. final_device = kwargs.pop('device', 'cpu')
  41. kwargs['device'] = 'meta'
  42. return module_cls(*args, **kwargs).to_empty(device=final_device)