ALinrunrun opened a new issue, #19503:
URL: https://github.com/apache/tvm/issues/19503
### Expected behavior
TVM Relay should preserve IEEE-754 semantics for non-finite floating-point
values.
For the graph:
`Y = Mul(X, INF) - Mul(X, INF)`
where` INF` is a constant initializer filled with `+inf`, the result should
match ONNX Runtime. Since `inf - inf` produces `NaN`, the expected output is
all `NaN`.
### Actual behavior
TVM Relay produces zeros instead of NaN:
```
input: [1. 2. 3. 4.]
ORT : [nan nan nan nan]
TVM : [0. 0. 0. 0.]
max_abs: 1.0
```
This appears to happen because Relay simplifies the `a - a` pattern to zero,
which is not valid when `a` may contain non-finite floating-point values such
as `inf` or `NaN`.
### Environment
- TVM: 0.14 environment / Relay ONNX frontend
- ONNX Runtime: 1.23
- Python: 3.11
- Target: llvm
- OS: Linux
### Steps to reproduce
```
import numpy as np
import onnx
from onnx import TensorProto, helper
import onnxruntime as ort
import tvm
from tvm import relay
from tvm.contrib import graph_executor
def build_model():
x = helper.make_tensor_value_info("X", TensorProto.FLOAT, [4])
y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [4])
inf_const = helper.make_tensor(
"INF", TensorProto.FLOAT, [4], [float("inf")] * 4
)
nodes = [
helper.make_node("Mul", ["X", "INF"], ["xa"]),
helper.make_node("Sub", ["xa", "xa"], ["Y"]),
]
graph = helper.make_graph(nodes, "g", [x], [y], initializer=[inf_const])
return helper.make_model(graph, opset_imports=[helper.make_opsetid("",
13)])
x = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
model = build_model()
ort_sess = ort.InferenceSession(
model.SerializeToString(), providers=["CPUExecutionProvider"]
)
ort_out = ort_sess.run(None, {"X": x})[0]
mod, params = relay.frontend.from_onnx(model, shape={"X": list(x.shape)})
with tvm.transform.PassContext(opt_level=3):
lib = relay.build(mod, target="llvm", params=params)
gm = graph_executor.GraphModule(lib["default"](tvm.cpu()))
gm.set_input("X", x)
gm.run()
tvm_out = gm.get_output(0).numpy()
print("input:", x)
print("ORT :", ort_out)
print("TVM :", tvm_out)
nan_mismatch = np.isnan(ort_out) ^ np.isnan(tvm_out)
finite_diff = np.abs(
np.nan_to_num(ort_out, nan=0.0).astype(np.float64)
- np.nan_to_num(tvm_out, nan=0.0).astype(np.float64)
)
finite_diff[nan_mismatch] = 1.0
print("max_abs:", finite_diff.max())
```
### 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]