Open rtadewald opened 6 months ago
Also ran into this today. It goes back to a change in transformers library.
The quickest fix is to go back to an older transformers version: pip install transformers==4.38.2
This is no good solution for the future of course. The problem is that the error occurs between Coqui TTS, torch and transformers library. I guess either torch or Coqui TTS would need to adjust for the change. If it's torch they for sure will adress this. If it's Coqui TTS we have a problem, since it isn't maintained anymore and they have transformers>=4.33.0 in their requirements.
If you'd want to work with latest version of transformers you need to change the source code. You can open transformers/generation/utils.py, go to _prepare_attention_mask_for_generation method, replace this code:
is_pad_token_in_inputs = (pad_token_id is not None) and (
torch.isin(elements=inputs, test_elements=pad_token_id).any()
)
is_pad_token_not_equal_to_eos_token_id = (eos_token_id is None) or ~(
torch.isin(elements=eos_token_id, test_elements=pad_token_id).any()
)
with this code
pad_token_id_tensor = torch.tensor(pad_token_id, device=inputs.device)
if eos_token_id is not None:
eos_token_id_tensor = torch.tensor(eos_token_id, device=inputs.device)
else:
eos_token_id_tensor = None
is_pad_token_in_inputs = (pad_token_id is not None) and (
torch.isin(elements=inputs, test_elements=pad_token_id_tensor).any()
)
is_pad_token_not_equal_to_eos_token_id = (eos_token_id is None) or ~(
torch.isin(elements=eos_token_id_tensor, test_elements=pad_token_id_tensor).any()
)
in front of the line with is_pad_token_in_inputs =
. This will also fix the error and allow you to use the latest transformers version. This also is not a very satisfying solution because noone wants to change transformers source code for every new version released. I'm also not sure if this fix has any side effects.
Thank you for this work. Can you provide the modified function with the update for temp fix to use mps?
def _prepare_attention_mask_for_generation( self, inputs: torch.Tensor, pad_token_id: Optional[torch.Tensor], eos_token_id: Optional[torch.Tensor], ) -> torch.LongTensor:
default_attention_mask = torch.ones(inputs.shape[:2], dtype=torch.long, device=inputs.device)
if pad_token_id is None:
return default_attention_mask
is_input_ids = len(inputs.shape) == 2 and inputs.dtype in [torch.int, torch.long]
if not is_input_ids:
return default_attention_mask
# Otherwise we have may have information -> try to infer the attention mask
"""if inputs.device.type == "mps":
# mps does not support torch.isin (https://github.com/pytorch/pytorch/issues/77764)
raise ValueError(
"Can't infer missing attention mask on `mps` device. Please provide an `attention_mask` or use a different device."
)"""
is_pad_token_in_inputs = (pad_token_id is not None) and (
torch.isin(elements=inputs, test_elements=pad_token_id).any()
)
is_pad_token_not_equal_to_eos_token_id = (eos_token_id is None) or ~(
torch.isin(elements=eos_token_id, test_elements=pad_token_id).any()
)
can_infer_attention_mask = is_pad_token_in_inputs * is_pad_token_not_equal_to_eos_token_id
attention_mask_from_padding = inputs.ne(pad_token_id).long()
attention_mask = (
attention_mask_from_padding * can_infer_attention_mask + default_attention_mask * ~can_infer_attention_mask
)
return attention_mask
This is the full fixed method:
def _prepare_attention_mask_for_generation(
self,
inputs: torch.Tensor,
pad_token_id: Optional[torch.Tensor],
eos_token_id: Optional[torch.Tensor],
) -> torch.LongTensor:
# No information for attention mask inference -> return default attention mask
default_attention_mask = torch.ones(inputs.shape[:2], dtype=torch.long, device=inputs.device)
if pad_token_id is None:
return default_attention_mask
is_input_ids = len(inputs.shape) == 2 and inputs.dtype in [torch.int, torch.long]
if not is_input_ids:
return default_attention_mask
# Otherwise we have may have information -> try to infer the attention mask
if inputs.device.type == "mps":
# mps does not support torch.isin (https://github.com/pytorch/pytorch/issues/77764)
raise ValueError(
"Can't infer missing attention mask on `mps` device. Please provide an `attention_mask` or use a different device."
)
is_pad_token_in_inputs = (pad_token_id is not None) and (
torch.isin(elements=inputs, test_elements=pad_token_id).any()
)
is_pad_token_not_equal_to_eos_token_id = (eos_token_id is None) or ~(
torch.isin(elements=eos_token_id, test_elements=pad_token_id).any()
)
can_infer_attention_mask = is_pad_token_in_inputs * is_pad_token_not_equal_to_eos_token_id
attention_mask_from_padding = inputs.ne(pad_token_id).long()
attention_mask = (
attention_mask_from_padding * can_infer_attention_mask + default_attention_mask * ~can_infer_attention_mask
)
return attention_mask
I guess this will still not work with mps looking at the code that says: "mps does not support torch.isin". This is something out of my scope and needs to be done by pytorch team.
Hello. First of all, thank you so much for sharing this amazing library with all of us.
I've tried to run the Coqui Engine in my Macbook Pro M1 Max Chip, using the coqui_test.py file in the test directory, but received the following error: Do you know what could it be?
Thank you in advance.