-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[Concept Entry] Created the export entry #6127
base: main
Are you sure you want to change the base?
Conversation
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.
Hey @dakshdeepHERE, I have a few suggestions, please check the comments and make the necessary changes.
title: Model Export with TorchScript | ||
description: Convert PyTorch models to TorchScript for production deployment in non-Python environments. | ||
subject: | ||
- 'Computer Science' | ||
- 'Data Science' | ||
- 'Machine Learning' | ||
tags: | ||
- 'Deployment' | ||
- 'TorchScript' | ||
- 'Model Export' | ||
- 'PyTorch' | ||
catalog_content: | ||
- 'learn-pytorch' | ||
- 'paths/data-science' |
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.
The headers Title
, Description
, Subject
, and Tags
should be in title case, and the sentences should be inside ''
.
title: Model Export with TorchScript | |
description: Convert PyTorch models to TorchScript for production deployment in non-Python environments. | |
subject: | |
- 'Computer Science' | |
- 'Data Science' | |
- 'Machine Learning' | |
tags: | |
- 'Deployment' | |
- 'TorchScript' | |
- 'Model Export' | |
- 'PyTorch' | |
catalog_content: | |
- 'learn-pytorch' | |
- 'paths/data-science' | |
Title: 'Model Export with TorchScript' | |
Description: 'Convert PyTorch models to TorchScript for optimized deployment in non-Python environments like mobile and embedded systems.' | |
Subject: | |
- 'Computer Science' | |
- 'Data Science' | |
- 'Machine Learning' | |
Tags: | |
- 'Deployment' | |
- 'TorchScript' | |
- 'Model Export' | |
- 'PyTorch' | |
CatalogContent: | |
- 'learn-pytorch' | |
- 'paths/data-science' |
import torch | ||
|
||
class DynamicModel(torch.nn.Module): | ||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
if x.sum() > 0: | ||
return x * 2 | ||
else: | ||
return x - 1 | ||
|
||
model = DynamicModel() | ||
scripted_model = torch.jit.script(model) # Handles dynamic control flow | ||
scripted_model.save("dynamic_model.pt") |
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.
The example should give some tangible output that helps the reader understand what's happening -
import torch | |
class DynamicModel(torch.nn.Module): | |
def forward(self, x: torch.Tensor) -> torch.Tensor: | |
if x.sum() > 0: | |
return x * 2 | |
else: | |
return x - 1 | |
model = DynamicModel() | |
scripted_model = torch.jit.script(model) # Handles dynamic control flow | |
scripted_model.save("dynamic_model.pt") | |
import torch | |
class DynamicModel(torch.nn.Module): | |
def forward(self, x: torch.Tensor) -> torch.Tensor: | |
if x.sum() > 0: | |
return x * 2 | |
else: | |
return x - 1 | |
model = DynamicModel() | |
scripted_model = torch.jit.script(model) # Handles dynamic control flow | |
scripted_model.save("dynamic_model.pt") | |
# Testing the scripted model | |
x1 = torch.tensor([1.0, -0.5, 3.0]) | |
x2 = torch.tensor([-2.0, -1.5, -0.5]) | |
print(scripted_model(x1)) | |
print(scripted_model(x2)) |
Add output below this -
tensor([ 2., -1., 6.])
tensor([-3.0000, -2.5000, -1.5000])
import torch | ||
import torchvision | ||
|
||
model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval() | ||
|
||
# Trace with example input | ||
dummy_input = torch.rand(1, 3, 224, 224) | ||
traced_model = torch.jit.trace(model, dummy_input) | ||
traced_model.save("resnet18_traced.pt") |
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.
Same here. The example should have an output -
import torch | |
import torchvision | |
model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval() | |
# Trace with example input | |
dummy_input = torch.rand(1, 3, 224, 224) | |
traced_model = torch.jit.trace(model, dummy_input) | |
traced_model.save("resnet18_traced.pt") | |
import torch | |
import torchvision | |
model = torchvision.models.resnet18(weights="IMAGENET1K_V1").eval() | |
# Trace with example input | |
dummy_input = torch.rand(1, 3, 224, 224) | |
traced_model = torch.jit.trace(model, dummy_input) | |
traced_model.save("resnet18_traced.pt") | |
# Running inference with traced model | |
output = traced_model(dummy_input) | |
print(output.shape) |
Add output below that:
torch.Size([1, 1000])
This confirms that the traced
ResNet
model processes an image and produces 1000 output logits (corresponding to ImageNet classes).
Description
Issue Solved
Type of Change
Checklist
main
branch.Issues Solved
section.