Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions examples/benchmark/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ def run(

results = []

start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
timer_event = getattr(torch, "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
start = timer_event.Event(enable_timing=True)
end = timer_event.Event(enable_timing=True)
for _ in tqdm(range(iterations)):
start.record()
out_tensor = stream.stream(image_tensor).cpu()
queue.put(out_tensor)
end.record()

torch.cuda.synchronize()
timer_event.synchronize()
results.append(start.elapsed_time(end))

print(f"Average time: {sum(results) / len(results)}ms")
Expand Down
7 changes: 4 additions & 3 deletions examples/benchmark/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ def run(

results = []

start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
timer_event = getattr(torch, "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
start = timer_event.Event(enable_timing=True)
end = timer_event.Event(enable_timing=True)

for _ in tqdm(range(iterations)):
start.record()
image_tensor = stream.preprocess_image(downloaded_image)
stream(image=image_tensor)
end.record()

torch.cuda.synchronize()
timer_event.synchronize()
results.append(start.elapsed_time(end))

print(f"Average time: {sum(results) / len(results)}ms")
Expand Down
9 changes: 6 additions & 3 deletions src/streamdiffusion/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time

from typing import List, Optional, Union, Any, Dict, Tuple, Literal

import numpy as np
Expand Down Expand Up @@ -30,6 +31,8 @@ def __init__(
self.dtype = torch_dtype
self.generator = None

self.timer_event = getattr(torch, str(self.device).split(':', 1)[0])

self.height = height
self.width = width

Expand Down Expand Up @@ -440,8 +443,8 @@ def predict_x0_batch(self, x_t_latent: torch.Tensor) -> torch.Tensor:
def __call__(
self, x: Union[torch.Tensor, PIL.Image.Image, np.ndarray] = None
) -> torch.Tensor:
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start = self.timer_event.Event(enable_timing=True)
end = self.timer_event.Event(enable_timing=True)
start.record()
if x is not None:
x = self.image_processor.preprocess(x, self.height, self.width).to(
Expand All @@ -463,7 +466,7 @@ def __call__(

self.prev_image_result = x_output
end.record()
torch.cuda.synchronize()
self.timer_event.synchronize()
inference_time = start.elapsed_time(end) / 1000
self.inference_time_ema = 0.9 * self.inference_time_ema + 0.1 * inference_time
return x_output
Expand Down
Loading