-
Notifications
You must be signed in to change notification settings - Fork 3.7k
diffusion: support zimage #14067
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
Merged
Merged
diffusion: support zimage #14067
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
python/sglang/multimodal_gen/configs/models/dits/zimage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copied and adapted from: https://github.com/hao-ai-lab/FastVideo | ||
|
|
||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from dataclasses import dataclass, field | ||
| from typing import Tuple | ||
|
|
||
| from sglang.multimodal_gen.configs.models.dits.base import DiTArchConfig, DiTConfig | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZImageArchConfig(DiTArchConfig): | ||
| all_patch_size: Tuple[int, ...] = (2,) | ||
| all_f_patch_size: Tuple[int, ...] = (1,) | ||
| in_channels: int = 16 | ||
| out_channels: int | None = None | ||
| dim: int = 3840 | ||
| num_layers: int = 30 | ||
| n_refiner_layers: int = 2 | ||
| num_attention_heads: int = 30 | ||
| n_kv_heads: int = 30 | ||
| norm_eps: float = 1e-5 | ||
| qk_norm: bool = True | ||
| cap_feat_dim: int = 2560 | ||
| rope_theta: float = 256.0 | ||
| t_scale: float = 1000.0 | ||
| axes_dims: Tuple[int, int, int] = (32, 48, 48) | ||
| axes_lens: Tuple[int, int, int] = (1024, 512, 512) | ||
|
|
||
| def __post_init__(self): | ||
| super().__post_init__() | ||
| self.out_channels = self.out_channels or self.in_channels | ||
| self.num_channels_latents = self.in_channels | ||
| self.hidden_size = self.dim | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZImageDitConfig(DiTConfig): | ||
| arch_config: ZImageArchConfig = field(default_factory=ZImageArchConfig) | ||
|
|
||
| prefix: str = "zimage" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
python/sglang/multimodal_gen/configs/pipeline_configs/zimage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copied and adapted from: https://github.com/hao-ai-lab/FastVideo | ||
| from dataclasses import dataclass, field | ||
| from typing import Callable | ||
|
|
||
| import torch | ||
|
|
||
| from sglang.multimodal_gen.configs.models import DiTConfig, EncoderConfig, VAEConfig | ||
| from sglang.multimodal_gen.configs.models.dits.zimage import ZImageDitConfig | ||
| from sglang.multimodal_gen.configs.models.encoders import ( | ||
| BaseEncoderOutput, | ||
| TextEncoderConfig, | ||
| ) | ||
| from sglang.multimodal_gen.configs.models.vaes.flux import FluxVAEConfig | ||
| from sglang.multimodal_gen.configs.pipeline_configs.base import ( | ||
| ImagePipelineConfig, | ||
| ModelTaskType, | ||
| ) | ||
|
|
||
|
|
||
| def zimage_preprocess_text(prompt: str): | ||
| messages = [ | ||
| {"role": "user", "content": prompt}, | ||
| ] | ||
| return messages | ||
|
|
||
|
|
||
| def zimage_postprocess_text(outputs: BaseEncoderOutput, _text_inputs) -> torch.Tensor: | ||
| device = outputs.hidden_states[-2].device | ||
| prompt_mask = _text_inputs.attention_mask.to(device).bool() | ||
| return outputs.hidden_states[-2][0][prompt_mask[0]] | ||
|
|
||
|
|
||
| class TransformersModelConfig(EncoderConfig): | ||
| tokenizer_kwargs: dict = field(default_factory=lambda: {}) | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZImagePipelineConfig(ImagePipelineConfig): | ||
|
|
||
| should_use_guidance: bool = False | ||
| task_type: ModelTaskType = ModelTaskType.T2I | ||
|
|
||
| dit_config: DiTConfig = field(default_factory=ZImageDitConfig) | ||
| vae_config: VAEConfig = field(default_factory=FluxVAEConfig) | ||
| text_encoder_configs: tuple[EncoderConfig, ...] = field( | ||
| default_factory=lambda: (TextEncoderConfig(),) | ||
| ) | ||
|
|
||
| preprocess_text_funcs: tuple[Callable, ...] = field( | ||
| default_factory=lambda: (zimage_preprocess_text,) | ||
| ) | ||
|
|
||
| postprocess_text_funcs: tuple[Callable, ...] = field( | ||
| default_factory=lambda: (zimage_postprocess_text,) | ||
| ) | ||
|
|
||
| def tokenize_prompt(self, prompts: list[str], tokenizer, tok_kwargs) -> dict: | ||
| # flatten to 1-d list | ||
| inputs = tokenizer.apply_chat_template( | ||
| prompts, | ||
| tokenize=True, | ||
| add_generation_prompt=True, | ||
| enable_thinking=True, | ||
| padding="max_length", | ||
| max_length=512, # TODO (yhyang201): set max length according to config | ||
| truncation=True, | ||
| return_tensors="pt", | ||
| return_dict=True, | ||
| ) | ||
| return inputs | ||
|
|
||
| def post_denoising_loop(self, latents, batch): | ||
| bs, channels, num_frames, height, width = latents.shape | ||
| return latents.view(bs, channels, height, width) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copied and adapted from: https://github.com/hao-ai-lab/FastVideo | ||
|
|
||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from dataclasses import dataclass, field | ||
|
|
||
| from sglang.multimodal_gen.configs.sample.base import SamplingParams | ||
| from sglang.multimodal_gen.configs.sample.teacache import TeaCacheParams | ||
|
|
||
|
|
||
| @dataclass | ||
| class ZImageSamplingParams(SamplingParams): | ||
| num_inference_steps: int = 9 | ||
|
|
||
| num_frames: int = 1 | ||
| height: int = 720 | ||
| width: int = 1280 | ||
| fps: int = 24 | ||
|
|
||
| guidance_scale: float = 0.0 | ||
|
|
||
| teacache_params: TeaCacheParams = field( | ||
| default_factory=lambda: TeaCacheParams( | ||
| teacache_thresh=0.15, | ||
| coefficients=[ | ||
| 7.33226126e02, | ||
| -4.01131952e02, | ||
| 6.75869174e01, | ||
| -3.14987800e00, | ||
| 9.61237896e-02, | ||
| ], | ||
| ) | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
max_lengthis hardcoded to 512. As the TODO comment suggests, this should be made configurable rather than being a fixed value.