You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "hxzd5568 (via GitHub)" <gi...@apache.org> on 2024/03/11 14:12:48 UTC

Re: [I] [Bug] [Relay] Wrong inference reusult about Pytorch model with dropout layer [tvm]

hxzd5568 commented on issue #15005:
URL: https://github.com/apache/tvm/issues/15005#issuecomment-1988540791

   The issue may be caused by PyTorch. The TVM is right. The dropout in the evalution mode should be set to be disabled. (see the design aim at https://pytorch.org/docs/stable/_modules/torch/nn/modules/dropout.html#Dropout2d)
   
   Dropout is designed to be only applied during training, so the output and the input should be the same. 
   And from the experiment, TVM models' output and input are the same.
   
   
   ```
   import torch
   from tvm import relay
   import tvm
   import numpy as np
   from torch.nn import Module
   
   input_data = torch.randn([5], dtype=torch.float64)
   class alpha_dropout(Module):
           def forward(self, *args):
               return torch.nn.functional.alpha_dropout(args[0], 0.2,training=True)
   m = alpha_dropout().float().eval()
   
   # TVM is right, but pytorch is wrong.
   
   torch_outputs = m(input_data)
   trace = torch.jit.trace(m, input_data)
   input_shapes = [('input0', torch.Size([5]))]
   
   mod, params = relay.frontend.from_pytorch(trace, input_shapes)
   with tvm.transform.PassContext(opt_level=3):
       exe = relay.create_executor('graph', mod=mod, params=params, device=tvm.device('llvm', 0), target='llvm').evaluate()
   
   input_tvm = {'input0': np.array(input_data, dtype='float64')}
   tvm_outputs = exe(**input_tvm).asnumpy()
   try:
       np.testing.assert_allclose(input_data, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the truth are the same')
   except:
       print('tvm & truth are diff')
        
   
   try:
       np.testing.assert_allclose(torch_outputs, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the torch result are the same')
   except:
       print('tvm & torch are diff')
   
   
   
   
   # =================== for functional.dropout =============
   
   class dropout1(Module):
           def forward(self, *args):
               return torch.nn.functional.dropout(args[0], 0.2)
   m = dropout1().float().eval()
   
   torch_outputs = m(input_data)
   trace = torch.jit.trace(m, input_data)
   input_shapes = [('input0', torch.Size([5]))]
   
   mod, params = relay.frontend.from_pytorch(trace, input_shapes)
   with tvm.transform.PassContext(opt_level=3):
       exe = relay.create_executor('graph', mod=mod, params=params, device=tvm.device('llvm', 0), target='llvm').evaluate()
   
   input_tvm = {'input0': np.array(input_data, dtype='float64')}
   tvm_outputs = exe(**input_tvm).asnumpy()
   try:
       np.testing.assert_allclose(input_data, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the truth are the same')
   except:
       print('tvm & truth are diff')
        
   
   try:
       np.testing.assert_allclose(torch_outputs, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the torch result are the same')
   except:
       print('tvm & torch are diff')
   
   
   # =================== disabling the 'training' option can solve the issue =============
       
   input_data = torch.randn([5], dtype=torch.float64)
   class alpha_dropout(Module):
           def forward(self, *args):
               return torch.nn.functional.alpha_dropout(args[0], 0.2,training=False)
   
   m = alpha_dropout().float().eval()
   torch_outputs = m(input_data)
   trace = torch.jit.trace(m, input_data)
   input_shapes = [('input0', torch.Size([5]))]
   
   mod, params = relay.frontend.from_pytorch(trace, input_shapes)
   with tvm.transform.PassContext(opt_level=3):
       exe = relay.create_executor('graph', mod=mod, params=params, device=tvm.device('llvm', 0), target='llvm').evaluate()
   
   input_tvm = {'input0': np.array(input_data, dtype='float64')}
   tvm_outputs = exe(**input_tvm).asnumpy()
   try:
       np.testing.assert_allclose(input_data, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the truth are the same')
   except:
       print('tvm & truth are diff')
        
   
   try:
       np.testing.assert_allclose(torch_outputs, tvm_outputs, rtol=1e-3, atol=1e-3)
       print('tvm result and the torch result are the same')
   except:
       print('tvm & torch are diff')
   
   ```


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