Skip to content

Commit eec1bed

Browse files
xin3hepre-commit-ci[bot]Copilot
authored
enable --eval for diffusion model (#1522)
Signed-off-by: Xin He <xin3.he@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
1 parent 0127590 commit eec1bed

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

auto_round/compressors/diffusion/eval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def diffusion_eval(
7070
image_save_dir,
7171
batch_size,
7272
gen_kwargs,
73+
limit=None,
7374
):
7475
if (
7576
not importlib.util.find_spec("clip")
@@ -82,6 +83,7 @@ def diffusion_eval(
8283
dataloader, _, _ = get_diffusion_dataloader(prompt_file, nsamples=-1, bs=batch_size)
8384
prompt_list = []
8485
image_list = []
86+
num_samples = 0
8587
for image_ids, prompts in dataloader:
8688
prompt_list.extend(prompts)
8789

@@ -95,6 +97,9 @@ def diffusion_eval(
9597
continue
9698
new_ids.append(image_id)
9799
new_prompts.append(prompts[idx])
100+
num_samples += 1
101+
if limit is not None and num_samples >= limit > 0:
102+
break
98103

99104
if len(new_prompts) == 0:
100105
continue
@@ -103,6 +108,9 @@ def diffusion_eval(
103108
for idx, image_id in enumerate(new_ids):
104109
output.images[idx].save(os.path.join(image_save_dir, str(image_id) + ".png"))
105110

111+
if limit is not None and num_samples >= limit > 0:
112+
break
113+
106114
result = {}
107115
for metric in metrics:
108116
result.update(metric_map[metric](prompt_list, image_list, pipe.device))

auto_round/eval/eval_cli.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
get_device_and_parallelism,
2525
get_device_str,
2626
get_model_dtype,
27+
is_diffusion_model,
2728
set_cuda_visible_devices,
2829
)
2930

@@ -113,6 +114,59 @@ def __init__(self, *args, **kwargs):
113114
help="(for vllm) Custom vllm arguments in format: 'arg1=value1,arg2=value2'. "
114115
"Example: 'tensor_parallel_size=2,gpu_memory_utilization=0.9'",
115116
)
117+
# Diffusion model specific arguments
118+
diffusion_args = self.add_argument_group("diffusion model arguments")
119+
diffusion_args.add_argument(
120+
"--prompt_file",
121+
default=None,
122+
type=str,
123+
help="File containing prompts for evaluation, one per line. "
124+
"Use this for batch evaluation with multiple prompts.",
125+
)
126+
diffusion_args.add_argument(
127+
"--prompt",
128+
default=None,
129+
type=str,
130+
help="Single prompt for quick testing. " "Overrides prompt_file if both are specified.",
131+
)
132+
diffusion_args.add_argument(
133+
"--metrics",
134+
"--metric",
135+
default="clip",
136+
help="Evaluation metrics for generated images. "
137+
"'clip': CLIP score measuring text-image alignment. "
138+
"'clip-iqa': CLIP-based image quality assessment. "
139+
"'imagereward': Learned metric for image quality.",
140+
)
141+
diffusion_args.add_argument(
142+
"--image_save_dir",
143+
default="./tmp_image_save",
144+
type=str,
145+
help="Directory to save generated images during evaluation. " "Useful for visual inspection of results.",
146+
)
147+
diffusion_args.add_argument(
148+
"--guidance_scale",
149+
default=7.5,
150+
type=float,
151+
help="Classifier-free guidance scale for diffusion models. "
152+
"Higher values (7-20) make the model follow the prompt more closely. "
153+
"Lower values give more creative/random results.",
154+
)
155+
diffusion_args.add_argument(
156+
"--num_inference_steps",
157+
default=50,
158+
type=int,
159+
help="Number of denoising steps in the diffusion process. "
160+
"More steps (50-100) usually give better quality but take longer. "
161+
"Fewer steps (10-30) are faster but lower quality.",
162+
)
163+
diffusion_args.add_argument(
164+
"--generator_seed",
165+
default=None,
166+
type=int,
167+
help="Random seed for image generation reproducibility. "
168+
"Using the same seed produces identical results across runs.",
169+
)
116170

117171

118172
def _eval_init(tasks, model_path, device, disable_trust_remote_code=False, dtype="auto"):
@@ -134,6 +188,13 @@ def eval(args):
134188
"lm_eval>=0.4.2", "lm-eval is required for evaluation, please install it with `pip install 'lm-eval>=0.4.2'`"
135189
)
136190

191+
if is_diffusion_model(args.model):
192+
from auto_round.eval.evaluation import evaluate_diffusion_model
193+
from auto_round.utils import diffusion_load_model
194+
195+
pipe, _ = diffusion_load_model(args.model)
196+
evaluate_diffusion_model(args, pipe=pipe)
197+
return
137198
if args.eval_backend == "vllm":
138199
assert isinstance(args.model, str), "vllm evaluation only supports model name or path."
139200
eval_with_vllm(args)

auto_round/eval/evaluation.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,28 @@ def simple_evaluate(
8585
)
8686

8787

88-
def evaluate_diffusion_model(autoround, model, args):
88+
def evaluate_diffusion_model(args, autoround=None, model=None, pipe=None):
8989
"""
9090
Evaluate diffusion models.
9191
9292
Args:
93-
autoround: AutoRound instance
94-
model: Quantized model
9593
args: Command line arguments
94+
autoround: AutoRound instance (option 1)
95+
model: Diffusion model instance (option 1)
96+
pipe: Diffusion pipeline instance (option 2)
9697
"""
98+
if pipe is None and (autoround is None or model is None):
99+
raise ValueError("Either 'pipe' must be provided, or both 'autoround' and 'model' must be provided.")
100+
97101
import torch
98102

99103
from auto_round.utils import detect_device, get_model_dtype, logger
100104

101105
# Prepare inference pipeline
102-
pipe = autoround.pipe
103-
pipe.to(model.dtype)
104-
pipe.transformer = model
106+
if pipe is None:
107+
pipe = autoround.pipe
108+
pipe.to(model.dtype)
109+
pipe.transformer = model
105110
device_str = detect_device(args.device_map if hasattr(args, "device_map") else "0")
106111
pipe = pipe.to(device_str)
107112

@@ -134,11 +139,11 @@ def evaluate_diffusion_model(autoround, model, args):
134139
logger.info(f"Image generated with prompt {args.prompt} is saved as {save_path}")
135140

136141
# Batch prompt evaluation
137-
if args.prompt_file is not None:
142+
elif args.prompt_file is not None:
138143
from auto_round.compressors.diffusion import diffusion_eval
139144

140145
metrics = args.metrics.split(",")
141-
diffusion_eval(pipe, args.prompt_file, metrics, args.image_save_dir, 1, gen_kwargs)
146+
diffusion_eval(pipe, args.prompt_file, metrics, args.image_save_dir, 1, gen_kwargs, args.limit)
142147

143148

144149
def load_gguf_model_for_eval(eval_folder, formats, args):
@@ -375,7 +380,7 @@ def run_model_evaluation(model, tokenizer, autoround, folders, formats, device_s
375380

376381
# Handle diffusion models separately
377382
if getattr(autoround, "diffusion", False):
378-
evaluate_diffusion_model(autoround, model, args)
383+
evaluate_diffusion_model(args, autoround=autoround, model=model)
379384
return
380385

381386
# Check if evaluation is needed for language models

0 commit comments

Comments
 (0)