| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import gc |
| import unittest |
|
|
| import torch |
|
|
| from diffusers import ( |
| AutoencoderDC, |
| ) |
| from diffusers.utils.testing_utils import ( |
| backend_empty_cache, |
| enable_full_determinism, |
| load_hf_numpy, |
| numpy_cosine_similarity_distance, |
| require_torch_accelerator, |
| slow, |
| torch_device, |
| ) |
|
|
|
|
| enable_full_determinism() |
|
|
|
|
| @slow |
| @require_torch_accelerator |
| class AutoencoderDCSingleFileTests(unittest.TestCase): |
| model_class = AutoencoderDC |
| ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f32c32-sana-1.0/blob/main/model.safetensors" |
| repo_id = "mit-han-lab/dc-ae-f32c32-sana-1.0-diffusers" |
| main_input_name = "sample" |
| base_precision = 1e-2 |
|
|
| def setUp(self): |
| super().setUp() |
| gc.collect() |
| backend_empty_cache(torch_device) |
|
|
| def tearDown(self): |
| super().tearDown() |
| gc.collect() |
| backend_empty_cache(torch_device) |
|
|
| def get_file_format(self, seed, shape): |
| return f"gaussian_noise_s={seed}_shape={'_'.join([str(s) for s in shape])}.npy" |
|
|
| def get_sd_image(self, seed=0, shape=(4, 3, 512, 512), fp16=False): |
| dtype = torch.float16 if fp16 else torch.float32 |
| image = torch.from_numpy(load_hf_numpy(self.get_file_format(seed, shape))).to(torch_device).to(dtype) |
| return image |
|
|
| def test_single_file_inference_same_as_pretrained(self): |
| model_1 = self.model_class.from_pretrained(self.repo_id).to(torch_device) |
| model_2 = self.model_class.from_single_file(self.ckpt_path, config=self.repo_id).to(torch_device) |
|
|
| image = self.get_sd_image(33) |
|
|
| with torch.no_grad(): |
| sample_1 = model_1(image).sample |
| sample_2 = model_2(image).sample |
|
|
| assert sample_1.shape == sample_2.shape |
|
|
| output_slice_1 = sample_1.flatten().float().cpu() |
| output_slice_2 = sample_2.flatten().float().cpu() |
|
|
| assert numpy_cosine_similarity_distance(output_slice_1, output_slice_2) < 1e-4 |
|
|
| def test_single_file_components(self): |
| model = self.model_class.from_pretrained(self.repo_id) |
| model_single_file = self.model_class.from_single_file(self.ckpt_path) |
|
|
| PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] |
| for param_name, param_value in model_single_file.config.items(): |
| if param_name in PARAMS_TO_IGNORE: |
| continue |
| assert model.config[param_name] == param_value, ( |
| f"{param_name} differs between pretrained loading and single file loading" |
| ) |
|
|
| def test_single_file_in_type_variant_components(self): |
| |
| |
| |
| |
| repo_id = "mit-han-lab/dc-ae-f128c512-in-1.0-diffusers" |
| ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f128c512-in-1.0/blob/main/model.safetensors" |
|
|
| model = self.model_class.from_pretrained(repo_id) |
| model_single_file = self.model_class.from_single_file(ckpt_path, config=repo_id) |
|
|
| PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] |
| for param_name, param_value in model_single_file.config.items(): |
| if param_name in PARAMS_TO_IGNORE: |
| continue |
| assert model.config[param_name] == param_value, ( |
| f"{param_name} differs between pretrained loading and single file loading" |
| ) |
|
|
| def test_single_file_mix_type_variant_components(self): |
| repo_id = "mit-han-lab/dc-ae-f128c512-mix-1.0-diffusers" |
| ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f128c512-mix-1.0/blob/main/model.safetensors" |
|
|
| model = self.model_class.from_pretrained(repo_id) |
| model_single_file = self.model_class.from_single_file(ckpt_path, config=repo_id) |
|
|
| PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"] |
| for param_name, param_value in model_single_file.config.items(): |
| if param_name in PARAMS_TO_IGNORE: |
| continue |
| assert model.config[param_name] == param_value, ( |
| f"{param_name} differs between pretrained loading and single file loading" |
| ) |
|
|