onnxruntime: [Performance] nearest neighbor Resize operator is significantly slower than pytorch for 3D tensors
Describe the issue
I have a model that I’d like to run using onnxruntime to decrease the install size. Here’s a table comparing the total time of ONNX and pytorch when running different amount of inference (time in seconds)
| 1 | 5 | 10 | 50 | 100 | |
|---|---|---|---|---|---|
| pytorch | 0.45 | 0.75 | 1.11 | 3.85 | 7.17 |
| onnxruntime | 0.26 | 0.79 | 1.51 | 6.98 | 13.87 |
To me it seems like while the first inference of pytorch is slow, sequential once are much faster compared to onnx. I believe this has to do with cudnn plans not being cached. When I digged into the pytorch code I found that the plan for convultion layers is being cached. In the run_single_conv function in Conv_v8.cpp, a function benchmark_cache.find(key); is executed which will search the cache to see if a convoltion plan with the same parameters has already been calculated. If not, it generates the plan and adds it to the cache. I was wondering if onnxruntime has something similar in place since I couldn’t find this.
To reproduce
I convert the model like this:
import torch
input_data = torch.randn((1, 1, 96, 96, 96)).cuda().half()
torch.onnx.export(
model, # model being run
input_data, # model input (or a tuple for multiple inputs)
"model.onnx", # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=11, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=["input"], # the model's input names
output_names=["output"], # the model's output names
keep_initializers_as_inputs=False,
)
And run inference as followes:
import onnxruntime
import numpy as np
import time
NUMBER: int = 1
opts = onnxruntime.SessionOptions()
# opts.enable_profiling = True
opts.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_EXTENDED
# opts.intra_op_num_threads = 1
ort_session = onnxruntime.InferenceSession(
"model.onnx",
opts,
providers=[
(
"CUDAExecutionProvider",
{
"device_id": 0,
# "arena_extend_strategy": "kNextPowerOfTwo",
# "do_copy_in_default_stream": False,
"cudnn_conv_use_max_workspace": "1",
# "gpu_mem_limit": 4 * 1024 * 1024 * 1024,
# "cudnn_conv1d_pad_to_nc1d": "1",
"cudnn_conv_algo_search": "HEURISTIC",
},
),
"CPUExecutionProvider",
],
)
def get_data():
return np.array(np.random.rand(1, 1, 96, 96, 96) * 20, dtype=np.float16)
# compute ONNX Runtime output prediction
start = time.time()
io_binding = ort_session.io_binding()
for i in range(NUMBER):
io_binding.bind_cpu_input("input", get_data())
io_binding.bind_output("output")
ort_session.run_with_iobinding(io_binding)
io_binding.copy_outputs_to_cpu()[0]
print(f"ONNX: {time.time() - start}")
Sadly I’m not able to share the model but I’m using a 3D Unet model similar to this model.
Urgency
No response
Platform
Linux
OS Version
Ubuntu 18.04
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.13.1
ONNX Runtime API
Python
Architecture
X64
Execution Provider
CUDA
Execution Provider Library Version
No response
Model File
No response
Is this a quantized model?
Yes
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 24 (9 by maintainers)
@zhanghuanrong, is there update on this? ORT is about 6x slower based on the profiling in T4 GPU:
Profiling of Torch:
Profiling result of ORT:
Example script for profiling:
I think one solution is to use the aten kernel for 3d upsample. @yufenglee to track this work.
@SimonRelu, it is not implemented yet. It is still in back log due to resource limit, and I’ll update if there is progress.
I did some debugging; it uses block=14 and threads=32 for the input, which seems not enough. Let me check aten::upsample_nearest3d to see whether we can leverage similar partition logic.
Update: The partitions of 14 * 32 are for mapping only. For resize, it uses 442368 * 256 threads, so it is reasonable.
@SimonRelu, sorry for late response. I am looking into the issue and will let you know once there is progress.