Closed Luismbpr closed 3 weeks ago
This appears to be a memory management issue when running BART locally, try reducing the memory usage by:
Yes, I would also guess that this is a memory issue! However, BART is a small enough model that it would expect most systems to be able to run it for some simple classification tasks, unless the batch size is very large. Does your local system have very limited memory?
This is literally the entire piece of code that I am trying to run, so I do not understand how should I reduce the batch size since that was the entire piece of the document.
from transformers import pipeline
import pandas as pd
import numpy as np
#import torch
task="zero-shot-classification"
model = "facebook/bart-large-mnli"
classifier = pipeline(task, model)
#device = torch.device("cpu")
#%% data prep
# first example: Raymond Chandler "The Big Sleep" (crime novel)
# second example: J.R.R. Tolkien "The Lord of the Rings" (fantasy novel)
# third example: Bill Bryson "A Short History of Nearly Everything"
documents = ["It was about eleven o’clock in the morning, mid October, with the sun not shining and a look of hard wet rain in the clearness of the foothills. I was wearing my powder-blue suit, with dark blue shirt, tie and display handkerchief, black brogues, black wool socks with dark blue clocks on them. I was neat, clean, shaved and sober, and I didn’t care who knew it. I was everything the well-dressed private detective ought to be. I was calling on four million dollars.",
"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.",
"Welcome. And congratulations. I am delighted that you could make it. Getting here wasn’t easy, I know. In fact, I suspect it was a little tougher than you realize. To begin with, for you to be here now trillions of drifting atoms had somehow to assemble in an intricate and curiously obliging manner to create you. It’s an arrangement so specialized and particular that it has never been tried before and will only exist this once."
]
## Candidate Label
candidate_labels = ["crime", "fantasy", "history"]
res = classifier(documents, candidate_labels=candidate_labels)
"... Does your local system have very limited memory?"
Not that I am aware of.
"... Does your local system have very limited memory?"
I will try to run a python script just as you mentioned
Can you write the same code in a .py
file and try to run it?
Can you write the same code in a
.py
file and try to run it?
from transformers import pipeline
import pandas as pd
import numpy as np
import torch
#import matplotlib.pyplot as plt
#%matplotlib inline
#import torch
task="zero-shot-classification"
model = "facebook/bart-large-mnli"
classifier = pipeline(task, model)
#device = torch.device("cpu")
#%% data prep
# first example: Raymond Chandler "The Big Sleep" (crime novel)
# second example: J.R.R. Tolkien "The Lord of the Rings" (fantasy novel)
# third example: Bill Bryson "A Short History of Nearly Everything"
documents = ["It was about eleven o’clock in the morning, mid October, with the sun not shining and a look of hard wet rain in the clearness of the foothills. I was wearing my powder-blue suit, with dark blue shirt, tie and display handkerchief, black brogues, black wool socks with dark blue clocks on them. I was neat, clean, shaved and sober, and I didn’t care who knew it. I was everything the well-dressed private detective ought to be. I was calling on four million dollars.",
"When Mr. Bilbo Baggins of Bag End announced that he would shortly be celebrating his eleventy-first birthday with a party of special magnificence, there was much talk and excitement in Hobbiton.",
"Welcome. And congratulations. I am delighted that you could make it. Getting here wasn’t easy, I know. In fact, I suspect it was a little tougher than you realize. To begin with, for you to be here now trillions of drifting atoms had somehow to assemble in an intricate and curiously obliging manner to create you. It’s an arrangement so specialized and particular that it has never been tried before and will only exist this once."
]
## Candidate Label
candidate_labels = ["crime", "fantasy", "history"]
res = classifier(documents, candidate_labels=candidate_labels)
print(res)
This returns this error
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
No module named 'onnxscript'
You can just install onnxscript like this:
pip install onnxscript
Trying to install
pip install onnxscript
This error shows up on this virtual environment with python 3.11 when trying to install onnxscript. This is the onnxscript and onnx installed on this version
onnx 1.17.0
onnxscript 0.1.0.dev20241104
This is the error shown when trying to run the python script
Hardware accelerator e.g. GPU is available in the environment, but no `device` argument is passed to the `Pipeline` object. Model will be on CPU.
zsh: bus error python NLP_07_ZeroShotClassification.py
How come the error changed when you switched from jupyter notebook to py file?
Anyways, your jupyter might have a virtual environment kindly check it! If yes then activate it and run the py file.
If the virtual environment is activated and still you are getting the error, try updating the torch version.
If there is still an error, wait for sometime, I'll pull the same versions and try to debug it. In the meantime, you can use gbd to debug it yourself too
I tried running it on different virtual environments virtual environment - Python 3.10.14 torch 2.2.2 torch-geometric 2.6.1 torch_scatter 2.1.2 torchaudio 2.2.2 torchinfo 1.8.0 torchmetrics 1.4.2 torchvision 0.17.2 transformers 4.46.1 numpy 2.0.2 onnx 1.17.0 onnxscript 0.1.0.dev20241104
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback):
cannot import name 'converter' from 'torch._export' (/Users/luis/miniforge3/envs/venv_0758_PyTorch_From_Basics_Cutting_Edge_310_001/lib/python3.10/site-packages/torch/_export/__init__.py)
virtual environment - Python 3.11.10 torch 2.5.1 torch_scatter 2.1.2 torchaudio 2.5.1 torchmetrics 1.5.1 torchtext 0.18.0 torchvision 0.20.1 transformers 4.46.1 numpy 1.26.4 onnx 1.17.0 onnxscript 0.1.0.dev20241104
Hardware accelerator e.g. GPU is available in the environment, but no `device` argument is passed to the `Pipeline` object. Model will be on CPU.
zsh: bus error python NLP_07_ZeroShotClassification.py
Use python3.11, along with torch 2.5 and transformers 4.46.1 (which is the same you have). Along with this can you try using GPU, you can do this by passing device into the pipeline function like this:
classifier = pipeline(task, model, device=0)
Use python3.11, along with torch 2.5 and transformers 4.46.1 (which is the same you have). Along with this can you try using GPU, you can do this by passing device into the pipeline function like this:
classifier = pipeline(task, model, device=0)
Setting the device solved the issue. I tried previously to use this without having any results
classifier = pipeline(task, model, device=torch.device('cpu'))
So this is the correct way and it solves the issue
classifier = pipeline(task, model, device=0)
Thank you for that.
Use python3.11, along with torch 2.5 and transformers 4.46.1 (which is the same you have). Along with this can you try using GPU, you can do this by passing device into the pipeline function like this:
classifier = pipeline(task, model, device=0)
This solved the issue. Thank you @Vishal-Padia
Glad I could help :)
System Info
This or a similar issue was posted on this source from another user. The issue/bug subject was closed due to the lack of information from the original user. Source: https://github.com/huggingface/transformers/issues/12209
I decided to open this issue since I am having a similar or the same issue where the kernel dies when trying to run the code on VSCode and on Jupyter Lab. Note: The code works well on Google Colab, but I want to be able to reproduce this on my local machine.
Who can help?
When running the code shown the kernel abruptly dies. That happened on VSCode and on Jupyter Lab as well. This is the error shown:
Information
Tasks
examples
folder (such as GLUE/SQuAD, ...)Reproduction
This is code code I am trying to run
When running the kernel abruptly dies. This is the error shown: On Jupyter Lab
On VSCode
On VSCode Logs
Expected behavior
The expected result should be a key with the sequence passed, the different labels and the corresponding scores for each of the documents.