huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
134.26k stars 26.85k forks source link

AttributeError: 'list' object has no attribute 'clone' with BartTokenizer #6924

Closed spate141 closed 4 years ago

spate141 commented 4 years ago

Environment info

Who can help

Information

The problem arises when using:

The tasks I am working on is:

To reproduce

Steps to reproduce the behavior:

from transformers import BartTokenizer, BartForConditionalGeneration

model = BartTokenizer.from_pretrained('/Downloads/facebook-bart-large-cnn')
tokenizer = BartForConditionalGeneration.from_pretrained('/Downloads/facebook-bart-large-cnn')

raw_text = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison.  Her next court appearance is scheduled for May 18.
"""

inputs = tokenizer([raw_text], max_length=1024, return_tensors='pt', truncation=True)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, kwargs) 720 result = self._slow_forward(*input, *kwargs) 721 else: --> 722 result = self.forward(input, kwargs) 723 for hook in itertools.chain( 724 _global_forward_hooks.values(),

~/anaconda3/lib/python3.7/site-packages/transformers/modeling_bart.py in forward(self, input_ids, attention_mask, encoder_outputs, decoder_input_ids, decoder_attention_mask, past_key_values, labels, use_cache, output_attentions, output_hidden_states, return_dict, **unused) 1074 output_attentions=output_attentions, 1075 output_hidden_states=output_hidden_states, -> 1076 return_dict=return_dict, 1077 ) 1078 lm_logits = F.linear(outputs[0], self.model.shared.weight, bias=self.final_logits_bias)

~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, kwargs) 720 result = self._slow_forward(*input, *kwargs) 721 else: --> 722 result = self.forward(input, kwargs) 723 for hook in itertools.chain( 724 _global_forward_hooks.values(),

~/anaconda3/lib/python3.7/site-packages/transformers/modeling_bart.py in forward(self, input_ids, attention_mask, decoder_input_ids, encoder_outputs, decoder_attention_mask, past_key_values, use_cache, output_attentions, output_hidden_states, return_dict, **kwargs) 904 decoder_input_ids=decoder_input_ids, 905 decoder_padding_mask=decoder_attention_mask, --> 906 causal_mask_dtype=self.shared.weight.dtype, 907 ) 908 else:

~/anaconda3/lib/python3.7/site-packages/transformers/modeling_bart.py in _prepare_bart_decoder_inputs(config, input_ids, decoder_input_ids, decoder_padding_mask, causal_mask_dtype) 146 pad_token_id = config.pad_token_id 147 if decoder_input_ids is None: --> 148 decoder_input_ids = shift_tokens_right(input_ids, pad_token_id) 149 bsz, tgt_len = decoder_input_ids.size() 150 if decoder_padding_mask is None:

~/anaconda3/lib/python3.7/site-packages/transformers/modeling_bart.py in shift_tokens_right(input_ids, pad_token_id) 204 def shift_tokens_right(input_ids, pad_token_id): 205 """Shift input ids one token to the right, and wrap the last non pad token (usually ).""" --> 206 prev_output_tokens = input_ids.clone() 207 index_of_eos = (input_ids.ne(pad_token_id).sum(dim=1) - 1).unsqueeze(-1) 208 prev_output_tokens[:, 0] = input_ids.gather(1, index_of_eos).squeeze()

AttributeError: 'list' object has no attribute 'clone'

sshleifer commented 4 years ago

I think you flipped model and tokenizer at the beginning. It should be


from transformers import BartTokenizer, BartForConditionalGeneration

tokenizer = BartTokenizer.from_pretrained('/Downloads/facebook-bart-large-cnn')
model = BartForConditionalGeneration.from_pretrained('/Downloads/facebook-bart-large-cnn')
sshleifer commented 4 years ago

Pls reopen if there is another issue!

spate141 commented 4 years ago

Damn, this was embarrassing bug on my end. Thank you! 🍻