GeezerVison is showing github.com — Update llama and derivatives to pass position embeddings explicitly f… · unslothai/unsloth@0671dbd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: unsloth/models/cohere.py +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -75,6 +75,7 @@ def CohereAttention_fast_forward( | 75 | 75 | output_attentions: bool = False, | 76 | 76 | use_cache: bool = False, | 77 | 77 | padding_mask: Optional[torch.LongTensor] = None, | | 78 | + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, | 78 | 79 | *args, **kwargs, | 79 | 80 | ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | 80 | 81 |
| @@ -112,12 +113,11 @@ def CohereAttention_fast_forward( | 112 | 113 | if past_key_value is not None: | 113 | 114 | kv_seq_len += past_key_value[0].shape[-2] | 114 | 115 |
| | 116 | + cos, sin = position_embeddings | 115 | 117 | if position_ids is None: | 116 | | - cos = self.rotary_emb.cos_cached | 117 | | - sin = self.rotary_emb.sin_cached | 118 | 118 | Q, K = fast_rope_embedding(Q, K, cos, sin) | 119 | 119 | else: | 120 | | - cos, sin = self.rotary_emb(V, seq_len = kv_seq_len) | | 120 | + cos, sin = cos[position_ids], sin[position_ids] | 121 | 121 | Q, K = inplace_rope_embedding(Q, K, cos, sin, position_ids) | 122 | 122 | pass | 123 | 123 |
| @@ -190,6 +190,7 @@ def CohereDecoderLayer_fast_forward( | 190 | 190 | output_attentions: Optional[bool] = False, | 191 | 191 | use_cache: Optional[bool] = False, | 192 | 192 | padding_mask: Optional[torch.LongTensor] = None, | | 193 | + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, | 193 | 194 | *args, **kwargs, | 194 | 195 | ): | 195 | 196 | if use_cache and hasattr(self, "_flag_for_generation"): #past_key_value is not None: | |
Collapse file Copy file name to clipboardExpand all lines: unsloth/models/llama.py +26-15Lines changed: 26 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -337,6 +337,7 @@ def LlamaAttention_fast_forward( | 337 | 337 | output_attentions: bool = False, | 338 | 338 | use_cache: bool = False, | 339 | 339 | padding_mask: Optional[torch.LongTensor] = None, | | 340 | + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, | 340 | 341 | *args, **kwargs, | 341 | 342 | ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | 342 | 343 |
| @@ -368,20 +369,24 @@ def LlamaAttention_fast_forward( | 368 | 369 | if past_key_value is not None: | 369 | 370 | kv_seq_len += past_key_value[0].shape[-2] | 370 | 371 |
| 371 | | - # Extend RoPE dynamically to fit in VRAM | 372 | | - rotary_emb = self.rotary_emb | 373 | | - rotary_emb.extend_rope_embedding(V, seq_len = kv_seq_len) | 374 | | - | 375 | | - if position_ids is None: | 376 | | - # Useful for LongRoPE | 377 | | - cos, sin = rotary_emb.get_cached(kv_seq_len) | 378 | | - # cos = self.rotary_emb.cos_cached | 379 | | - # sin = self.rotary_emb.sin_cached | 380 | | - Q, K = fast_rope_embedding(Q, K, cos, sin) | | 372 | + if position_embeddings: | | 373 | + cos, sin = position_embeddings | 381 | 374 | else: | 382 | | - cos, sin = rotary_emb(V, seq_len = kv_seq_len) | 383 | | - Q, K = inplace_rope_embedding(Q, K, cos, sin, position_ids) | 384 | | - pass | | 375 | + # Extend RoPE dynamically to fit in VRA | | 376 | + rotary_emb = self.rotary_emb | | 377 | + rotary_emb.extend_rope_embedding(V, seq_len=kv_seq_len) | | 378 | + | | 379 | + if position_ids is None: | | 380 | + # Useful for LongRoPE | | 381 | + cos, sin = rotary_emb.get_cached(kv_seq_len) | | 382 | + else: | | 383 | + cos, sin = rotary_emb(V, seq_len=kv_seq_len) | | 384 | + | | 385 | + Q, K = ( | | 386 | + fast_rope_embedding(Q, K, cos, sin) | | 387 | + if position_ids is None | | 388 | + else inplace_rope_embedding(Q, K, cos, sin, position_ids) | | 389 | + ) | 385 | 390 |
| 386 | 391 | if past_key_value is not None: | 387 | 392 | K = torch.cat([past_key_value[0], K], dim = 2) | @@ -452,6 +457,7 @@ def LlamaDecoderLayer_fast_forward( | 452 | 457 | output_attentions: Optional[bool] = False, | 453 | 458 | use_cache: Optional[bool] = False, | 454 | 459 | padding_mask: Optional[torch.LongTensor] = None, | | 460 | + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, | 455 | 461 | *args, **kwargs, | 456 | 462 | ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: | 457 | 463 | """ | @@ -479,6 +485,7 @@ def LlamaDecoderLayer_fast_forward( | 479 | 485 | output_attentions=output_attentions, | 480 | 486 | use_cache=use_cache, | 481 | 487 | padding_mask=padding_mask, | | 488 | + position_embeddings = position_embeddings, | 482 | 489 | ) | 483 | 490 | hidden_states += residual | 484 | 491 |
| @@ -499,6 +506,7 @@ def LlamaDecoderLayer_fast_forward( | 499 | 506 | output_attentions=output_attentions, | 500 | 507 | use_cache=use_cache, | 501 | 508 | padding_mask=padding_mask, | | 509 | + position_embeddings = position_embeddings, | 502 | 510 | ) | 503 | 511 | hidden_states = residual + hidden_states | 504 | 512 |
| @@ -777,8 +785,11 @@ def LlamaModel_fast_forward( | 777 | 785 | pass | 778 | 786 |
| 779 | 787 |
| 780 | | - if IS_GRANITE: | 781 | | - position_embeddings = self.rotary_emb(hidden_states, position_ids, self.max_position_embeddings) | | 788 | + if transformers_version > "4.47.1" and hasattr(self,'rotary_emb'): | | 789 | + # Transformers main has made it mandatory to pass position_embeddings | | 790 | + # https://github.com/huggingface/transformers/pull/34858 | | 791 | + position_embeddings = self.rotary_emb(hidden_states, position_ids, self.config.max_position_embeddings) | | 792 | + print(f'position_embeddings: {position_embeddings}') | 782 | 793 | else: | 783 | 794 | position_embeddings = None | 784 | 795 |
| |
Collapse file Copy file name to clipboardExpand all lines: unsloth/models/mistral.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,6 +47,7 @@ def MistralAttention_fast_forward( | 47 | 47 | output_attentions: bool = False, | 48 | 48 | use_cache: bool = False, | 49 | 49 | padding_mask: Optional[torch.LongTensor] = None, | | 50 | + position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, | 50 | 51 | *args, **kwargs, | 51 | 52 | ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | 52 | 53 |
| |
|