13

I am working with PyTorch and the Hugging Face Transformers library to fine-tune a BERT model (UFNLP/gatortron-base) for a downstream task.

I received a warning related to NumPy initialization:

C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\storage.py:321: UserWarning: Failed to initialize NumPy: _ARRAY_API not found (Triggered internally at ..\torch\csrc\utils\tensor_numpy.cpp:84.)

My code:

type himport torch
from transformers import BertTokenizer, BertModel

tokenizer = BertTokenizer.from_pretrained('UFNLP/gatortron-base')
model = BertModel.from_pretrained('UFNLP/gatortron-base')

model.eval()

def prepare_input(text):
    tokens = tokenizer.encode_plus(text, return_tensors='pt', add_special_tokens=True, max_length=512, truncation=True)
    return tokens['input_ids'], tokens['attention_mask']

def get_response(input_ids, attention_mask):        
    with torch.no_grad():
        outputs = model(input_ids=input_ids, attention_mask=attention_mask)
        if 'logits' in outputs:
            predictions = torch.argmax(outputs['logits'], dim=-1)
        else:
            # Adjust this based on the actual structure of `outputs`
            predictions = torch.argmax(outputs[0], dim=-1) 

        # predictions = torch.argmax(outputs.logits, dim=-1)
        return tokenizer.decode(predictions[0], skip_special_tokens=True)

input_text = "Hello, how are you?"
input_ids, attention_mask = prepare_input(input_text)
response = get_response(input_ids, attention_mask)
print("Response from the model:", response)ere
  • Python: 3.12
  • NumPy: 1.19.5
1
  • 1
    I think it's a duplicate question, have you tried it like this link? Also, I found similar issue in github. Try re-install numpy as 1.25.2 Commented Jun 28, 2024 at 8:16

3 Answers 3

14

pip install --force-reinstall -v "numpy==1.25.2"

Fixed the issue for me.

This was following this github discussion from : https://github.com/stitionai/devika/issues/606

All thanks to @HOBE for the comment above

Sign up to request clarification or add additional context in comments.

1 Comment

Version 1.25.2 gave me this error: AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter' This seems to have been fixed in v1.26.4: pip install --force-reinstall -v "numpy==1.26.4"
1

Most ML libraries (PyTorch in your case) are still in the process of adding NumPy 2 support.

pip uninstall numpy 
pip install "numpy<2"

This will install the latest 1.x version (e.g., 1.26.4 for me), which is fully compatible with PyTorch.

If you want to stay on Numpy 2, you'd need to check which versions of PyTorch support it.

Comments

0

I had this same problem, and I sorted it out by upgrading my torch version from 1.x.x to 2.x.x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.