mirror of
https://github.com/THUDM/CogVideo.git
synced 2026-05-31 00:08:14 +08:00
fix: replace mutable default arguments in trainer.py
Using mutable default arguments (e.g. `= []`) in Python function signatures is a well-known anti-pattern (W0102). The default list is shared across all calls, which can lead to unexpected behavior if the list is ever mutated in place. Replace `= []` with `= None` and add explicit `None` guards for `__move_components_to_device` and `__move_components_to_cpu`.
This commit is contained in:
parent
7a1af71545
commit
c84ebba304
@ -713,7 +713,9 @@ class Trainer:
|
|||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid mixed precision: {self.args.mixed_precision}")
|
raise ValueError(f"Invalid mixed precision: {self.args.mixed_precision}")
|
||||||
|
|
||||||
def __move_components_to_device(self, dtype, ignore_list: List[str] = []):
|
def __move_components_to_device(self, dtype, ignore_list: List[str] = None):
|
||||||
|
if ignore_list is None:
|
||||||
|
ignore_list = []
|
||||||
ignore_list = set(ignore_list)
|
ignore_list = set(ignore_list)
|
||||||
components = self.components.model_dump()
|
components = self.components.model_dump()
|
||||||
for name, component in components.items():
|
for name, component in components.items():
|
||||||
@ -723,7 +725,9 @@ class Trainer:
|
|||||||
self.components, name, component.to(self.accelerator.device, dtype=dtype)
|
self.components, name, component.to(self.accelerator.device, dtype=dtype)
|
||||||
)
|
)
|
||||||
|
|
||||||
def __move_components_to_cpu(self, unload_list: List[str] = []):
|
def __move_components_to_cpu(self, unload_list: List[str] = None):
|
||||||
|
if unload_list is None:
|
||||||
|
unload_list = []
|
||||||
unload_list = set(unload_list)
|
unload_list = set(unload_list)
|
||||||
components = self.components.model_dump()
|
components = self.components.model_dump()
|
||||||
for name, component in components.items():
|
for name, component in components.items():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user