|
| 1 | +# Copyright 2026 FlagOS Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +import torch |
| 17 | + |
| 18 | +from . import base, consts |
| 19 | + |
| 20 | + |
| 21 | +class FloatPowerBenchmark(base.GenericBenchmark): |
| 22 | + def set_shapes(self, shape_file_path=None): |
| 23 | + self.shapes = [(4,), (1024,), (65536,), (512, 512), (2048, 2048)] |
| 24 | + |
| 25 | + |
| 26 | +def _tensor_tensor_input(shape, dtype, device): |
| 27 | + base_tensor = torch.rand(shape, dtype=dtype, device=device).add_(0.25) |
| 28 | + exponent = torch.rand(shape, dtype=dtype, device=device).mul_(4).sub_(2) |
| 29 | + yield base_tensor, exponent |
| 30 | + |
| 31 | + |
| 32 | +def _tensor_scalar_input(shape, dtype, device): |
| 33 | + base_tensor = torch.rand(shape, dtype=dtype, device=device).add_(0.25) |
| 34 | + yield base_tensor, 1.234 |
| 35 | + |
| 36 | + |
| 37 | +def _scalar_tensor_input(shape, dtype, device): |
| 38 | + exponent = torch.rand(shape, dtype=dtype, device=device).mul_(4).sub_(2) |
| 39 | + yield 2.0, exponent |
| 40 | + |
| 41 | + |
| 42 | +def _tensor_tensor_out_input(shape, dtype, device): |
| 43 | + base_tensor = torch.rand(shape, dtype=dtype, device=device).add_(0.25) |
| 44 | + exponent = torch.rand(shape, dtype=dtype, device=device).mul_(4).sub_(2) |
| 45 | + out = torch.empty(shape, dtype=torch.float64, device=device) |
| 46 | + yield base_tensor, exponent, {"out": out} |
| 47 | + |
| 48 | + |
| 49 | +def _tensor_scalar_out_input(shape, dtype, device): |
| 50 | + base_tensor = torch.rand(shape, dtype=dtype, device=device).add_(0.25) |
| 51 | + out = torch.empty(shape, dtype=torch.float64, device=device) |
| 52 | + yield base_tensor, 1.234, {"out": out} |
| 53 | + |
| 54 | + |
| 55 | +def _scalar_tensor_out_input(shape, dtype, device): |
| 56 | + exponent = torch.rand(shape, dtype=dtype, device=device).mul_(4).sub_(2) |
| 57 | + out = torch.empty(shape, dtype=torch.float64, device=device) |
| 58 | + yield 2.0, exponent, {"out": out} |
| 59 | + |
| 60 | + |
| 61 | +def _run_benchmark(op_name, input_fn, torch_op): |
| 62 | + bench = FloatPowerBenchmark( |
| 63 | + input_fn=input_fn, |
| 64 | + op_name=op_name, |
| 65 | + torch_op=torch_op, |
| 66 | + dtypes=consts.FLOAT_DTYPES, |
| 67 | + ) |
| 68 | + bench.run() |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.float_power_tensor_tensor |
| 72 | +def test_float_power_tensor_tensor(): |
| 73 | + _run_benchmark( |
| 74 | + "float_power_tensor_tensor", |
| 75 | + _tensor_tensor_input, |
| 76 | + torch.ops.aten.float_power.Tensor_Tensor, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.float_power_tensor_scalar |
| 81 | +def test_float_power_tensor_scalar(): |
| 82 | + _run_benchmark( |
| 83 | + "float_power_tensor_scalar", |
| 84 | + _tensor_scalar_input, |
| 85 | + torch.ops.aten.float_power.Tensor_Scalar, |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.float_power_scalar_tensor |
| 90 | +def test_float_power_scalar_tensor(): |
| 91 | + _run_benchmark( |
| 92 | + "float_power_scalar_tensor", |
| 93 | + _scalar_tensor_input, |
| 94 | + torch.ops.aten.float_power.Scalar, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.float_power_tensor_tensor_out |
| 99 | +def test_float_power_tensor_tensor_out(): |
| 100 | + _run_benchmark( |
| 101 | + "float_power_tensor_tensor_out", |
| 102 | + _tensor_tensor_out_input, |
| 103 | + torch.ops.aten.float_power.Tensor_Tensor_out, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.float_power_tensor_scalar_out |
| 108 | +def test_float_power_tensor_scalar_out(): |
| 109 | + _run_benchmark( |
| 110 | + "float_power_tensor_scalar_out", |
| 111 | + _tensor_scalar_out_input, |
| 112 | + torch.ops.aten.float_power.Tensor_Scalar_out, |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.float_power_scalar_tensor_out |
| 117 | +def test_float_power_scalar_tensor_out(): |
| 118 | + _run_benchmark( |
| 119 | + "float_power_scalar_tensor_out", |
| 120 | + _scalar_tensor_out_input, |
| 121 | + torch.ops.aten.float_power.Scalar_out, |
| 122 | + ) |
0 commit comments