Bird
Raised Fist0
PyTorchml~20 mins

ONNX export in PyTorch - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
ONNX Export Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
ONNX export output shape verification
Consider the following PyTorch model and ONNX export code. What will be the shape of the output tensor when running the ONNX model with the given input shape?
PyTorch
import torch
import torch.nn as nn
import onnx

class SimpleModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(10, 5)
    def forward(self, x):
        return self.linear(x)

model = SimpleModel()
model.eval()
x = torch.randn(3, 10)

# Export to ONNX
onnx_path = "simple_model.onnx"
torch.onnx.export(model, x, onnx_path, input_names=["input"], output_names=["output"], opset_version=11)

# Assume we load and run the ONNX model with input shape (3, 10)
# What is the shape of the output tensor?
A(3, 5)
B(10, 5)
C(3, 10)
D(5, 3)
Attempts:
2 left
💡 Hint
Think about how the linear layer transforms the input shape.
Model Choice
intermediate
1:30remaining
Choosing correct opset version for ONNX export
You want to export a PyTorch model using torch.onnx.export. Which opset_version should you choose to ensure compatibility with most ONNX runtimes and support for common operators?
Aopset_version=7
Bopset_version=11
Copset_version=15
Dopset_version=9
Attempts:
2 left
💡 Hint
Check the default recommended opset version in PyTorch documentation.
🔧 Debug
advanced
2:30remaining
ONNX export error due to dynamic axes
You try to export a PyTorch model to ONNX with dynamic batch size using this code: torch.onnx.export(model, x, "model.onnx", dynamic_axes={"input": {0: "batch_size"}}) But you get an error about missing output dynamic axes. What is the most likely cause?
AYou must specify dynamic axes for both input and output tensors.
BThe model does not support dynamic batch size.
CYou forgot to set opset_version=15.
DYou need to convert the model to eval mode before export.
Attempts:
2 left
💡 Hint
Dynamic axes must be declared for all tensors that vary in size.
Metrics
advanced
2:00remaining
Verifying ONNX model correctness with output comparison
After exporting a PyTorch model to ONNX, you want to verify the ONNX model produces the same output as the PyTorch model for the same input. Which metric is best to check this?
ACross-entropy loss between PyTorch and ONNX outputs
BAccuracy score between PyTorch and ONNX outputs
CMean squared error (MSE) between PyTorch and ONNX outputs
DConfusion matrix of PyTorch and ONNX outputs
Attempts:
2 left
💡 Hint
You want to measure numerical closeness of outputs.
🧠 Conceptual
expert
3:00remaining
Understanding ONNX export limitations with control flow
Which of the following statements about exporting PyTorch models with control flow (like loops or conditionals) to ONNX is TRUE?
AControl flow must be removed from the model before exporting to ONNX.
BONNX export can handle control flow only if it is traced, not scripted.
CONNX supports all Python control flow constructs natively during export.
DONNX supports control flow if the model is scripted with torch.jit.script before export.
Attempts:
2 left
💡 Hint
Think about how PyTorch JIT scripting helps ONNX export.

Practice

(1/5)
1. What is the main purpose of exporting a PyTorch model to ONNX format?
easy
A. To save the model in a universal format for sharing and deployment
B. To train the model faster on GPUs
C. To convert the model into a TensorFlow model automatically
D. To visualize the model architecture in PyTorch

Solution

  1. Step 1: Understand ONNX export purpose

    ONNX is designed to save models in a format that can be used across different frameworks and platforms.
  2. Step 2: Compare options

    Only To save the model in a universal format for sharing and deployment correctly describes the universal sharing and deployment purpose of ONNX export.
  3. Final Answer:

    To save the model in a universal format for sharing and deployment -> Option A
  4. Quick Check:

    ONNX export = universal format [OK]
Hint: ONNX = share and deploy models universally [OK]
Common Mistakes:
  • Confusing ONNX export with training speedup
  • Thinking ONNX converts models to TensorFlow automatically
  • Assuming ONNX export is for visualization only
2. Which of the following is the correct way to prepare a PyTorch model for ONNX export?
easy
A. Call model.train() before export
B. Export without setting any input
C. Use a dummy input tensor matching the model input shape
D. Use model.eval() after export

Solution

  1. Step 1: Identify preparation steps for ONNX export

    Model should be in evaluation mode and a dummy input tensor matching input shape is needed for tracing.
  2. Step 2: Evaluate options

    Use a dummy input tensor matching the model input shape correctly states the use of a dummy input tensor. Call model.train() before export is wrong because model.train() is for training mode. Export without setting any input misses input, and Use model.eval() after export is incorrect order.
  3. Final Answer:

    Use a dummy input tensor matching the model input shape -> Option C
  4. Quick Check:

    Dummy input needed = Use a dummy input tensor matching the model input shape [OK]
Hint: Always use dummy input tensor before export [OK]
Common Mistakes:
  • Forgetting to set model.eval() before export
  • Not providing dummy input tensor
  • Calling model.train() instead of eval()
3. Given the code below, what will be the output of print(onnx_model.graph.input[0].name) after export?
import torch
import torch.nn as nn

class SimpleModel(nn.Module):
    def forward(self, x):
        return x * 2

model = SimpleModel()
model.eval()
dummy_input = torch.randn(1, 3)
torch.onnx.export(model, dummy_input, "model.onnx", input_names=["input_tensor"])
import onnx
onnx_model = onnx.load("model.onnx")
print(onnx_model.graph.input[0].name)
medium
A. x
B. input_tensor
C. input0
D. data

Solution

  1. Step 1: Check input_names parameter in export

    The export call sets input_names=["input_tensor"], so the input name in ONNX graph should be "input_tensor".
  2. Step 2: Confirm printed input name

    Loading the ONNX model and printing the first input name will output "input_tensor" as set.
  3. Final Answer:

    input_tensor -> Option B
  4. Quick Check:

    input_names param = input_tensor [OK]
Hint: input_names param sets ONNX input name [OK]
Common Mistakes:
  • Assuming default input name like 'input0'
  • Confusing PyTorch variable name with ONNX input name
  • Not setting input_names and expecting custom name
4. You try to export a PyTorch model to ONNX but get an error: RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same. What is the best fix?
medium
A. Export without specifying input_names
B. Use model.train() instead of model.eval()
C. Remove dummy input tensor
D. Move both the model and the dummy input to CPU before export

Solution

  1. Step 1: Understand the error cause

    The error means model weights are on CPU but input tensor is on GPU (cuda), causing type mismatch.
  2. Step 2: Fix by aligning device

    Moving both the model and the dummy input to CPU ensures both are on the same device (CPU), fixing the mismatch.
  3. Final Answer:

    Move both the model and the dummy input to CPU before export -> Option D
  4. Quick Check:

    Device mismatch fix = move model and input to CPU [OK]
Hint: Ensure model and input are on same device before export [OK]
Common Mistakes:
  • Ignoring device mismatch and exporting anyway
  • Switching to train mode instead of fixing device
  • Removing dummy input tensor causing other errors
5. You want to export a PyTorch model to ONNX with dynamic batch size support. Which argument should you add to torch.onnx.export to enable this?
hard
A. dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}
B. enable_dynamic_batch=True
C. set_dynamic=True
D. dynamic_batch=True

Solution

  1. Step 1: Identify how to specify dynamic axes in ONNX export

    PyTorch ONNX export uses the dynamic_axes argument to mark dimensions as dynamic, e.g., batch size dimension 0.
  2. Step 2: Check options for dynamic batch size

    Only dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}} correctly uses dynamic_axes with dictionary specifying batch dimension 0 for input and output.
  3. Final Answer:

    dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}} -> Option A
  4. Quick Check:

    Dynamic batch size = dynamic_axes param [OK]
Hint: Use dynamic_axes dict to set dynamic batch size [OK]
Common Mistakes:
  • Using nonexistent parameters like enable_dynamic_batch
  • Forgetting to specify dynamic axes for output
  • Assuming batch size is dynamic by default