(self, decode_fn: Callable[..., torch.Tensor], *args: Any, **kwargs: Any)
| 110 | |
| 111 | def _resolve_device(self, args: tuple[Any, ...]) -> torch.device: |
| 112 | if self._device is not None: |
| 113 | return self._device |
| 114 | for arg in args: |
| 115 | if isinstance(arg, torch.Tensor) and arg.device.type == "cuda": |
| 116 | self._device = arg.device |
| 117 | return arg.device |
| 118 | self._device = torch.device("cuda") |
| 119 | return self._device |
| 120 | |
| 121 | def _ensure_stream(self, device: torch.device) -> torch.cuda.Stream: |
| 122 | if self._stream is None: |
| 123 | self._stream = torch.cuda.Stream(device=device) |
| 124 | return self._stream |
| 125 | |
| 126 | def _sync_if_cuda(self, args: tuple[Any, ...]) -> None: |
| 127 | if not torch.cuda.is_available(): |
| 128 | return |
| 129 | device = self._resolve_device(args) |
| 130 | if device.type != "cuda": |
| 131 | return |
| 132 | torch.cuda.synchronize(device) |
| 133 | |
| 134 | def submit(self, decode_fn: Callable[..., Any], *args: Any, **kwargs: Any) -> None: |
| 135 | self._num_submitted += 1 |
| 136 | if not self.enabled: |
| 137 | self._sync_if_cuda(args) |
| 138 | t0 = time.perf_counter() |
| 139 | output = decode_fn(*args, **kwargs) |
| 140 | if self._collect_outputs: |
| 141 | self._chunks.append(output) |
| 142 | self._sync_if_cuda(args) |
| 143 | chunk_ms = (time.perf_counter() - t0) * 1000.0 |
| 144 | self._sync_decode_ms += chunk_ms |
| 145 | logger.info( |
| 146 | "[AsyncVAEChunkDecoder] sync VAE chunk {}/{} decode={:.6f} seconds", |
| 147 | self._num_submitted, |
| 148 | self._num_submitted, |
| 149 | chunk_ms / 1000.0, |
| 150 | ) |
| 151 | if self._on_chunk is not None: |
| 152 | self._on_chunk(output, self._num_submitted - 1) |
| 153 | return |
| 154 | |
| 155 | device = self._resolve_device(args) |
| 156 | stream = self._ensure_stream(device) |
| 157 | # LongLive: diffusion_done.record() then vae_stream.wait_event(diffusion_done) |
| 158 | diffusion_done = torch.cuda.Event() |
| 159 | diffusion_done.record(torch.cuda.current_stream(device)) |
| 160 | if self._dispatch_in_worker: |
| 161 | for value in list(args) + list(kwargs.values()): |
| 162 | if isinstance(value, torch.Tensor) and value.device.type == "cuda": |
| 163 | value.record_stream(stream) |
no test coverage detected