|
| 1 | +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 2 | +# See https://llvm.org/LICENSE.txt for license information. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | +# Also available under a BSD-style license. See LICENSE. |
| 5 | + |
| 6 | +import torch |
| 7 | + |
| 8 | +from torch_mlir_e2e_test.torchscript.framework import TestUtils |
| 9 | +from torch_mlir_e2e_test.torchscript.registry import register_test_case |
| 10 | +from torch_mlir_e2e_test.torchscript.annotations import annotate_args, export |
| 11 | + |
| 12 | +# ============================================================================== |
| 13 | + |
| 14 | + |
| 15 | +class IndexSelectSingleIdxModule(torch.nn.Module): |
| 16 | + def __init__(self): |
| 17 | + super().__init__() |
| 18 | + |
| 19 | + @export |
| 20 | + @annotate_args([ |
| 21 | + None, |
| 22 | + ([4, 5, 6], torch.float32, True), |
| 23 | + ([1], torch.int64, True), |
| 24 | + ]) |
| 25 | + |
| 26 | + def forward(self, input, indices): |
| 27 | + return torch.index_select(input, 1, indices) |
| 28 | + |
| 29 | +@register_test_case(module_factory=lambda: IndexSelectSingleIdxModule()) |
| 30 | +def IndexSelectSingleIdxModule_basic(module, tu: TestUtils): |
| 31 | + module.forward(torch.randn(4, 5, 6), torch.tensor([2])) |
| 32 | + |
| 33 | + |
| 34 | +class IndexSelectTwoIdxModule(torch.nn.Module): |
| 35 | + def __init__(self): |
| 36 | + super().__init__() |
| 37 | + |
| 38 | + @export |
| 39 | + @annotate_args([ |
| 40 | + None, |
| 41 | + ([4, 5, 6], torch.float32, True), |
| 42 | + ([2], torch.int64, True), |
| 43 | + ]) |
| 44 | + |
| 45 | + def forward(self, input, indices): |
| 46 | + return torch.index_select(input, 2, indices) |
| 47 | + |
| 48 | +@register_test_case(module_factory=lambda: IndexSelectTwoIdxModule()) |
| 49 | +def IndexSelectTwoIdxModule_basic(module, tu: TestUtils): |
| 50 | + module.forward(torch.randn(4, 5, 6), torch.tensor([2, 4])) |
| 51 | + |
| 52 | + |
| 53 | +class IndexSelectWholeDimensionModule(torch.nn.Module): |
| 54 | + def __init__(self): |
| 55 | + super().__init__() |
| 56 | + |
| 57 | + @export |
| 58 | + @annotate_args([ |
| 59 | + None, |
| 60 | + ([4, 5, 6], torch.float32, True), |
| 61 | + ([4], torch.int64, True), |
| 62 | + ]) |
| 63 | + |
| 64 | + def forward(self, input, indices): |
| 65 | + return torch.index_select(input, 0, indices) |
| 66 | + |
| 67 | +@register_test_case(module_factory=lambda: IndexSelectWholeDimensionModule()) |
| 68 | +def IndexSelectWholeDimensionModule_basic(module, tu: TestUtils): |
| 69 | + module.forward(torch.randn(4, 5, 6), torch.tensor([0, 1, 2, 3])) |
| 70 | + |
| 71 | + |
| 72 | +class IndexSelectWholeTensorModule(torch.nn.Module): |
| 73 | + def __init__(self): |
| 74 | + super().__init__() |
| 75 | + |
| 76 | + @export |
| 77 | + @annotate_args([ |
| 78 | + None, |
| 79 | + ([3], torch.float32, True), |
| 80 | + ([3], torch.int64, True), |
| 81 | + ]) |
| 82 | + |
| 83 | + def forward(self, input, indices): |
| 84 | + return torch.index_select(input, 0, indices) |
| 85 | + |
| 86 | +@register_test_case(module_factory=lambda: IndexSelectWholeTensorModule()) |
| 87 | +def IndexSelectWholeTensorModule_basic(module, tu: TestUtils): |
| 88 | + module.forward(torch.randn(3), torch.tensor([0, 1, 2])) |
| 89 | + |
| 90 | + |
| 91 | +class IndexSelectDynamicModule(torch.nn.Module): |
| 92 | + def __init__(self): |
| 93 | + super().__init__() |
| 94 | + |
| 95 | + @export |
| 96 | + @annotate_args([ |
| 97 | + None, |
| 98 | + ([-1, -1, -1], torch.float32, True), |
| 99 | + ([-1], torch.int64, True), |
| 100 | + ]) |
| 101 | + |
| 102 | + def forward(self, input, indices): |
| 103 | + return torch.index_select(input, 2, indices) |
| 104 | + |
| 105 | +@register_test_case(module_factory=lambda: IndexSelectDynamicModule()) |
| 106 | +def IndexSelectDynamicModulebasic(module, tu: TestUtils): |
| 107 | + module.forward(torch.randn(4, 5, 6), torch.tensor([0, 4])) |
| 108 | + |
| 109 | + |
| 110 | +class IndexSelectDynamicInputSizeModule(torch.nn.Module): |
| 111 | + def __init__(self): |
| 112 | + super().__init__() |
| 113 | + |
| 114 | + @export |
| 115 | + @annotate_args([ |
| 116 | + None, |
| 117 | + ([-1, -1, -1], torch.float32, True), |
| 118 | + ([2], torch.int64, True), |
| 119 | + ]) |
| 120 | + |
| 121 | + def forward(self, input, indices): |
| 122 | + return torch.index_select(input, 2, indices) |
| 123 | + |
| 124 | +@register_test_case(module_factory=lambda: IndexSelectDynamicInputSizeModule()) |
| 125 | +def IndexSelectDynamicInputSizeModule_basic(module, tu: TestUtils): |
| 126 | + module.forward(torch.randn(4, 5, 6), torch.tensor([0, 2])) |
| 127 | + |
| 128 | + |
| 129 | +class IndexSelectDynamicIndexSizeModule(torch.nn.Module): |
| 130 | + def __init__(self): |
| 131 | + super().__init__() |
| 132 | + |
| 133 | + @export |
| 134 | + @annotate_args([ |
| 135 | + None, |
| 136 | + ([4, 5, 6], torch.float32, True), |
| 137 | + ([-1], torch.int64, True), |
| 138 | + ]) |
| 139 | + |
| 140 | + def forward(self, input, indices): |
| 141 | + return torch.index_select(input, 1, indices) |
| 142 | + |
| 143 | +@register_test_case(module_factory=lambda: IndexSelectDynamicIndexSizeModule()) |
| 144 | +def IndexSelectDynamicIndexSizeModule_basic(module, tu: TestUtils): |
| 145 | + module.forward(torch.randn(4, 5, 6), torch.tensor([1, 2])) |
0 commit comments