Skip to content

Commit 7b94189

Browse files
authored
[E2E] add nan case in elementwise comparison e2e tests (#2575)
1 parent 5eae0ad commit 7b94189

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

lib/Conversion/TorchToLinalg/Uncategorized.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static Value createComparisonTemplate(OpBuilder &b, Location loc, Type type,
5757

5858
static Value createGreaterThan(OpBuilder &b, Location loc, Type elementalType,
5959
Value lhs, Value rhs) {
60-
return createComparisonTemplate<arith::CmpFPredicate::UGT,
60+
return createComparisonTemplate<arith::CmpFPredicate::OGT,
6161
arith::CmpIPredicate::ugt,
6262
arith::CmpIPredicate::sgt>(
6363
b, loc, elementalType, lhs, rhs);
@@ -66,23 +66,23 @@ static Value createGreaterThan(OpBuilder &b, Location loc, Type elementalType,
6666
static Value createGreaterThanOrEqual(OpBuilder &b, Location loc,
6767
Type elementalType, Value lhs,
6868
Value rhs) {
69-
return createComparisonTemplate<arith::CmpFPredicate::UGE,
69+
return createComparisonTemplate<arith::CmpFPredicate::OGE,
7070
arith::CmpIPredicate::uge,
7171
arith::CmpIPredicate::sge>(
7272
b, loc, elementalType, lhs, rhs);
7373
}
7474

7575
static Value createLessThan(OpBuilder &b, Location loc, Type elementalType,
7676
Value lhs, Value rhs) {
77-
return createComparisonTemplate<arith::CmpFPredicate::ULT,
77+
return createComparisonTemplate<arith::CmpFPredicate::OLT,
7878
arith::CmpIPredicate::ult,
7979
arith::CmpIPredicate::slt>(
8080
b, loc, elementalType, lhs, rhs);
8181
}
8282

8383
static Value createLessThanOrEqual(OpBuilder &b, Location loc,
8484
Type elementalType, Value lhs, Value rhs) {
85-
return createComparisonTemplate<arith::CmpFPredicate::ULE,
85+
return createComparisonTemplate<arith::CmpFPredicate::OLE,
8686
arith::CmpIPredicate::ule,
8787
arith::CmpIPredicate::sle>(
8888
b, loc, elementalType, lhs, rhs);

projects/pt1/python/torch_mlir_e2e_test/test_suite/elementwise_comparison.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ def forward(self, x, y):
160160

161161
@register_test_case(module_factory=lambda: ElementwiseGeFloatTensorModule())
162162
def ElementwiseGeFloatTensorModule_basic(module, tu: TestUtils):
163-
module.forward(tu.rand(3, 5), tu.rand(5))
163+
module.forward(
164+
torch.tensor([[1.0, 2.2, torch.nan], [6.0, 2.0, 3.1]]).to(torch.float32),
165+
torch.tensor([6.0, 2.1, torch.nan]).to(torch.float32))
164166

165167
# ==============================================================================
166168

@@ -200,7 +202,9 @@ def forward(self, x, y):
200202

201203
@register_test_case(module_factory=lambda: ElementwiseGtFloatTensorModule())
202204
def ElementwiseGtFloatTensorModule_basic(module, tu: TestUtils):
203-
module.forward(tu.rand(3, 5), tu.rand(5))
205+
module.forward(
206+
torch.tensor([[1.0, 2.2, torch.nan], [6.0, 2.0, 3.1]]).to(torch.float32),
207+
torch.tensor([6.0, 2.1, torch.nan]).to(torch.float32))
204208

205209
# ==============================================================================
206210

@@ -378,6 +382,28 @@ def ElementwiseLeFloatTensorModule_basic(module, tu: TestUtils):
378382

379383
# ==============================================================================
380384

385+
class ElementwiseLeFloatTensorNanModule(torch.nn.Module):
386+
def __init__(self):
387+
super().__init__()
388+
389+
@export
390+
@annotate_args([
391+
None,
392+
([-1, -1], torch.float32, True),
393+
([-1], torch.float32, True),
394+
])
395+
def forward(self, x, y):
396+
return torch.le(x, y)
397+
398+
399+
@register_test_case(module_factory=lambda: ElementwiseLeFloatTensorNanModule())
400+
def ElementwiseLeFloatTensorNanModule_basic(module, tu: TestUtils):
401+
module.forward(
402+
torch.tensor([[1.0, 2.2, torch.nan], [6.0, 2.0, 3.1]]).to(torch.float32),
403+
torch.tensor([6.0, 2.1, torch.nan]).to(torch.float32))
404+
405+
# ==============================================================================
406+
381407
class ElementwiseLeIntTensorModule(torch.nn.Module):
382408
def __init__(self):
383409
super().__init__()
@@ -414,7 +440,9 @@ def forward(self, x, y):
414440

415441
@register_test_case(module_factory=lambda: ElementwiseLtFloatTensorModule())
416442
def ElementwiseLtFloatTensorModule_basic(module, tu: TestUtils):
417-
module.forward(tu.rand(3, 5), tu.rand(5))
443+
module.forward(
444+
torch.tensor([[1.0, 2.2, torch.nan], [6.0, 2.0, 3.1]]).to(torch.float32),
445+
torch.tensor([6.0, 2.1, torch.nan]).to(torch.float32))
418446

419447
# ==============================================================================
420448

0 commit comments

Comments
 (0)