IF icon indicating copy to clipboard operation
IF copied to clipboard

ZeroDivisionError while loading pretrained models

Open Gianluca124 opened this issue 2 years ago • 12 comments

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?

Gianluca124 avatar May 09 '23 09:05 Gianluca124

I have the same problem with original notebook.

shlyahin avatar May 10 '23 10:05 shlyahin

Same

KapDEK avatar May 10 '23 17:05 KapDEK

Same here

Fairly reckless temporary workaround: I patched modeling.py:526 to be: mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) )

Now I am able to generate images with the sample notebook

adamdavidcole avatar May 10 '23 18:05 adamdavidcole

Same here

Fairly reckless temporary workaround: I patched modeling.py:526 to be: mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) )

Now I am able to generate images with the sample notebook

It works, thank you

Gianluca124 avatar May 12 '23 08:05 Gianluca124

Same here

Fairly reckless temporary workaround: I patched modeling.py:526 to be: mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) )

Now I am able to generate images with the sample notebook

@adamdavidcole @Gianluca124 can you please help me with how to implement this patch in google colab ?

sayedathar11 avatar May 12 '23 08:05 sayedathar11

Same here Fairly reckless temporary workaround: I patched modeling.py:526 to be: mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) ) Now I am able to generate images with the sample notebook

@adamdavidcole @Gianluca124 can you please help me with how to implement this patch in google colab ?

In my case I forked the accelerate repository here on GitHub, I made the modification and then loaded on Colab my repository with the correction suggested by @adamdavidcole

Gianluca124 avatar May 12 '23 08:05 Gianluca124

here

Can you please help me with how to fork the accelerate repository in colab environment with steps. Thank You.

sayedathar11 avatar May 12 '23 09:05 sayedathar11

here

Can you please help me with how to fork the accelerate repository in colab environment with steps. Thank You.

  1. Go to https://github.com/huggingface/accelerate and fork the repository
  2. Now go to the forked repository (it should be something like https://github.com/{your_username}/accelerate) and made the modification inside the https://github.com/{your_username}/accelerate/blob/main/src/accelerate/utils/modeling.py script
  3. In Colab, instead of installing the original accelerate package you use !pip install git+https://github.com/{your_username}/accelerate

The rest of the notebook stays the same

Gianluca124 avatar May 12 '23 09:05 Gianluca124

here

Can you please help me with how to fork the accelerate repository in colab environment with steps. Thank You.

  1. Go to https://github.com/huggingface/accelerate and fork the repository
  2. Now go to the forked repository (it should be something like https://github.com/{your_username}/accelerate) and made the modification inside the https://github.com/{your_username}/accelerate/blob/main/src/accelerate/utils/modeling.py script
  3. In Colab, instead of installing the original accelerate package you use !pip install git+https://github.com/{your_username}/accelerate

The rest of the notebook stays the same

Thanks alot will check this

sayedathar11 avatar May 12 '23 09:05 sayedathar11

Same here

Fairly reckless temporary workaround: I patched modeling.py:526 to be: mean_leaves = int(sum([module_sizes[n] for n in leaves]) / ( len(leaves) or 1) )

Now I am able to generate images with the sample notebook

Thank you, it works

shlyahin avatar May 12 '23 14:05 shlyahin

here

Can you please help me with how to fork the accelerate repository in colab environment with steps. Thank You.

  1. Go to https://github.com/huggingface/accelerate and fork the repository
  2. Now go to the forked repository (it should be something like https://github.com/{your_username}/accelerate) and made the modification inside the https://github.com/{your_username}/accelerate/blob/main/src/accelerate/utils/modeling.py script
  3. In Colab, instead of installing the original accelerate package you use !pip install git+https://github.com/{your_username}/accelerate

The rest of the notebook stays the same

Thanks alot @Gianluca124 @adamdavidcole I was able to run the code and generate images :)

sayedathar11 avatar May 15 '23 07:05 sayedathar11

It's works for me with this adjust. Thank you. But now is in line 536.

ximo1984 avatar May 16 '23 13:05 ximo1984