Bird
Raised Fist0
PyTorchml~20 mins

ONNX Runtime inference in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using ONNX Runtime with a PyTorch model?
easy
A. To train the model with more data
B. To convert the model into a TensorFlow format
C. To run the model faster and on different devices
D. To visualize the model architecture

Solution

  1. Step 1: Understand ONNX Runtime's role

    ONNX Runtime is designed to run machine learning models efficiently across various hardware.
  2. Step 2: Compare options

    Training or converting to TensorFlow is not the main purpose; visualization is unrelated.
  3. Final Answer:

    To run the model faster and on different devices -> Option C
  4. Quick Check:

    ONNX Runtime = fast, cross-device inference [OK]
Hint: ONNX Runtime is for fast, flexible model running [OK]
Common Mistakes:
  • Confusing inference with training
  • Thinking ONNX Runtime converts models to other frameworks
  • Assuming it is for visualization
2. Which of the following is the correct way to load an ONNX model using ONNX Runtime in Python?
easy
A. session = onnxruntime.load_model('model.onnx')
B. session = onnx.load('model.onnx')
C. session = torch.load('model.onnx')
D. session = onnxruntime.InferenceSession('model.onnx')

Solution

  1. Step 1: Recall ONNX Runtime loading syntax

    ONNX Runtime uses InferenceSession to load ONNX models.
  2. Step 2: Check each option

    session = onnxruntime.InferenceSession('model.onnx') uses correct syntax. session = onnx.load('model.onnx') is for ONNX library, not runtime. session = torch.load('model.onnx') is PyTorch loading, not ONNX. session = onnxruntime.load_model('model.onnx') is invalid method.
  3. Final Answer:

    session = onnxruntime.InferenceSession('model.onnx') -> Option D
  4. Quick Check:

    InferenceSession loads ONNX models [OK]
Hint: Use InferenceSession to load ONNX models [OK]
Common Mistakes:
  • Using torch.load for ONNX files
  • Confusing onnx.load with onnxruntime
  • Trying non-existent load_model method
3. Given the following code snippet, what will be the output of print(output)?
import onnxruntime
import numpy as np

session = onnxruntime.InferenceSession('model.onnx')
input_name = session.get_inputs()[0].name
input_data = np.array([[1.0, 2.0, 3.0]], dtype=np.float32)
output = session.run(None, {input_name: input_data})
print(output)
medium
A. A list containing the model's prediction array
B. A numpy array of input_data
C. An error because input_data is not a tensor
D. None, because session.run returns nothing

Solution

  1. Step 1: Understand session.run output

    session.run returns a list of numpy arrays with model outputs.
  2. Step 2: Analyze code behavior

    Input is correctly prepared as numpy array; no error occurs; output is a list with prediction arrays.
  3. Final Answer:

    A list containing the model's prediction array -> Option A
  4. Quick Check:

    session.run returns list of outputs [OK]
Hint: session.run returns list of numpy arrays [OK]
Common Mistakes:
  • Expecting a single numpy array instead of list
  • Thinking input must be a PyTorch tensor
  • Assuming session.run returns None
4. You run ONNX Runtime inference but get an error: TypeError: expected np.ndarray (got list). What is the likely cause?
medium
A. Output is not captured correctly
B. Input data was passed as a Python list instead of a numpy array
C. ONNX Runtime session was not created
D. Model file is corrupted

Solution

  1. Step 1: Interpret the error message

    The error says input expected is numpy array but got list.
  2. Step 2: Identify cause

    Passing input as Python list instead of np.ndarray causes this error.
  3. Final Answer:

    Input data was passed as a Python list instead of a numpy array -> Option B
  4. Quick Check:

    Input must be numpy array, not list [OK]
Hint: Always convert inputs to numpy arrays before inference [OK]
Common Mistakes:
  • Passing Python lists directly as input
  • Assuming model file corruption causes this error
  • Ignoring session creation errors
5. You want to run inference on a batch of 5 images using ONNX Runtime. Your model expects input shape (batch_size, 3, 224, 224). Which code snippet correctly prepares the input and runs inference?
hard
A. import numpy as np batch_input = np.random.rand(5, 3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input})
B. batch_input = torch.rand(5, 3, 224, 224) output = session.run(None, {input_name: batch_input})
C. batch_input = np.random.rand(3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input})
D. batch_input = np.random.rand(5, 224, 224, 3).astype(np.float32) output = session.run(None, {input_name: batch_input})

Solution

  1. Step 1: Check input shape requirements

    Model expects (batch_size, 3, 224, 224) so batch size 5 means shape (5,3,224,224).
  2. Step 2: Validate each option's input shape and type

    import numpy as np batch_input = np.random.rand(5, 3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) uses correct shape and numpy float32 array. batch_input = torch.rand(5, 3, 224, 224) output = session.run(None, {input_name: batch_input}) uses torch tensor (not accepted). batch_input = np.random.rand(3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) misses batch dimension. batch_input = np.random.rand(5, 224, 224, 3).astype(np.float32) output = session.run(None, {input_name: batch_input}) swaps channel and width/height.
  3. Final Answer:

    import numpy as np batch_input = np.random.rand(5, 3, 224, 224).astype(np.float32) output = session.run(None, {input_name: batch_input}) -> Option A
  4. Quick Check:

    Correct shape and numpy array input [OK]
Hint: Match model input shape and use numpy float32 arrays [OK]
Common Mistakes:
  • Using torch tensors instead of numpy arrays
  • Incorrect input shape order
  • Missing batch dimension
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard