|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +from types import SimpleNamespace |
| 5 | + |
| 6 | +import diffusers |
| 7 | +import torch |
| 8 | +from safetensors.torch import save_file |
| 9 | + |
| 10 | +from vllm_omni.diffusion.data import OmniDiffusionConfig |
| 11 | + |
| 12 | + |
| 13 | +def test_anima_registration(): |
| 14 | + # Verify that Anima modules were dynamically injected into the diffusers package |
| 15 | + import vllm_omni.diffusion.models.anima # noqa: F401 |
| 16 | + from vllm_omni.diffusion.registry import DiffusionModelRegistry |
| 17 | + |
| 18 | + assert hasattr(diffusers, "AnimaModularPipeline") |
| 19 | + assert hasattr(diffusers.modular_pipelines, "AnimaModularPipeline") |
| 20 | + assert hasattr(diffusers.models, "AnimaTextConditioner") |
| 21 | + assert hasattr(diffusers.models.condition_embedders, "AnimaTextConditioner") |
| 22 | + assert DiffusionModelRegistry._try_load_model_cls("AnimaPipeline") is not None |
| 23 | + |
| 24 | + |
| 25 | +def test_enrich_config_single_file(tmp_path): |
| 26 | + # Verify single-file config enrichment path |
| 27 | + dummy_checkpoint = tmp_path / "model.safetensors" |
| 28 | + dummy_checkpoint.write_text("dummy") |
| 29 | + |
| 30 | + config = OmniDiffusionConfig( |
| 31 | + model=str(dummy_checkpoint), |
| 32 | + diffusion_load_format="diffusers_single_file", |
| 33 | + model_class_name="AnimaModularPipeline", |
| 34 | + ) |
| 35 | + config.enrich_config() |
| 36 | + |
| 37 | + assert config.model_class_name == "DiffusersAdapterPipeline" |
| 38 | + assert config.diffusers_pipeline_cls is diffusers.AnimaModularPipeline |
| 39 | + |
| 40 | + |
| 41 | +def test_enrich_config_single_file_autodetects_local_file(tmp_path): |
| 42 | + dummy_checkpoint = tmp_path / "model.safetensors" |
| 43 | + dummy_checkpoint.write_text("dummy") |
| 44 | + |
| 45 | + config = OmniDiffusionConfig( |
| 46 | + model=str(dummy_checkpoint), |
| 47 | + model_class_name="AnimaModularPipeline", |
| 48 | + ) |
| 49 | + config.enrich_config() |
| 50 | + |
| 51 | + assert config.diffusion_load_format == "diffusers_single_file" |
| 52 | + assert config.model_class_name == "DiffusersAdapterPipeline" |
| 53 | + assert config.diffusers_pipeline_cls is diffusers.AnimaModularPipeline |
| 54 | + |
| 55 | + |
| 56 | +def test_enrich_config_native_anima_single_file_stays_native(tmp_path): |
| 57 | + dummy_checkpoint = tmp_path / "model.safetensors" |
| 58 | + dummy_checkpoint.write_text("dummy") |
| 59 | + |
| 60 | + config = OmniDiffusionConfig( |
| 61 | + model=str(dummy_checkpoint), |
| 62 | + model_class_name="AnimaPipeline", |
| 63 | + ) |
| 64 | + config.enrich_config() |
| 65 | + |
| 66 | + assert config.diffusion_load_format == "default" |
| 67 | + assert config.model_class_name == "AnimaPipeline" |
| 68 | + assert config.diffusers_pipeline_cls is None |
| 69 | + |
| 70 | + |
| 71 | +def test_native_anima_single_file_allows_load_kwargs(tmp_path): |
| 72 | + dummy_checkpoint = tmp_path / "model.safetensors" |
| 73 | + dummy_checkpoint.write_text("dummy") |
| 74 | + |
| 75 | + config = OmniDiffusionConfig( |
| 76 | + model=str(dummy_checkpoint), |
| 77 | + model_class_name="AnimaPipeline", |
| 78 | + diffusers_load_kwargs={"local_files_only": True}, |
| 79 | + ) |
| 80 | + config.enrich_config() |
| 81 | + |
| 82 | + assert config.diffusion_load_format == "default" |
| 83 | + assert config.diffusers_load_kwargs == {"local_files_only": True} |
| 84 | + |
| 85 | + |
| 86 | +def test_native_anima_converts_original_cosmos_transformer_keys(): |
| 87 | + from vllm_omni.diffusion.models.anima.pipeline_anima import AnimaPipeline |
| 88 | + |
| 89 | + converted = AnimaPipeline._convert_cosmos_2_transformer_state_dict( |
| 90 | + { |
| 91 | + "net.x_embedder.proj.1.weight": "patch", |
| 92 | + "net.blocks.0.self_attn.q_proj.weight": "q", |
| 93 | + "net.blocks.0.self_attn.q_norm.weight": "q_norm", |
| 94 | + "net.blocks.0.mlp.layer1.weight": "mlp", |
| 95 | + "net.final_layer.linear.weight": "out", |
| 96 | + "net.accum_iteration": "drop", |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + assert converted == { |
| 101 | + "patch_embed.proj.weight": "patch", |
| 102 | + "transformer_blocks.0.attn1.to_q.weight": "q", |
| 103 | + "transformer_blocks.0.attn1.norm_q.weight": "q_norm", |
| 104 | + "transformer_blocks.0.ff.net.0.proj.weight": "mlp", |
| 105 | + "proj_out.weight": "out", |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +def test_native_anima_loads_synthetic_single_file(tmp_path, monkeypatch): |
| 110 | + import vllm_omni.diffusion.models.anima.pipeline_anima as pipeline_anima |
| 111 | + from vllm_omni.diffusion.models.anima.native_cosmos_transformer import NativeCosmosTransformer3DModel |
| 112 | + from vllm_omni.diffusion.models.anima.native_text_conditioner import NativeAnimaTextConditioner |
| 113 | + |
| 114 | + tiny_transformer_config = { |
| 115 | + "in_channels": 1, |
| 116 | + "out_channels": 1, |
| 117 | + "num_attention_heads": 1, |
| 118 | + "attention_head_dim": 12, |
| 119 | + "num_layers": 1, |
| 120 | + "mlp_ratio": 1.0, |
| 121 | + "text_embed_dim": 4, |
| 122 | + "adaln_lora_dim": 3, |
| 123 | + "max_size": (1, 2, 2), |
| 124 | + "patch_size": (1, 1, 1), |
| 125 | + "rope_scale": (1.0, 1.0, 1.0), |
| 126 | + "concat_padding_mask": True, |
| 127 | + "extra_pos_embed_type": None, |
| 128 | + } |
| 129 | + monkeypatch.setattr(pipeline_anima, "_ANIMA_TRANSFORMER_CONFIG", tiny_transformer_config) |
| 130 | + |
| 131 | + transformer = NativeCosmosTransformer3DModel(**tiny_transformer_config) |
| 132 | + text_conditioner = NativeAnimaTextConditioner( |
| 133 | + source_dim=4, |
| 134 | + target_dim=4, |
| 135 | + model_dim=4, |
| 136 | + num_layers=1, |
| 137 | + num_attention_heads=1, |
| 138 | + target_vocab_size=8, |
| 139 | + min_sequence_length=4, |
| 140 | + ) |
| 141 | + transformer_state = {name: tensor.detach().clone() for name, tensor in transformer.state_dict().items()} |
| 142 | + text_conditioner_state = {name: tensor.detach().clone() for name, tensor in text_conditioner.state_dict().items()} |
| 143 | + checkpoint_state = { |
| 144 | + **{f"transformer.{name}": tensor for name, tensor in transformer_state.items()}, |
| 145 | + **{f"text_conditioner.{name}": tensor for name, tensor in text_conditioner_state.items()}, |
| 146 | + } |
| 147 | + |
| 148 | + checkpoint_path = tmp_path / "anima.safetensors" |
| 149 | + save_file(checkpoint_state, str(checkpoint_path)) |
| 150 | + |
| 151 | + pipeline = pipeline_anima.AnimaPipeline.__new__(pipeline_anima.AnimaPipeline) |
| 152 | + pipeline.od_config = SimpleNamespace(model=str(checkpoint_path), dtype=torch.float32) |
| 153 | + pipeline.device = torch.device("cpu") |
| 154 | + |
| 155 | + def assert_loaded(loaded_transformer, loaded_text_conditioner): |
| 156 | + for name, tensor in transformer_state.items(): |
| 157 | + assert torch.equal(loaded_transformer.state_dict()[name], tensor) |
| 158 | + for name, tensor in text_conditioner_state.items(): |
| 159 | + assert torch.equal(loaded_text_conditioner.state_dict()[name], tensor) |
| 160 | + |
| 161 | + loaded_transformer, loaded_text_conditioner = pipeline._load_native_denoiser_components(dict(checkpoint_state)) |
| 162 | + assert_loaded(loaded_transformer, loaded_text_conditioner) |
| 163 | + |
| 164 | + loaded_transformer, loaded_text_conditioner = pipeline._load_native_denoiser_components() |
| 165 | + assert_loaded(loaded_transformer, loaded_text_conditioner) |
| 166 | + |
| 167 | + |
| 168 | +def test_enrich_config_single_file_rejects_unknown_pipeline(tmp_path): |
| 169 | + dummy_checkpoint = tmp_path / "model.safetensors" |
| 170 | + dummy_checkpoint.write_text("dummy") |
| 171 | + |
| 172 | + config = OmniDiffusionConfig( |
| 173 | + model=str(dummy_checkpoint), |
| 174 | + diffusion_load_format="diffusers_single_file", |
| 175 | + model_class_name="MissingPipeline", |
| 176 | + ) |
| 177 | + try: |
| 178 | + config.enrich_config() |
| 179 | + except ValueError as exc: |
| 180 | + assert "Could not find diffusers pipeline class MissingPipeline" in str(exc) |
| 181 | + else: |
| 182 | + raise AssertionError("Expected unknown single-file pipeline class to fail.") |
0 commit comments