Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gaurav-95 opened this issue Jan 16, 2023 · 6 comments

Comments

@gaurav-95
Copy link

gaurav-95 commented Jan 16, 2023

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.

  • transformers 4.25.1
  • tensorflow 2.11.0

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
Copy link
Contributor

susnato commented Jan 16, 2023

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
Copy link
Author

gaurav-95 commented Jan 16, 2023

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
Copy link
Contributor

susnato commented Jan 16, 2023

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

  • transformers version: 4.25.1
  • Platform: Linux-5.15.0-58-generic-x86_64-with-glibc2.35
  • Python version: 3.9.15
  • Huggingface_hub version: 0.11.1
  • PyTorch version (GPU?): 1.13.1 (True)
  • Tensorflow version (GPU?): 2.10.0 (True)
  • Flax version (CPU?/GPU?/TPU?): not installed (NA)
  • Jax version: not installed
  • JaxLib version: not installed
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

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
Copy link
Author

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
Copy link

Odawgthat commented Feb 4, 2023

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?

@gmadw
Copy link

gmadw commented Oct 28, 2024

我遇到了同样的问题,把tensorflow卸载,einops降到0.7.0版本后,又可以正常出图了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants