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

NameError: name 'torch' is not defined #1503

Closed
sdugoten opened this issue Sep 29, 2023 · 10 comments
Closed

NameError: name 'torch' is not defined #1503

sdugoten opened this issue Sep 29, 2023 · 10 comments

Comments

@sdugoten
Copy link

I am trying to convert using the following command

ct2-transformers-converter --model g:\whisper-large-v2-cantonese\pytorch_model.bin --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16

from this model https://huggingface.co/simonl0909/whisper-large-v2-cantonese

I got error like this

PS G:> ct2-transformers-converter --model g:\whisper-large-v2-cantonese\pytorch_model.bin --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16
Traceback (most recent call last):
File "D:\Program Files\Python\Phthon3.10.6\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Program Files\Python\Phthon3.10.6\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "D:\Program Files\Python\Phthon3.10.6\Scripts\ct2-transformers-converter.exe_main
.py", line 7, in
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\transformers.py", line 1771, in main
converter.convert_from_args(args)
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\converter.py", line 50, in convert_from_args
return self.convert(
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\converter.py", line 89, in convert
model_spec = self._load()
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\transformers.py", line 97, in _load
with torch.no_grad():
NameError: name 'torch' is not defined

@winstxnhdw
Copy link

Have you tried

ct2-transformers-converter --model simonl0909/whisper-large-v2-cantonese --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16

@sdugoten
Copy link
Author

Have you tried

ct2-transformers-converter --model simonl0909/whisper-large-v2-cantonese --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16

G:>ct2-transformers-converter --model simonl0909/whisper-large-v2-cantonese --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16

Traceback (most recent call last):
File "D:\Program Files\Python\Phthon3.10.6\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\Program Files\Python\Phthon3.10.6\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "D:\Program Files\Python\Phthon3.10.6\Scripts\ct2-transformers-converter.exe_main
.py", line 7, in
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\transformers.py", line 1771, in main
converter.convert_from_args(args)
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\converter.py", line 50, in convert_from_args
return self.convert(
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\converter.py", line 89, in convert
model_spec = self._load()
File "D:\Program Files\Python\Phthon3.10.6\lib\site-packages\ctranslate2\converters\transformers.py", line 97, in _load
with torch.no_grad():
NameError: name 'torch' is not defined

@winstxnhdw
Copy link

winstxnhdw commented Sep 29, 2023

Can you do

pip install torch

@sdugoten
Copy link
Author

Can you do

pip install torch

20230929_162334

It's already installed, the version that works with CUDA

@sdugoten
Copy link
Author

Can you do

pip install torch

Just for your information, I try it one more time on my Mac instead of the Windows that you saw above, same result

temp3 % ct2-transformers-converter --model simonl0909/whisper-large-v2-cantonese --output_dir fast_canton --copy_files tokenizer_config.json --quantization float16

Traceback (most recent call last):
File "/opt/homebrew/bin//ct2-transformers-converter", line 8, in
sys.exit(main())
File "/opt/homebrew/lib/python3.10/site-packages/ctranslate2/converters/transformers.py", line 1771, in main
converter.convert_from_args(args)
File "/opt/homebrew/lib/python3.10/site-packages/ctranslate2/converters/converter.py", line 50, in convert_from_args
return self.convert(
File "/opt/homebrew/lib/python3.10/site-packages/ctranslate2/converters/converter.py", line 89, in convert
model_spec = self._load()
File "/opt/homebrew/lib/python3.10/site-packages/ctranslate2/converters/transformers.py", line 97, in _load
with torch.no_grad():
NameError: name 'torch' is not defined

goten@Gotens-Mac-Studio-116 temp3 %
goten@Gotens-Mac-Studio-116 temp3 %
goten@Gotens-Mac-Studio-116 temp3 % pip3 list
Package Version


aiohttp 3.8.4
aiosignal 1.3.1
async-timeout 4.0.2
attrs 23.1.0
bbook-maker 0.3.0
beautifulsoup4 4.12.2
bs4 0.0.1
certifi 2022.12.7
charset-normalizer 3.1.0
ctranslate2 3.20.0
EbookLib 0.18
filelock 3.12.4
frozenlist 1.3.3
idna 3.4
Jinja2 3.1.2
lxml 4.9.2
markdown-it-py 2.2.0
MarkupSafe 2.1.3
mdurl 0.1.2
mpmath 1.3.0
multidict 6.0.4
networkx 3.1
numpy 1.26.0
openai 0.27.5
pip 23.2.1
protobuf 4.21.12
Pygments 2.15.1
PyYAML 6.0.1
regex 2023.3.23
requests 2.29.0
rich 13.3.5
setuptools 67.2.0
six 1.16.0
soupsieve 2.4.1
sympy 1.12
tiktoken 0.3.3
torch 2.0.1
tqdm 4.65.0
typing_extensions 4.8.0
urllib3 1.26.15
wheel 0.38.4
yarl 1.9.2

@phineas-pta
Copy link

pip install transformers huggingface-hub

@winstxnhdw
Copy link

Please use a proper Python virtual environment like mamba or venv next time to avoid this unnecessary pain.

@sdugoten
Copy link
Author

pip install transformers huggingface-hub

thanks for help, I eventually use my Mac to convert the file into .bin that can be used by faster-whisper. I wonder if something can be converted that is used by Whisper OpenAI that takes .py file?

Sorry keep asking silly question as I am no programmer.

@winstxnhdw
Copy link

winstxnhdw commented Sep 29, 2023

No, it's not possible. .bin and .py files are not close to being equivalents.

@Anthonyg5005
Copy link

Anthonyg5005 commented Oct 22, 2023

a bit late but make sure you use the correct version of pytorch and that transformers and huggingface-hub are installed. if it still doesn't work add import torch at the beginning of transformers.py
there's a chance you might also need to import transformers and huggingface-hub as well. I had to do this once on a linux vm but not on my windows computer.
after checking again all I did was make sure those were all installed correctly with correct versions from the pytorch website then replaced this:
try:
import huggingface_hub
import torch
import transformers
except ImportError:
pass

with this:
import huggingface_hub
import torch
import transformers

@vince62s vince62s closed this as completed Nov 9, 2023
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

5 participants