# Copyright 2026 Bytedance Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import os
from dataclasses import dataclass, field
from typing import Any, Optional
from omegaconf import MISSING
from verl.base_config import BaseConfig
from verl.utils import hf_processor, hf_tokenizer
from verl.utils.fs import copy_to_local
from verl.utils.import_utils import import_external_libs
from verl.workers.config.model import MtpConfig
from verl_omni.utils.fs import resolve_model_local_dir
from .rollout import DiffusionPipelineConfig, DiffusionRolloutAlgoConfig
__all__ = ["DiffusionModelAROptimizerConfig", "DiffusionModelARConfig", "DiffusionModelConfig"]
logger = logging.getLogger(__name__)
@dataclass
class DiffusionModelAROptimizerConfig(BaseConfig):
# Partial overlay on the DiT optimizer. None inherits that field from actor.optim.
# Learning rate. None inherits actor.optim.lr.
lr: Optional[float] = None
# Weight decay. None inherits actor.optim.weight_decay.
weight_decay: Optional[float] = None
# Adam betas. None inherits actor.optim.betas.
betas: Optional[tuple[float, float]] = None
# Gradient clip norm. None inherits actor.optim.clip_grad.
clip_grad: Optional[float] = None
# Warmup steps ratio. None inherits actor.optim.lr_warmup_steps_ratio.
lr_warmup_steps_ratio: Optional[float] = None
# Warmup steps. None inherits actor.optim.lr_warmup_steps.
lr_warmup_steps: Optional[int] = None
# Scheduler type ("constant" or "cosine"). None inherits actor.optim.lr_scheduler_type.
lr_scheduler_type: Optional[str] = None
# Minimum LR ratio for cosine schedule. None inherits actor.optim.min_lr_ratio.
min_lr_ratio: Optional[float] = None
# Number of cosine cycles. None inherits actor.optim.num_cycles.
num_cycles: Optional[float] = None
# Extra kwargs forwarded to the optimizer constructor. None inherits actor.optim.override_optimizer_config.
override_optimizer_config: Optional[dict] = None
@dataclass
class DiffusionModelARConfig(BaseConfig):
# LoRA configs reuse diffusion model's
override_config: dict[str, Any] = field(default_factory=dict)
# Partial optimizer overlay. Unset fields inherit actor.optim.
optim: DiffusionModelAROptimizerConfig = field(default_factory=DiffusionModelAROptimizerConfig)
[docs]
@dataclass
class DiffusionModelConfig(BaseConfig):
_mutable_fields = {
"model_type",
"algorithm",
"mtp",
"tokenizer_path",
"tokenizer",
"processor",
"local_path",
"local_tokenizer_path",
"architecture",
"transformer_config",
"extra_tokenizer_map",
"hf_config",
}
path: str = MISSING
architecture: Optional[str] = None
# diffusers ``transformer/config.json``; used by DiffusionFlopsCounter for MFU.
transformer_config: Optional[dict[str, Any]] = None
algorithm: str = MISSING
local_path: Optional[str] = None
tokenizer_path: Optional[str] = None
local_tokenizer_path: Optional[str] = None
hf_config: Any = None
# model type, e.g., "diffusion_model"
model_type: str = "diffusion_model"
# whether to load tokenizer. This is useful when we only want to load model config
load_tokenizer: bool = True
tokenizer: Any = None
processor: Any = None
# Optional per-text-encoder tokenizers for models with multiple text encoders
extra_tokenizers: Optional[dict[str, Any]] = None
# Loaded tokenizers built from ``extra_tokenizers``:
# ``{name: {"tokenizer": PreTrainedTokenizer, "max_length": int | None}}``.
extra_tokenizer_map: Any = None
# whether to use shared memory
use_shm: bool = False
trust_remote_code: bool = False
# custom chat template for the model
custom_chat_template: Optional[str] = None
# AR use only
use_remove_padding: bool = False
external_lib: Optional[str] = None
enable_gradient_checkpointing: bool = True
attn_backend: str = "_flash_3_varlen_hub"
# Compile repeated diffusion transformer blocks before FSDP2 sharding.
use_regional_compile: bool = False
regional_compile_options: dict[str, Any] = field(
default_factory=lambda: {
"backend": "inductor",
"mode": "default",
"fullgraph": False,
"dynamic": True,
}
)
lora_rank: int = 0
lora_alpha: int = 64
lora_init_weights: str = "gaussian"
target_modules: Optional[Any] = "all-linear" # allow both "all-linear" and ["q_proj","k_proj"]
target_parameters: Optional[list[str]] = None # for lora adapter on nn.Parameter
exclude_modules: Optional[str] = None
# megatron lora config
lora: dict[str, Any] = field(default_factory=dict)
# path to pre-trained LoRA adapter to load for continued training
lora_adapter_path: Optional[str] = None
# Named LoRA policy states required by the algorithm. "reference" uses disabled adapters.
policy_state_adapters: tuple[str, ...] = ("default",)
# dtype to convert LoRA parameters to (e.g., "fp32", "bf16"). Default None means no conversion.
lora_dtype: Optional[str] = None
mtp: Optional[MtpConfig] = field(default_factory=MtpConfig)
pipeline: DiffusionPipelineConfig = field(default_factory=DiffusionPipelineConfig)
algo: Optional[DiffusionRolloutAlgoConfig] = field(default_factory=DiffusionRolloutAlgoConfig)
fsdp_layer_prefixes: list[str] = field(default_factory=lambda: ["transformer_blocks."])
# Optional model config path. If unset, the backend uses
# ``<local_path>/<transformer_subfolder>``.
config_path: Optional[str] = None
# Subfolder containing the diffusion text encoder weights/config.
text_encoder_subfolder: str = "text_encoder"
# Subfolder containing the diffusion transformer weights/config.
transformer_subfolder: str = "transformer"
# Trainable AR config
ar: DiffusionModelARConfig = field(default_factory=DiffusionModelARConfig)
def __post_init__(self):
import_external_libs(self.external_lib)
valid_backends = {"native", "_native_npu", "flash_varlen_hub", "_flash_3_varlen_hub"}
if self.attn_backend not in valid_backends:
raise ValueError(f"Invalid attn_backend: {self.attn_backend}. Must be one of {sorted(valid_backends)}")
if self.attn_backend in ["flash_varlen_hub", "_flash_3_varlen_hub"]:
try:
import kernels # noqa: F401
except ImportError as e:
raise ImportError(f"attn_backend '{self.attn_backend}' requires `kernels` package. ") from e
self.local_path = resolve_model_local_dir(self.path, use_shm=self.use_shm)
if self.tokenizer_path is None:
tokenizer_path = os.path.join(self.local_path, "tokenizer")
self.tokenizer_path = tokenizer_path if os.path.exists(tokenizer_path) else self.local_path
if self.architecture is None:
model_index_path = os.path.join(self.local_path, "model_index.json")
with open(model_index_path) as f:
self.architecture = json.load(f)["_class_name"]
if self.transformer_config is None:
config_path = os.path.join(self.local_path, "transformer", "config.json")
if os.path.isfile(config_path):
try:
with open(config_path) as f:
self.transformer_config = json.load(f)
except (OSError, json.JSONDecodeError) as exc:
logger.warning("Diffusion MFU disabled: failed to read %s: %s", config_path, exc)
else:
logger.warning(
"Diffusion MFU disabled: transformer config not found at %s. "
"Expected the diffusers pipeline layout `<local_path>/transformer/config.json`.",
config_path,
)
# construct tokenizer
if self.load_tokenizer:
self.local_tokenizer_path = copy_to_local(self.tokenizer_path, use_shm=self.use_shm)
self.tokenizer = hf_tokenizer(
self.local_tokenizer_path, trust_remote_code=self.trust_remote_code, use_fast=True
)
processor_path = os.path.join(self.local_path, "processor")
if os.path.exists(processor_path):
self.processor = hf_processor(processor_path, trust_remote_code=self.trust_remote_code)
else:
self.processor = None
self.extra_tokenizer_map = {}
for name, spec in (self.extra_tokenizers or {}).items():
path = spec.get("path") if hasattr(spec, "get") else None
if not path:
raise ValueError(f"extra_tokenizers[{name!r}] must define a 'path' entry.")
if not os.path.isabs(path):
path = os.path.join(self.local_path, path)
local_path = copy_to_local(path, use_shm=self.use_shm)
tokenizer = hf_tokenizer(local_path, trust_remote_code=self.trust_remote_code, use_fast=True)
max_length = spec.get("max_length")
self.extra_tokenizer_map[name] = {
"tokenizer": tokenizer,
"max_length": int(max_length) if max_length is not None else None,
}
# Ensure target_modules is a str or list[str] (only if not None)
if self.target_modules is not None:
if not isinstance(self.target_modules, (str | list)):
raise TypeError(
"target_modules must be a string or a list of strings, "
f"but got {type(self.target_modules).__name__}"
)
if isinstance(self.target_modules, list):
for x in self.target_modules:
if not isinstance(x, str):
raise TypeError(
f"All elements in target_modules list must be strings, but found {type(x).__name__}"
)
if self.lora_rank > 0:
# Validate LoRA targets via the model adapter's hook so this generic config
# stays architecture-agnostic (non-fatal lookup; unregistered architectures
# fall back to the default no-op).
from verl_omni.pipelines.model_base import DiffusionModelBase
adapter = DiffusionModelBase.peek_class(self.architecture, self.algorithm)
if adapter is not None:
adapter.validate_lora_config(self)
def get_processor(self):
return self.processor if self.processor is not None else self.tokenizer