T5 AttributeError: 'T5Encoder' object has no attribute 'main_input_name'

See original GitHub issue

I trained a T5 model and when I ran the inference it gave me error as mentioned in title. I installed required package as shown below. It was working yesterday but now it returned me this attribute error.

!pip install sentencepiece==0.1.95
!pip install torch
!pip install transformers
!pip install pytorch_lightning==0.7.5
import torch
from transformers import T5ForConditionalGeneration,T5Tokenizer

def get_model_output(sentence,model):
    c_output = []
    text =  sentence + ' </s>'

    max_len = 256

    encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
    input_ids, attention_masks = encoding["input_ids"].to("cpu"), encoding["attention_mask"].to("cpu")


    # set top_k = 50 and set top_p = 0.95 and num_return_sequences = 3
    beam_outputs= model.generate(
          input_ids=input_ids, attention_mask=attention_masks,
          max_length=64,
          num_beams=10, 
          no_repeat_ngram_size=2, 
          do_sample = False,
          num_return_sequences=5, 
          early_stopping=True
      )
    final_outputs =[]
    for beam_output in beam_outputs:
        sent = tokenizer.decode(beam_output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
        if sent.lower() != sentence.lower() and sent not in final_outputs:
            final_outputs.append(sent)

    for final_output in final_outputs:
        c_output.append(final_output)
    return `c_output[0]`

Here is the error message:

[/usr/local/lib/python3.7/dist-packages/transformers/generation_utils.py](https://localhost:8080/#) in generate(self, inputs, max_length, min_length, do_sample, early_stopping, num_beams, temperature, top_k, top_p, repetition_penalty, bad_words_ids, bos_token_id, pad_token_id, eos_token_id, length_penalty, no_repeat_ngram_size, encoder_no_repeat_ngram_size, num_return_sequences, max_time, max_new_tokens, decoder_start_token_id, use_cache, num_beam_groups, diversity_penalty, prefix_allowed_tokens_fn, logits_processor, stopping_criteria, output_attentions, output_hidden_states, output_scores, return_dict_in_generate, forced_bos_token_id, forced_eos_token_id, remove_invalid_values, synced_gpus, **model_kwargs)
   1067         # otherwise model_input_name is None
   1068         # all model-specific keyword inputs are removed from `model_kwargs`
-> 1069         inputs_tensor, model_input_name, model_kwargs = self._prepare_model_inputs(inputs, bos_token_id, model_kwargs)
   1070         batch_size = inputs_tensor.shape[0]
   1071 

[/usr/local/lib/python3.7/dist-packages/transformers/generation_utils.py](https://localhost:8080/#) in _prepare_model_inputs(self, inputs, bos_token_id, model_kwargs)
    392             self.config.is_encoder_decoder
    393             and hasattr(self, "encoder")
--> 394             and self.encoder.main_input_name != self.main_input_name
    395         ):
    396             input_name = self.encoder.main_input_name

[/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in __getattr__(self, name)
   1176                 return modules[name]
   1177         raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1178             type(self).__name__, name))
   1179 
   1180     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'T5Encoder' object has no attribute 'main_input_name'

Can anyone suggest? Thanks in advance!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
magichz86commented, Mar 4, 2022

I got the same issue,the version of my transformer is 4.16.2. Solved the issue by reducing the version to 4.10.0.

0reactions
dyxohjl666commented, Nov 6, 2022

However, the rest of the project depends on newer transformers version. How should I change the code accordingly to avoid this error?

I met the same issue. Have you solved it? 😭

Read more comments on GitHub >

github_iconTop Results From Across the Web

Source code for transformers.models.t5.modeling_t5
Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the :meth:`~transformers.PreTrainedModel.
Read more >
AttributeError: 'T5Config' object has no attribute 'adapters'
I've created the .pkl object of the T5-base model and tried to execute it but suddenly I got this error message. I wondered...
Read more >
EncT5: Fine-tuning T5 Encoder for Discriminative Tasks
Table 1: Results on the GLUE test set. Following the GLUE leaderboard, for tasks with multiple metrics (including. MNLI), the metrics are averaged...
Read more >
Projectwise transformers gets 'NoneType' object has no ...
ProjectWiseWSGConnector: Uploading file "testfile.shp" to Bentley ProjectWise; Python Exception <AttributeError>: 'NoneType' object has no attribute 'encode ...
Read more >
Module Main has No Attribute... (on Pipelines and Pickles)
pickle and joblib work the same way in this regard. Import information is stored with the pickled object, which is why you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found