GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update llama and derivatives to pass position embeddings explicitly f… · unslothai/unsloth@0671dbd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • unsloth/models
    • cohere.py
    • llama.py
    • mistral.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎unsloth/models/cohere.py‎

Copy file name to clipboardExpand all lines: unsloth/models/cohere.py
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def CohereAttention_fast_forward(
7575
output_attentions: bool = False,
7676
use_cache: bool = False,
7777
padding_mask: Optional[torch.LongTensor] = None,
78+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
7879
*args, **kwargs,
7980
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
8081

@@ -112,12 +113,11 @@ def CohereAttention_fast_forward(
112113
if past_key_value is not None:
113114
kv_seq_len += past_key_value[0].shape[-2]
114115

116+
cos, sin = position_embeddings
115117
if position_ids is None:
116-
cos = self.rotary_emb.cos_cached
117-
sin = self.rotary_emb.sin_cached
118118
Q, K = fast_rope_embedding(Q, K, cos, sin)
119119
else:
120-
cos, sin = self.rotary_emb(V, seq_len = kv_seq_len)
120+
cos, sin = cos[position_ids], sin[position_ids]
121121
Q, K = inplace_rope_embedding(Q, K, cos, sin, position_ids)
122122
pass
123123

@@ -190,6 +190,7 @@ def CohereDecoderLayer_fast_forward(
190190
output_attentions: Optional[bool] = False,
191191
use_cache: Optional[bool] = False,
192192
padding_mask: Optional[torch.LongTensor] = None,
193+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
193194
*args, **kwargs,
194195
):
195196
if use_cache and hasattr(self, "_flag_for_generation"): #past_key_value is not None:
Collapse file

‎unsloth/models/llama.py‎

Copy file name to clipboardExpand all lines: unsloth/models/llama.py
+26-15Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def LlamaAttention_fast_forward(
337337
output_attentions: bool = False,
338338
use_cache: bool = False,
339339
padding_mask: Optional[torch.LongTensor] = None,
340+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
340341
*args, **kwargs,
341342
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
342343

@@ -368,20 +369,24 @@ def LlamaAttention_fast_forward(
368369
if past_key_value is not None:
369370
kv_seq_len += past_key_value[0].shape[-2]
370371

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
381374
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+
)
385390

386391
if past_key_value is not None:
387392
K = torch.cat([past_key_value[0], K], dim = 2)
@@ -452,6 +457,7 @@ def LlamaDecoderLayer_fast_forward(
452457
output_attentions: Optional[bool] = False,
453458
use_cache: Optional[bool] = False,
454459
padding_mask: Optional[torch.LongTensor] = None,
460+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
455461
*args, **kwargs,
456462
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
457463
"""
@@ -479,6 +485,7 @@ def LlamaDecoderLayer_fast_forward(
479485
output_attentions=output_attentions,
480486
use_cache=use_cache,
481487
padding_mask=padding_mask,
488+
position_embeddings = position_embeddings,
482489
)
483490
hidden_states += residual
484491

@@ -499,6 +506,7 @@ def LlamaDecoderLayer_fast_forward(
499506
output_attentions=output_attentions,
500507
use_cache=use_cache,
501508
padding_mask=padding_mask,
509+
position_embeddings = position_embeddings,
502510
)
503511
hidden_states = residual + hidden_states
504512

@@ -777,8 +785,11 @@ def LlamaModel_fast_forward(
777785
pass
778786

779787

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}')
782793
else:
783794
position_embeddings = None
784795

Collapse file

‎unsloth/models/mistral.py‎

Copy file name to clipboardExpand all lines: unsloth/models/mistral.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def MistralAttention_fast_forward(
4747
output_attentions: bool = False,
4848
use_cache: bool = False,
4949
padding_mask: Optional[torch.LongTensor] = None,
50+
position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
5051
*args, **kwargs,
5152
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
5253

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.