|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +import pytest |
| 13 | +import torch |
| 14 | +from hidet.testing.torch_utils import check_module, FunctionalModule |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "a_shape, b_shape, indices", |
| 19 | + [ |
| 20 | + [[10, 11, 12, 13], [10, 9, 8], (slice(None), slice(9), slice(8), 0)], |
| 21 | + [[5, 4, 3, 2], [2, 2], (slice(2, 4), slice(2, 4), 0, 1)], |
| 22 | + [[1, 1, 1024], [10], (0, 0, slice(1000, 1010))], |
| 23 | + [[4, 4, 4, 4], [1, 1, 1], (slice(1), 0, slice(1), slice(1))], |
| 24 | + [[4, 4, 4, 4], [2, 3], (3, slice(2), 2, slice(3))], |
| 25 | + [[4, 4, 4, 4], [2, 3], (slice(2), 0, slice(3), 0)], |
| 26 | + [[4, 4, 4, 4], [4, 4], (0, Ellipsis, 0)], |
| 27 | + [[10, 10], [10, 10], (Ellipsis,)], |
| 28 | + [[1, 3, 28, 28, 85], [1, 3, 28, 28, 2], (Ellipsis, slice(2))], |
| 29 | + ], |
| 30 | +) |
| 31 | +def test_setitem_with_tensor(a_shape, b_shape, indices): |
| 32 | + def check_setitem(x, y, indices): |
| 33 | + x[indices] = y |
| 34 | + return x |
| 35 | + |
| 36 | + check_module( |
| 37 | + FunctionalModule(op=check_setitem), args=[torch.randn(a_shape), torch.randn(b_shape), indices], atol=0, rtol=0 |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + "a_shape, setvalue, indices", |
| 43 | + [ |
| 44 | + [[10, 11, 12, 13], 1.0, (slice(None), slice(9), slice(8), 0)], |
| 45 | + [[5, 4, 3, 2], 1.0, (slice(2, 4), slice(2, 4), 0, 1)], |
| 46 | + [[1, 1, 1024], 1.0, (0, 0, slice(1000, 1010))], |
| 47 | + [[4, 4, 4, 4], 1.0, (slice(1), 0, slice(1), slice(1))], |
| 48 | + [[4, 4, 4, 4], 1.0, (3, slice(2), 2, slice(3))], |
| 49 | + [[4, 4, 4, 4], 1.0, (slice(2), 0, slice(3), 0)], |
| 50 | + [[4, 4, 4, 4], 1.0, (0, Ellipsis, 0)], |
| 51 | + [[10, 10], 1.0, (Ellipsis,)], |
| 52 | + [[1, 3, 28, 28, 85], 1.0, (Ellipsis, slice(2))], |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_setitem_with_scalar(a_shape, setvalue, indices): |
| 56 | + def check_setitem(x, setvalue, indices): |
| 57 | + x[indices] = setvalue |
| 58 | + return x |
| 59 | + |
| 60 | + check_module(FunctionalModule(op=check_setitem), args=[torch.randn(a_shape), setvalue, indices], atol=0, rtol=0) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + pytest.main([__file__]) |
0 commit comments