question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ONNX inference with dropout

See original GitHub issue

I have a model based on self-attention, I’d like to enable dropout in ONNX model inference.

But I found that when I set training=torch.onnx.TrainingMode.TRAINING,, and inference with onnxruntime, the model output are identical for each inference with the same input.

here is the code I used to export model:

    torch.onnx.export(model,  # model being run
                      onnx_input,  # model input (or a tuple for multiple inputs)
                      onnx_path,  # where to save the model (can be a file or file-like object)
                      training=torch.onnx.TrainingMode.TRAINING,  # export in Training mode
                      verbose=True,
                      export_params=True,  # store the trained parameter weights inside the model file
                      opset_version=12,  # the ONNX version to export the model to
                      do_constant_folding=False,  # whether to execute constant folding for optimization
                      input_names=input_names,  # the model's input names
                      output_names=output_names,  # the model's output names
                      dynamic_axes={
                          'speakers': {0: 'batch_size'},  # variable length axes
                          'phonemes': {0: 'batch_size', 1: 'seq_len'},
                          'src_lens': {0: 'batch_size'},
                          'output': {0: 'batch_size', 1: 'mel_len'},
                          'postnet_output': {0: 'batch_size', 1: 'mel_len'},
                          'p_predictions': {0: 'batch_size', 1: 'seq_len'},
                          'e_predictions': {0: 'batch_size', 1: 'seq_len'},
                          'log_d_predictions': {0: 'batch_size', 1: 'seq_len'},
                          'd_rounded': {0: 'batch_size', 1: 'seq_len'},
                          'src_masks': {0: 'batch_size', 1: 'seq_len'},
                          'mel_masks': {0: 'batch_size', 1: 'mel_len'},
                          'output_src_lens': {0: 'batch_size'},
                          'mel_lens': {0: 'batch_size'}
                      })
    print("done")

And actually, the output of the exported ONNX model is similar to Pytorch inference output with model.eval() mode, the output are roughly the same, with only small difference.

This is weird.

When I set training=torch.onnx.TrainingMode.EVAL, the exported ONNX model does not contain dropout layer, and when set training to TRAINING, the exported ONNX model does contain dropout layer, but generate same output each inference time,

It seems that the droput mask is fixed when I export ONNX model with TRAINING mode, or the dropout seed is fixed, how could I make the dropout seed not fixed in the exported ONNX model?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
leng-yuecommented, Jun 9, 2022

I found a better solution 😃

ort.InferenceSession("your_model", disabled_optimizers=["EliminateDropout"])
2reactions
weixsongcommented, Oct 12, 2021

Thanks for providing the model. Finally, I found that by default the Dropout node will be removed by ONNXRuntime’s optimizer: microsoft/onnxruntime#1202 since generally inference should not involve Dropout. onnxruntime.set_seed is used for Dropout with ORT training. If you really want Dropout to apply your model for ORT inference, you can try something like this to disable optimizer:

import onnxruntime as rt

sess_options = rt.SessionOptions()
# Set graph optimization level
sess_options.graph_optimization_level = rt.GraphOptimizationLevel.ORT_ENABLE_EXTENDED

session = rt.InferenceSession("your_model", sess_options)
output = session .run(None, model_input)

Then you should see Dropout will create different outputs. Thanks.

Reference: https://onnxruntime.ai/docs/performance/graph-optimizations.html

Thank you very much. Yes, this config solved my issue.

But I need to set with DISABLE:

sess_options.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_DISABLE_ALL

with this setup, the ORT generates different output each time. Great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Onnx Export with Dropout Layer = Training, rest eval
Hi there, I am implementing a bayesian neural network. For this I need the dropout layers to be active during inference while the...
Read more >
Graph optimizations - ONNX Runtime
Basic Graph Optimizations · Identity Elimination · Slice Elimination · Unsqueeze Elimination · Dropout Elimination.
Read more >
onnx/training - Gitter
Hi, I am working on onnx to tensorflow and updating the Dropout operator ... we want to use onnx to train the model...
Read more >
Cannot save dropout layer in a tensorflow model with tf2onnx ...
import tf2onnx import onnx from tensorflow.keras.layers import ... I want to use the dropout layer in training_mode during inference to use ...
Read more >
Running inference on MXNet/Gluon from an ONNX model
Open Neural Network Exchange (ONNX) provides an open source format for AI models. It defines an extensible computation graph model, as well as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found