Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map ops from FFE emit of TTIR to MLIR if it is supported #549

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions forge/csrc/passes/lower_to_mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ class MLIRGenerator
lowering_handler_map["concatenate"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::ConcatOp>;
lowering_handler_map["sigmoid"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::SigmoidOp>;
lowering_handler_map["max_pool2d"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::MaxPool2dOp>;
lowering_handler_map["abs"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::AbsOp>;
lowering_handler_map["exp"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::ExpOp>;
lowering_handler_map["maximum"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::MaximumOp>;
lowering_handler_map["less"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::LessEqualOp>;
lowering_handler_map["greater"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::GreaterThanOp>;
lowering_handler_map["not_equal"] = &MLIRGenerator::emit_mlir_ttforge_op<mlir::tt::ttir::NotEqualOp>;
}
};
} // namespace
Expand Down
199 changes: 199 additions & 0 deletions forge/test/mlir/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,205 @@
from forge.op.eval.common import compare_with_golden_pcc, compare_with_golden
from forge.tensor import to_forge_tensors, to_pt_tensors

shapes = [(1, 1, 256, 256), (1, 1, 1, 128), (1, 1, 1, 384), (1, 1, 32, 32), (1, 1, 6, 6), (1, 1, 29, 29)]


@pytest.mark.parametrize("shape", shapes)
def test_abs(shape):
class abs(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x):
return torch.abs(x)

inputs = [torch.rand(shape)]

framework_model = abs()
fw_out = framework_model(*inputs)

compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


shapes = [
(1, 128, 28, 28),
(1, 64, 28, 28),
(1, 256, 28, 28),
(1, 128, 14, 14),
(1, 128, 56, 56),
(1, 32, 64, 64),
(1, 512, 7, 7),
]


@pytest.mark.parametrize("shape", shapes)
def test_exp(shape):
class exp(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x):
return torch.exp(x)

inputs = [torch.rand(shape)]

framework_model = exp()
fw_out = framework_model(*inputs)

compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


shapes = [
((1, 12, 256, 256), (1,)),
((1, 16, 256, 256), (1,)),
((1, 32, 256, 256), (1,)),
((1, 12, 32, 32), (1,)),
((1, 16, 32, 32), (1,)),
((1, 32, 32, 32), (1,)),
]


@pytest.mark.parametrize("shape_x, shape_y", shapes)
def test_maximum(shape_x, shape_y):
class maximum(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x, y):
return torch.maximum(x, y)

x = torch.rand(shape_x)
y = torch.rand(shape_y)

framework_model = maximum()
inputs = [x, y]

fw_out = framework_model(*inputs)
compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


shapes = [
((1, 128, 28, 28), (1, 128, 28, 28)),
((1, 64, 28, 28), (1, 64, 28, 28)),
((1, 256, 28, 28), (1, 256, 28, 28)),
((1, 128, 14, 14), (1, 128, 14, 14)),
((1, 128, 56, 56), (1, 128, 56, 56)),
((1, 32, 64, 64), (1, 32, 64, 64)),
((1, 512, 7, 7), (1, 512, 7, 7)),
((1, 32, 32, 32), (1, 32, 32, 32)),
]


@pytest.mark.parametrize("shape_x, shape_y", shapes)
def test_less(shape_x, shape_y):
class less(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x, y):
return torch.less(x, y)

x = torch.rand(shape_x)
y = torch.rand(shape_y)

framework_model = less()
inputs = [x, y]

fw_out = framework_model(*inputs)
compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


shapes = [
((1, 128, 28, 28), (1, 128, 28, 28)),
((1, 64, 28, 28), (1, 64, 28, 28)),
((1, 256, 28, 28), (1, 256, 28, 28)),
((1, 128, 14, 14), (1, 128, 14, 14)),
((1, 128, 56, 56), (1, 128, 56, 56)),
((1, 32, 64, 64), (1, 32, 64, 64)),
((1, 512, 7, 7), (1, 512, 7, 7)),
((1, 32, 32, 32), (1, 32, 32, 32)),
]


@pytest.mark.parametrize("shape_x, shape_y", shapes)
def test_greater_op(shape_x, shape_y):
class greater(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x, y):
return torch.greater(x, y)

x = torch.rand(shape_x)
y = torch.rand(shape_y)

framework_model = greater()
inputs = [x, y]

fw_out = framework_model(*inputs)
compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


shapes = [
((1, 128, 28, 28), (1, 128, 28, 28)),
((1, 64, 28, 28), (1, 64, 28, 28)),
((1, 256, 28, 28), (1, 256, 28, 28)),
((1, 128, 14, 14), (1, 128, 14, 14)),
((1, 128, 56, 56), (1, 128, 56, 56)),
((1, 32, 64, 64), (1, 32, 64, 64)),
((1, 512, 7, 7), (1, 512, 7, 7)),
((1, 32, 32, 32), (1, 32, 32, 32)),
]


@pytest.mark.parametrize("shape_x, shape_y", shapes)
def test_not_equal(shape_x, shape_y):
class not_equal(nn.Module):
def __init__(self):
super().__init__()

def forward(self, x, y):
return torch.ne(x, y)

x = torch.rand(shape_x)
y = torch.rand(shape_y)

framework_model = not_equal()
inputs = [x, y]

fw_out = framework_model(*inputs)
compiled_model = forge.compile(framework_model, sample_inputs=inputs)
co_out = compiled_model(*inputs)

co_out = [co.to("cpu") for co in co_out]
fw_out = [fw_out] if isinstance(fw_out, torch.Tensor) else fw_out
assert all([compare_with_golden_pcc(golden=fo, calculated=co, pcc=0.99) for fo, co in zip(fw_out, co_out)])


@pytest.mark.parametrize(
"batch_size, num_channels, height, width",
Expand Down
Loading