Views
No views yet
1weights_dtype = "uint4" # 4-bit weights
2use_svd = True # SVD decomposition enabled
3svd_rank = 32 # SVD rank for quality preservation
4quantized_matmul_dtype = "int8" # Compute precision
5group_size = 0 # Auto group size1import torch
2from diffusers import DiffusionPipeline
3from sdnq.loader import apply_sdnq_options_to_model
4
5# Load the quantized model
6pipe = DiffusionPipeline.from_pretrained(
7 "YOUR_USERNAME/Z-Image-SDNQ-uint4-svd-r32",
8 torch_dtype=torch.bfloat16,
9 trust_remote_code=True
10)
11
12# Apply SDNQ configuration
13pipe.transformer = apply_sdnq_options_to_model(
14 pipe.transformer,
15 use_quantized_matmul=False # Set to False for Windows/No-Triton
16)
17pipe.text_encoder = apply_sdnq_options_to_model(
18 pipe.text_encoder,
19 use_quantized_matmul=False
20)
21
22pipe = pipe.to("cuda")
23
24# Generate image
25image = pipe(
26 prompt="a beautiful landscape with mountains and a lake at sunset",
27 num_inference_steps=30,
28 guidance_scale=1.0
29).images[0]
30
31image.save("output.png")1import torch
2from diffusers import DiffusionPipeline
3from sdnq.loader import apply_sdnq_options_to_model
4
5# Configuration
6WIDTH = 768
7HEIGHT = 1344 # 9:16 aspect ratio
8PROMPT = "a beautiful landscape with mountains and a lake at sunset, highly detailed, 8k, masterpiece"
9STEPS = 30
10GUIDANCE = 1.0
11
12# Load model
13pipe = DiffusionPipeline.from_pretrained(
14 "YOUR_USERNAME/Z-Image-SDNQ-uint4-svd-r32",
15 torch_dtype=torch.bfloat16,
16 trust_remote_code=True
17)
18
19# Configure SDNQ
20pipe.transformer = apply_sdnq_options_to_model(
21 pipe.transformer,
22 use_quantized_matmul=False
23)
24pipe.text_encoder = apply_sdnq_options_to_model(
25 pipe.text_encoder,
26 use_quantized_matmul=False
27)
28
29pipe = pipe.to("cuda")
30
31# Generate
32image = pipe(
33 prompt=PROMPT,
34 num_inference_steps=STEPS,
35 guidance_scale=GUIDANCE,
36 width=WIDTH,
37 height=HEIGHT
38).images[0]
39
40image.save("portrait_9x16.png")| Aspect Ratio | Resolution | Steps | Guidance Scale |
|---|---|---|---|
| 1:1 (Square) | 768x768 | 30 | 1.0 |
| 16:9 (Landscape) | 1344x768 | 30 | 1.0 |
| 9:16 (Portrait) | 768x1344 | 30 | 1.0 |
| 4:3 | 1024x768 | 30 | 1.0 |
pip install torch diffusers transformers sdnq1from diffusers import DiffusionPipeline
2from sdnq.loader import sdnq_post_load_quant, save_sdnq_model
3
4# Load base model
5pipe = DiffusionPipeline.from_pretrained(
6 "Tongyi-MAI/Z-Image",
7 torch_dtype=torch.bfloat16,
8 trust_remote_code=True
9)
10
11# Apply quantization to transformer
12quantized_transformer = sdnq_post_load_quant(
13 pipe.transformer,
14 weights_dtype="uint4",
15 use_svd=True,
16 svd_rank=32,
17 quantized_matmul_dtype="int8",
18 group_size=0
19)
20
21pipe.transformer = quantized_transformer
22
23# Save
24save_sdnq_model(pipe, "./Z-Image-SDNQ-uint4-svd-r32", is_pipeline=True)pip install sdnq)1@article{zimage2024,
2 title={Z-Image: Efficient Text-to-Image Synthesis},
3 author={Tongyi-MAI Team},
4 year={2024}
5}