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
10 changes: 10 additions & 0 deletions examples/language-modeling/run_lora_clm.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ class ModelArguments:
)
},
)
use_fused_rope: bool = field(
default=True,
metadata={
"help": (
"Whether to use Habana fused-rope for fine-tuning. The current support is limited to Llama only.",
)
},
)
load_meta_device: bool = field(
default=False,
metadata={
Expand Down Expand Up @@ -540,6 +548,8 @@ def main():
if model_args.use_flash_attention:
model.generation_config.use_flash_attention = True
model.generation_config.flash_attention_recompute = model_args.flash_attention_recompute
if model_args.use_fused_rope is False:
model.generation_config.use_fused_rope = False

if hasattr(model.generation_config, "pad_token_id") and model.generation_config.pad_token_id is not None:
tokenizer.pad_token_id = model.generation_config.pad_token_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ def __init__(self, **kwargs):
self.kv_cache_fp8 = kwargs.get("kv_cache_fp8", None)
self.use_flash_attention = kwargs.get("use_flash_attention", None)
self.flash_attention_recompute = kwargs.get("flash_attention_recompute", None)
self.use_fused_rope = kwargs.get("use_fused_rope", None)
1 change: 1 addition & 0 deletions optimum/habana/transformers/generation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ def generate(
# determine whether flash attention needs to be used
model_kwargs["use_flash_attention"] = generation_config.use_flash_attention
model_kwargs["flash_attention_recompute"] = True if generation_config.flash_attention_recompute else False
model_kwargs["use_fused_rope"] = False if generation_config.use_fused_rope is False else True

if not self.config.is_encoder_decoder:
calculated_max_length = input_ids.shape[-1]
Expand Down
18 changes: 15 additions & 3 deletions optimum/habana/transformers/models/llama/modeling_llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def pre_attn_forward(
use_flash_attention: Optional[bool] = False,
flash_attention_recompute: Optional[bool] = False,
cache_idx: int = None,
use_fused_rope: Optional[bool] = True,
**kwargs,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
"""
Expand Down Expand Up @@ -274,7 +275,9 @@ def pre_attn_forward(
kv_seq_len = past_key_value[0].shape[-2]

cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
query_states, key_states = apply_customized_rope(query_states, key_states, cos, sin, position_ids)
query_states, key_states = apply_customized_rope(
query_states, key_states, cos, sin, position_ids, use_fused_rope=use_fused_rope
)

if past_key_value is not None or reuse_cache:
# reuse k, v, self_attention
Expand Down Expand Up @@ -456,6 +459,7 @@ def forward(
use_flash_attention: Optional[bool] = False,
flash_attention_recompute: Optional[bool] = False,
cache_idx: int = None,
use_fused_rope: Optional[bool] = True,
**kwargs,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
"""
Expand Down Expand Up @@ -486,6 +490,7 @@ def forward(
use_flash_attention=use_flash_attention,
flash_attention_recompute=flash_attention_recompute,
cache_idx=cache_idx,
use_fused_rope=use_fused_rope,
**kwargs,
)
self.self_attn.attention_all_reduce(output_pre_attn)
Expand Down Expand Up @@ -516,6 +521,7 @@ def pre_attn(
use_flash_attention: Optional[bool] = False,
flash_attention_recompute: Optional[bool] = False,
cache_idx: int = None,
use_fused_rope: Optional[bool] = True,
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
hidden_states = self.input_layernorm(hidden_states)
output_attn, attn_weights, present_key_value = self.self_attn.pre_attn_forward(
Expand All @@ -531,6 +537,7 @@ def pre_attn(
use_flash_attention,
flash_attention_recompute,
cache_idx=cache_idx,
use_fused_rope=use_fused_rope,
)
return output_attn, attn_weights, present_key_value

Expand Down Expand Up @@ -580,6 +587,7 @@ def forward(
use_flash_attention: Optional[bool] = False,
flash_attention_recompute: Optional[bool] = False,
cache_idx: int = None,
use_fused_rope: Optional[bool] = True,
) -> Union[Tuple, BaseModelOutputWithPast]:
"""
Copied from LlamaModel.forward: https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py
Expand Down Expand Up @@ -682,6 +690,7 @@ def forward(
False,
use_flash_attention,
flash_attention_recompute,
use_fused_rope,
)
else:
layer_outputs = decoder_layer(
Expand All @@ -697,6 +706,7 @@ def forward(
use_flash_attention=use_flash_attention,
flash_attention_recompute=flash_attention_recompute,
cache_idx=cache_idx,
use_fused_rope=use_fused_rope,
)

hidden_states = layer_outputs[0]
Expand Down Expand Up @@ -771,6 +781,7 @@ def forward(
use_flash_attention: Optional[bool] = False,
flash_attention_recompute: Optional[bool] = False,
cache_idx: int = None,
use_fused_rope: Optional[bool] = True,
) -> Union[Tuple, CausalLMOutputWithPast]:
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
output_hidden_states = (
Expand All @@ -794,6 +805,7 @@ def forward(
use_flash_attention=use_flash_attention,
flash_attention_recompute=flash_attention_recompute,
cache_idx=cache_idx,
use_fused_rope=use_fused_rope,
)
hidden_states = outputs[0]
_, seq_len, _ = hidden_states.shape
Expand Down Expand Up @@ -911,8 +923,8 @@ def prepare_inputs_for_generation(
return model_inputs


def apply_customized_rope(q, k, cos, sin, position_ids):
if q.device.type == "hpu" and FusedRoPE:
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:
# TODO: remove `.clone()` when it is fixed in SynapseAI
return FusedRoPE.apply(
q, cos.unsqueeze(0).unsqueeze(0).clone(), sin.unsqueeze(0).unsqueeze(0).clone(), position_ids
Expand Down
4 changes: 4 additions & 0 deletions optimum/habana/transformers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,8 @@ def hpu_deepspeed_checkpointing(function, *checkpoint_args):
inputs["use_flash_attention"] = True
if self.model.generation_config.flash_attention_recompute:
inputs["flash_attention_recompute"] = True
if self.model.generation_config.use_fused_rope is False:
inputs["use_fused_rope"] = False

# TODO: keep syncs for fast DDP?
with self.accelerator.accumulate(model):
Expand Down Expand Up @@ -1684,6 +1686,8 @@ def evaluation_loop(
inputs["use_flash_attention"] = True
if self.model.generation_config.flash_attention_recompute:
inputs["flash_attention_recompute"] = True
if self.model.generation_config.use_fused_rope is False:
inputs["use_fused_rope"] = False

# Prediction step
loss, logits, labels = self.prediction_step(model, inputs, prediction_loss_only, ignore_keys=ignore_keys)
Expand Down