InvokeAI icon indicating copy to clipboard operation
InvokeAI copied to clipboard

Is anyone actually using --batch_size?

Open bakkot opened this issue 4 years ago • 16 comments

There's a lot of code which is complicated by the need to handle batch_size > 1, but I don't know why you'd ever use it instead of -n. On my machine --batch_size 2 takes just as long -n 2, but uses twice the memory.

bakkot avatar Aug 30 '22 03:08 bakkot

My god, yes, I desperately want to get rid of the batch code. (I also wanted to get rid of the grid code, but was vetoed by users.) In addition to the code overhead, it will require significant surgery to recover the seeds of images after the first one, meaning that you can only regenerate the second and subsequent images by regenerating the whole batch again.

The only reason I can think of for using batch is that -b2 is subjectively faster than -n2 (at least on my machine, though I haven't timed it). However, it uses twice the memory and is generally impractical for anyone who isn't running an HPC-class computer. Another potential advantage is that if the code were written right, batches could be spread out over two or more GPUs. However this isn't being done in the base CompViz code and it is beyond my pay-grade to make those changes. Perhaps iterations could use multiple GPUs as well.

I seem to be ranting. In summary, I agree with bakkot.

lstein avatar Aug 30 '22 03:08 lstein

I don't use it either because I'm always terrified of it crashing on this 8gb GPU... I'd be in favor of removing it, my impression is people who have the VRAM go for larger resolution

ghost avatar Aug 30 '22 03:08 ghost

I don't use it either because I'm always terrified of it crashing on this 8gb GPU... I'd be in favor of removing it, my impression is people who have the VRAM go for larger resolution

-n should work fine on an 8GB card. It's the -b that causes a massive memory overhead.

@lstein @bakkot I vote to either archive or remove -b .. It's causing more issues than it is solving and functionally it makes no difference to the working of the repo and the program.

blessedcoolant avatar Aug 30 '22 04:08 blessedcoolant

Possible to extract it into an optional plugin?

david-ford avatar Aug 30 '22 04:08 david-ford

Possible to extract it into an optional plugin?

No. It's too entangled with the internals; that's the problem with it.

bakkot avatar Aug 30 '22 04:08 bakkot

Is anyone actually using --batch_size?

never used it.

morganavr avatar Aug 30 '22 10:08 morganavr

I don't use batch size, always -n

lkewis avatar Aug 30 '22 12:08 lkewis

Speaking purely as an end user with a 10gb 3080, I determined almost immediately that batch size > 1 was useless to me and have been using -n without looking back.

namion avatar Aug 30 '22 13:08 namion

Never used batch size, only and always -n with a 8GB card.

theRolento avatar Aug 30 '22 13:08 theRolento

I hear taps playing.

On Tue, Aug 30, 2022 at 9:06 AM Daniele @.***> wrote:

Never used batch size, only and always -n with a 8GB card.

— Reply to this email directly, view it on GitHub https://github.com/lstein/stable-diffusion/issues/204#issuecomment-1231642131, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA3EVPGGL4XWX5OZRAJB3LV3YBNZANCNFSM57764Y3Q . You are receiving this because you were mentioned.Message ID: @.***>

--

Lincoln Stein

Head, Adaptive Oncology, OICR

Senior Principal Investigator, OICR

Professor, Department of Molecular Genetics, University of Toronto

Tel: 416-673-8514

Cell: 416-817-8240

@.***

E**xecutive Assistant

Michelle Xin

Tel: 647-260-7927

@.*** @.**>

Ontario Institute for Cancer Research

MaRS Centre, 661 University Avenue, Suite 510, Toronto, Ontario, Canada M5G 0A3

@OICR_news https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Ftwitter.com%2Foicr_news&data=04%7C01%7CMichelle.Xin%40oicr.on.ca%7C9fa8636ff38b4a60ff5a08d926dd2113%7C9df949f8a6eb419d9caa1f8c83db674f%7C0%7C0%7C637583553462287559%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=PS9KzggzFoecbbt%2BZQyhkWkQo9D0hHiiujsbP7Idv4s%3D&reserved=0 | www.oicr.on.ca

Collaborate. Translate. Change lives.

This message and any attachments may contain confidential and/or privileged information for the sole use of the intended recipient. Any review or distribution by anyone other than the person for whom it was originally intended is strictly prohibited. If you have received this message in error, please contact the sender and delete all copies. Opinions, conclusions or other information contained in this message may not be that of the organization.

lstein avatar Aug 30 '22 13:08 lstein

either archive or remove -b

The beauty of git + GitHub - archiving isn't necessary: nothing ever goes away. Those old commits could be resurrected at any time in the future if desired (I'm not doing the rebase, though 😆).

tildebyte avatar Aug 30 '22 16:08 tildebyte

8GB card here, don't and can't use batch size option. Plus the issue with seeds is definitely something that makes batched generation less desirable.

BlueAmulet avatar Aug 30 '22 20:08 BlueAmulet

It's used in embedding training. So... prolly not a good idea to rip it out if we want to be able to add custom ideas.

It's primarily there for training and part of the diffusion process. It loads both images at the same time in memory and uses them in a similar way to what it does when training a new model. It is a pretty big overhead though.

david-ford avatar Aug 30 '22 23:08 david-ford

but was vetoed by users.

Yeah I use the grid code all the time, so removing it would probably not be great.

zoru22 avatar Sep 01 '22 04:09 zoru22

Hi, RTX 3090 user here, using -b all the time. on 512x512 I can do -b 6 -b 3 on larger resolution.

It's a bit faster, makes a difference for me -b 6 = 23.25s -n 6 = 27.82s

I do agree this difference may not be significant enough to justify spaghetti code. I wonder however if the batch code is currently fully optimized. With glide3-xl for instance, using batch adds very little extra compute time (it does 24 images almost in the same time it does just 1), so there using batch is really meaningful. I would have thought we would have the same perf gain with SD.

So,

  • I would agree to remove -b to clean up the code
  • But maybe there are significant potential perfs gains with batch, done differently.

AngainorDev avatar Sep 02 '22 07:09 AngainorDev

I was playing with this on GCP [1] to see if using more expensive GPU is worth it. Here are some numbers for generating 512x512 images with 50 steps (all default settings).

On T4 (16GB VRAM, with n1-highmem-4 4vCPU 26GB RAM), the cheapest one I got working, with batch size = 1 I got an image in ~10-12 seconds.

On A100 (40GB VRAM, with a2-highgpu-1g 12vCPU 85GB RAM):

  • batch size = 1: ~5s, memory usage ~7G
  • batch size = 2: ~6s, memory usage ~9G
  • batch size = 4: ~10s, memory usage ~17G
  • batch size = 8: ~20s, memory usage ~30G

So generating 4 images would take ~40s on T4 but only ~10s on A100 using batch size 4, for a 4x speedup. However you get roughly the same for 2x batch size 2, after that it seems we get diminishing returns.

It would be worth it (at least for some usecases) if we could get 8 images in the same time as one (like @AngainorDev says you get with glide3-xl) but given the A100 instance was about 6x more expensive [2] I'll be sticking with T4 and batch size = 1 for now.

[1] https://cloud.google.com/compute/docs/gpus [2] For spot pricing I got roughly $0.17/hour and $1.11/hour for those two setups.

lacop avatar Sep 02 '22 09:09 lacop