|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +import flag_gems |
| 5 | + |
| 6 | +from . import attr_util as attr_utils |
| 7 | +from . import performance_utils as utils |
| 8 | + |
| 9 | + |
| 10 | +# TODO(Qiming): Consolidate this to a base package |
| 11 | +class NormBenchmark(utils.GenericBenchmark): |
| 12 | + # TODO: add new metric |
| 13 | + |
| 14 | + def set_more_shapes(self): |
| 15 | + return [ |
| 16 | + # 3D shapes represented as [batch_size, channels, hidden_size] |
| 17 | + (16, 16, 64), |
| 18 | + (16, 16, 1024), |
| 19 | + (16, 16, 4098), |
| 20 | + # 4D shapes represented as [batch_size, channels, H, W] |
| 21 | + (1, 8, 4, 4), |
| 22 | + (16, 8, 128, 128), |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +def batchnorm_input_fn(shape, dtype, device): |
| 27 | + C = shape[1] |
| 28 | + inp = torch.randn(shape, dtype=dtype, device=device) |
| 29 | + weight = torch.randn((C,), dtype=dtype, device=device) |
| 30 | + bias = torch.randn((C,), dtype=dtype, device=device) |
| 31 | + running_mean = None |
| 32 | + running_var = None |
| 33 | + training = True |
| 34 | + momentum = 0.1 |
| 35 | + eps = 1e-5 |
| 36 | + cudnn_enabled = True |
| 37 | + yield inp, weight, bias, running_mean, running_var, training, momentum, eps, cudnn_enabled |
| 38 | + |
| 39 | + if utils.Config.bench_level == utils.BenchLevel.COMPREHENSIVE: |
| 40 | + running_mean = torch.randn((C,), dtype=dtype, device=device) |
| 41 | + running_var = torch.randn((C,), dtype=dtype, device=device) |
| 42 | + yield inp, weight, bias, running_mean, running_var, training, momentum, eps, cudnn_enabled |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.batch_norm_backward |
| 46 | +def test_batch_norm_backward(): |
| 47 | + def batch_norm_backward_input_fn(shape, dtype, device): |
| 48 | + for forward_args in batchnorm_input_fn(shape, dtype, device): |
| 49 | + ( |
| 50 | + inp, |
| 51 | + weight, |
| 52 | + bias, |
| 53 | + running_mean, |
| 54 | + running_var, |
| 55 | + training, |
| 56 | + _, |
| 57 | + eps, |
| 58 | + _, |
| 59 | + ) = forward_args |
| 60 | + |
| 61 | + grad_output = torch.randn_like(inp) |
| 62 | + channels = weight.shape[0] if weight is not None else inp.shape[1] |
| 63 | + |
| 64 | + if running_mean is None: |
| 65 | + running_mean = torch.zeros(channels, dtype=dtype, device=device) |
| 66 | + if running_var is None: |
| 67 | + running_var = torch.ones(channels, dtype=dtype, device=device) |
| 68 | + |
| 69 | + save_mean = torch.randn(channels, dtype=torch.float32, device=device) |
| 70 | + save_invstd = torch.randn(channels, dtype=torch.float32, device=device) |
| 71 | + output_mask = [True, weight is not None, bias is not None] |
| 72 | + |
| 73 | + yield ( |
| 74 | + grad_output, |
| 75 | + inp, |
| 76 | + weight, |
| 77 | + running_mean, |
| 78 | + running_var, |
| 79 | + save_mean, |
| 80 | + save_invstd, |
| 81 | + training, |
| 82 | + eps, |
| 83 | + output_mask, |
| 84 | + ) |
| 85 | + |
| 86 | + bench = NormBenchmark( |
| 87 | + input_fn=batch_norm_backward_input_fn, |
| 88 | + op_name="native_batch_norm_backward", |
| 89 | + torch_op=torch.ops.aten.native_batch_norm_backward, |
| 90 | + dtypes=[torch.float32] |
| 91 | + if flag_gems.vendor_name == "mthreads" |
| 92 | + else attr_utils.FLOAT_DTYPES, |
| 93 | + ) |
| 94 | + bench.set_gems(flag_gems.batch_norm_backward) |
| 95 | + |
| 96 | + bench.run() |
0 commit comments