Skip to content

Commit

Permalink
Merge pull request #146 from fiskbil/enable-xformers-option
Browse files Browse the repository at this point in the history
Add argument for enabling xFormers optimizations
  • Loading branch information
Sanster authored Nov 29, 2022
2 parents 0d2d1ab + a024072 commit d44dd88
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Available command line arguments:
| --sd-run-local | Once the model as downloaded, you can pass this arg and remove `--hf_access_token` | |
| --sd-disable-nsfw | Disable stable-diffusion NSFW checker. | |
| --sd-cpu-textencoder | Always run stable-diffusion TextEncoder model on CPU. | |
| --sd-enable-xformers | Enable xFormers optimizations. See: [facebookresearch/xformers](https://github.com/facebookresearch/xformers) | |
| --device | cuda or cpu | cuda |
| --port | Port for backend flask web server | 8080 |
| --gui | Launch lama-cleaner as a desktop application | |
Expand Down
5 changes: 4 additions & 1 deletion lama_cleaner/model/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ def init_model(self, device: torch.device, **kwargs):
use_auth_token=kwargs["hf_access_token"],
**model_kwargs
)
# https://huggingface.co/docs/diffusers/v0.3.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionInpaintPipeline.enable_attention_slicing
# https://huggingface.co/docs/diffusers/v0.7.0/en/api/pipelines/stable_diffusion#diffusers.StableDiffusionInpaintPipeline.enable_attention_slicing
self.model.enable_attention_slicing()
# https://huggingface.co/docs/diffusers/v0.7.0/en/optimization/fp16#memory-efficient-attention
if kwargs['sd_enable_xformers']:
self.model.enable_xformers_memory_efficient_attention()
self.model = self.model.to(device)

if kwargs['sd_cpu_textencoder']:
Expand Down
5 changes: 5 additions & 0 deletions lama_cleaner/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def parse_args():
action="store_true",
help="After first time Stable Diffusion model downloaded, you can add this arg and remove --hf_access_token",
)
parser.add_argument(
"--sd-enable-xformers",
action="store_true",
help="Enable xFormers optimizations. Requires that xformers package has been installed. See: https://github.com/facebookresearch/xformers"
)
parser.add_argument("--device", default="cuda", type=str, choices=["cuda", "cpu"])
parser.add_argument("--gui", action="store_true", help="Launch as desktop app")
parser.add_argument(
Expand Down
1 change: 1 addition & 0 deletions lama_cleaner/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def main(args):
sd_disable_nsfw=args.sd_disable_nsfw,
sd_cpu_textencoder=args.sd_cpu_textencoder,
sd_run_local=args.sd_run_local,
sd_enable_xformers=args.sd_enable_xformers,
callback=diffuser_callback,
)

Expand Down

0 comments on commit d44dd88

Please sign in to comment.