Text-to-Image
Diffusers
PyTorch
Safetensors
IFPipeline
if

ZeroDivisionError while loading pretrained models

#20
by gianlucav128 - opened

I was trying the Colab Notebook: in particular, I modified the script in order to generate an image for a list of prompts.
At first everything went well, but then the runtime disconnected, probably because GPU usage limits have been exceeded (I have the free version of Colab). Since then, with the same exact code, I got a ZeroDivisionError when I try to generate the images, in particular when the Text Embedding model is loaded. The error is the following:

│ /usr/local/lib/python3.10/dist-packages/accelerate/big_modeling.py:466 in                        │
│ load_checkpoint_and_dispatch                                                                     │
│                                                                                                  │
│   463 │   │   │   "'sequential'."                                                                │
│   464 │   │   )                                                                                  │
│   465 │   if device_map != "sequential":                                                         │
│ ❱ 466 │   │   max_memory = get_balanced_memory(                                                  │
│   467 │   │   │   model,                                                                         │
│   468 │   │   │   max_memory=max_memory,                                                         │
│   469 │   │   │   no_split_module_classes=no_split_module_classes,                               │
│                                                                                                  │
│ /usr/local/lib/python3.10/dist-packages/accelerate/utils/modeling.py:526 in get_balanced_memory  │
│                                                                                                  │
│    523 │   module_sizes = {n: v for n, v in module_sizes.items() if n not in leaves}             │
│    524 │   # Once removed, leaves are the final modules.                                         │
│    525 │   leaves = [n for n in module_sizes if len([p for p in module_sizes if n == "" or p.st  │
│ ❱  526 │   mean_leaves = int(sum([module_sizes[n] for n in leaves]) / len(leaves))               │
│    527 │   buffer = int(1.25 * max(buffer, mean_leaves))                                         │
│    528 │   per_gpu += buffer 

ZeroDivisionError: division by zero

Has anyone idea how to fix this?

I got this error as well.

I got the same issue.

Thanks to a user on GitHub I was able to find a fairly reckless temporary workaround to the problem. I modified modeling.py to be:

mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) )

Now I'm able to generate images without problems.

Cause: the upgrade to accelerate~=0.20 cause the error

Solution 1 was tested with accelerate~=0.18

Solution 1:
replace:

! pip install --upgrade \ accelerate~=0.18

with

! pip install accelerate~=0.18

Solution 2 not recommended:
#!pip install git+https://github.com/huggingface/accelerate

Solution 3:
wait a couple of weeks until accelerate patched version is installed via! pip install --upgrade \ accelerate~=0.18

Sign up or log in to comment