3737 require_accelerator ,
3838 torch_device ,
3939)
40- from .common import cast_inputs_to_dtype , check_device_map_is_respected
40+ from .common import BaseModelOutputMixin , cast_inputs_to_dtype , check_device_map_is_respected
4141
4242
4343def require_offload_support (func ):
@@ -69,7 +69,7 @@ def wrapper(self, *args, **kwargs):
6969
7070
7171@is_cpu_offload
72- class CPUOffloadTesterMixin :
72+ class CPUOffloadTesterMixin ( BaseModelOutputMixin ) :
7373 """
7474 Mixin class for testing CPU offloading functionality.
7575
@@ -94,16 +94,14 @@ def model_split_percents(self) -> list[float]:
9494
9595 @require_offload_support
9696 @torch .no_grad ()
97- def test_cpu_offload (self , tmp_path , atol = 1e-5 , rtol = 0 ):
97+ def test_cpu_offload (self , base_model_output , tmp_path , atol = 1e-5 , rtol = 0 ):
98+ torch .manual_seed (0 )
9899 config = self .get_init_dict ()
99100 inputs_dict = self .get_dummy_inputs ()
100101 model = self .model_class (** config ).eval ()
101102
102103 model = model .to (torch_device )
103104
104- torch .manual_seed (0 )
105- base_output = model (** inputs_dict )
106-
107105 model_size = compute_module_sizes (model )["" ]
108106 # We test several splits of sizes to make sure it works
109107 max_gpu_sizes = [int (p * model_size ) for p in self .model_split_percents ]
@@ -120,21 +118,19 @@ def test_cpu_offload(self, tmp_path, atol=1e-5, rtol=0):
120118 new_output = new_model (** inputs_dict )
121119
122120 assert_tensors_close (
123- base_output [ 0 ] , new_output [0 ], atol = atol , rtol = rtol , msg = "Output should match with CPU offloading"
121+ base_model_output , new_output [0 ], atol = atol , rtol = rtol , msg = "Output should match with CPU offloading"
124122 )
125123
126124 @require_offload_support
127125 @torch .no_grad ()
128- def test_disk_offload_without_safetensors (self , tmp_path , atol = 1e-5 , rtol = 0 ):
126+ def test_disk_offload_without_safetensors (self , base_model_output , tmp_path , atol = 1e-5 , rtol = 0 ):
127+ torch .manual_seed (0 )
129128 config = self .get_init_dict ()
130129 inputs_dict = self .get_dummy_inputs ()
131130 model = self .model_class (** config ).eval ()
132131
133132 model = model .to (torch_device )
134133
135- torch .manual_seed (0 )
136- base_output = model (** inputs_dict )
137-
138134 model_size = compute_module_sizes (model )["" ]
139135 max_size = int (self .model_split_percents [0 ] * model_size )
140136 # Force disk offload by setting very small CPU memory
@@ -154,21 +150,19 @@ def test_disk_offload_without_safetensors(self, tmp_path, atol=1e-5, rtol=0):
154150 new_output = new_model (** inputs_dict )
155151
156152 assert_tensors_close (
157- base_output [ 0 ] , new_output [0 ], atol = atol , rtol = rtol , msg = "Output should match with disk offloading"
153+ base_model_output , new_output [0 ], atol = atol , rtol = rtol , msg = "Output should match with disk offloading"
158154 )
159155
160156 @require_offload_support
161157 @torch .no_grad ()
162- def test_disk_offload_with_safetensors (self , tmp_path , atol = 1e-5 , rtol = 0 ):
158+ def test_disk_offload_with_safetensors (self , base_model_output , tmp_path , atol = 1e-5 , rtol = 0 ):
159+ torch .manual_seed (0 )
163160 config = self .get_init_dict ()
164161 inputs_dict = self .get_dummy_inputs ()
165162 model = self .model_class (** config ).eval ()
166163
167164 model = model .to (torch_device )
168165
169- torch .manual_seed (0 )
170- base_output = model (** inputs_dict )
171-
172166 model_size = compute_module_sizes (model )["" ]
173167 model .cpu ().save_pretrained (str (tmp_path ))
174168
@@ -183,7 +177,7 @@ def test_disk_offload_with_safetensors(self, tmp_path, atol=1e-5, rtol=0):
183177 new_output = new_model (** inputs_dict )
184178
185179 assert_tensors_close (
186- base_output [ 0 ] ,
180+ base_model_output ,
187181 new_output [0 ],
188182 atol = atol ,
189183 rtol = rtol ,
@@ -192,7 +186,7 @@ def test_disk_offload_with_safetensors(self, tmp_path, atol=1e-5, rtol=0):
192186
193187
194188@is_group_offload
195- class GroupOffloadTesterMixin :
189+ class GroupOffloadTesterMixin ( BaseModelOutputMixin ) :
196190 """
197191 Mixin class for testing group offloading functionality.
198192
@@ -209,10 +203,9 @@ class GroupOffloadTesterMixin:
209203
210204 @require_group_offload_support
211205 @pytest .mark .parametrize ("record_stream" , [False , True ])
212- def test_group_offloading (self , record_stream , atol = 1e-5 , rtol = 0 ):
206+ def test_group_offloading (self , base_model_output , record_stream , atol = 1e-5 , rtol = 0 ):
213207 init_dict = self .get_init_dict ()
214208 inputs_dict = self .get_dummy_inputs ()
215- torch .manual_seed (0 )
216209
217210 @torch .no_grad ()
218211 def run_forward (model ):
@@ -224,10 +217,7 @@ def run_forward(model):
224217 model .eval ()
225218 return model (** inputs_dict )[0 ]
226219
227- model = self .model_class (** init_dict )
228-
229- model .to (torch_device )
230- output_without_group_offloading = run_forward (model )
220+ output_without_group_offloading = base_model_output
231221
232222 torch .manual_seed (0 )
233223 model = self .model_class (** init_dict )
0 commit comments