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

#235 Added Notebook Example for Torchscript #794

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
Binary file added notebooks/simple_model_scripted.pt
Binary file not shown.
92 changes: 92 additions & 0 deletions notebooks/torchscript_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.optim as optim\n",
"import torch.utils.data as data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class SimpleModel(nn.Module):\n",
" def __init__(self):\n",
" super(SimpleModel, self).__init__()\n",
" self.fc = nn.Linear(10, 5)\n",
"\n",
" def forward(self, x):\n",
" return self.fc(x)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = SimpleModel()\n",
"scripted_model = torch.jit.script(model)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"scripted_model.save(\"simple_model_scripted.pt\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[ 0.4575, 0.6755, 0.1485, -0.5884, -1.2903]],\n",
" grad_fn=<AddmmBackward0>)\n"
]
}
],
"source": [
"loaded_model = torch.jit.load(\"simple_model_scripted.pt\")\n",
"x = torch.randn(1, 10)\n",
"output = loaded_model(x)\n",
"print(output)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "bot",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.19"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
61 changes: 61 additions & 0 deletions tests/torchscript_example_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import torch
Copy link
Contributor

Choose a reason for hiding this comment

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

Please rename the file to start with test_ to match the others in the folder

import unittest
import numpy as np

class TestTorchScriptModel(unittest.TestCase):

@classmethod
def setUpClass(cls):
# Load the TorchScript model
cls.model = torch.jit.load('notebooks/simple_model_scripted.pt')
Copy link
Contributor

Choose a reason for hiding this comment

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

We like to avoid checking in the model binaries. Can you please recreate it here and use it for your test, and then remove the binary file?

cls.model.eval() # Set the model to evaluation mode

def test_model_output_shape(self):
"""Test if the model outputs the correct shape."""
input_tensor = torch.randn(1, 5) # Adjust shape based on model input requirements
output_tensor = self.model(input_tensor)
self.assertEqual(output_tensor.shape, (1, 5), "Output shape mismatch")

def test_model_output_values(self):
"""Test if the model output values are within an expected range."""
input_tensor = torch.randn(1, 5)
output_tensor = self.model(input_tensor)
# Example: Check if all output values are within the range -1 to 1
self.assertTrue(torch.all(output_tensor >= -1) and torch.all(output_tensor <= 1),
"Output values out of expected range")

def test_model_with_different_inputs(self):
"""Test the model with various types of inputs to ensure robustness."""
inputs = [
torch.zeros(1, 5),
torch.ones(1, 5),
torch.randn(1, 5),
torch.full((1, 5), 0.5)
]
for input_tensor in inputs:
output_tensor = self.model(input_tensor)
self.assertEqual(output_tensor.shape, (1, 5), "Output shape mismatch with different inputs")

def test_model_gradients(self):
"""Test if the model's gradients are computed correctly."""
input_tensor = torch.randn(1, 5, requires_grad=True)
output_tensor = self.model(input_tensor)
output_tensor.sum().backward()
self.assertIsNotNone(input_tensor.grad, "Gradients were not computed")

def test_scripted_model_serialization(self):
"""Test if the scripted model can be reloaded and produce consistent outputs."""
input_tensor = torch.randn(1, 5)
output_original = self.model(input_tensor)

# Save and reload the scripted model
torch.jit.save(self.model, 'test_scripted_model.pt')
reloaded_model = torch.jit.load('test_scripted_model.pt')
reloaded_model.eval()

output_reloaded = reloaded_model(input_tensor)
self.assertTrue(torch.allclose(output_original, output_reloaded),
"Outputs differ after reloading the scripted model")

if __name__ == '__main__':
unittest.main()
Loading