(
i: int,
module: Module,
input: Any,
kwargs: Dict[str, Any],
device: Optional[Union[int, torch.device]] = None,
stream: Optional[torch.cuda.Stream] = None,
)
| 56 | grad_enabled, autocast_enabled = torch.is_grad_enabled(), torch.is_autocast_enabled() |
| 57 | |
| 58 | def _worker( |
| 59 | i: int, |
| 60 | module: Module, |
| 61 | input: Any, |
| 62 | kwargs: Dict[str, Any], |
| 63 | device: Optional[Union[int, torch.device]] = None, |
| 64 | stream: Optional[torch.cuda.Stream] = None, |
| 65 | ) -> None: |
| 66 | torch.set_grad_enabled(grad_enabled) |
| 67 | if device is None: |
| 68 | t = get_a_var(input) |
| 69 | if t is None: |
| 70 | with lock: |
| 71 | results[i] = ExceptionWrapper( |
| 72 | where=f"in replica {i}, no device was provided and no tensor input was found; " |
| 73 | "device cannot be resolved") |
| 74 | return |
| 75 | device = t.get_device() |
| 76 | if stream is None: |
| 77 | stream = torch.cuda.current_stream(device) |
| 78 | try: |
| 79 | with torch.cuda.device(device), torch.cuda.stream(stream), autocast(enabled=autocast_enabled): |
| 80 | # this also avoids accidental slicing of `input` if it is a Tensor |
| 81 | if not isinstance(input, (list, tuple)): |
| 82 | input = (input,) |
| 83 | output = module(*input, **kwargs) |
| 84 | with lock: |
| 85 | results[i] = output |
| 86 | except Exception: |
| 87 | with lock: |
| 88 | results[i] = ExceptionWrapper( |
| 89 | where=f"in replica {i} on device {device}") |
| 90 | |
| 91 | if len(modules) > 1: |
| 92 | threads = [threading.Thread(target=_worker, |
no test coverage detected
searching dependent graphs…