forked from tenstorrent/tt-buda-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch_perceiverio_learned.py
58 lines (43 loc) · 1.99 KB
/
pytorch_perceiverio_learned.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# SPDX-FileCopyrightText: © 2024 Tenstorrent AI ULC
# SPDX-License-Identifier: Apache-2.0
# Perceiver IO Learned Demo Script
import os
import pybuda
import requests
from PIL import Image
from transformers import AutoImageProcessor, PerceiverForImageClassificationLearned
def run_perceiverio_learned_pytorch():
# Load feature extractor and model checkpoint from HuggingFace
model_ckpt = "deepmind/vision-perceiver-learned"
image_processor = AutoImageProcessor.from_pretrained(model_ckpt)
model = PerceiverForImageClassificationLearned.from_pretrained(model_ckpt)
model.eval()
# Set PyBuda configuration parameters
compiler_cfg = pybuda.config._get_global_compiler_config()
compiler_cfg.balancer_policy = "Ribbon"
compiler_cfg.default_df_override = pybuda.DataFormat.Float16_b
os.environ["PYBUDA_RIBBON2"] = "1"
compiler_cfg.enable_auto_fusing = False
os.environ["PYBUDA_DISABLE_PADDING_PASS"] = "1"
available_devices = pybuda.detect_available_devices()
if available_devices:
if available_devices[0] == pybuda.BackendDevice.Grayskull:
os.environ["TT_BACKEND_OVERLAY_MAX_EXTRA_BLOB_SIZE"] = f"{101*1024}"
# Load data sample
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
label = "tabby, tabby cat"
# Data preprocessing
inputs = image_processor(image, return_tensors="pt")
pixel_values = inputs["pixel_values"]
# Run inference on Tenstorrent device
output_q = pybuda.run_inference(
pybuda.PyTorchModule("pt_" + str(model_ckpt.split("/")[-1].replace("-", "_")), model), inputs=[(pixel_values,)]
)
output = output_q.get() # return last queue object
# Data postprocessing
predicted_value = output[0].value().argmax(-1).item()
predicted_label = model.config.id2label[predicted_value]
print(f"True Label: {label} | Predicted Label: {predicted_label}")
if __name__ == "__main__":
run_perceiverio_learned_pytorch()