Ines Sieulle
Ines Sieulle

Reputation: 89

Cannot import name 'rank_zero_only' from 'pytorch_lightning.utilities.distributed'

I am using VQGAN+CLIP_(Zooming)_(z+quantize_method_with_addons).ipynb Google Repository and when I click the cell "Loading of libraries and definitions"

It sent an error :

ImportError                               Traceback (most recent call last)
<ipython-input-6-fe8fafeed45d> in <module>
     24 from omegaconf import OmegaConf
     25 from PIL import Image
---> 26 from taming.models import cond_transformer, vqgan
     27 import torch
     28 from torch import nn, optim

1 frames
/content/taming-transformers/main.py in <module>
     10 from pytorch_lightning.trainer import Trainer
     11 from pytorch_lightning.callbacks import ModelCheckpoint, Callback, LearningRateMonitor
---> 12 from pytorch_lightning.utilities.distributed import rank_zero_only
     13 
     14 from taming.data.utils import custom_collate

ImportError: cannot import name 'rank_zero_only' from 'pytorch_lightning.utilities.distributed' (/usr/local/lib/python3.7/dist-packages/pytorch_lightning/utilities/distributed.py)

I don't know how to solde this problem. I don't know how to manually install Pytorch as it said "NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the "Open Examples" button below."

Thank you in advance if you have the solution.

Inès

I triad !pip install but I may not really know where to put this cell/line of code

Upvotes: 8

Views: 22319

Answers (6)

Sheykhmousa
Sheykhmousa

Reputation: 199

Check your PyTorch Lightning version:

import pytorch_lightning as pl
print(pl.__version__)

If you're using an older version, try updating PyTorch Lightning:

install --upgrade pytorch_lightning

In newer versions of PyTorch Lightning, some imports have changed. Try importing from lightning.pytorch.utilities instead:

from lightning.pytorch.utilities import rank_zero_only

If you're using a very recent version, the import might have changed again. You could try:

from lightning.fabric.utilities.rank_zero import rank_zero_only

Upvotes: 0

Hugh Liu
Hugh Liu

Reputation: 31

rank_zero_only function has been move from distributed.py to rank_zero.py in pl1.6 milestone in PR11747. And you can import this function via distributed during pl version 1.7.x.

Since version 1.8.0, pytorch-lighting has removed pytorch_lightning.utilities.distributed.py but saying nothing about it in the changelog. (You can get the package from here.)

So, there are two solutions here:

  1. pip install --force --reinstall pytorch-lighting<1.8.0
  2. import rank_zero relative functions from from pytorch_lightning.utilities.rank_zero if pytorch-lighting>=1.8.0

Upvotes: 1

amar nabil taabane
amar nabil taabane

Reputation: 151

now rank_zero_only is in /pytorch_lightning/utilities/rank_zero.py

from pytorch_lightning.utilities.rank_zero import rank_zero_only

Upvotes: 9

kelper
kelper

Reputation: 1

  1. use @vishisht-rao solution to downgrade the package version of pytorch-lightning to 1.7.7; this step by itself did not solve the problem for me.

    pip install pytorch-lightning==1.6.5
    
  2. run pip list|grep lightning to find the installed version of lightning-utilities. Mine was lightning-utilities==0.4.2

  3. Downgrade the package version of lightning-utilities to 0.4.0

    pip install lightning-utilities==0.4.0
    

Downgrading the package versions of both pytorch-lightning and lightning-utilities solved the problem for me.

Upvotes: 0

Decoder
Decoder

Reputation: 11

Finally, after long research, I found the solution for it, try to run it, you will face an issue with CLIP module, once you resolve python issue as well, so for that follow the second code.

conda install pytorch-lightning -c conda-forge

Once you clone it, try to follow below command

Step:1 cd CLIP 
Step2: python setup.py

after that,

type: cd..

Once you do that, you will be redirected to previous directory named "VQGAN-CLIP"

and finally, run the following command:

python generate.py -p "A painting of an apple in a fruit bowl"

Once it is done, then run your generate python file, It will work fine.

Upvotes: 1

Vishisht Rao
Vishisht Rao

Reputation: 131

pytorch_lightning has recently released a new version which will throw this error (version 1.8.0.post1 released on November 2nd 2022).

https://pypi.org/project/pytorch-lightning/#history

Just install an older version of pytorch_lightning and it will work.

In my system, I ran "pip install pytorch-lightning==1.6.5", higher versions may work as well, you can check them out by clicking on the link provided above and then clicking on release history.

Upvotes: 13

Related Questions