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/04/18 10:11:00 UTC

[I] [Bug] [FRONTEND][ONNX] failed to import TopK operator when K is a constant tensor: ValueError: TopK k must be a constant [tvm]

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

   ### Expected behavior
   
   The onnx frontend should import the model correctly.
   
   ### Actual behavior
   
   When importing the following onnx model, tvm crashes as follows:
   
   ![Image](https://github.com/user-attachments/assets/93dd0469-43c3-44d8-9bfe-5039050e6b5c)
   ```c
   Traceback (most recent call last):
     File "/home/carla/Documents/test/test.py", line 189, in <module>
       main()
     File "/home/carla/Documents/test/test.py", line 178, in main
       check_correctness(onnx_model)
     File "/home/carla/Documents/test/test.py", line 104, in check_correctness
       tvm_model = from_onnx(model, opset=opset, keep_params_in_input=True)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/carla/Documents/tvm/python/tvm/relax/frontend/onnx/onnx_frontend.py", line 3690, in from_onnx
       return g.from_onnx(graph, opset)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/carla/Documents/tvm/python/tvm/relax/frontend/onnx/onnx_frontend.py", line 3321, in from_onnx
       self._construct_nodes(graph)
     File "/home/carla/Documents/tvm/python/tvm/relax/frontend/onnx/onnx_frontend.py", line 3496, in _construct_nodes
       op = self._convert_operator(op_name, inputs, attr, self.opset)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/carla/Documents/tvm/python/tvm/relax/frontend/onnx/onnx_frontend.py", line 3596, in _convert_operator
       sym = op_function(self.bb, inputs, attrs, [self._nodes, self._params])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/home/carla/Documents/tvm/python/tvm/relax/frontend/onnx/onnx_frontend.py", line 2706, in _impl_v11
       raise ValueError("TopK k must be a constant")
   ValueError: TopK k must be a constant
   [17:35:01] /home/carla/Documents/tvm/src/relax/ir/block_builder.cc:64: Warning: BlockBuilder destroyed with remaining blocks!
   ```
   The attribute K of TopK should be a constant. In this model, K is a constant tensor, which meets the constraint of TopK. Thus, this may be a bug of the onnx frontend in TVM.
   
   ### Environment
   
   OS: Ubuntu 20.04
   TVM: 0.21.dev0(c00f52a70)
   
   ### Steps to reproduce
   
   This bug can be reproduced by the following code with the model in the attachment. As shown in the code, the model can be executed by onnxruntime.
   
   ```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("a157.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)
           
       # Convert the onnx model into relax through the onnx importer.
       tvm_model = from_onnx(onnx_model, keep_params_in_input=True)
      
   if __name__ == "__main__":
       
       main()
   ```
   
   [testcase.zip](https://github.com/user-attachments/files/19811321/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] [FRONTEND][ONNX] failed to import TopK operator when K is a constant tensor: ValueError: TopK k must be a constant [tvm]

Posted by "coffezhou (via GitHub)" <gi...@apache.org>.
coffezhou closed issue #17856: [Bug]  [FRONTEND][ONNX] failed to import TopK operator when K is a constant tensor: ValueError: TopK k must be a constant
URL: https://github.com/apache/tvm/issues/17856


-- 
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] [FRONTEND][ONNX] failed to import TopK operator when K is a constant tensor: ValueError: TopK k must be a constant [tvm]

Posted by "erdayang (via GitHub)" <gi...@apache.org>.
erdayang commented on issue #17856:
URL: https://github.com/apache/tvm/issues/17856#issuecomment-2830697268

   I have the same confusion. TVM's support for ONNX feels quite immature. I wonder if its support for other frontends is the same.


-- 
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] [FRONTEND][ONNX] failed to import TopK operator when K is a constant tensor: ValueError: TopK k must be a constant [tvm]

Posted by "coffezhou (via GitHub)" <gi...@apache.org>.
coffezhou commented on issue #17856:
URL: https://github.com/apache/tvm/issues/17856#issuecomment-2930022215

   This issue is caused by the keep_params_in_input parameter. 
   If keep_params_in_input=True, parameters will be treated as input variables. If false, parameters are treated as constant and folded directly into the graph. 
   In this issue, for the TopK operator, if keep_params_in_input=True, the input 'k' cannot be treated as a constant.


-- 
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