|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import inspect
|
| 16 | +import json |
| 17 | +import os |
| 18 | +import tempfile |
16 | 19 | import unittest
|
17 | 20 |
|
18 | 21 | import numpy as np
|
|
24 | 27 |
|
25 | 28 | from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
|
26 | 29 | from ..test_pipelines_common import PipelineTesterMixin, to_np
|
| 30 | +from .cosmos_guardrail import DummyCosmosSafetyChecker |
27 | 31 |
|
28 | 32 |
|
29 | 33 | enable_full_determinism()
|
30 | 34 |
|
31 | 35 |
|
| 36 | +class CosmosPipelineWrapper(CosmosPipeline): |
| 37 | + @staticmethod |
| 38 | + def from_pretrained(*args, **kwargs): |
| 39 | + kwargs["safety_checker"] = DummyCosmosSafetyChecker() |
| 40 | + return CosmosPipeline.from_pretrained(*args, **kwargs) |
| 41 | + |
| 42 | + |
32 | 43 | class CosmosPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
|
33 |
| - pipeline_class = CosmosPipeline |
| 44 | + pipeline_class = CosmosPipelineWrapper |
34 | 45 | params = TEXT_TO_IMAGE_PARAMS - {"cross_attention_kwargs"}
|
35 | 46 | batch_params = TEXT_TO_IMAGE_BATCH_PARAMS
|
36 | 47 | image_params = TEXT_TO_IMAGE_IMAGE_PARAMS
|
@@ -106,8 +117,7 @@ def get_dummy_components(self):
|
106 | 117 | "text_encoder": text_encoder,
|
107 | 118 | "tokenizer": tokenizer,
|
108 | 119 | # We cannot run the Cosmos Guardrail for fast tests due to the large model size
|
109 |
| - "safety_checker": None, |
110 |
| - "requires_safety_checker": False, |
| 120 | + "safety_checker": DummyCosmosSafetyChecker(), |
111 | 121 | }
|
112 | 122 | return components
|
113 | 123 |
|
@@ -149,13 +159,6 @@ def test_inference(self):
|
149 | 159 | max_diff = np.abs(generated_video - expected_video).max()
|
150 | 160 | self.assertLessEqual(max_diff, 1e10)
|
151 | 161 |
|
152 |
| - def test_components_function(self): |
153 |
| - init_components = self.get_dummy_components() |
154 |
| - init_components = {k: v for k, v in init_components.items() if not isinstance(v, (str, int, float))} |
155 |
| - pipe = self.pipeline_class(**init_components, requires_safety_checker=False) |
156 |
| - self.assertTrue(hasattr(pipe, "components")) |
157 |
| - self.assertTrue(set(pipe.components.keys()) == set(init_components.keys())) |
158 |
| - |
159 | 162 | def test_callback_inputs(self):
|
160 | 163 | sig = inspect.signature(self.pipeline_class.__call__)
|
161 | 164 | has_callback_tensor_inputs = "callback_on_step_end_tensor_inputs" in sig.parameters
|
@@ -216,7 +219,7 @@ def callback_inputs_change_tensor(pipe, i, t, callback_kwargs):
|
216 | 219 | assert output.abs().sum() < 1e10
|
217 | 220 |
|
218 | 221 | def test_inference_batch_single_identical(self):
|
219 |
| - self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-3) |
| 222 | + self._test_inference_batch_single_identical(batch_size=3, expected_max_diff=1e-2) |
220 | 223 |
|
221 | 224 | def test_attention_slicing_forward_pass(
|
222 | 225 | self, test_max_difference=True, test_mean_pixel_difference=True, expected_max_diff=1e-3
|
@@ -282,3 +285,61 @@ def test_vae_tiling(self, expected_diff_max: float = 0.2):
|
282 | 285 | expected_diff_max,
|
283 | 286 | "VAE tiling should not affect the inference results",
|
284 | 287 | )
|
| 288 | + |
| 289 | + def test_serialization_with_variants(self): |
| 290 | + components = self.get_dummy_components() |
| 291 | + pipe = self.pipeline_class(**components) |
| 292 | + model_components = [ |
| 293 | + component_name |
| 294 | + for component_name, component in pipe.components.items() |
| 295 | + if isinstance(component, torch.nn.Module) |
| 296 | + ] |
| 297 | + model_components.remove("safety_checker") |
| 298 | + variant = "fp16" |
| 299 | + |
| 300 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 301 | + pipe.save_pretrained(tmpdir, variant=variant, safe_serialization=False) |
| 302 | + |
| 303 | + with open(f"{tmpdir}/model_index.json", "r") as f: |
| 304 | + config = json.load(f) |
| 305 | + |
| 306 | + for subfolder in os.listdir(tmpdir): |
| 307 | + if not os.path.isfile(subfolder) and subfolder in model_components: |
| 308 | + folder_path = os.path.join(tmpdir, subfolder) |
| 309 | + is_folder = os.path.isdir(folder_path) and subfolder in config |
| 310 | + assert is_folder and any(p.split(".")[1].startswith(variant) for p in os.listdir(folder_path)) |
| 311 | + |
| 312 | + def test_torch_dtype_dict(self): |
| 313 | + components = self.get_dummy_components() |
| 314 | + if not components: |
| 315 | + self.skipTest("No dummy components defined.") |
| 316 | + |
| 317 | + pipe = self.pipeline_class(**components) |
| 318 | + |
| 319 | + specified_key = next(iter(components.keys())) |
| 320 | + |
| 321 | + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdirname: |
| 322 | + pipe.save_pretrained(tmpdirname, safe_serialization=False) |
| 323 | + torch_dtype_dict = {specified_key: torch.bfloat16, "default": torch.float16} |
| 324 | + loaded_pipe = self.pipeline_class.from_pretrained( |
| 325 | + tmpdirname, safety_checker=DummyCosmosSafetyChecker(), torch_dtype=torch_dtype_dict |
| 326 | + ) |
| 327 | + |
| 328 | + for name, component in loaded_pipe.components.items(): |
| 329 | + if name == "safety_checker": |
| 330 | + continue |
| 331 | + if isinstance(component, torch.nn.Module) and hasattr(component, "dtype"): |
| 332 | + expected_dtype = torch_dtype_dict.get(name, torch_dtype_dict.get("default", torch.float32)) |
| 333 | + self.assertEqual( |
| 334 | + component.dtype, |
| 335 | + expected_dtype, |
| 336 | + f"Component '{name}' has dtype {component.dtype} but expected {expected_dtype}", |
| 337 | + ) |
| 338 | + |
| 339 | + @unittest.skip( |
| 340 | + "The pipeline should not be runnable without a safety checker. The test creates a pipeline without passing in " |
| 341 | + "a safety checker, which makes the pipeline default to the actual Cosmos Guardrail. The Cosmos Guardrail is " |
| 342 | + "too large and slow to run on CI." |
| 343 | + ) |
| 344 | + def test_encode_prompt_works_in_isolation(self): |
| 345 | + pass |
0 commit comments