Skip to content

Commit

Permalink
Albert is ExecuTorch compatible (huggingface#34476)
Browse files Browse the repository at this point in the history
Co-authored-by: Guang Yang <[email protected]>
  • Loading branch information
2 people authored and BernardZach committed Dec 5, 2024
1 parent d6147a0 commit 4f7b5e2
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion tests/models/albert/test_modeling_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import unittest

from transformers import AlbertConfig, is_torch_available
from packaging import version

from transformers import AlbertConfig, AutoTokenizer, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

Expand Down Expand Up @@ -342,3 +344,45 @@ def test_inference_no_head_absolute_embedding(self):
)

self.assertTrue(torch.allclose(output[:, 1:4, 1:4], expected_slice, atol=1e-4))

@slow
def test_export(self):
if version.parse(torch.__version__) < version.parse("2.4.0"):
self.skipTest(reason="This test requires torch >= 2.4 to run.")

distilbert_model = "albert/albert-base-v2"
device = "cpu"
attn_implementation = "sdpa"
max_length = 64

tokenizer = AutoTokenizer.from_pretrained(distilbert_model)
inputs = tokenizer(
f"Paris is the {tokenizer.mask_token} of France.",
return_tensors="pt",
padding="max_length",
max_length=max_length,
)

model = AlbertForMaskedLM.from_pretrained(
distilbert_model,
device_map=device,
attn_implementation=attn_implementation,
)

logits = model(**inputs).logits
eg_predicted_mask = tokenizer.decode(logits[0, 4].topk(5).indices)
self.assertEqual(
eg_predicted_mask.split(),
["capital", "capitol", "comune", "arrondissement", "bastille"],
)

exported_program = torch.export.export(
model,
args=(inputs["input_ids"],),
kwargs={"attention_mask": inputs["attention_mask"]},
strict=True,
)

result = exported_program.module().forward(inputs["input_ids"], inputs["attention_mask"])
ep_predicted_mask = tokenizer.decode(result.logits[0, 4].topk(5).indices)
self.assertEqual(eg_predicted_mask, ep_predicted_mask)

0 comments on commit 4f7b5e2

Please sign in to comment.