Created
May 16, 2024 20:49
-
-
Save xmfan/c6aae6128635fbe0705fd8b7fdf1caa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
from time import time | |
import numpy as np | |
import os | |
import sys | |
from torch.profiler import profile, record_function, ProfilerActivity | |
times=[] | |
from transformers import AutoImageProcessor, ViTForImageClassification | |
from datasets import load_dataset | |
dataset = load_dataset("huggingface/cats-image") | |
image = dataset["test"]["image"][0] | |
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224") | |
model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224") | |
inputs = image_processor(image, return_tensors="pt") | |
## to enable torch inductor uncomment below line | |
model = torch.compile(model) | |
with torch.set_grad_enabled(False): | |
for _ in range(50): | |
model(**inputs) #Warmup | |
print("Warmup over") | |
with profile(activities=[ProfilerActivity.CPU], record_shapes=True) as prof: | |
with record_function("model_inference"): | |
for _ in range(100): | |
model(**inputs) | |
print(prof.key_averages(group_by_input_shape=True).table(sort_by="self_cpu_time_total")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment