-
Notifications
You must be signed in to change notification settings - Fork 48
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
monorimet
wants to merge
1
commit into
main
Choose a base branch
from
ean-resnet50
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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() | ||
|
||
|
||
# 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)}" | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?