0
0
PyTorchml~20 mins

TorchScript for production in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TorchScript Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of TorchScript tracing with control flow
Consider the following PyTorch model and its TorchScript tracing. What will be the output when running scripted_model(torch.tensor(3))?
PyTorch
import torch
import torch.nn as nn

class MyModel(nn.Module):
    def forward(self, x):
        if x.item() > 2:
            return x * 2
        else:
            return x + 2

model = MyModel()
scripted_model = torch.jit.trace(model, torch.tensor(1))
output = scripted_model(torch.tensor(3))
print(output.item())
A4
B5
C3
D6
Attempts:
2 left
💡 Hint
Tracing records operations for the example input. Think about what happens when input changes.
Model Choice
intermediate
1:30remaining
Choosing between tracing and scripting for dynamic control flow
You have a PyTorch model with dynamic control flow depending on input values. Which TorchScript method should you use to correctly convert the model for production?
Atorch.jit.trace
Btorch.jit.optimize_for_inference
Ctorch.jit.freeze
Dtorch.jit.script
Attempts:
2 left
💡 Hint
Tracing records operations for fixed inputs, scripting analyzes code.
Hyperparameter
advanced
1:30remaining
Optimizing TorchScript model for inference speed
Which of the following options is the best practice to improve inference speed of a TorchScript model in production?
AUse torch.jit.freeze on the scripted model
BUse torch.jit.trace with multiple example inputs
CIncrease batch size during training
DAdd dropout layers before scripting
Attempts:
2 left
💡 Hint
Freezing removes unnecessary parts and optimizes the graph.
🔧 Debug
advanced
2:00remaining
Identifying error in TorchScript scripted model
Given this scripted model code, what error will it raise when running scripted_model(torch.tensor([1,2,3]))?
PyTorch
import torch
import torch.nn as nn

class Model(nn.Module):
    def forward(self, x):
        return x[0] + x[3]

model = Model()
scripted_model = torch.jit.script(model)
output = scripted_model(torch.tensor([1,2,3]))
AIndexError: index 3 is out of bounds for dimension 0 with size 3
BNo error, output is 4
CRuntimeError: TorchScript does not support list indexing
DTypeError: unsupported operand type(s) for +: 'Tensor' and 'int'
Attempts:
2 left
💡 Hint
Check the tensor size and indexing carefully.
🧠 Conceptual
expert
2:30remaining
Understanding TorchScript serialization and deployment
Which statement about TorchScript models saved with torch.jit.save and loaded with torch.jit.load is TRUE?
AThe saved model cannot be used without the original Python source code
BThe loaded model can only run on the same device type it was saved from
CThe saved model includes both the model code and its parameters in a single file
DThe saved model loses all optimizations applied during scripting
Attempts:
2 left
💡 Hint
Think about portability and self-containment of TorchScript files.