Zheng-Bicheng commented on PR #16651:
URL: https://github.com/apache/tvm/pull/16651#issuecomment-1968519462

   @jiangjiajun 我添加了对PaddleSlim量化模型的支持,但是TVM在这方面好像是存在问题的。
   
   ## Paddle测试
   
   我使用了PaddleSlim训练的MobileNetV1_QAT进行测试,测试代码为:
   
   ```python
   import paddle
   import tvm
   from tvm import relay
   from tvm.contrib import graph_executor
   import numpy as np
   
   log_file = "tune.json"
   if __name__ == "__main__":
       input_shape = [1, 3, 224, 224]
       input_name = "inputs"
   
       paddle.enable_static()
       prefix = "MobileNetV1_QAT/inference"
       params_file_path = prefix + ".pdiparams"
       exe = paddle.static.Executor(paddle.CPUPlace())
       prog, feed_target_names, fetch_targets = 
paddle.static.load_inference_model(prefix, exe)
       # build
       mod, params = relay.frontend.from_paddle(prog, shape_dict={input_name: 
input_shape})
   
       with tvm.transform.PassContext(opt_level=5):
           lib = relay.build(mod, target="llvm", params=params)
       # create input data
       input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)
   
       # tvm inference
       ctx = tvm.cpu()
       tvm_model = graph_executor.GraphModule(lib['default'](ctx))
       tvm_model.set_input(input_name, input_data)
       tvm_model.run()
       tvm_output = tvm_model.get_output(0).asnumpy()
   
       # paddle inference
       paddle_output, = exe.run(prog, feed={feed_target_names[0]: input_data}, 
fetch_list=fetch_targets)
       print(np.argmax(tvm_output[0]), np.argmax(paddle_output[0]))
       np.testing.assert_allclose(tvm_output[0], paddle_output[0], rtol=1e-5, 
atol=1e-5)
   ```
   
   发现无法通过测试,误差如下:
   
   ```text
   AssertionError: 
   Not equal to tolerance rtol=1e-05, atol=1e-05
   
   Mismatched elements: 5 / 1000 (0.5%)
   Max absolute difference: 0.02359476
   Max relative difference: 1.
    x: array([0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,
          0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,
          0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,...
    y: array([0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,
          0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,
          0.      , 0.      , 0.      , 0.      , 0.      , 0.      ,...
   ```
   
   ## ONNX测试
   
   
为了验证是否是TVM和Paddle框架的推理机制不同导致的这个问题,我补充测试了ONNX框架,输入模型是Paddle2ONNX导出的同一个模型,测试代码如下:
   
   ```python
   import tvm
   from tvm import relay
   from tvm.contrib import graph_executor
   import numpy as np
   import onnx
   import onnxruntime as rt
   
   onnx_model_path = "MobileNetV1_QAT/inference.onnx"
   log_file = "tune.json"
   if __name__ == "__main__":
       input_shape = [1, 3, 224, 224]
       input_name = "inputs"
   
       # build
       onnx_model = onnx.load_model(onnx_model_path)
       mod, params = relay.frontend.from_onnx(onnx_model, shape={input_name: 
input_shape})
   
       with tvm.transform.PassContext(opt_level=5):
           lib = relay.build(mod, target="llvm", params=params)
       # create input data
       input_data = np.random.randn(1, 3, 224, 224).astype(np.float32)
   
       # tvm inference
       ctx = tvm.cpu()
       tvm_model = graph_executor.GraphModule(lib['default'](ctx))
       tvm_model.set_input(input_name, input_data)
       tvm_model.run()
       tvm_output = tvm_model.get_output(0).asnumpy()
   
       sess = rt.InferenceSession(onnx_model_path,None)
       input_name = sess.get_inputs()[0].name
       out_name = sess.get_outputs()[0].name
       onnx_output = sess.run([out_name], {input_name:input_data})[0]
   
       print(np.max(tvm_output[0] - onnx_output[0]))
       print(np.argmax(tvm_output[0] - onnx_output[0]))
       np.testing.assert_allclose(tvm_output[0], onnx_output[0], rtol=1e-5, 
atol=1e-5)
   ```
   
   发现仍然通不过测试,且误差非常大
   
   ```text
   Mismatched elements: 270 / 1000 (27%)
   Max absolute difference: 0.01025282
   Max relative difference: 0.52028346
    x: array([6.539907e-06, 6.727985e-05, 5.355392e-05, 4.267169e-06,
          1.363640e-04, 1.172559e-04, 1.836461e-04, 6.608244e-06,
          7.928120e-06, 3.821332e-06, 1.304903e-05, 4.503604e-05,...
    y: array([6.145719e-06, 7.556988e-05, 5.032599e-05, 4.009968e-06,
          1.281447e-04, 1.101883e-04, 1.725770e-04, 6.209937e-06,
          7.450255e-06, 4.292187e-06, 1.465689e-05, 4.232152e-05,...
   ```


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

Reply via email to