|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import hypothesis.strategies as st |
| 17 | +import torch |
| 18 | +import torch.nn as nn |
| 19 | +from hypothesis import given, settings |
| 20 | + |
| 21 | +from .common import GradSampleHooks_test |
| 22 | + |
| 23 | + |
| 24 | +class RMSNorm_test(GradSampleHooks_test): |
| 25 | + @given( |
| 26 | + N=st.integers(1, 4), |
| 27 | + Z=st.integers(1, 4), |
| 28 | + H=st.integers(1, 3), |
| 29 | + W=st.integers(5, 10), |
| 30 | + input_dim=st.integers(2, 4), |
| 31 | + norm_dim=st.integers(1, 3), |
| 32 | + ) |
| 33 | + @settings(deadline=60000) |
| 34 | + def test_input_norm( |
| 35 | + self, N: int, Z: int, W: int, H: int, input_dim: int, norm_dim: int |
| 36 | + ): |
| 37 | + if norm_dim >= input_dim: |
| 38 | + return |
| 39 | + normalized_shape, x_shape = self.get_x_shape_and_norm_shape( |
| 40 | + H, N, W, Z, input_dim, norm_dim |
| 41 | + ) |
| 42 | + |
| 43 | + norm = nn.RMSNorm(normalized_shape, elementwise_affine=True) |
| 44 | + x = torch.randn(x_shape) |
| 45 | + self.run_test(x, norm, batch_first=True, ew_compatible=False) |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def get_x_shape_and_norm_shape(H, N, W, Z, input_dim, norm_dim): |
| 49 | + if norm_dim == 1: |
| 50 | + normalized_shape = W |
| 51 | + if input_dim == 2: |
| 52 | + x_shape = [N, W] |
| 53 | + if input_dim == 3: |
| 54 | + x_shape = [N, Z, W] |
| 55 | + if input_dim == 4: |
| 56 | + x_shape = [N, Z, H, W] |
| 57 | + elif norm_dim == 2: |
| 58 | + if input_dim == 3: |
| 59 | + normalized_shape = [Z, W] |
| 60 | + x_shape = [N, Z, W] |
| 61 | + if input_dim == 4: |
| 62 | + normalized_shape = [H, W] |
| 63 | + x_shape = [N, Z, H, W] |
| 64 | + elif norm_dim == 3: |
| 65 | + normalized_shape = [Z, H, W] |
| 66 | + x_shape = [N, Z, H, W] |
| 67 | + return normalized_shape, x_shape |
0 commit comments