Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized SD3 pipeline #1682

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions examples/stable-diffusion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ huggingface-cli login
Here is how to generate SD3 images with a single prompt:

```bash
PT_HPU_MAX_COMPOUND_OP_SIZE=1 \
regisss marked this conversation as resolved.
Show resolved Hide resolved
python text_to_image_generation.py \
--model_name_or_path stabilityai/stable-diffusion-3-medium-diffusers \
--prompts "Sailing ship painting by Van Gogh" \
Expand All @@ -321,9 +320,47 @@ python text_to_image_generation.py \
--bf16
```

> [!NOTE]
> For improved performance of the SD3 pipeline on Gaudi, it is recommended to configure the environment
> by setting PT_HPU_MAX_COMPOUND_OP_SIZE to 1.
This model can also be quantized with some ops running in FP8 precision.

Before quantization, run stats collection using measure mode:

```bash
QUANT_CONFIG=quantization/stable-diffusion-3/measure_config.json \
python text_to_image_generation.py \
--model_name_or_path stabilityai/stable-diffusion-3-medium-diffusers \
--prompts "Sailing ship painting by Van Gogh" \
--num_images_per_prompt 10 \
--batch_size 1 \
--num_inference_steps 28 \
--image_save_dir /tmp/stable_diffusion_3_images \
--scheduler default \
--use_habana \
--use_hpu_graphs \
--gaudi_config Habana/stable-diffusion \
--sdp_on_bf16 \
--bf16 \
--quant_mode measure
```

After stats collection, here is how to run SD3 in quantization mode:

```bash
QUANT_CONFIG=quantization/stable-diffusion-3/quantize_config.json \
python text_to_image_generation.py \
--model_name_or_path stabilityai/stable-diffusion-3-medium-diffusers \
--prompts "Sailing ship painting by Van Gogh" \
--num_images_per_prompt 10 \
--batch_size 1 \
--num_inference_steps 28 \
--image_save_dir /tmp/stable_diffusion_3_images \
--scheduler default \
--use_habana \
--use_hpu_graphs \
--gaudi_config Habana/stable-diffusion \
--sdp_on_bf16 \
--bf16 \
--quant_mode quantize
```

### FLUX.1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"method": "HOOKS",
"mode": "MEASURE",
"dump_stats_path": "quantization/stable-diffusion-3/measure_all/fp8"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"method": "HOOKS",
"mode": "QUANTIZE",
"scale_method": "maxabs_hw_opt_weight",
"dump_stats_path": "quantization/stable-diffusion-3/measure_all/fp8"
}
9 changes: 9 additions & 0 deletions examples/stable-diffusion/text_to_image_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ def main():
default=None,
help="The file with prompts (for large number of images generation).",
)
parser.add_argument(
"--lora_scale",
type=float,
default=None,
help="A lora scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded.",
)
args = parser.parse_args()

if args.optimize and not args.use_habana:
Expand Down Expand Up @@ -380,6 +386,9 @@ def main():
if args.throughput_warmup_steps is not None:
kwargs_call["throughput_warmup_steps"] = args.throughput_warmup_steps

if args.lora_scale is not None:
kwargs_call["lora_scale"] = args.lora_scale

negative_prompts = args.negative_prompts
if args.distributed:
distributed_state = PartialState()
Expand Down
Loading