delldu commented on a change in pull request #8443:
URL: https://github.com/apache/tvm/pull/8443#discussion_r670215799
##########
File path: tests/python/frontend/pytorch/test_forward.py
##########
@@ -3912,6 +3911,98 @@ def forward(self, x):
verify_model(Flip(axis=-1), input_data=input)
[email protected]_gpu
+def test_forward_im2col():
+ torch.set_grad_enabled(False)
+
+ class Im2col(Module):
+ def __init__(self, kernel_size, dilation, padding, stride):
+ super(Im2col, self).__init__()
+ self.kernel_size = (kernel_size, kernel_size)
+ self.dilation = (dilation, dilation)
+ self.padding = (padding, padding)
+ self.stride = (stride, stride)
+
+ def forward(self, x):
+ #
***********************************************************************************
+ #
+ # !!! DO NOT USE !!!
+ # F.unfold(x, kernel_size=3, dilation=1, padding=1, stride=1)
+ # for it broken TVM "if conditional expression" in torch script
mode
+ #
+ #
***********************************************************************************
+ return torch._C._nn.im2col(
+ x, self.kernel_size, self.dilation, self.padding, self.stride
+ )
+
+ input = torch.randn(2, 3, 32, 32)
+ verify_model(Im2col(5, 1, 1, 2), input_data=input)
+ verify_model(Im2col(3, 1, 2, 1), input_data=input)
+ verify_model(Im2col(5, 1, 2, 2), input_data=input)
+
+
[email protected]_gpu
+def test_forward_grid_sampler():
+ torch.set_grad_enabled(False)
+
+ class GridSampler(Module):
+ def __init__(self, output_h, output_w):
+ super(GridSampler, self).__init__()
+ self.output_h = output_h
+ self.output_w = output_w
+
+ # normalize to [-1.0, 1.0]
+ h = torch.arange(0, output_h) / (output_h - 1.0) * 2.0 - 1.0
+ w = torch.arange(0, output_w) / (output_w - 1.0) * 2.0 - 1.0
+ grid = torch.zeros(output_h, output_w, 2)
+ grid[:, :, 0] = w.unsqueeze(0).repeat(output_h, 1)
+ grid[:, :, 1] = h.unsqueeze(0).repeat(output_w, 1).transpose(0, 1)
+ self.grid = grid.unsqueeze(0)
+
+ def forward(self, input):
+ batch = input.size(0)
+ grid = self.grid.repeat(batch, 1, 1, 1).to(input.device)
+
+ # Torch grid_sample default: mode='bilinear',
padding_mode='zeros', align_corners=False
+ # tvm seems align corners as True
+
+ #
***********************************************************************************
+ #
+ # !!! DO NOT USE !!!
+ # F.grid_sample(input, grid, mode='bilinear',
padding_mode='zeros', align_corners=True)
+ # for it broken TVM "if conditional expression" in torch script
mode
Review comment:
As F.unfold, F.grid_sample also check parameter with if condition. If
you call F.grid_sample, you will get exceptions. This can be reproduced with
"F.grid_sample" test. Please kindly reference:
[F.grid_sample](https://pytorch.org/docs/stable/_modules/torch/nn/functional.html#grid_sample
)
--
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]