MCPcopy Create free account
hub / github.com/pytorch/pytorch / _worker

Function _worker

torch/nn/parallel/parallel_apply.py:58–89  ·  view source on GitHub ↗
(
        i: int,
        module: Module,
        input: Any,
        kwargs: Dict[str, Any],
        device: Optional[Union[int, torch.device]] = None,
        stream: Optional[torch.cuda.Stream] = None,
    )

Source from the content-addressed store, hash-verified

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,

Callers 1

parallel_applyFunction · 0.85

Calls 8

ExceptionWrapperClass · 0.90
autocastClass · 0.90
get_a_varFunction · 0.85
isinstanceFunction · 0.85
current_streamMethod · 0.80
streamMethod · 0.80
get_deviceMethod · 0.45
deviceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…