|
| 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 | +import torch.nn.functional as F |
| 18 | + |
| 19 | +from . import base, consts |
| 20 | + |
| 21 | + |
| 22 | +class NormBenchmark(base.GenericBenchmark): |
| 23 | + def set_more_shapes(self): |
| 24 | + return [ |
| 25 | + # 3D shapes represented as [batch_size, channels, hidden_size] |
| 26 | + (16, 16, 64), |
| 27 | + (16, 16, 1024), |
| 28 | + (16, 16, 4098), |
| 29 | + # 4D shapes represented as [batch_size, channels, H, W] |
| 30 | + (1, 8, 4, 4), |
| 31 | + (16, 8, 128, 128), |
| 32 | + ] |
| 33 | + |
| 34 | + |
| 35 | +def native_batch_norm_legit_no_training_input_fn(shape, dtype, device): |
| 36 | + C = shape[1] |
| 37 | + inp = torch.randn(shape, dtype=dtype, device=device) |
| 38 | + weight = torch.randn((C,), dtype=dtype, device=device) |
| 39 | + bias = torch.randn((C,), dtype=dtype, device=device) |
| 40 | + running_mean = torch.randn((C,), dtype=dtype, device=device) |
| 41 | + running_var = torch.abs(torch.randn((C,), dtype=dtype, device=device)) + 0.1 |
| 42 | + momentum = 0.1 |
| 43 | + eps = 1e-5 |
| 44 | + yield inp, weight, bias, running_mean, running_var, momentum, eps |
| 45 | + |
| 46 | + |
| 47 | +def torch_native_batch_norm_legit_no_training( |
| 48 | + inp, weight, bias, running_mean, running_var, momentum, eps |
| 49 | +): |
| 50 | + return F.batch_norm( |
| 51 | + inp, running_mean, running_var, weight, bias, training=False, eps=eps |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.native_batch_norm_legit_no_training |
| 56 | +def test_native_batch_norm_legit_no_training(): |
| 57 | + bench = NormBenchmark( |
| 58 | + input_fn=native_batch_norm_legit_no_training_input_fn, |
| 59 | + op_name="native_batch_norm_legit_no_training", |
| 60 | + torch_op=torch_native_batch_norm_legit_no_training, |
| 61 | + dtypes=consts.FLOAT_DTYPES, |
| 62 | + ) |
| 63 | + bench.run() |
0 commit comments