Skip to content
Merged
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
12 changes: 8 additions & 4 deletions optimum/habana/transformers/models/llama/modeling_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@

try:
from habana_frameworks.torch.hpex.kernels import RotaryPosEmbeddingHelperV2 as FusedRoPE

has_fused_rope = True
except ImportError:
has_fused_rope = False
print("Not using HPU fused kernel for apply_rotary_pos_emb")
FusedRoPE = None

try:
from habana_frameworks.torch.hpex.normalization import FusedRMSNorm as FusedRMSNorm

has_fused_rms_norm = True
except ImportError:
has_fused_rms_norm = False
print("Not using HPU fused kernel for RMSNorm")
FusedRMSNorm = None

try:
from habana_frameworks.torch.hpex.kernels import FusedSDPA
Expand All @@ -52,7 +56,7 @@ def gaudi_llama_rmsnorm_forward(self, hidden_states):
The only differences are:
- override RMSNorm with Habana fused RMSNorm
"""
if hidden_states.device.type == "hpu" and FusedRMSNorm:
if hidden_states.device.type == "hpu" and has_fused_rms_norm:
# mixed dtypes are not good for FusedRMSNorm, both inputs need to have same dtype
if hidden_states.dtype != self.weight.dtype:
orig_dtype = hidden_states.dtype
Expand Down Expand Up @@ -952,7 +956,7 @@ def prepare_inputs_for_generation(


def apply_customized_rope(q, k, cos, sin, position_ids, use_fused_rope=True):
if q.device.type == "hpu" and FusedRoPE and use_fused_rope:
if q.device.type == "hpu" and has_fused_rope and use_fused_rope:
# TODO: remove `.clone()` when SynapseAI v1.15 is released
return FusedRoPE.apply(
q, cos.unsqueeze(0).unsqueeze(0).clone(), sin.unsqueeze(0).unsqueeze(0).clone(), position_ids
Expand Down