Skip to content

Commit 840576a

Browse files
committed
enable_group_offloading -> enable_group_offload for naming consistency
1 parent 3f20e6b commit 840576a

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

docs/source/en/optimization/memory.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ In order to properly offload models after they're called, it is required to run
162162

163163
Group offloading is the middle ground between sequential and model offloading. It works by offloading groups of internal layers (either `torch.nn.ModuleList` or `torch.nn.Sequential`), which uses less memory than model-level offloading. It is also faster than sequential-level offloading because the number of device synchronizations is reduced.
164164

165-
To enable group offloading, call the [`~ModelMixin.enable_group_offloading`] method on the model if it is a Diffusers model implementation. For any other model implementation, use [`~hooks.group_offloading.apply_group_offloading`]:
165+
To enable group offloading, call the [`~ModelMixin.enable_group_offload`] method on the model if it is a Diffusers model implementation. For any other model implementation, use [`~hooks.group_offloading.apply_group_offloading`]:
166166

167167
```python
168168
import torch
@@ -175,8 +175,8 @@ onload_device = torch.device("cuda")
175175
offload_device = torch.device("cpu")
176176
pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16)
177177

178-
# We can utilize the enable_group_offloading method for Diffusers model implementations
179-
pipe.transformer.enable_group_offloading(onload_device=onload_device, offload_device=offload_device, offload_type="leaf_level", use_stream=True)
178+
# We can utilize the enable_group_offload method for Diffusers model implementations
179+
pipe.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type="leaf_level", use_stream=True)
180180

181181
# For any other model implementations, the apply_group_offloading function can be used
182182
apply_group_offloading(pipe.text_encoder, onload_device=onload_device, offload_type="block_level", num_blocks_per_group=2)

src/diffusers/models/modeling_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def enable_layerwise_casting(
446446
self, storage_dtype, compute_dtype, skip_modules_pattern, skip_modules_classes, non_blocking
447447
)
448448

449-
def enable_group_offloading(
449+
def enable_group_offload(
450450
self,
451451
onload_device: torch.device,
452452
offload_device: torch.device = torch.device("cpu"),
@@ -469,7 +469,7 @@ def enable_group_offloading(
469469
... "THUDM/CogVideoX-5b", subfolder="transformer", torch_dtype=torch.bfloat16
470470
... )
471471
472-
>>> transformer.enable_group_offloading(
472+
>>> transformer.enable_group_offload(
473473
... onload_device=torch.device("cuda"),
474474
... offload_device=torch.device("cpu"),
475475
... offload_type="leaf_level",

tests/hooks/test_group_offloading.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,23 @@ def run_forward(model):
109109
self.model.to("cpu")
110110

111111
model = self.get_model()
112-
model.enable_group_offloading(torch_device, offload_type="block_level", num_blocks_per_group=3)
112+
model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=3)
113113
output_with_group_offloading1, mem1 = run_forward(model)
114114

115115
model = self.get_model()
116-
model.enable_group_offloading(torch_device, offload_type="block_level", num_blocks_per_group=1)
116+
model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=1)
117117
output_with_group_offloading2, mem2 = run_forward(model)
118118

119119
model = self.get_model()
120-
model.enable_group_offloading(
121-
torch_device, offload_type="block_level", num_blocks_per_group=1, use_stream=True
122-
)
120+
model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=1, use_stream=True)
123121
output_with_group_offloading3, mem3 = run_forward(model)
124122

125123
model = self.get_model()
126-
model.enable_group_offloading(torch_device, offload_type="leaf_level")
124+
model.enable_group_offload(torch_device, offload_type="leaf_level")
127125
output_with_group_offloading4, mem4 = run_forward(model)
128126

129127
model = self.get_model()
130-
model.enable_group_offloading(torch_device, offload_type="leaf_level", use_stream=True)
128+
model.enable_group_offload(torch_device, offload_type="leaf_level", use_stream=True)
131129
output_with_group_offloading5, mem5 = run_forward(model)
132130

133131
# Precision assertions - offloading should not impact the output
@@ -144,12 +142,12 @@ def test_error_raised_if_streams_used_and_no_cuda_device(self):
144142
original_is_available = torch.cuda.is_available
145143
torch.cuda.is_available = lambda: False
146144
with self.assertRaises(ValueError):
147-
self.model.enable_group_offloading(
145+
self.model.enable_group_offload(
148146
onload_device=torch.device("cuda"), offload_type="leaf_level", use_stream=True
149147
)
150148
torch.cuda.is_available = original_is_available
151149

152150
def test_error_raised_if_supports_group_offloading_false(self):
153151
self.model._supports_group_offloading = False
154152
with self.assertRaisesRegex(ValueError, "does not support group offloading"):
155-
self.model.enable_group_offloading(onload_device=torch.device("cuda"))
153+
self.model.enable_group_offload(onload_device=torch.device("cuda"))

tests/models/test_modeling_common.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,24 +1454,22 @@ def run_forward(model):
14541454

14551455
torch.manual_seed(0)
14561456
model = self.model_class(**init_dict)
1457-
model.enable_group_offloading(torch_device, offload_type="block_level", num_blocks_per_group=1)
1457+
model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=1)
14581458
output_with_group_offloading1 = run_forward(model)
14591459

14601460
torch.manual_seed(0)
14611461
model = self.model_class(**init_dict)
1462-
model.enable_group_offloading(
1463-
torch_device, offload_type="block_level", num_blocks_per_group=1, non_blocking=True
1464-
)
1462+
model.enable_group_offload(torch_device, offload_type="block_level", num_blocks_per_group=1, non_blocking=True)
14651463
output_with_group_offloading2 = run_forward(model)
14661464

14671465
torch.manual_seed(0)
14681466
model = self.model_class(**init_dict)
1469-
model.enable_group_offloading(torch_device, offload_type="leaf_level")
1467+
model.enable_group_offload(torch_device, offload_type="leaf_level")
14701468
output_with_group_offloading3 = run_forward(model)
14711469

14721470
torch.manual_seed(0)
14731471
model = self.model_class(**init_dict)
1474-
model.enable_group_offloading(torch_device, offload_type="leaf_level", use_stream=True)
1472+
model.enable_group_offload(torch_device, offload_type="leaf_level", use_stream=True)
14751473
output_with_group_offloading4 = run_forward(model)
14761474

14771475
self.assertTrue(torch.allclose(output_without_group_offloading, output_with_group_offloading1, atol=1e-5))

tests/pipelines/test_pipelines_common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ def create_pipe():
20592059
pipe.set_progress_bar_config(disable=None)
20602060
return pipe
20612061

2062-
def enable_group_offloading_on_component(pipe, group_offloading_kwargs):
2062+
def enable_group_offload_on_component(pipe, group_offloading_kwargs):
20632063
# We intentionally don't test VAE's here. This is because some tests enable tiling on the VAE. If
20642064
# tiling is enabled and a forward pass is run, when cuda streams are used, the execution order of
20652065
# the layers is not traced correctly. This causes errors. For apply group offloading to VAE, a
@@ -2077,9 +2077,9 @@ def enable_group_offloading_on_component(pipe, group_offloading_kwargs):
20772077
component = getattr(pipe, component_name)
20782078
if not getattr(component, "_supports_group_offloading", True):
20792079
continue
2080-
if hasattr(component, "enable_group_offloading"):
2080+
if hasattr(component, "enable_group_offload"):
20812081
# For diffusers ModelMixin implementations
2082-
component.enable_group_offloading(torch.device(torch_device), **group_offloading_kwargs)
2082+
component.enable_group_offload(torch.device(torch_device), **group_offloading_kwargs)
20832083
else:
20842084
# For other models not part of diffusers
20852085
apply_group_offloading(
@@ -2105,11 +2105,11 @@ def run_forward(pipe):
21052105
output_without_group_offloading = run_forward(pipe)
21062106

21072107
pipe = create_pipe()
2108-
enable_group_offloading_on_component(pipe, {"offload_type": "block_level", "num_blocks_per_group": 1})
2108+
enable_group_offload_on_component(pipe, {"offload_type": "block_level", "num_blocks_per_group": 1})
21092109
output_with_group_offloading1 = run_forward(pipe)
21102110

21112111
pipe = create_pipe()
2112-
enable_group_offloading_on_component(pipe, {"offload_type": "leaf_level"})
2112+
enable_group_offload_on_component(pipe, {"offload_type": "leaf_level"})
21132113
output_with_group_offloading2 = run_forward(pipe)
21142114

21152115
if torch.is_tensor(output_without_group_offloading):

0 commit comments

Comments
 (0)