| 28 | |
| 29 | |
| 30 | class EmptyInitOnDevice(torch.overrides.TorchFunctionMode): |
| 31 | def __init__(self, device=None): |
| 32 | self.device = device |
| 33 | |
| 34 | def __torch_function__(self, func, types, args=(), kwargs=None): |
| 35 | kwargs = kwargs or {} |
| 36 | if getattr(func, '__module__', None) == 'torch.nn.init': |
| 37 | if 'tensor' in kwargs: |
| 38 | return kwargs['tensor'] |
| 39 | else: |
| 40 | return args[0] |
| 41 | if self.device is not None and func in torch.utils._device._device_constructors() and kwargs.get('device') is None: |
| 42 | kwargs['device'] = self.device |
| 43 | return func(*args, **kwargs) |
| 44 | |
| 45 | |
| 46 | def with_empty_init(func): |