|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import contextlib |
15 | 16 | import gc |
16 | 17 | import unittest |
17 | 18 |
|
|
20 | 21 | from diffusers.models import ModelMixin |
21 | 22 | from diffusers.pipelines.pipeline_utils import DiffusionPipeline |
22 | 23 | from diffusers.utils import get_logger |
| 24 | +from diffusers.utils.import_utils import compare_versions |
23 | 25 | from diffusers.utils.testing_utils import require_torch_gpu, torch_device |
24 | 26 |
|
25 | 27 |
|
@@ -58,6 +60,39 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: |
58 | 60 | return x |
59 | 61 |
|
60 | 62 |
|
| 63 | +# This model implementation contains one type of block (single_blocks) instantiated before another type of block (double_blocks). |
| 64 | +# The invocation order of these blocks, however, is first the double_blocks and then the single_blocks. |
| 65 | +# With group offloading implementation before https://github.com/huggingface/diffusers/pull/11375, such a modeling implementation |
| 66 | +# would result in a device mismatch error because of the assumptions made by the code. The failure case occurs when using: |
| 67 | +# offload_type="block_level", num_blocks_per_group=2, use_stream=True |
| 68 | +# Post the linked PR, the implementation will work as expected. |
| 69 | +class DummyModelWithMultipleBlocks(ModelMixin): |
| 70 | + def __init__( |
| 71 | + self, in_features: int, hidden_features: int, out_features: int, num_layers: int, num_single_layers: int |
| 72 | + ) -> None: |
| 73 | + super().__init__() |
| 74 | + |
| 75 | + self.linear_1 = torch.nn.Linear(in_features, hidden_features) |
| 76 | + self.activation = torch.nn.ReLU() |
| 77 | + self.single_blocks = torch.nn.ModuleList( |
| 78 | + [DummyBlock(hidden_features, hidden_features, hidden_features) for _ in range(num_single_layers)] |
| 79 | + ) |
| 80 | + self.double_blocks = torch.nn.ModuleList( |
| 81 | + [DummyBlock(hidden_features, hidden_features, hidden_features) for _ in range(num_layers)] |
| 82 | + ) |
| 83 | + self.linear_2 = torch.nn.Linear(hidden_features, out_features) |
| 84 | + |
| 85 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 86 | + x = self.linear_1(x) |
| 87 | + x = self.activation(x) |
| 88 | + for block in self.double_blocks: |
| 89 | + x = block(x) |
| 90 | + for block in self.single_blocks: |
| 91 | + x = block(x) |
| 92 | + x = self.linear_2(x) |
| 93 | + return x |
| 94 | + |
| 95 | + |
61 | 96 | class DummyPipeline(DiffusionPipeline): |
62 | 97 | model_cpu_offload_seq = "model" |
63 | 98 |
|
@@ -212,3 +247,23 @@ def test_error_raised_if_group_offloading_applied_on_sequential_offloaded_module |
212 | 247 | pipe.enable_sequential_cpu_offload() |
213 | 248 | with self.assertRaisesRegex(ValueError, "Cannot apply group offloading"): |
214 | 249 | pipe.model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=3) |
| 250 | + |
| 251 | + def test_block_level_stream_with_invocation_order_different_from_initialization_order(self): |
| 252 | + if torch.device(torch_device).type != "cuda": |
| 253 | + return |
| 254 | + model = DummyModelWithMultipleBlocks( |
| 255 | + in_features=self.in_features, |
| 256 | + hidden_features=self.hidden_features, |
| 257 | + out_features=self.out_features, |
| 258 | + num_layers=self.num_layers, |
| 259 | + num_single_layers=self.num_layers + 1, |
| 260 | + ) |
| 261 | + model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=1, use_stream=True) |
| 262 | + |
| 263 | + context = contextlib.nullcontext() |
| 264 | + if compare_versions("diffusers", "<=", "0.33.0"): |
| 265 | + # Will raise a device mismatch RuntimeError mentioning weights are on CPU but input is on device |
| 266 | + context = self.assertRaisesRegex(RuntimeError, "Expected all tensors to be on the same device") |
| 267 | + |
| 268 | + with context: |
| 269 | + model(self.input) |
0 commit comments