Simple block wrapping a mixer class with LayerNorm/RMSNorm and residual connection" This Block has a slightly different structure compared to a regular prenorm Transformer block. The standard block is: LN -> MHA/MLP -> Add. [Ref: https://arxiv.org/abs/2002.0
(
self, dim, mixer_cls, norm_cls=nn.LayerNorm, fused_add_norm=False, residual_in_fp32=False,drop_path=0.,
)
| 66 | |
| 67 | class Block(nn.Module): |
| 68 | def __init__( |
| 69 | self, dim, mixer_cls, norm_cls=nn.LayerNorm, fused_add_norm=False, residual_in_fp32=False,drop_path=0., |
| 70 | ): |
| 71 | """ |
| 72 | Simple block wrapping a mixer class with LayerNorm/RMSNorm and residual connection" |
| 73 | |
| 74 | This Block has a slightly different structure compared to a regular |
| 75 | prenorm Transformer block. |
| 76 | The standard block is: LN -> MHA/MLP -> Add. |
| 77 | [Ref: https://arxiv.org/abs/2002.04745] |
| 78 | Here we have: Add -> LN -> Mixer, returning both |
| 79 | the hidden_states (output of the mixer) and the residual. |
| 80 | This is purely for performance reasons, as we can fuse add and LayerNorm. |
| 81 | The residual needs to be provided (except for the very first block). |
| 82 | """ |
| 83 | super().__init__() |
| 84 | self.residual_in_fp32 = residual_in_fp32 |
| 85 | self.fused_add_norm = fused_add_norm |
| 86 | self.mixer = mixer_cls(dim) |
| 87 | self.norm = norm_cls(dim) |
| 88 | self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() |
| 89 | if self.fused_add_norm: |
| 90 | assert RMSNorm is not None, "RMSNorm import fails" |
| 91 | assert isinstance( |
| 92 | self.norm, (nn.LayerNorm, RMSNorm) |
| 93 | ), "Only LayerNorm and RMSNorm are supported for fused_add_norm" |
| 94 | |
| 95 | def forward( |
| 96 | self, hidden_states: Tensor, residual: Optional[Tensor] = None, inference_params=None |