The Kernel crashed while executing code in the current cell or a previous cell. from transformers import pipeline
Author: Luismbpr
Pull request: no
Labels: name: bug

### 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.

```Markdown
MacOS Sequoia Version 15.1 (24B83)
```

```Markdown
!python --version
Python 3.11.10
```

```Markdown
transformers-cli env
- `transformers` version: 4.46.1
- Platform: macOS-15.1-arm64-arm-64bit
- Python version: 3.11.10
- Huggingface_hub version: 0.26.2
- Safetensors version: 0.4.5
- Accelerate version: not installed
- Accelerate config: not found
- PyTorch version (GPU?): 2.5.1 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
```

```Markdown
Package                   Version
------------------------- --------------
transformers              4.46.1
graphviz                  0.20.3
huggingface               0.0.1
huggingface-hub           0.26.2
hugsvision                0.75.5
ipykernel                 6.29.5
ipython                   8.29.0
ipywidgets                8.1.5
Jinja2                    3.1.4
joblib                    1.4.2
jupyter                   1.1.1
jupyter_client            7.4.9
jupyter-console           6.6.3
jupyter_core              5.7.2
jupyter-events            0.10.0
jupyter-lsp               2.2.5
jupyter_server            2.14.2
jupyter_server_terminals  0.5.3
jupyterlab                4.3.0
jupyterlab_pygments       0.3.0
jupyterlab_server         2.27.3
jupyterlab_widgets        3.0.13
MarkupSafe                3.0.2
matplotlib                3.9.2
matplotlib-inline         0.1.7
nb_conda                  2.2.1
nb_conda_kernels          2.5.1
notebook                  6.5.7
notebook_shim             0.2.4
numpy                     1.26.4
pandas                    2.2.3
pickleshare               0.7.5
pillow                    11.0.0
pip                       24.3.1
pure_eval                 0.2.3
PyAudio                   0.2.14
pyparsing                 3.2.0
PySocks                   1.7.1
python-dateutil           2.9.0
python-json-logger        2.0.7
pytorch-lightning         2.4.0
PyYAML                    6.0.2
safetensors               0.4.5
scikit-learn              1.5.2
scipy                     1.14.1
tokenizers                0.20.1
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
torchviz                  0.0.2
```



### 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:
```Shell
{
	"name": "",
	"message": "",
	"stack": "The Kernel crashed while executing code in the current cell or a previous cell. 
Please review the code in the cell(s) to identify a possible cause of the failure. 
Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. 
View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
}
```

### Information

- [ ] The official example scripts
- [x] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [x] My own task or dataset (give details below)

### Reproduction

This is code code I am trying to run
```python
from transformers import pipeline
import pandas as pd
import numpy as np

task="zero-shot-classification"
model = "facebook/bart-large-mnli"
classifier = pipeline(task, model)

documents = ["string01",
             "string02...",
             "string03..."]

## Candidate Label
candidate_labels = ["crime", "fantasy", "history"]
res = classifier(documents, candidate_labels=candidate_labels)
```

When running the kernel abruptly dies. This is the error shown:
On Jupyter Lab
```Shell
Kernel Restarting
The kernel for notebooks/360_NLP/Notebooks/NLP_07_01_Zero_Shot_Classification.ipynb appears to have died. It will restart automatically.
```

On VSCode
```Markdown
The Kernel crashed while executing code in the current cell or a previous cell. 
Please review the code in the cell(s) to identify a possible cause of the failure. 
Click [here](https://aka.ms/vscodeJupyterKernelCrash) for more info. 
View Jupyter [log](command:jupyter.viewOutput) for further details.
```

```Shell
{
	"name": "",
	"message": "",
	"stack": "The Kernel crashed while executing code in the current cell or a previous cell. 
Please review the code in the cell(s) to identify a possible cause of the failure. 
Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. 
View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
}
```

On VSCode Logs
```Shell
[error] Disposing session as kernel process died ExitCode: undefined, Reason: 

```

### 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.

Comments:
This appears to be a memory management issue when running BART locally, try reducing the memory usage by:
1. Setting a smaller batch size
2. Using model [half precision (float16)](https://huggingface.co/docs/transformers/en/main_classes/pipelines#pipeline-fp16-inference)

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.

```Python

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)
```

```Markdown
"... Does your local system have very limited memory?"
Not that I am aware of.
```

```Markdown
"... 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?

```Python

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
```Shell
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
```shell
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
```shell
onnx                      1.17.0
onnxscript                0.1.0.dev20241104
```
This is the error shown when trying to run the python script
```shell
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

```Shell
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

```Shell
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
```python
classifier = pipeline(task, model, device=torch.device('cpu'))
```

So this is the correct way and it solves the issue
```Python
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 :)