r"""Pass the input through the encoder layer. Args: hidden_states: the sequence to the encoder layer (required). residual: hidden_states = Mixer(LN(residual))
(
self, hidden_states: Tensor, residual: Optional[Tensor] = None, inference_params=None
)
| 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 |
| 97 | ): |
| 98 | r"""Pass the input through the encoder layer. |
| 99 | |
| 100 | Args: |
| 101 | hidden_states: the sequence to the encoder layer (required). |
| 102 | residual: hidden_states = Mixer(LN(residual)) |
| 103 | """ |
| 104 | if not self.fused_add_norm: |
| 105 | if residual is None: |
| 106 | residual = hidden_states |
| 107 | else: |
| 108 | residual = residual + self.drop_path(hidden_states) |
| 109 | |
| 110 | hidden_states = self.norm(residual.to(dtype=self.norm.weight.dtype)) |
| 111 | if self.residual_in_fp32: |
| 112 | residual = residual.to(torch.float32) |
| 113 | else: |
| 114 | fused_add_norm_fn = rms_norm_fn if isinstance(self.norm, RMSNorm) else layer_norm_fn |
| 115 | if residual is None: |
| 116 | hidden_states, residual = fused_add_norm_fn( |
| 117 | hidden_states, |
| 118 | self.norm.weight, |
| 119 | self.norm.bias, |
| 120 | residual=residual, |
| 121 | prenorm=True, |
| 122 | residual_in_fp32=self.residual_in_fp32, |
| 123 | eps=self.norm.eps, |
| 124 | ) |
| 125 | else: |
| 126 | hidden_states, residual = fused_add_norm_fn( |
| 127 | self.drop_path(hidden_states), |
| 128 | self.norm.weight, |
| 129 | self.norm.bias, |
| 130 | residual=residual, |
| 131 | prenorm=True, |
| 132 | residual_in_fp32=self.residual_in_fp32, |
| 133 | eps=self.norm.eps, |
| 134 | ) |
| 135 | |
| 136 | # augment x |
| 137 | hidden_states = self.mixer(hidden_states, inference_params=inference_params) |
| 138 | return hidden_states, residual |
| 139 | |
| 140 | def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs): |
| 141 | return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs) |
nothing calls this directly
no outgoing calls
no test coverage detected