Move image IO ops out of time measurements
Run model n times and average the runtime
This commit is contained in:
parent
6d3570d0e2
commit
07c9771a30
37
benchmark.py
37
benchmark.py
|
|
@ -1,7 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import time
|
import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import habana_frameworks.torch.core as ht
|
|
||||||
import torch
|
import torch
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
@ -11,27 +10,39 @@ from clip.utils import get_device_initial
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def run_model(model_name, device):
|
def run_model(model_name, device):
|
||||||
model, transform = clip.load(model_name, device=get_device_initial(device), jit=False)
|
model, transform = clip.load(
|
||||||
|
model_name, device=get_device_initial(device), jit=False
|
||||||
|
)
|
||||||
|
|
||||||
image = transform(Image.open("CLIP.png")).unsqueeze(0).to(device)
|
image = transform(Image.open("CLIP.png")).unsqueeze(0).to(device)
|
||||||
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)
|
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
|
start_time = time.perf_counter()
|
||||||
|
|
||||||
logits_per_image, _ = model(image, text)
|
logits_per_image, _ = model(image, text)
|
||||||
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
|
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
|
||||||
return probs
|
|
||||||
|
end_time = time.perf_counter()
|
||||||
|
logger.info(f"Execution time: {end_time - start_time:.4f} seconds")
|
||||||
|
return probs, end_time - start_time
|
||||||
|
|
||||||
|
|
||||||
|
def run_n_times(model_name, device, n):
|
||||||
|
times = []
|
||||||
|
logger.info(f"Running {model_name} on {device} {n} times")
|
||||||
|
for _ in range(n):
|
||||||
|
logger.info(f"Run {_ + 1} of {n}")
|
||||||
|
_, time = run_model(model_name, device)
|
||||||
|
times.append(time)
|
||||||
|
return np.mean(times)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
logger.info("Running on HPU")
|
hpu_time = run_n_times("RN50", "hpu", 10)
|
||||||
start_time = time.time()
|
cpu_time = run_n_times("RN50", "cpu", 10)
|
||||||
run_model("RN50", "hpu")
|
|
||||||
end_time = time.time()
|
|
||||||
logger.info(f"HPU execution time: {end_time - start_time:.4f} seconds")
|
|
||||||
|
|
||||||
logger.info("Running on CPU")
|
logger.info(f"HPU time: {hpu_time:.4f} seconds")
|
||||||
start_time = time.time()
|
logger.info(f"CPU time: {cpu_time:.4f} seconds")
|
||||||
run_model("RN50", "cpu")
|
|
||||||
end_time = time.time()
|
|
||||||
logger.info(f"CPU execution time: {end_time - start_time:.4f} seconds")
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue