Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion sam_audio/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _from_pretrained(
local_files_only: bool,
token: Union[str, bool, None],
map_location: str = "cpu",
low_cpu_mem_usage: bool = False,
strict: bool = True,
revision: Optional[str] = None,
**model_kwargs,
Expand Down Expand Up @@ -58,5 +59,21 @@ def _from_pretrained(
weights_only=True,
map_location=map_location,
)
model.load_state_dict(state_dict, strict=strict)

if low_cpu_mem_usage:
model_state = model.state_dict()
for k, v in list(state_dict.items()):
if k in model_state:
try:
model_state[k].copy_(v)
except Exception:
# fallback to regular assignment if in-place copy fails
model_state[k] = v
# free the tensor asap to keep peak memory low
del state_dict[k]
# load remaining keys (if any) normally
if len(state_dict) > 0:
model.load_state_dict(state_dict, strict=strict)
else:
model.load_state_dict(state_dict, strict=strict)
return model