|
| 1 | +import tempfile |
1 | 2 | import unittest |
2 | 3 |
|
| 4 | +import numpy as np |
3 | 5 | import torch |
4 | 6 |
|
| 7 | +from diffusers import DiffusionPipeline |
5 | 8 | from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor |
6 | 9 |
|
7 | 10 |
|
@@ -77,42 +80,41 @@ def test_only_cross_attention(self): |
77 | 80 |
|
78 | 81 | class DeprecatedAttentionBlockTests(unittest.TestCase): |
79 | 82 | def test_conversion_when_using_device_map(self): |
80 | | - # To-DO for Sayak: enable this test again and to test `device_map='balanced'` once we have this in accelerate https://github.com/huggingface/accelerate/pull/2641 |
81 | | - pass |
82 | | - # pipe = DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe", safety_checker=None) |
83 | | - |
84 | | - # pre_conversion = pipe( |
85 | | - # "foo", |
86 | | - # num_inference_steps=2, |
87 | | - # generator=torch.Generator("cpu").manual_seed(0), |
88 | | - # output_type="np", |
89 | | - # ).images |
90 | | - |
91 | | - # # the initial conversion succeeds |
92 | | - # pipe = DiffusionPipeline.from_pretrained( |
93 | | - # "hf-internal-testing/tiny-stable-diffusion-pipe", device_map="sequential", safety_checker=None |
94 | | - # ) |
95 | | - |
96 | | - # conversion = pipe( |
97 | | - # "foo", |
98 | | - # num_inference_steps=2, |
99 | | - # generator=torch.Generator("cpu").manual_seed(0), |
100 | | - # output_type="np", |
101 | | - # ).images |
102 | | - |
103 | | - # with tempfile.TemporaryDirectory() as tmpdir: |
104 | | - # # save the converted model |
105 | | - # pipe.save_pretrained(tmpdir) |
106 | | - |
107 | | - # # can also load the converted weights |
108 | | - # pipe = DiffusionPipeline.from_pretrained(tmpdir, device_map="sequential", safety_checker=None) |
109 | | - |
110 | | - # after_conversion = pipe( |
111 | | - # "foo", |
112 | | - # num_inference_steps=2, |
113 | | - # generator=torch.Generator("cpu").manual_seed(0), |
114 | | - # output_type="np", |
115 | | - # ).images |
116 | | - |
117 | | - # self.assertTrue(np.allclose(pre_conversion, conversion, atol=1e-5)) |
118 | | - # self.assertTrue(np.allclose(conversion, after_conversion, atol=1e-5)) |
| 83 | + pipe = DiffusionPipeline.from_pretrained( |
| 84 | + "hf-internal-testing/tiny-stable-diffusion-torch", safety_checker=None |
| 85 | + ) |
| 86 | + |
| 87 | + pre_conversion = pipe( |
| 88 | + "foo", |
| 89 | + num_inference_steps=2, |
| 90 | + generator=torch.Generator("cpu").manual_seed(0), |
| 91 | + output_type="np", |
| 92 | + ).images |
| 93 | + |
| 94 | + # the initial conversion succeeds |
| 95 | + pipe = DiffusionPipeline.from_pretrained( |
| 96 | + "hf-internal-testing/tiny-stable-diffusion-torch", device_map="balanced", safety_checker=None |
| 97 | + ) |
| 98 | + |
| 99 | + conversion = pipe( |
| 100 | + "foo", |
| 101 | + num_inference_steps=2, |
| 102 | + generator=torch.Generator("cpu").manual_seed(0), |
| 103 | + output_type="np", |
| 104 | + ).images |
| 105 | + |
| 106 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 107 | + # save the converted model |
| 108 | + pipe.save_pretrained(tmpdir) |
| 109 | + |
| 110 | + # can also load the converted weights |
| 111 | + pipe = DiffusionPipeline.from_pretrained(tmpdir, device_map="balanced", safety_checker=None) |
| 112 | + after_conversion = pipe( |
| 113 | + "foo", |
| 114 | + num_inference_steps=2, |
| 115 | + generator=torch.Generator("cpu").manual_seed(0), |
| 116 | + output_type="np", |
| 117 | + ).images |
| 118 | + |
| 119 | + self.assertTrue(np.allclose(pre_conversion, conversion, atol=1e-3)) |
| 120 | + self.assertTrue(np.allclose(conversion, after_conversion, atol=1e-3)) |
0 commit comments