Skip to content

Commit 8f8ef4c

Browse files
committed
update
1 parent cdaa269 commit 8f8ef4c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# coding=utf-8
2+
# Copyright 2024 HuggingFace Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import gc
17+
import unittest
18+
19+
import torch
20+
21+
from diffusers import (
22+
AutoencoderDC,
23+
)
24+
from diffusers.utils.testing_utils import (
25+
backend_empty_cache,
26+
enable_full_determinism,
27+
load_hf_numpy,
28+
numpy_cosine_similarity_distance,
29+
require_torch_accelerator,
30+
slow,
31+
torch_device,
32+
)
33+
34+
35+
enable_full_determinism()
36+
37+
38+
@slow
39+
@require_torch_accelerator
40+
class AutoencoderDCSingleFileTests(unittest.TestCase):
41+
model_class = AutoencoderDC
42+
ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f32c32-sana-1.0/blob/main/model.safetensors"
43+
repo_id = "mit-han-lab/dc-ae-f32c32-sana-1.0-diffusers"
44+
main_input_name = "sample"
45+
base_precision = 1e-2
46+
47+
def setUp(self):
48+
super().setUp()
49+
gc.collect()
50+
backend_empty_cache(torch_device)
51+
52+
def tearDown(self):
53+
super().tearDown()
54+
gc.collect()
55+
backend_empty_cache(torch_device)
56+
57+
def get_file_format(self, seed, shape):
58+
return f"gaussian_noise_s={seed}_shape={'_'.join([str(s) for s in shape])}.npy"
59+
60+
def get_sd_image(self, seed=0, shape=(4, 3, 512, 512), fp16=False):
61+
dtype = torch.float16 if fp16 else torch.float32
62+
image = torch.from_numpy(load_hf_numpy(self.get_file_format(seed, shape))).to(torch_device).to(dtype)
63+
return image
64+
65+
def test_single_file_inference_same_as_pretrained(self):
66+
model_1 = self.model_class.from_pretrained(self.repo_id).to(torch_device)
67+
model_2 = self.model_class.from_single_file(self.ckpt_path, config=self.repo_id).to(torch_device)
68+
69+
image = self.get_sd_image(33)
70+
71+
with torch.no_grad():
72+
sample_1 = model_1(image).sample
73+
sample_2 = model_2(image).sample
74+
75+
assert sample_1.shape == sample_2.shape
76+
77+
output_slice_1 = sample_1.flatten().float().cpu()
78+
output_slice_2 = sample_2.flatten().float().cpu()
79+
80+
assert numpy_cosine_similarity_distance(output_slice_1, output_slice_2) < 1e-4
81+
82+
def test_single_file_components(self):
83+
model = self.model_class.from_pretrained(self.repo_id)
84+
model_single_file = self.model_class.from_single_file(self.ckpt_path)
85+
86+
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"]
87+
for param_name, param_value in model_single_file.config.items():
88+
if param_name in PARAMS_TO_IGNORE:
89+
continue
90+
assert (
91+
model.config[param_name] == param_value
92+
), f"{param_name} differs between pretrained loading and single file loading"
93+
94+
def test_single_file_in_type_variant_components(self):
95+
# `in` variant checkpoints require passing in a `config` parameter
96+
# in order to set the scaling factor correctly.
97+
# `in` and `mix` variants have the same keys and we cannot automatically infer a scaling factor.
98+
# We default to using teh `mix` config
99+
repo_id = "mit-han-lab/dc-ae-f128c512-in-1.0-diffusers"
100+
ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f128c512-in-1.0/blob/main/model.safetensors"
101+
102+
model = self.model_class.from_pretrained(repo_id)
103+
model_single_file = self.model_class.from_single_file(ckpt_path, config=repo_id)
104+
105+
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"]
106+
for param_name, param_value in model_single_file.config.items():
107+
if param_name in PARAMS_TO_IGNORE:
108+
continue
109+
assert (
110+
model.config[param_name] == param_value
111+
), f"{param_name} differs between pretrained loading and single file loading"
112+
113+
def test_single_file_mix_type_variant_components(self):
114+
repo_id = "mit-han-lab/dc-ae-f128c512-mix-1.0-diffusers"
115+
ckpt_path = "https://huggingface.co/mit-han-lab/dc-ae-f128c512-mix-1.0/blob/main/model.safetensors"
116+
117+
model = self.model_class.from_pretrained(repo_id)
118+
model_single_file = self.model_class.from_single_file(ckpt_path, config=repo_id)
119+
120+
PARAMS_TO_IGNORE = ["torch_dtype", "_name_or_path", "_use_default_values", "_diffusers_version"]
121+
for param_name, param_value in model_single_file.config.items():
122+
if param_name in PARAMS_TO_IGNORE:
123+
continue
124+
assert (
125+
model.config[param_name] == param_value
126+
), f"{param_name} differs between pretrained loading and single file loading"

0 commit comments

Comments
 (0)