huggingface / transformers

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

AttributeError: module 'tensorflow' has no attribute 'Tensor' when using documentation code (Tokenizer.batch_decode) #21135

Closed gaurav-95 closed 1 year ago

gaurav-95 commented 1 year ago

System Info

Windows 10, VSCode

Who can help?

No response

Information

I was referring to the documentation on huggingface to run the facebook OPT model here:

https://huggingface.co/docs/transformers/main/en/model_doc/opt#transformers.OPTForCausalLM

And I've received the following error on my Windows 10 machine in VScode.

Traceback (most recent call last):
  File "c:/Users/Admin/Desktop/Projects/NLP_Cron/script_chat.py", line 11, in <module>
    print(tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0])
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\tokenization_utils_base.py", line 3429, in batch_decode
    return [
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\tokenization_utils_base.py", line 3430, in <listcomp>
    self.decode(
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\tokenization_utils_base.py", line 3466, in decode
    token_ids = to_py_obj(token_ids)
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\utils\generic.py", line 160, in to_py_obj
    elif is_tf_tensor(obj):
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\utils\generic.py", line 136, in is_tf_tensor
    return False if not is_tf_available() else _is_tensorflow(x)
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\utils\generic.py", line 129, in _is_tensorflow
    return isinstance(x, tf.Tensor)
AttributeError: module 'tensorflow' has no attribute 'Tensor'

I first thought it was specific to this model, But I'm facing the same issue on other models. I have tried uninstalling TensorFlow and reinstalling it.

I have upgraded "transformers" library as well. But to no avail. This seems to be a recent problem.

The virtual environment I'm using says I have these versions of tensorflow and transformers.

Reproduction

Steps to reproduce the behaviour:

  1. Go to https://huggingface.co/docs/transformers/main/en/model_doc/opt#transformers.tensorflow
  2. Run the example snippet consisting of this code
from transformers import GPT2Tokenizer, OPTForCausalLM

model = OPTForCausalLM.from_pretrained("facebook/opt-350m")
tokenizer = GPT2Tokenizer.from_pretrained("facebook/opt-350m")

prompt = "Hey, are you consciours? Can you talk to me?"
inputs = tokenizer(prompt, return_tensors="pt")

# Generate
generate_ids = model.generate(inputs.input_ids, max_length=30)
print(tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0])

Assuming required libraries are installed error message shows up.

Expected behavior

Expected the output as per shown in documentation.

susnato commented 1 year ago

Hi, @gaurav-95 Please run transformers-cli env in terminal and share the full system info so it's easier to reproduce the error.

gaurav-95 commented 1 year ago

transformers-cli env gives me this.

Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\Scripts\transformers-cli.exe\__main__.py", line 4, in <module>
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\commands\transformers_cli.py", line 24, in <module>
    from .pt_to_tf import PTtoTFCommand
  File "C:\Users\Admin\Desktop\Projects\NLP_Cron\cronenv\lib\site-packages\transformers\commands\pt_to_tf.py", line 46, in <module>
    tf.config.experimental.enable_tensor_float_32_execution(False)
AttributeError: module 'tensorflow' has no attribute 'config'

Could you elaborate on what system info do you need? Im running on an Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz 1.19 GHz 20.0 GB (19.8 GB usable) RAM

No dedicated gpu in machine. My virtualenvironment is called "cronenv"

Update: I was able to run the same code on a google colab notebook, seems like a problem with my environment.

susnato commented 1 year ago

Hi, @gaurav-95 Actually if you run the above code it should output something like this,

Since you are not getting it could you please check your transformers installation? (just run import transformers and check if it successfully imports or gives an error)

gaurav-95 commented 1 year ago

Thanks for getting back and hinting towards the problem. I can confirm there was something wrong with my python installation.

Steps that resolved it for me.

  1. I made a requirements file of the existing install
  2. I deleted the existing virtual environment.
  3. Re-installed python.
  4. Re-installed dependencies from saved requirements file.
  5. Ran code and it works now!
Odawgthat commented 1 year ago

Thanks for getting back and hinting towards the problem. I can confirm there was something wrong with my python installation.

Steps that resolved it for me.

  1. I made a requirements file of the existing install
  2. I deleted the existing virtual environment.
  3. Re-installed python.
  4. Re-installed dependencies from saved requirements file.
  5. Ran code and it works now!

LOL, I got the same thing happen to me as well I think??? Ill give this a try :) BTW how did you delete the env?