Skip to content

Commit

Permalink
Add test for grid_sample and floor op (#953)
Browse files Browse the repository at this point in the history
Test cases for grid sample op and floor op has been added.
Assertion from tm.py has been removed to support greater than 1 batch
size inputs for grid_sample op.
  • Loading branch information
meenakshiramanathan1 authored Jan 9, 2025
1 parent 2542e33 commit d7d006e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 0 additions & 2 deletions forge/forge/op/eval/forge/tm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,8 +1028,6 @@ def decompose(type, attr, dc, inputs):
# by adding the size on that dimension
stop = act.shape[dim] + stop

assert dim != -4, "No support for indexing on dimension -4 (w)"

is_one_dim = len(act.shape) == 1
if is_one_dim:
# If input is a one-dimensional tensor, reshape it to a 2D tensor with one dimension equal to 1
Expand Down
60 changes: 60 additions & 0 deletions forge/test/mlir/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest
import torch
from torch import nn
import torch.nn.functional as F

import forge
from forge.tensor import to_forge_tensors
Expand Down Expand Up @@ -2017,3 +2018,62 @@ def forward(self, x):
compiled_model = forge.compile(framework_model, sample_inputs=inputs)

verify(inputs, framework_model, compiled_model)


@pytest.mark.xfail(
reason="RuntimeError: Found Unsupported operations while lowering from TTForge to TTIR in forward graph - adv_index"
)
@pytest.mark.parametrize(
"img, grid",
[
((1, 2, 4, 4), (1, 6, 2, 2)),
((1, 32, 50, 50), (1, 2500, 4, 2)),
((1, 3, 8, 8), (1, 3, 3, 2)),
((1, 3, 16, 16), (1, 8, 8, 2)),
((5, 2, 10, 10), (5, 12, 3, 2)),
((3, 8, 32, 32), (3, 25, 4, 2)),
],
)
@pytest.mark.parametrize("align_corners", [True, False])
def test_grid_sample(img, grid, align_corners, test_device):
class GridSampleModule(nn.Module):
def __init__(self, interpolation="bilinear", align_corners=align_corners):
super(GridSampleModule, self).__init__()
self.interpolation = interpolation
self.align_corners = align_corners

def forward(self, img, grid):
output = F.grid_sample(img, grid, mode=self.interpolation, align_corners=align_corners)
return output

# TO-DO: Support for nearest interpolation mode is yet to be added
model = GridSampleModule(interpolation="bilinear", align_corners=align_corners)
model.eval()
img = torch.randn(img)
grid = torch.randn(grid)
output = model(img, grid)
compiled_model = forge.compile(model, sample_inputs=[img, grid], module_name="grid_sample")


@pytest.mark.xfail(reason="RuntimeError: BinaryOpType cannot be mapped to BcastOpMath")
@pytest.mark.parametrize(
"input_data",
[
torch.tensor([-0.8166, 1.5308, -0.2530, -0.2091]),
torch.tensor([-3.7, -1.2, 0.0, 1.5, 3.9]),
torch.tensor([1.0, 2.0, -1.0, -2.0]),
torch.tensor([-12345.678, 12345.678, -0.999, 0.999, 3.14159, -3.14159]),
],
)
def test_floor(input_data):
class Floor(nn.Module):
def __init__(self):
super().__init__()

def forward(self, a):
return torch.floor(a)

framework_model = Floor()
compiled_model = forge.compile(framework_model, sample_inputs=[input_data], module_name="floor")

verify([input_data], framework_model, compiled_model)
2 changes: 1 addition & 1 deletion third_party/tvm

0 comments on commit d7d006e

Please sign in to comment.