|
13 | 13 | TransformerBlock, |
14 | 14 | ) |
15 | 15 | from executorch.examples.models.llama.model_args import ModelArgs |
| 16 | +from executorch.examples.models.llama.norm import ( |
| 17 | + RMSNormWithInputScale, |
| 18 | + ScalelessRMSNorm, |
| 19 | +) |
16 | 20 | from executorch.examples.models.llama.rope import Rope |
17 | 21 |
|
18 | 22 |
|
19 | 23 | class TestTransformerBlock(unittest.TestCase): |
| 24 | + @staticmethod |
| 25 | + def _model_args(**overrides) -> ModelArgs: |
| 26 | + args = { |
| 27 | + "dim": 4, |
| 28 | + "hidden_dim": 8, |
| 29 | + "n_heads": 1, |
| 30 | + "n_kv_heads": 1, |
| 31 | + "head_dim": 4, |
| 32 | + **overrides, |
| 33 | + } |
| 34 | + return ModelArgs(**args) |
| 35 | + |
| 36 | + def test_dedicated_weightless_post_ffn_norm(self) -> None: |
| 37 | + args = self._model_args() |
| 38 | + args.weightless_post_ffn_norm = True # pyrefly: ignore[missing-attribute] |
| 39 | + |
| 40 | + block = TransformerBlock(args, AttentionSkip()).eval() |
| 41 | + |
| 42 | + self.assertIsInstance(block.post_ffn_norm, ScalelessRMSNorm) |
| 43 | + |
| 44 | + def test_learnable_post_ffn_norm_takes_precedence(self) -> None: |
| 45 | + args = self._model_args(use_ffn_learnable_scales=True) |
| 46 | + args.weightless_post_ffn_norm = True # pyrefly: ignore[missing-attribute] |
| 47 | + |
| 48 | + block = TransformerBlock(args, AttentionSkip()).eval() |
| 49 | + |
| 50 | + self.assertIsInstance(block.post_ffn_norm, RMSNormWithInputScale) |
| 51 | + |
| 52 | + def test_shared_post_ffn_norm_does_not_select_weightless_norm(self) -> None: |
| 53 | + args = self._model_args(post_ffn_norm=True) |
| 54 | + |
| 55 | + block = TransformerBlock(args, AttentionSkip()).eval() |
| 56 | + |
| 57 | + self.assertFalse(hasattr(block, "post_ffn_norm")) |
| 58 | + |
| 59 | + def test_default_has_no_post_ffn_norm(self) -> None: |
| 60 | + block = TransformerBlock(self._model_args(), AttentionSkip()).eval() |
| 61 | + |
| 62 | + self.assertFalse(hasattr(block, "post_ffn_norm")) |
| 63 | + |
20 | 64 | def test_zero_centered_gamma_applies_to_main_norms(self) -> None: |
21 | 65 | args = ModelArgs( |
22 | 66 | dim=4, |
|
0 commit comments