|
| 1 | +<!-- Copyright 2025 The HuggingFace Team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. --> |
| 14 | + |
| 15 | +# HunyuanImage2.1 |
| 16 | + |
| 17 | + |
| 18 | +HunyuanImage-2.1 is a 17B text-to-image model that is capable of generating 2K (2048 x 2048) resolution images |
| 19 | + |
| 20 | +HunyuanImage-2.1 comes in the following variants: |
| 21 | + |
| 22 | +| model type | model id | |
| 23 | +|:----------:|:--------:| |
| 24 | +| HunyuanImage-2.1 | [hunyuanvideo-community/HunyuanImage-2.1-Diffusers](https://huggingface.co/hunyuanvideo-community/HunyuanImage-2.1-Diffusers) | |
| 25 | +| HunyuanImage-2.1-Distilled | [hunyuanvideo-community/HunyuanImage-2.1-Distilled-Diffusers](https://huggingface.co/hunyuanvideo-community/HunyuanImage-2.1-Distilled-Diffusers) | |
| 26 | +| HunyuanImage-2.1-Refiner | [hunyuanvideo-community/HunyuanImage-2.1-Refiner-Diffusers](https://huggingface.co/hunyuanvideo-community/HunyuanImage-2.1-Refiner-Diffusers) | |
| 27 | + |
| 28 | +> [!TIP] |
| 29 | +> [Caching](../../optimization/cache) may also speed up inference by storing and reusing intermediate outputs. |
| 30 | +
|
| 31 | +## HunyuanImage-2.1 |
| 32 | + |
| 33 | +HunyuanImage-2.1 applies [Adaptive Projected Guidance (APG)](https://huggingface.co/papers/2410.02416) combined with Classifier-Free Guidance (CFG) in the denoising loop. `HunyuanImagePipeline` has a `guider` component (read more about [Guider](../modular_diffusers/guiders.md)) and does not take a `guidance_scale` parameter at runtime. To change guider-related parameters, e.g., `guidance_scale`, you can update the `guider` configuration instead. |
| 34 | + |
| 35 | +```python |
| 36 | +import torch |
| 37 | +from diffusers import HunyuanImagePipeline |
| 38 | + |
| 39 | +pipe = HunyuanImagePipeline.from_pretrained( |
| 40 | + "hunyuanvideo-community/HunyuanImage-2.1-Diffusers", |
| 41 | + torch_dtype=torch.bfloat16 |
| 42 | +) |
| 43 | +pipe = pipe.to("cuda") |
| 44 | +``` |
| 45 | + |
| 46 | +You can inspect the `guider` object: |
| 47 | + |
| 48 | +```py |
| 49 | +>>> pipe.guider |
| 50 | +AdaptiveProjectedMixGuidance { |
| 51 | + "_class_name": "AdaptiveProjectedMixGuidance", |
| 52 | + "_diffusers_version": "0.36.0.dev0", |
| 53 | + "adaptive_projected_guidance_momentum": -0.5, |
| 54 | + "adaptive_projected_guidance_rescale": 10.0, |
| 55 | + "adaptive_projected_guidance_scale": 10.0, |
| 56 | + "adaptive_projected_guidance_start_step": 5, |
| 57 | + "enabled": true, |
| 58 | + "eta": 0.0, |
| 59 | + "guidance_rescale": 0.0, |
| 60 | + "guidance_scale": 3.5, |
| 61 | + "start": 0.0, |
| 62 | + "stop": 1.0, |
| 63 | + "use_original_formulation": false |
| 64 | +} |
| 65 | + |
| 66 | +State: |
| 67 | + step: None |
| 68 | + num_inference_steps: None |
| 69 | + timestep: None |
| 70 | + count_prepared: 0 |
| 71 | + enabled: True |
| 72 | + num_conditions: 2 |
| 73 | + momentum_buffer: None |
| 74 | + is_apg_enabled: False |
| 75 | + is_cfg_enabled: True |
| 76 | +``` |
| 77 | + |
| 78 | +To update the guider with a different configuration, use the `new()` method. For example, to generate an image with `guidance_scale=5.0` while keeping all other default guidance parameters: |
| 79 | + |
| 80 | +```py |
| 81 | +import torch |
| 82 | +from diffusers import HunyuanImagePipeline |
| 83 | + |
| 84 | +pipe = HunyuanImagePipeline.from_pretrained( |
| 85 | + "hunyuanvideo-community/HunyuanImage-2.1-Diffusers", |
| 86 | + torch_dtype=torch.bfloat16 |
| 87 | +) |
| 88 | +pipe = pipe.to("cuda") |
| 89 | + |
| 90 | +# Update the guider configuration |
| 91 | +pipe.guider = pipe.guider.new(guidance_scale=5.0) |
| 92 | + |
| 93 | +prompt = ( |
| 94 | + "A cute, cartoon-style anthropomorphic penguin plush toy with fluffy fur, standing in a painting studio, " |
| 95 | + "wearing a red knitted scarf and a red beret with the word 'Tencent' on it, holding a paintbrush with a " |
| 96 | + "focused expression as it paints an oil painting of the Mona Lisa, rendered in a photorealistic photographic style." |
| 97 | +) |
| 98 | + |
| 99 | +image = pipe( |
| 100 | + prompt=prompt, |
| 101 | + num_inference_steps=50, |
| 102 | + height=2048, |
| 103 | + width=2048, |
| 104 | +).images[0] |
| 105 | +image.save("image.png") |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +## HunyuanImage-2.1-Distilled |
| 110 | + |
| 111 | +use `distilled_guidance_scale` with the guidance-distilled checkpoint, |
| 112 | + |
| 113 | +```py |
| 114 | +import torch |
| 115 | +from diffusers import HunyuanImagePipeline |
| 116 | +pipe = HunyuanImagePipeline.from_pretrained("hunyuanvideo-community/HunyuanImage-2.1-Distilled-Diffusers", torch_dtype=torch.bfloat16) |
| 117 | +pipe = pipe.to("cuda") |
| 118 | + |
| 119 | +prompt = ( |
| 120 | + "A cute, cartoon-style anthropomorphic penguin plush toy with fluffy fur, standing in a painting studio, " |
| 121 | + "wearing a red knitted scarf and a red beret with the word 'Tencent' on it, holding a paintbrush with a " |
| 122 | + "focused expression as it paints an oil painting of the Mona Lisa, rendered in a photorealistic photographic style." |
| 123 | +) |
| 124 | + |
| 125 | +out = pipe( |
| 126 | + prompt, |
| 127 | + num_inference_steps=8, |
| 128 | + distilled_guidance_scale=3.25, |
| 129 | + height=2048, |
| 130 | + width=2048, |
| 131 | + generator=generator, |
| 132 | +).images[0] |
| 133 | + |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +## HunyuanImagePipeline |
| 138 | + |
| 139 | +[[autodoc]] HunyuanImagePipeline |
| 140 | + - all |
| 141 | + - __call__ |
| 142 | + |
| 143 | +## HunyuanImageRefinerPipeline |
| 144 | + |
| 145 | +[[autodoc]] HunyuanImageRefinerPipeline |
| 146 | + - all |
| 147 | + - __call__ |
| 148 | + |
| 149 | + |
| 150 | +## HunyuanImagePipelineOutput |
| 151 | + |
| 152 | +[[autodoc]] pipelines.hunyuan_image.pipeline_output.HunyuanImagePipelineOutput |
0 commit comments