[Bug][Relax][PyTorch] `grid_sample` returns the wrong shape and values after `from_exported_program`

Đang mở
#20,882 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch

Hướng nghiên cứu

Start with the PyTorch importer's _grid_sampler_2d converter, which is reached through from_exported_program, and compare its grid argument with what relax.op.image.grid_sample expects. Run the two reproduction cases from the issue: the 1x1x4x4 input with a 1x3x5x2 grid, and the 2x2 grid. Done means the TVM output shape and values match eager PyTorch in both cases.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

needs-triage type: bug
Expected behavior

For a grid of shape [N, H, W, 2], F.grid_sample returns [N, C, H, W]. The model converted with from_exported_program should give the same shape and values as eager.

Actual behavior

TVM returns the wrong shape, with no error or warning. With a 2 x 2 grid the shape happens to be right, but the values are wrong.

eager: torch.Size([1, 1, 3, 5])
tvm:   (1, 1, 5, 2)
eager: [[[[2.5, 4.5], [10.5, 12.5]]]]
tvm:   [[[[2.5, 10.5], [12.5, 10.5]]]]

Possibly _grid_sampler_2d passes PyTorch's [N, H, W, 2] grid directly, while relax.op.image.grid_sample works on [N, 2, H, W].

Environment
  • TVM: main @ 2bf0532 (0.28.dev0), built from source, target llvm
  • PyTorch: 2.12.0+cu130
  • Python: 3.12, Ubuntu x86_64
Steps to reproduce
import torch
import tvm
import tvm_tirx
from tvm import relax
from tvm.relax.frontend.torch import from_exported_program


class M(torch.nn.Module):
    def forward(self, x, grid):
        return torch.nn.functional.grid_sample(x, grid, align_corners=False)


x = torch.arange(16, dtype=torch.float32).reshape(1, 1, 4, 4)
grid = torch.zeros(1, 3, 5, 2)
print("eager:", M()(x, grid).shape)

ep = torch.export.export(M(), (x, grid))
mod = from_exported_program(ep)
ex = tvm.compile(mod, target="llvm")
vm = relax.VirtualMachine(ex, tvm_tirx.cpu())
out = vm["main"](tvm_tirx.runtime.tensor(x.numpy()), tvm_tirx.runtime.tensor(grid.numpy()))
print("tvm:  ", out[0].numpy().shape)

# A 2 x 2 grid has the right shape by accident, but the values are wrong
grid = torch.tensor([[[[-0.5, -0.5], [0.5, -0.5]], [[-0.5, 0.5], [0.5, 0.5]]]])
print("eager:", M()(x, grid).tolist())

ep = torch.export.export(M(), (x, grid))
mod = from_exported_program(ep)
ex = tvm.compile(mod, target="llvm")
vm = relax.VirtualMachine(ex, tvm_tirx.cpu())
out = vm["main"](tvm_tirx.runtime.tensor(x.numpy()), tvm_tirx.runtime.tensor(grid.numpy()))
print("tvm:  ", out[0].numpy().tolist())
Ngôn ngữ chính
Python
Star
13.9k
Fork
4k
Merge trung bình
8 giờ 5 phút
Pull request đã merge (30 ngày)
170

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của apache/tvm

Tất cả issue của apache/tvm

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.