Cannot import name 'rank_zero_only' from 'pytorch_lightning.utilities.distributed'
Asked Answered
B

6

8

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

Bick answered 2/11, 2022 at 13:28 Comment(0)
A
13

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.

Approve answered 3/11, 2022 at 10:38 Comment(2)
Thanks, that works! I used pytorch_lightning==1.7.7 which worked out!Vintage
Thanks @Gira. v1.7.7 solved my problem as well. Inès, would you flag the answer as Accepted?Filamentary
D
8

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

from pytorch_lightning.utilities.rank_zero import rank_zero_only

Durtschi answered 29/3, 2023 at 10:0 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Totter
F
1

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.

Fatness answered 2/11, 2022 at 22:48 Comment(0)
G
1

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
Girasol answered 10/8, 2023 at 7:26 Comment(0)
C
0
  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.

Circinate answered 4/2, 2023 at 14:23 Comment(0)
F
0

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
Flatfoot answered 1/7, 2024 at 14:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.