ALinrunrun opened a new issue, #19563:
URL: https://github.com/apache/tvm/issues/19563

   ### Expected behavior
   
   TVM Relax should execute ONNX `Asin` and `Acos` consistently with ONNX 
Runtime for normal finite float32 inputs.
   
   For mid-range inputs such as `0.32`, `0.42`, and `0.47`, TVM should produce 
results close to ONNX Runtime / libm.
   
   ### Actual behavior
   
   TVM Relax shows noticeable precision loss for both `Asin` and `Acos`:
   
   ```
   -- Asin --
     x=0.32  ORT=0.32572949  TVM=0.32559016  libm=0.32572948  err=-0.0001393
     x=0.42  ORT=0.4334453   TVM=0.43285862  libm=0.43344531  err=-0.0005867
     x=0.47  ORT=0.48929077  TVM=0.48820966  libm=0.48929078  err=-0.001081
   
   -- Acos --
     x=0.32  ORT=1.2450669   TVM=1.2452062   libm=1.2450668   err=+0.0001394
     x=0.42  ORT=1.137351    TVM=1.1379378   libm=1.137351    err=+0.0005868
     x=0.47  ORT=1.0815055   TVM=1.0825868   libm=1.0815056   err=+0.001081
   ```
   
   The discrepancy appears when importing ONNX Asin and Acos models through the 
Relax ONNX frontend and compiling them for the llvm target.
   
   ### Environment
   
   TVM: 0.14 environment / Relax ONNX frontend
   ONNX Runtime: 1.23
   Python: 3.11
   Target: llvm
   OS: Linux
   
   ### Steps to reproduce
   
   ```
   import warnings
   
   warnings.filterwarnings("ignore")
   
   import math
   import numpy as np
   import onnxruntime as ort
   import tvm
   from onnx import TensorProto, helper
   from tvm import relax
   from tvm.relax.frontend.onnx import from_onnx
   
   
   def build_model(op):
       node = helper.make_node(op, ["x"], ["y"])
   
       graph = helper.make_graph(
           [node],
           "g",
           [helper.make_tensor_value_info("x", TensorProto.FLOAT, [3])],
           [helper.make_tensor_value_info("y", TensorProto.FLOAT, [3])],
       )
   
       model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 
20)])
       model.ir_version = 9
       return model
   
   
   x = np.array([0.32, 0.42, 0.47], dtype=np.float32)
   
   for op, fn in (("Asin", math.asin), ("Acos", math.acos)):
       model = build_model(op)
   
       ort_out = ort.InferenceSession(
           model.SerializeToString(),
           providers=["CPUExecutionProvider"],
       ).run(None, {"x": x})[0]
   
       mod = from_onnx(model)
   
       with tvm.transform.PassContext(opt_level=3):
           ex = tvm.compile(mod, target=tvm.target.Target("llvm"))
   
       vm = relax.VirtualMachine(ex, tvm.cpu())
   
       out = vm["main"](tvm.runtime.tensor(x, tvm.cpu()))
       tvm_out = (out[0] if isinstance(out, (list, tuple)) else out).numpy()
   
       print(f"-- {op} --")
   
       for i, xv in enumerate(x):
           libm = fn(float(xv))
           err = tvm_out[i] - libm
           print(
               f"  x={xv:.2f}  "
               f"ORT={ort_out[i]:.8g}  "
               f"TVM={tvm_out[i]:.8g}  "
               f"libm={libm:.8g}  "
               f"err={err:+.4g}"
           )
   ```
   
   ### 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]

Reply via email to