|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +"""GLM-Image DiT configuration. |
| 3 | +
|
| 4 | +GLM-Image uses a 7B diffusion transformer (DiT) decoder that expands tokens |
| 5 | +from the autoregressive vision-language encoder into high-resolution images. |
| 6 | +""" |
| 7 | +from dataclasses import dataclass, field |
| 8 | + |
| 9 | +from fastvideo.configs.models.dits.base import DiTArchConfig, DiTConfig |
| 10 | + |
| 11 | + |
| 12 | +def is_blocks(n: str, m) -> bool: |
| 13 | + return "transformer_blocks" in n and str.isdigit(n.split(".")[-1]) |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class GlmImageDiTArchConfig(DiTArchConfig): |
| 18 | + """Architecture config for GlmImageTransformer2DModel.""" |
| 19 | + |
| 20 | + _fsdp_shard_conditions: list = field(default_factory=lambda: [is_blocks]) |
| 21 | + |
| 22 | + # GLM-Image DiT settings (7B model) |
| 23 | + # hidden_size = num_attention_heads * attention_head_dim = 64 * 64 = 4096 |
| 24 | + hidden_size: int = 4096 |
| 25 | + num_attention_heads: int = 32 |
| 26 | + attention_head_dim: int = 128 |
| 27 | + in_channels: int = 16 |
| 28 | + out_channels: int = 16 |
| 29 | + num_layers: int = 30 |
| 30 | + |
| 31 | + # Text and condition dims |
| 32 | + text_embed_dim: int = 1472 |
| 33 | + time_embed_dim: int = 512 |
| 34 | + condition_dim: int = 256 |
| 35 | + |
| 36 | + # VQ settings for AR tokens |
| 37 | + prior_vq_quantizer_codebook_size: int = 16384 |
| 38 | + |
| 39 | + # Patch embedding |
| 40 | + patch_size: int = 2 |
| 41 | + |
| 42 | + # Positional embedding max resolution |
| 43 | + max_height: int = 2048 |
| 44 | + max_width: int = 2048 |
| 45 | + |
| 46 | + # QK normalization |
| 47 | + qk_norm: str = "layer_norm" |
| 48 | + eps: float = 1e-5 |
| 49 | + |
| 50 | + # LoRA exclusions |
| 51 | + exclude_lora_layers: list[str] = field(default_factory=lambda: ["image_projector", "glyph_projector", "prior_token_embedding"]) |
| 52 | + |
| 53 | + # Param name mappings for weight loading (HF -> custom) |
| 54 | + param_names_mapping: dict = field( |
| 55 | + default_factory=lambda: { |
| 56 | + # Projectors (mapped from FeedForward net.0.proj -> fc_in, net.2 -> fc_out) |
| 57 | + r"^image_projector\.net\.0\.proj\.(.*)$": r"image_projector.fc_in.\1", |
| 58 | + r"^image_projector\.net\.2\.(.*)$": r"image_projector.fc_out.\1", |
| 59 | + r"^glyph_projector\.net\.0\.proj\.(.*)$": r"glyph_projector.fc_in.\1", |
| 60 | + r"^glyph_projector\.net\.2\.(.*)$": r"glyph_projector.fc_out.\1", |
| 61 | + r"^prior_projector\.net\.0\.proj\.(.*)$": r"prior_projector.fc_in.\1", |
| 62 | + r"^prior_projector\.net\.2\.(.*)$": r"prior_projector.fc_out.\1", |
| 63 | + |
| 64 | + r"^prior_token_embedding\.(.*)$": r"prior_token_embedding.\1", |
| 65 | + |
| 66 | + # Transformer blocks |
| 67 | + r"^transformer_blocks\.(\d+)\.norm1\.(.*)$": r"transformer_blocks.\1.norm1.\2", |
| 68 | + r"^transformer_blocks\.(\d+)\.attn1\.to_q\.(.*)$": r"transformer_blocks.\1.attn1.to_q.\2", |
| 69 | + r"^transformer_blocks\.(\d+)\.attn1\.to_k\.(.*)$": r"transformer_blocks.\1.attn1.to_k.\2", |
| 70 | + r"^transformer_blocks\.(\d+)\.attn1\.to_v\.(.*)$": r"transformer_blocks.\1.attn1.to_v.\2", |
| 71 | + r"^transformer_blocks\.(\d+)\.attn1\.to_out\.0\.(.*)$": r"transformer_blocks.\1.attn1.to_out.0.\2", |
| 72 | + |
| 73 | + # FeedForward in blocks (net.0.proj -> fc_in, net.2 -> fc_out) |
| 74 | + r"^transformer_blocks\.(\d+)\.ff\.net\.0\.proj\.(.*)$": r"transformer_blocks.\1.ff.fc_in.\2", |
| 75 | + r"^transformer_blocks\.(\d+)\.ff\.net\.2\.(.*)$": r"transformer_blocks.\1.ff.fc_out.\2", |
| 76 | + |
| 77 | + # Output |
| 78 | + r"^norm_out\.(.*)$": r"norm_out.\1", |
| 79 | + r"^proj_out\.(.*)$": r"proj_out.\1", |
| 80 | + }) |
| 81 | + |
| 82 | + reverse_param_names_mapping: dict = field(default_factory=dict) |
| 83 | + lora_param_names_mapping: dict = field(default_factory=dict) |
| 84 | + |
| 85 | + def __post_init__(self): |
| 86 | + super().__post_init__() |
| 87 | + self.num_channels_latents = self.out_channels |
| 88 | + |
| 89 | + |
| 90 | +@dataclass |
| 91 | +class GlmImageDiTConfig(DiTConfig): |
| 92 | + """Configuration for GLM-Image DiT model.""" |
| 93 | + |
| 94 | + arch_config: DiTArchConfig = field(default_factory=GlmImageDiTArchConfig) |
| 95 | + prefix: str = "GlmImage" |
0 commit comments