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

Feat (examples/sdxl): Updates to SDXL entry-point #1020

Merged
merged 14 commits into from
Sep 12, 2024
Merged
17 changes: 12 additions & 5 deletions src/brevitas_examples/stable_diffusion/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def sdpa_zp_stats_type():
'weight_quant']
layer_map[torch.nn.Conv2d] = (layer_map[torch.nn.Conv2d][0], conv_qkwargs)

if args.quantize_sdp:
if args.sdpa_bit_width > 0:
# `args.weight_quant_granularity` must be compatible with `args.sdpa_quant_format`
sdpa_quantizers = generate_quantizers(
dtype=dtype,
Expand Down Expand Up @@ -484,9 +484,17 @@ def sdpa_zp_stats_type():
print(f"Checkpoint loaded!")
pipe = pipe.to(args.device)
elif not args.dry_run:
if (args.linear_input_bit_width > 0 or args.conv_input_bit_width > 0 or
args.sdpa_bit_width > 0 or
args.quantize_sdp) and args.input_scale_type == 'static':
# Model needs calibration if any of its activation quantizers are 'static'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet

activation_bw = [
args.linear_input_bit_width,
args.conv_input_bit_width,
args.sdpa_bit_width,]
activation_st = [
args.input_scale_type,
args.input_scale_type,
args.sdpa_scale_type,]
needs_calibration = any(map(lambda b, st: (b > 0) and st == 'static', activation_bw, activation_st))
if needs_calibration:
print("Applying activation calibration")
with torch.no_grad(), calibration_mode(pipe.unet):
run_val_inference(
Expand Down Expand Up @@ -952,7 +960,6 @@ def sdpa_zp_stats_type():
'dry-run',
default=False,
help='Generate a quantized model without any calibration. Default: Disabled')
add_bool_arg(parser, 'quantize-sdp', default=False, help='Quantize SDP. Default: Disabled')
add_bool_arg(
parser,
'override-conv-quant-config',
Expand Down
Loading