cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

HuggingFace bert-large-uncased gives NameError

Carl_B
New Contributor II

Hello,

I am trying to run the LLM bert-large-uncased from HuggingFace.

I have downloaded the transformers from github. I have installed the various packages. I am now getting an error message: NameError: name 'torch' is not defined.

Not sure what the problem is. Is pytorch not installed? Any help would be appreciated.

C

 

1 ACCEPTED SOLUTION

Accepted Solutions

lingareddy_Alva
Honored Contributor III

Hi @Carl_B 

The error indicates that PyTorch is not installed in your environment. Just try below this.

Install PyTorch
Option 1: Using pip
pip install torch torchvision torchaudio

Option 2: Using conda (if you're using Anaconda/Miniconda)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

Option 3: CPU-only version (if no GPU)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

After installation, test it:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # Check if CUDA is available

Complete Setup for BERT
Once PyTorch is installed, you'll also need:
# Install transformers properly via pip instead of GitHub
pip install transformers

# Additional dependencies you might need
pip install datasets tokenizers accelerate

Test BERT Model Loading

from transformers import AutoTokenizer, AutoModel
import torch

# Load BERT model and tokenizer
model_name = "bert-large-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)

print("BERT model loaded successfully!")
print(f"Model device: {next(model.parameters()).device}")

If You're Using Databricks
If you're running this on Databricks, install via notebook cell:
%pip install torch torchvision torchaudio transformers

Then restart the Python kernel:
dbutils.library.restartPython()

 

LR

View solution in original post

1 REPLY 1

lingareddy_Alva
Honored Contributor III

Hi @Carl_B 

The error indicates that PyTorch is not installed in your environment. Just try below this.

Install PyTorch
Option 1: Using pip
pip install torch torchvision torchaudio

Option 2: Using conda (if you're using Anaconda/Miniconda)
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

Option 3: CPU-only version (if no GPU)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

After installation, test it:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # Check if CUDA is available

Complete Setup for BERT
Once PyTorch is installed, you'll also need:
# Install transformers properly via pip instead of GitHub
pip install transformers

# Additional dependencies you might need
pip install datasets tokenizers accelerate

Test BERT Model Loading

from transformers import AutoTokenizer, AutoModel
import torch

# Load BERT model and tokenizer
model_name = "bert-large-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)

print("BERT model loaded successfully!")
print(f"Model device: {next(model.parameters()).device}")

If You're Using Databricks
If you're running this on Databricks, install via notebook cell:
%pip install torch torchvision torchaudio transformers

Then restart the Python kernel:
dbutils.library.restartPython()

 

LR

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now