Skip to content

[quantization] SDNQ core loading#14277

Draft
asomoza wants to merge 2 commits into
mainfrom
sdnq-loader
Draft

[quantization] SDNQ core loading#14277
asomoza wants to merge 2 commits into
mainfrom
sdnq-loader

Conversation

@asomoza

@asomoza asomoza commented Jul 23, 2026

Copy link
Copy Markdown
Member

What does this PR do?

This PR adds basic support for SDNQ:

  • Load a prequantized SDNQ checkpoint with a normal from_pretrained call — no special arguments, the quantization config is read from the model's config.json.
  • Quantize a model on the fly by passing SDNQConfig(...) (or via PipelineQuantizationConfig) to from_pretrained.
  • Save and reload quantized models, including inside full pipelines.

Test code

load a pre quantized model

import torch

from diffusers import DiffusionPipeline


prompt = (
    "A highly detailed photograph of a cozy vintage bookshop interior at golden hour. "
    "Warm sunlight streams through a rain-streaked window, casting long shadows across worn "
    "oak floorboards. Floor-to-ceiling shelves are packed with hundreds of colorful leather-bound "
    "books, their gold-embossed spines catching the light. On the wooden counter sits a chalkboard "
    "sign with elegant handwritten text reading 'Rare Books & Coffee', beside a steaming ceramic "
    "mug, a brass reading lamp, and a sleeping orange tabby cat curled on a stack of newspapers. "
    "Dust motes float in the sunbeams. Intricate textures on the aged paper, cracked leather, and "
    "polished brass. Sharp focus, rich warm color grading, shallow depth of field, photorealistic, 8k."
)

pipe = DiffusionPipeline.from_pretrained(
    "Disty0/Z-Image-Turbo-SDNQ-int8",
    dtype=torch.bfloat16,
).to("cuda")

image = pipe(prompt, num_inference_steps=8, guidance_scale=0.0).images[0]
image.save("zimage_sdnq_load.png")
zimage_sdnq_load

pre quantize model on the fly

import torch

from diffusers import DiffusionPipeline, PipelineQuantizationConfig, SDNQConfig


prompt = (
    "A highly detailed photograph of a cozy vintage bookshop interior at golden hour. "
    "Warm sunlight streams through a rain-streaked window, casting long shadows across worn "
    "oak floorboards. Floor-to-ceiling shelves are packed with hundreds of colorful leather-bound "
    "books, their gold-embossed spines catching the light. On the wooden counter sits a chalkboard "
    "sign with elegant handwritten text reading 'Rare Books & Coffee', beside a steaming ceramic "
    "mug, a brass reading lamp, and a sleeping orange tabby cat curled on a stack of newspapers. "
    "Dust motes float in the sunbeams. Intricate textures on the aged paper, cracked leather, and "
    "polished brass. Sharp focus, rich warm color grading, shallow depth of field, photorealistic, 8k."
)

pipe = DiffusionPipeline.from_pretrained(
    "Tongyi-MAI/Z-Image-Turbo",
    quantization_config=PipelineQuantizationConfig(
        quant_mapping={"transformer": SDNQConfig(weights_dtype="int8")}
    ),
    dtype=torch.bfloat16,
).to("cuda")

image = pipe(prompt, num_inference_steps=8, guidance_scale=0.0).images[0]
image.save("zimage_sdnq_quantize.png")
zimage_sdnq_quantize

save quantized DiT

import torch

from diffusers import SDNQConfig, ZImageTransformer2DModel


save_dir = "z-image-turbo-sdnq-int8"

# Quantize the transformer during load.
transformer = ZImageTransformer2DModel.from_pretrained(
    "Tongyi-MAI/Z-Image-Turbo",
    subfolder="transformer",
    quantization_config=SDNQConfig(weights_dtype="int8"),
    dtype=torch.bfloat16,
)

# Serialize the quantized weights + config.
transformer.save_pretrained(save_dir)

@github-actions github-actions Bot added documentation Improvements or additions to documentation quantization tests modular-pipelines utils pipelines size/L PR with diff > 200 LOC labels Jul 23, 2026
@asomoza
asomoza marked this pull request as draft July 23, 2026 19:15
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation modular-pipelines pipelines quantization size/L PR with diff > 200 LOC tests utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants