|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +from . import base, consts, utils |
| 5 | + |
| 6 | + |
| 7 | +class IndexCopyBenchmark(base.GenericBenchmark): |
| 8 | + def set_more_shapes(self): |
| 9 | + return [(1, 2), (4096, 256), (200, 40999, 3)] |
| 10 | + |
| 11 | + def get_gbps(self, bench_fn_args, latency): |
| 12 | + index = bench_fn_args[2] |
| 13 | + src = bench_fn_args[3] |
| 14 | + io_amount = sum([utils.size_in_bytes(item) for item in [index, src, src]]) |
| 15 | + return io_amount * 1e-9 / (latency * 1e-3) |
| 16 | + |
| 17 | + |
| 18 | +def _tensor_input_fn(shape, dtype, device): |
| 19 | + inp = torch.randn(shape, dtype=dtype, device=device) |
| 20 | + dim = 0 if len(shape) == 1 else 1 |
| 21 | + src_shape = list(inp.shape) |
| 22 | + index_max = src_shape[dim] |
| 23 | + index_len = index_max // 2 if index_max >= 2 else 1 |
| 24 | + index = torch.randperm(index_len, device=device) |
| 25 | + src_shape[dim] = index_len |
| 26 | + src = torch.randn(src_shape, dtype=dtype, device=device) |
| 27 | + yield inp, dim, index, src |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.index_copy |
| 31 | +def test_index_copy(): |
| 32 | + bench = IndexCopyBenchmark( |
| 33 | + input_fn=_tensor_input_fn, |
| 34 | + op_name="index_copy", |
| 35 | + torch_op=torch.index_copy, |
| 36 | + dtypes=consts.FLOAT_DTYPES, |
| 37 | + ) |
| 38 | + bench.run() |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.index_copy_ |
| 42 | +def test_index_copy_(): |
| 43 | + bench = IndexCopyBenchmark( |
| 44 | + input_fn=_tensor_input_fn, |
| 45 | + op_name="index_copy_", |
| 46 | + torch_op=torch.Tensor.index_copy_, |
| 47 | + dtypes=consts.FLOAT_DTYPES, |
| 48 | + inplace=True, |
| 49 | + ) |
| 50 | + bench.run() |
0 commit comments