[Fix] diffusion/flux: use true_cfg_scale as the FLUX CFG weight (#44260)
This commit is contained in:
@@ -231,6 +231,24 @@ Use `--lora-path` at startup or the [LoRA management API](/docs/sglang-diffusion
|
||||
- [`XLabs-AI/flux-furry-lora`](https://huggingface.co/XLabs-AI/flux-furry-lora)
|
||||
- [`XLabs-AI/flux-RealismLora`](https://huggingface.co/XLabs-AI/flux-RealismLora)
|
||||
|
||||
#### 4.2.4 Negative prompts (true CFG)
|
||||
|
||||
FLUX.1-dev is guidance-distilled: its guidance embedding comes from the server's `--embedded-cfg-scale` (3.5 for FLUX.1), and the requests above run one DiT forward per step. A negative prompt turns on classifier-free guidance, which adds an unconditional forward per step:
|
||||
|
||||
- `--true-cfg-scale` sets the CFG weight, as in diffusers `FluxPipeline`.
|
||||
- Without `--true-cfg-scale`, the negative prompt uses `--guidance-scale` as the CFG weight.
|
||||
|
||||
```bash
|
||||
sglang generate \
|
||||
--model-path black-forest-labs/FLUX.1-dev \
|
||||
--prompt "A red bicycle leaning against a stone wall beside a quiet lake at sunset" \
|
||||
--negative-prompt "blurry, distorted, low quality" \
|
||||
--true-cfg-scale 2.0 \
|
||||
--width 1024 --height 1024 --num-inference-steps 50 --seed 42
|
||||
```
|
||||
|
||||
Keep the weight moderate. On top of the distilled guidance, CFG over-exposes FLUX.1-dev in diffusers as well as in SGLang: at 1024x1024 and 28 steps, about 45% of pixels clip to 0 or 255 at a weight of 4, and 8-15% at 1.5-2.
|
||||
|
||||
## 5. Benchmark
|
||||
|
||||
### 5.1 Speedup Benchmark
|
||||
|
||||
@@ -125,6 +125,12 @@ class FluxPipelineConfig(ImagePipelineConfig):
|
||||
def prepare_sigmas(self, sigmas, num_inference_steps):
|
||||
return self._prepare_sigmas(sigmas, num_inference_steps)
|
||||
|
||||
def get_classifier_free_guidance_scale(self, batch, guidance_scale: float) -> float:
|
||||
# true_cfg_scale is the CFG weight when given, as in diffusers FluxPipeline.
|
||||
if batch.true_cfg_scale is not None:
|
||||
return batch.true_cfg_scale
|
||||
return guidance_scale
|
||||
|
||||
def prepare_latent_shape(self, batch, batch_size, num_frames):
|
||||
height = 2 * (
|
||||
batch.height // (self.vae_config.arch_config.vae_scale_factor * 2)
|
||||
|
||||
@@ -1411,7 +1411,7 @@ class SamplingParams:
|
||||
"--true-cfg-scale",
|
||||
type=float,
|
||||
dest="true_cfg_scale",
|
||||
help="True CFG scale for models that distinguish distilled guidance from standard CFG (e.g., Qwen-Image)",
|
||||
help="True CFG scale for models that distinguish distilled guidance from standard CFG (e.g., Qwen-Image, FLUX.1)",
|
||||
)
|
||||
add_argument(
|
||||
"--guidance-rescale",
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
|
||||
from sglang.multimodal_gen.configs.pipeline_configs.flux import (
|
||||
Flux2PipelineConfig,
|
||||
FluxPipelineConfig,
|
||||
)
|
||||
|
||||
|
||||
class TestFluxTrueCFGScale(unittest.TestCase):
|
||||
def test_true_cfg_scale_is_the_cfg_weight(self):
|
||||
for config in (FluxPipelineConfig(), Flux2PipelineConfig()):
|
||||
with self.subTest(config=type(config).__name__):
|
||||
batch = SimpleNamespace(true_cfg_scale=4.0)
|
||||
self.assertEqual(
|
||||
config.get_classifier_free_guidance_scale(batch, 3.5), 4.0
|
||||
)
|
||||
|
||||
def test_guidance_scale_is_the_cfg_weight_without_true_cfg_scale(self):
|
||||
batch = SimpleNamespace(true_cfg_scale=None)
|
||||
self.assertEqual(
|
||||
FluxPipelineConfig().get_classifier_free_guidance_scale(batch, 3.5), 3.5
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user