0
0
PyTorchml~20 mins

ONNX Runtime inference in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ONNX Runtime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
ONNX Runtime: Model output shape
Given the following PyTorch model exported to ONNX and loaded with ONNX Runtime, what is the shape of the output tensor after inference?
PyTorch
import torch
import onnxruntime as ort
import numpy as np

class SimpleModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = torch.nn.Linear(4, 3)
    def forward(self, x):
        return self.linear(x)

model = SimpleModel()
# Export to ONNX
dummy_input = torch.randn(1, 4)
torch.onnx.export(model, dummy_input, 'simple.onnx', input_names=['input'], output_names=['output'])

# Load with ONNX Runtime
session = ort.InferenceSession('simple.onnx')
input_name = session.get_inputs()[0].name
input_data = np.random.randn(1, 4).astype(np.float32)
outputs = session.run(None, {input_name: input_data})
output = outputs[0]
output.shape
A(3, 1)
B(4, 3)
C(1, 3)
D(1, 4)
Attempts:
2 left
💡 Hint
Remember the linear layer transforms input features from 4 to 3, batch size is 1.
Model Choice
intermediate
1:30remaining
Choosing ONNX Runtime for inference
Which of the following is the main advantage of using ONNX Runtime for model inference compared to running PyTorch models directly?
AONNX Runtime supports running models on multiple hardware platforms efficiently.
BONNX Runtime only supports CPU inference, not GPU.
CONNX Runtime requires rewriting the model in a new language.
DONNX Runtime cannot optimize model execution speed.
Attempts:
2 left
💡 Hint
Think about hardware compatibility and performance.
Hyperparameter
advanced
1:30remaining
Batch size effect on ONNX Runtime inference
If you increase the batch size of input data when running inference with ONNX Runtime, which of the following is true?
AONNX Runtime automatically splits batches into size 1 internally.
BInference latency per batch decreases and throughput decreases.
CBatch size has no effect on inference speed or throughput.
DInference latency per batch increases but throughput (samples per second) may improve.
Attempts:
2 left
💡 Hint
Think about how processing more samples at once affects speed.
🔧 Debug
advanced
1:30remaining
ONNX Runtime inference error diagnosis
You run ONNX Runtime inference with input data shape (1, 5) but the model expects input shape (1, 4). What error will ONNX Runtime most likely raise?
Aonnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: Unexpected input shape
BZeroDivisionError: division by zero
CFileNotFoundError: model file not found
DTypeError: unsupported operand type(s) for +: 'int' and 'str'
Attempts:
2 left
💡 Hint
Consider what happens if input shape does not match model expectation.
Metrics
expert
2:00remaining
Evaluating ONNX Runtime inference speed
You measure ONNX Runtime inference time for 1000 samples with batch size 10 and get 2 seconds total. What is the approximate throughput in samples per second?
A5000 samples/second
B500 samples/second
C50 samples/second
D5 samples/second
Attempts:
2 left
💡 Hint
Throughput = total samples / total time.