You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "coffezhou (via GitHub)" <gi...@apache.org> on 2025/05/21 02:41:27 UTC
[I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
coffezhou opened a new issue, #18003:
URL: https://github.com/apache/tvm/issues/18003
### Expected behavior
The output of the TVM CPU backend should be identical with those of onnxruntime and TVM CUDA backend.
### Actual behavior
For the following model:

when compile the model for the CPU target, the output is:
```c
[[[[False]]]]
```
However, the outputs of onnxruntime and TVM CUDA backend are as follows:
```c
[[[[ True]]]]
```
### Environment
OS: Ubuntu 20.04
TVM: 0.21.dev0 (3db71bb3a)
onnxruntime: 1.21.0
CUDA: 11.8
GPU: NVIDIA GeForce RTX 3080
### Steps to reproduce
This bug can be reproduced by the following code with the model in the attachment.
```python
import sys
import numpy as np
import onnx
import onnxruntime
import tvm
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx
import pickle
def main():
onnx_model = onnx.load("a2631.onnx")
shape_onnx_model = onnx.shape_inference.infer_shapes(onnx_model)
onnx.save(shape_onnx_model, '1111.onnx')
with open("inputs.pkl", "rb") as fp:
inputs = pickle.load(fp)
try:
ort_session = onnxruntime.InferenceSession(
onnx_model.SerializeToString(), providers=["CPUExecutionProvider"]
)
ort_output = ort_session.run([], inputs)
except Exception as e:
print(e)
sys.exit(1)
print("ONNXRuntime:\n", ort_output)
# Convert the onnx model into relax through the onnx importer.
tvm_model = from_onnx(onnx_model, keep_params_in_input=True)
# Convert operators for inference mode.
tvm_model = relax.transform.DecomposeOpsForInference()(tvm_model)
# Legalize any relax ops into tensorir.
tvm_model = relax.transform.LegalizeOps()(tvm_model)
# Separate model from parameters.
tvm_model, params = relax.frontend.detach_params(tvm_model)
# Prepare inputs.
input_list = [
inputs[key.name_hint] for key in tvm_model["main"].params if key.name_hint in inputs
]
if params:
input_list += params["main"]
# Compile the relax graph into a VM then run.
#----------------------cpu-----------------------
with tvm.transform.PassContext(opt_level=2):
target = tvm.target.Target("llvm", host="llvm")
relax_pipeline = relax.pipeline.get_default_pipeline(target)
ex = relax.build(tvm_model, target="llvm", relax_pipeline=relax_pipeline)
vm = relax.VirtualMachine(ex, tvm.cpu())
# Run model and check outputs.
vm.set_input("main", *input_list)
vm.invoke_stateful("main")
tvm_cpu_output = vm.get_outputs("main")
print("TVM-CPU:\n", tvm_cpu_output)
#----------------------cpu-----------------------
#----------------------cuda-----------------------
with tvm.target.Target("cuda"):
tvm_model = tvm.tir.transform.DefaultGPUSchedule()(tvm_model)
with tvm.transform.PassContext(opt_level=3):
ex = tvm.compile(tvm_model, target="cuda")
vm1 = relax.VirtualMachine(ex, tvm.cuda())
vm1.set_input("main", *input_list)
vm1.invoke_stateful("main")
tvm_gpu_output = vm1.get_outputs("main")
print("TVM-CUDA:\n", tvm_gpu_output)
#----------------------cuda-----------------------
if __name__ == "__main__":
main()
```
[testcase.zip](https://github.com/user-attachments/files/20359142/testcase.zip)
### Triage
* needs-triage
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
Posted by "coffezhou (via GitHub)" <gi...@apache.org>.
coffezhou commented on issue #18003:
URL: https://github.com/apache/tvm/issues/18003#issuecomment-2908734017
> > > The domain of acosf in CUDA is [-1, 1], but your test data contains many values outside this domain, which causes acosf to produce numerous nan values, resulting in incorrect results.
> >
> >
> > Thank you for your reply! I am confused why the inconsistent behavior of different backends is legal in TVM. Does there exist a way or document to find all inconsistent behaviors that are valid for different backends in TVM?
>
> I think TVM aims for compatibility and performance optimization, rather than absolute consistency across all backends. The inconsistent behaviors among backends are due to implementation details and hardware characteristics.
>
> Moreover, it is extremely difficult to enumerate all the inconsistent behaviors of different backends.
Thanks! In future work, I will carefully check the inconsistent behaviors of different backends. Hence, this issue could be closed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
Posted by "jikechao (via GitHub)" <gi...@apache.org>.
jikechao closed issue #18003: [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend
URL: https://github.com/apache/tvm/issues/18003
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
Posted by "eaten-cake (via GitHub)" <gi...@apache.org>.
eaten-cake commented on issue #18003:
URL: https://github.com/apache/tvm/issues/18003#issuecomment-2908001250
The domain of acosf in CUDA is [-1, 1], but your test data contains many values outside this domain, which causes acosf to produce numerous nan values, resulting in incorrect results.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
Posted by "eaten-cake (via GitHub)" <gi...@apache.org>.
eaten-cake commented on issue #18003:
URL: https://github.com/apache/tvm/issues/18003#issuecomment-2908580423
> > The domain of acosf in CUDA is [-1, 1], but your test data contains many values outside this domain, which causes acosf to produce numerous nan values, resulting in incorrect results.
>
> Thank you for your reply! I am confused why the inconsistent behavior of different backends is legal in TVM. Does there exist a way or document to find all inconsistent behaviors that are valid for different backends in TVM?
I think TVM aims for compatibility and performance optimization, rather than absolute consistency across all backends. The inconsistent behaviors among backends are due to implementation details and hardware characteristics.
Moreover, it is extremely difficult to enumerate all the inconsistent behaviors of different backends.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Bug] The output of the TVM CPU backend is not identical with those of onnxruntime and TVM CUDA backend [tvm]
Posted by "coffezhou (via GitHub)" <gi...@apache.org>.
coffezhou commented on issue #18003:
URL: https://github.com/apache/tvm/issues/18003#issuecomment-2908326298
> The domain of acosf in CUDA is [-1, 1], but your test data contains many values outside this domain, which causes acosf to produce numerous nan values, resulting in incorrect results.
Thank you for your reply! I am confused why the inconsistent behavior of different backends is legal in TVM. Does there exist a way or document to find all inconsistent behaviors that are valid for different backends in TVM?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org