Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions QEfficient/transformers/models/falcon/modeling_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,6 @@ def _set_cos_sin_cache(self, seq_len, device, dtype):
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)

def forward(self, x, seq_len=None):
# x: [bs, num_attention_heads, seq_len, head_size]
if seq_len > self.max_seq_len_cached:
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)

return (
self.cos_cached[:seq_len].to(dtype=x.dtype) * self.attention_scaling,
self.sin_cached[:seq_len].to(dtype=x.dtype) * self.attention_scaling,
)


def qeff_apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
"""Applies Rotary Position Embedding to the query and key tensors.
Expand Down Expand Up @@ -108,9 +98,6 @@ class QEffFalconAttention(FalconAttention):
- add new args position idx for the cache_kwargs for kv retention
"""

def __qeff_init__(self):
self.rotary_emb = QEffFalconRotaryEmbedding(config=self.config)

def forward(
self,
hidden_states: torch.Tensor,
Expand All @@ -125,6 +112,8 @@ def forward(
use_cache: bool = False,
output_attentions: bool = False,
cache_position: Optional[torch.LongTensor] = None,
cos_cached: Optional[torch.Tensor] = None,
sin_cached: Optional[torch.Tensor] = None,
):
fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size]
num_kv_heads = self.num_heads if self.new_decoder_architecture else self.num_kv_heads
Expand All @@ -137,9 +126,8 @@ def forward(
key_layer = key_layer.transpose(1, 2).reshape(batch_size, num_kv_heads, query_length, self.head_dim)
value_layer = value_layer.transpose(1, 2).reshape(batch_size, num_kv_heads, query_length, self.head_dim)

kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
cos, sin = self.rotary_emb(value_layer, seq_len=kv_seq_len)
query_layer, key_layer = qeff_apply_rotary_pos_emb(query_layer, key_layer, cos, sin, position_ids)
# kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
query_layer, key_layer = qeff_apply_rotary_pos_emb(query_layer, key_layer, cos_cached, sin_cached, position_ids)

if layer_past is not None:
cache_kwargs = {"batch_index": batch_index, "position_ids": position_ids}
Expand Down Expand Up @@ -184,6 +172,8 @@ def forward(
use_cache: bool = False,
output_attentions: bool = False,
cache_position: Optional[torch.LongTensor] = None,
sin_cached=None,
cos_cached=None,
**kwargs,
):
residual = hidden_states
Expand All @@ -208,6 +198,8 @@ def forward(
use_cache=use_cache,
output_attentions=output_attentions,
cache_position=cache_position,
sin_cached=sin_cached,
cos_cached=cos_cached,
)

if not self.config.new_decoder_architecture:
Expand Down Expand Up @@ -245,6 +237,11 @@ class QEffFalconModel(FalconModel):
- update causal attention mask
"""

def __qeff_init__(self):
self.rotary_emb = QEffFalconRotaryEmbedding(config=self.config)
self.sin_cached = torch.nn.Parameter(self.rotary_emb.sin_cached * self.rotary_emb.attention_scaling)
self.cos_cached = torch.nn.Parameter(self.rotary_emb.cos_cached * self.rotary_emb.attention_scaling)

def forward(
self,
input_ids: torch.LongTensor = None,
Expand Down Expand Up @@ -322,6 +319,8 @@ def forward(
output_attentions=output_attentions,
alibi=alibi,
cache_position=cache_position,
sin_cached=self.sin_cached,
cos_cached=self.cos_cached,
)

hidden_states = outputs[0]
Expand Down
33 changes: 17 additions & 16 deletions QEfficient/transformers/models/gemma/modeling_gemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ def _set_cos_sin_cache(self, seq_len, device, dtype):
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)

def forward(self, x, seq_len=None):
# x: [bs, num_attention_heads, seq_len, head_size]
if seq_len > self.max_seq_len_cached:
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)

return (
self.cos_cached[:seq_len].to(dtype=x.dtype),
self.sin_cached[:seq_len].to(dtype=x.dtype),
)


def qeff_apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
"""Applies Rotary Position Embedding to the query and key tensors.
Expand Down Expand Up @@ -128,9 +118,6 @@ class QEffGemmaAttention(GemmaAttention):
- add new args cache idx for the kv retention
"""

def __qeff_init__(self):
self.rotary_emb = QEffGemmaRotaryEmbedding(config=self.config)

def forward(
self,
hidden_states: torch.Tensor,
Expand All @@ -140,6 +127,8 @@ def forward(
comp_ctx_lengths: Optional[torch.LongTensor] = None,
batch_index: Optional[torch.LongTensor] = None,
cache_position: Optional[torch.LongTensor] = None,
cos_cached: Optional[torch.Tensor] = None,
sin_cached: Optional[torch.Tensor] = None,
**kwargs,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
input_shape = hidden_states.shape[:-1]
Expand All @@ -149,9 +138,10 @@ def forward(
key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)

kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
query_states, key_states = qeff_apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
# kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
query_states, key_states = qeff_apply_rotary_pos_emb(
query_states, key_states, cos_cached, sin_cached, position_ids
)

if past_key_value is not None:
cache_kwargs = {"batch_index": batch_index, "position_ids": position_ids}
Expand Down Expand Up @@ -194,6 +184,8 @@ def forward(
batch_index: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = False,
cache_position: Optional[torch.LongTensor] = None,
sin_cached=None,
cos_cached=None,
**kwargs,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
"""
Expand Down Expand Up @@ -223,6 +215,8 @@ def forward(
batch_index=batch_index,
use_cache=use_cache,
cache_position=cache_position,
sin_cached=sin_cached,
cos_cached=cos_cached,
**kwargs,
)
hidden_states = residual + hidden_states
Expand All @@ -243,6 +237,11 @@ class QEffGemmaModel(GemmaModel):
- add new args cache idx for the kv retention
"""

def __qeff_init__(self):
self.rotary_emb = QEffGemmaRotaryEmbedding(config=self.config)
self.sin_cached = torch.nn.Parameter(self.rotary_emb.sin_cached)
self.cos_cached = torch.nn.Parameter(self.rotary_emb.cos_cached)

def forward(
self,
input_ids: torch.LongTensor = None,
Expand Down Expand Up @@ -310,6 +309,8 @@ def forward(
batch_index=batch_index,
use_cache=use_cache,
cache_position=cache_position,
sin_cached=self.sin_cached,
cos_cached=self.cos_cached,
**kwargs,
)

Expand Down
37 changes: 19 additions & 18 deletions QEfficient/transformers/models/gemma2/modeling_gemma2.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ def _set_cos_sin_cache(self, seq_len, device, dtype):
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)

def forward(self, x, seq_len=None):
# x: [bs, num_attention_heads, seq_len, head_size]
if seq_len > self.max_seq_len_cached:
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)

return (
self.cos_cached[:seq_len].to(dtype=x.dtype),
self.sin_cached[:seq_len].to(dtype=x.dtype),
)


def qeff_apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
"""Applies Rotary Position Embedding to the query and key tensors.
Expand Down Expand Up @@ -135,9 +125,6 @@ class QEffGemma2Attention(Gemma2Attention):
- add new args cache idx for the kv retention
"""

def __qeff_init__(self):
self.rotary_emb = QEffGemma2RotaryEmbedding(config=self.config)

def forward(
self,
hidden_states: torch.Tensor,
Expand All @@ -147,6 +134,8 @@ def forward(
comp_ctx_lengths: Optional[torch.LongTensor] = None,
batch_index: Optional[torch.LongTensor] = None,
cache_position: Optional[torch.LongTensor] = None,
cos_cached: Optional[torch.Tensor] = None,
sin_cached: Optional[torch.Tensor] = None,
**kwargs,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
input_shape = hidden_states.shape[:-1]
Expand All @@ -156,15 +145,16 @@ def forward(
key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)

kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
query_states, key_states = qeff_apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
# kv_seq_len = past_key_value.get_seq_length(self.layer_idx, cache_position)
query_states, key_states = qeff_apply_rotary_pos_emb(
query_states, key_states, cos_cached, sin_cached, position_ids
)

if past_key_value is not None:
# sin and cos are specific to RoPE models; cache_position needed for the static cache
cache_kwargs = {
"sin": sin,
"cos": cos,
"sin": sin_cached,
"cos": cos_cached,
"batch_index": batch_index,
"position_ids": position_ids,
}
Expand Down Expand Up @@ -208,6 +198,8 @@ def forward(
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = False,
cache_position: Optional[torch.LongTensor] = None,
sin_cached=None,
cos_cached=None,
**kwargs,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
"""
Expand Down Expand Up @@ -241,6 +233,8 @@ def forward(
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
sin_cached=sin_cached,
cos_cached=cos_cached,
**kwargs,
)
hidden_states = self.post_attention_layernorm(hidden_states)
Expand Down Expand Up @@ -271,6 +265,11 @@ class QEffGemma2Model(Gemma2Model):
- add new args cache idx for the kv retention
"""

def __qeff_init__(self):
self.rotary_emb = QEffGemma2RotaryEmbedding(config=self.config)
self.sin_cached = torch.nn.Parameter(self.rotary_emb.sin_cached)
self.cos_cached = torch.nn.Parameter(self.rotary_emb.cos_cached)

def forward(
self,
input_ids: torch.LongTensor = None,
Expand Down Expand Up @@ -355,6 +354,8 @@ def forward(
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
sin_cached=self.sin_cached,
cos_cached=self.cos_cached,
**kwargs,
)

Expand Down
Loading
Loading