Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an example for resnet50 in half precision. #307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 85 additions & 0 deletions examples/resnet/resnet-50-fp16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from torchvision.models import resnet50, ResNet50_Weights
import torch
import numpy as np
from shark_turbine.aot import *
import iree.runtime as rt

# Loading feature extractor and pretrained model from huggingface
# extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-18")
model = resnet50(weights="DEFAULT")
float_model = model.eval().float()
model = model.eval().half()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we are only doing f16 instead of having an option/flag for whatever precision you want?



# define a function to do inference
# this will get passed to the compiled module as a jittable function
def vision_forward(pixel_values_tensor: torch.Tensor):
with torch.no_grad():
logits = model.forward(pixel_values_tensor)
predicted_id = torch.argmax(logits, -1)
return predicted_id


def vision_forward_float(pixel_values_tensor: torch.Tensor):
with torch.no_grad():
logits = float_model.forward(pixel_values_tensor)
predicted_id = torch.argmax(logits, -1)
return predicted_id


# a dynamic module for doing inference
# this will be compiled AOT to a memory buffer
class Resnet50_f16(CompiledModule):
params = export_parameters(model)

def forward(self, x=AbstractTensor(None, 3, 224, 224, dtype=torch.float16)):
# set a constraint for the dynamic number of batches
# interestingly enough, it doesn't seem to limit BATCH_SIZE
const = [x.dynamic_dim(0) < 16]
return jittable(vision_forward)(x, constraints=const)


# build an mlir module with 1-shot exporter
exported = export(Resnet50_f16)
# compile exported module to a memory buffer
compiled_binary = exported.compile(save_to=None)


# return type is rt.array_interop.DeviceArray
# np.array of outputs can be accessed via to_host() method
def shark_infer(x):
config = rt.Config("local-task")
vmm = rt.load_vm_module(
rt.VmModule.wrap_buffer(config.vm_instance, compiled_binary.map_memory()),
config,
)
y = vmm.forward(x)
return y


# prints the text corresponding to output label codes
def print_labels(class_id):
weights = ResNet50_Weights.DEFAULT
for l in class_id:
print(weights.meta["categories"][l])


# finds discrepancies between id0 and id1
def largest_error(array1, array2):
absolute_diff = np.abs(array1 - array2)
max_error = np.max(absolute_diff)
return max_error


# load some examples and check for discrepancies between
# compiled module and standard inference (forward function)

x = torch.randn((10, 3, 224, 224), dtype=torch.float16)
x_float = torch.randn((10, 3, 224, 224), dtype=torch.float32)
y0 = shark_infer(x).to_host()
float_model = float_model.float()
y1 = np.asarray(vision_forward_float(x_float))
print_labels(y0)
print(
f"Largest error between turbine (fp16) and pytorch (fp32) baseline is {largest_error(y0,y1)}"
)
Loading