Skip to content

Fix Unit Test error brought by Transformer Breaking Changes #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 16 additions & 2 deletions benchmark/scripts/benchmark_rope.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import torch
import triton

from transformers import __version__ as transformers_version
from transformers.models.llama.configuration_llama import LlamaConfig
from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding
from transformers.models.llama.modeling_llama import apply_rotary_pos_emb
from utils import QUANTILES
Expand Down Expand Up @@ -30,7 +32,13 @@ def bench_speed_rope(input: SingleBenchmarkRunInput) -> SingleBenchmarkRunOutput
seq_len = extra_benchmark_config["seq_len"] if "seq_len" in extra_benchmark_config else input.x

head_dim = hidden_size // num_q_heads
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)

if transformers_version < "4.48.0":
# LlamaRotaryEmbedding constructor signature changed in transformers 4.48.0
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
else:
llama_config = LlamaConfig(head_dim=head_dim)
rotary_emb = LlamaRotaryEmbedding(llama_config, device=device)
q = torch.randn(
(1, seq_len, num_q_heads, head_dim),
device=device,
Expand Down Expand Up @@ -105,7 +113,13 @@ def bench_memory_rope(input: SingleBenchmarkRunInput) -> SingleBenchmarkRunOutpu
seq_len = extra_benchmark_config["seq_len"] if "seq_len" in extra_benchmark_config else input.x

head_dim = hidden_size // num_q_heads
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)

if transformers_version < "4.48.0":
# LlamaRotaryEmbedding constructor signature changed in transformers 4.48.0
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
else:
llama_config = LlamaConfig(head_dim=head_dim)
rotary_emb = LlamaRotaryEmbedding(llama_config, device=device)
Comment on lines +117 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we are repeating same code in all places. Shall we prepare a common function to get the rotary embeddings?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @kvignesh1420 thanks for the great advice, I added a more general helper function here: #526.

q = torch.randn(
(1, seq_len, num_q_heads, head_dim),
device=device,
Expand Down
16 changes: 14 additions & 2 deletions test/transformers/test_rope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import torch

from test.utils import supports_bfloat16
from transformers import __version__ as transformers_version
from transformers.models.llama.configuration_llama import LlamaConfig
from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding
from transformers.models.llama.modeling_llama import apply_rotary_pos_emb

Expand Down Expand Up @@ -57,7 +59,12 @@ def test_correctness(
atol,
rtol,
):
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
if transformers_version < "4.48.0":
# LlamaRotaryEmbedding constructor signature changed in transformers 4.48.0
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
else:
llama_config = LlamaConfig(head_dim=head_dim)
rotary_emb = LlamaRotaryEmbedding(llama_config, device=device)

_tensor_q = torch.randn((bsz, seq_len, num_q_heads, head_dim), device=device).transpose(1, 2).to(dtype)

Expand Down Expand Up @@ -133,7 +140,12 @@ def test_functional_correctness(
k1 = _k.clone().requires_grad_(True)
k2 = _k.clone().requires_grad_(True)

rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
if transformers_version < "4.48.0":
# LlamaRotaryEmbedding constructor signature changed in transformers 4.48.0
rotary_emb = LlamaRotaryEmbedding(head_dim, device=device)
else:
llama_config = LlamaConfig(head_dim=head_dim)
rotary_emb = LlamaRotaryEmbedding(llama_config, device=device)

pos_ids = torch.arange(seq_len, device=device, dtype=torch.long).unsqueeze(0)
if expand_position_ids:
Expand Down
Loading