|
| 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 | + |
| 16 | +# GLM-Image |
| 17 | + |
| 18 | +## Overview |
| 19 | + |
| 20 | +GLM-Image is an image generation model adopts a hybrid autoregressive + diffusion decoder architecture, effectively pushing the upper bound of visual fidelity and fine-grained details. In general image generation quality, it aligns with industry-standard LDM-based approaches, while demonstrating significant advantages in knowledge-intensive image generation scenarios. |
| 21 | + |
| 22 | +Model architecture: a hybrid autoregressive + diffusion decoder design、 |
| 23 | + |
| 24 | ++ Autoregressive generator: a 9B-parameter model initialized from [GLM-4-9B-0414](https://huggingface.co/zai-org/GLM-4-9B-0414), with an expanded vocabulary to incorporate visual tokens. The model first generates a compact encoding of approximately 256 tokens, then expands to 1K–4K tokens, corresponding to 1K–2K high-resolution image outputs. You can check AR model in class `GlmImageForConditionalGeneration` of `transformers` library. |
| 25 | ++ Diffusion Decoder: a 7B-parameter decoder based on a single-stream DiT architecture for latent-space image decoding. It is equipped with a Glyph Encoder text module, significantly improving accurate text rendering within images. |
| 26 | + |
| 27 | +Post-training with decoupled reinforcement learning: the model introduces a fine-grained, modular feedback strategy using the GRPO algorithm, substantially enhancing both semantic understanding and visual detail quality. |
| 28 | + |
| 29 | ++ Autoregressive module: provides low-frequency feedback signals focused on aesthetics and semantic alignment, improving instruction following and artistic expressiveness. |
| 30 | ++ Decoder module: delivers high-frequency feedback targeting detail fidelity and text accuracy, resulting in highly realistic textures, lighting, and color reproduction, as well as more precise text rendering. |
| 31 | + |
| 32 | +GLM-Image supports both text-to-image and image-to-image generation within a single model |
| 33 | + |
| 34 | ++ Text-to-image: generates high-detail images from textual descriptions, with particularly strong performance in information-dense scenarios. |
| 35 | ++ Image-to-image: supports a wide range of tasks, including image editing, style transfer, multi-subject consistency, and identity-preserving generation for people and objects. |
| 36 | + |
| 37 | +This pipeline was contributed by [zRzRzRzRzRzRzR](https://github.com/zRzRzRzRzRzRzR). The codebase can be found [here](https://huggingface.co/zai-org/GLM-Image). |
| 38 | + |
| 39 | +## Usage examples |
| 40 | + |
| 41 | +### Text to Image Generation |
| 42 | + |
| 43 | +```python |
| 44 | +import torch |
| 45 | +from diffusers.pipelines.glm_image import GlmImagePipeline |
| 46 | + |
| 47 | +pipe = GlmImagePipeline.from_pretrained("zai-org/GLM-Image",torch_dtype=torch.bfloat16,device_map="cuda") |
| 48 | +prompt = "A beautifully designed modern food magazine style dessert recipe illustration, themed around a raspberry mousse cake. The overall layout is clean and bright, divided into four main areas: the top left features a bold black title 'Raspberry Mousse Cake Recipe Guide', with a soft-lit close-up photo of the finished cake on the right, showcasing a light pink cake adorned with fresh raspberries and mint leaves; the bottom left contains an ingredient list section, titled 'Ingredients' in a simple font, listing 'Flour 150g', 'Eggs 3', 'Sugar 120g', 'Raspberry puree 200g', 'Gelatin sheets 10g', 'Whipping cream 300ml', and 'Fresh raspberries', each accompanied by minimalist line icons (like a flour bag, eggs, sugar jar, etc.); the bottom right displays four equally sized step boxes, each containing high-definition macro photos and corresponding instructions, arranged from top to bottom as follows: Step 1 shows a whisk whipping white foam (with the instruction 'Whip egg whites to stiff peaks'), Step 2 shows a red-and-white mixture being folded with a spatula (with the instruction 'Gently fold in the puree and batter'), Step 3 shows pink liquid being poured into a round mold (with the instruction 'Pour into mold and chill for 4 hours'), Step 4 shows the finished cake decorated with raspberries and mint leaves (with the instruction 'Decorate with raspberries and mint'); a light brown information bar runs along the bottom edge, with icons on the left representing 'Preparation time: 30 minutes', 'Cooking time: 20 minutes', and 'Servings: 8'. The overall color scheme is dominated by creamy white and light pink, with a subtle paper texture in the background, featuring compact and orderly text and image layout with clear information hierarchy." |
| 49 | +image = pipe( |
| 50 | + prompt=prompt, |
| 51 | + height=32 * 32, |
| 52 | + width=36 * 32, |
| 53 | + num_inference_steps=30, |
| 54 | + guidance_scale=1.5, |
| 55 | + generator=torch.Generator(device="cuda").manual_seed(42), |
| 56 | +).images[0] |
| 57 | + |
| 58 | +image.save("output_t2i.png") |
| 59 | +``` |
| 60 | + |
| 61 | +### Image to Image Generation |
| 62 | + |
| 63 | +```python |
| 64 | +import torch |
| 65 | +from diffusers.pipelines.glm_image import GlmImagePipeline |
| 66 | +from PIL import Image |
| 67 | + |
| 68 | +pipe = GlmImagePipeline.from_pretrained("zai-org/GLM-Image",torch_dtype=torch.bfloat16,device_map="cuda") |
| 69 | +image_path = "cond.jpg" |
| 70 | +prompt = "Replace the background of the snow forest with an underground station featuring an automatic escalator." |
| 71 | +image = Image.open(image_path).convert("RGB") |
| 72 | +image = pipe( |
| 73 | + prompt=prompt, |
| 74 | + image=[image], # can input multiple images for multi-image-to-image generation such as [image, image1] |
| 75 | + height=33 * 32, |
| 76 | + width=32 * 32, |
| 77 | + num_inference_steps=30, |
| 78 | + guidance_scale=1.5, |
| 79 | + generator=torch.Generator(device="cuda").manual_seed(42), |
| 80 | +).images[0] |
| 81 | + |
| 82 | +image.save("output_i2i.png") |
| 83 | +``` |
| 84 | + |
| 85 | ++ Since the AR model used in GLM-Image is configured with `do_sample=True` and a temperature of `0.95` by default, the generated images can vary significantly across runs. We do not recommend setting do_sample=False, as this may lead to incorrect or degenerate outputs from the AR model. |
| 86 | + |
| 87 | +## GlmImagePipeline |
| 88 | + |
| 89 | +[[autodoc]] pipelines.glm_image.pipeline_glm_image.GlmImagePipeline |
| 90 | + - all |
| 91 | + - __call__ |
| 92 | + |
| 93 | +## GlmImagePipelineOutput |
| 94 | + |
| 95 | +[[autodoc]] pipelines.glm_image.pipeline_output.GlmImagePipelineOutput |
0 commit comments