Skip to content

Commit

Permalink
Show correct error message on using BT for SDPA models (#1599)
Browse files Browse the repository at this point in the history
show correct error message bt
  • Loading branch information
fxmarty authored Dec 15, 2023
1 parent 4a0f16f commit 7376c6a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 6 additions & 7 deletions optimum/bettertransformer/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def transform(
The converted model if the conversion has been successful.
"""

hf_config = model.config
if hf_config.model_type in ["falcon", "gpt_bigcode", "llama", "whisper"]:
raise ValueError(
f"Transformers now supports natively BetterTransformer optimizations (torch.nn.functional.scaled_dot_product_attention) for the model type {hf_config.model_type}. Please upgrade to transformers>=4.36 and torch>=2.1.1 to use it. Details: https://huggingface.co/docs/transformers/perf_infer_gpu_one#flashattention-and-memory-efficient-attention-through-pytorchs-scaleddotproductattention"
)

# Check if we have to load the model using `accelerate`
if hasattr(model, "hf_device_map"):
load_accelerate = True
Expand Down Expand Up @@ -236,13 +242,6 @@ def transform(
f"BetterTransformer requires torch>=2.0 but {torch.__version__} is installed. Please upgrade PyTorch."
)

hf_config = model.config

if hf_config.model_type in ["falcon", "gpt_bigcode", "llama", "whisper"]:
raise ValueError(
f"Transformers now supports natively BetterTransformer optimizations (torch.nn.functional.scaled_dot_product_attention) for the model type {hf_config.model_type}. Please upgrade to transformers>=4.36 and torch>=2.1.1 to use it. Details: https://huggingface.co/docs/transformers/perf_infer_gpu_one#flashattention-and-memory-efficient-attention-through-pytorchs-scaleddotproductattention"
)

if load_accelerate:
# Remove the hooks from the original model to avoid weights being on `meta` device.
remove_hook_from_module(model, recurse=True)
Expand Down
10 changes: 10 additions & 0 deletions tests/bettertransformer/test_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
]


class TestsWhisper(unittest.TestCase):
def test_error_message(self):
model = AutoModel.from_pretrained("openai/whisper-tiny")

with self.assertRaises(ValueError) as cm:
model = BetterTransformer.transform(model)

self.assertTrue("Transformers now supports natively BetterTransformer optimizations" in str(cm.exception))


class BetterTransformersBarkTest(BetterTransformersTestMixin, unittest.TestCase):
r"""
Testing suite for Bark - tests all the tests defined in `BetterTransformersTestMixin`
Expand Down

0 comments on commit 7376c6a

Please sign in to comment.