| 493 | |
| 494 | |
| 495 | class EAGLEDecoderLayer(nn.Module): |
| 496 | def __init__(self, config, index): |
| 497 | super().__init__() |
| 498 | self.hidden_size = config.hidden_size |
| 499 | self.self_attn = EAGLEAttention(config=config) |
| 500 | self.mlp = EAGLEMLP(config) |
| 501 | self.index = index |
| 502 | if self.index != 0: |
| 503 | self.input_layernorm = EAGLERMSNorm(config.hidden_size, eps=config.rms_norm_eps) |
| 504 | self.post_attention_layernorm = EAGLERMSNorm(config.hidden_size, eps=config.rms_norm_eps) |
| 505 | |
| 506 | def forward( |
| 507 | self, |
| 508 | hidden_states: torch.Tensor, |
| 509 | attention_mask: Optional[torch.Tensor] = None, |
| 510 | position_ids: Optional[torch.LongTensor] = None, |
| 511 | past_key_value: Optional[Tuple[torch.Tensor]] = None, |
| 512 | output_attentions: Optional[bool] = False, |
| 513 | use_cache: Optional[bool] = False, |
| 514 | ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: |
| 515 | """ |
| 516 | Args: |
| 517 | hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` |
| 518 | attention_mask (`torch.FloatTensor`, *optional*): attention mask of size |
| 519 | `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values. |
| 520 | output_attentions (`bool`, *optional*): |
| 521 | Whether or not to return the attentions tensors of all attention layers. See `attentions` under |
| 522 | returned tensors for more detail. |
| 523 | use_cache (`bool`, *optional*): |
| 524 | If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding |
| 525 | (see `past_key_values`). |
| 526 | past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states |
| 527 | """ |
| 528 | |
| 529 | residual = hidden_states |
| 530 | |
| 531 | if self.index != 0: |
| 532 | hidden_states = self.input_layernorm(hidden_states) |
| 533 | |
| 534 | # Self Attention |
| 535 | hidden_states, self_attn_weights, present_key_value = self.self_attn( |
| 536 | hidden_states=hidden_states, |
| 537 | attention_mask=attention_mask, |
| 538 | position_ids=position_ids, |
| 539 | past_key_value=past_key_value, |
| 540 | output_attentions=output_attentions, |
| 541 | use_cache=use_cache, |
| 542 | ) |
| 543 | hidden_states = residual + hidden_states |
| 544 | |
| 545 | # Fully Connected |
| 546 | residual = hidden_states |
| 547 | hidden_states = self.post_attention_layernorm(hidden_states) |
| 548 | hidden_states = self.mlp(hidden_states) |
| 549 | hidden_states = residual + hidden_states |
| 550 | |
| 551 | outputs = (hidden_states,) |
| 552 | |