Spaces:
Paused
Paused
Update optimization_utils.py
Browse files97 unmodified lines989910010110210310410510610797 unmodified linescaptured_call.kwargs = e.kwargsdef drain_module_parameters(module: torch.nn.Module):state_dict_meta = {name: {'device': tensor.device, 'dtype': tensor.dtype} for name, tensor in module.state_dict().items()}state_dict = {name: torch.nn.Parameter(torch.empty_like(tensor, device='cpu')) for name, tensor in module.state_dict().items()}module.load_state_dict(state_dict, assign=True)for name, param in state_dict.items():meta = state_dict_meta[name]param.data = torch.Tensor([]).to(**meta)97 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113297 unmodified linescaptured_call.kwargs = e.kwargs# def drain_module_parameters(module: torch.nn.Module):# state_dict_meta = {name: {'device': tensor.device, 'dtype': tensor.dtype} for name, tensor in module.state_dict().items()}# state_dict = {name: torch.nn.Parameter(torch.empty_like(tensor, device='cpu')) for name, tensor in module.state_dict().items()}# module.load_state_dict(state_dict, assign=True)# for name, param in state_dict.items():# meta = state_dict_meta[name]# param.data = torch.Tensor([]).to(**meta)def drain_module_parameters(module: torch.nn.Module):state_dict_meta = {name: {'device': tensor.device, 'dtype': tensor.dtype}for name, tensor in module.state_dict().items()}state_dict = {}for name, tensor in module.state_dict().items():try:param = torch.nn.Parameter(torch.empty_like(tensor, device='cpu'))except NotImplementedError:# Fallback: dequantize (or convert) if empty_like isn't implementedparam = torch.nn.Parameter(tensor.dequantize().to('cpu') if hasattr(tensor, 'dequantize') else tensor.to('cpu'))state_dict[name] = parammodule.load_state_dict(state_dict, assign=True)for name, param in state_dict.items():meta = state_dict_meta[name]try:param.data = torch.Tensor([]).to(**meta)except NotImplementedError:# Fallback for quantized tensorsparam.data = (param.dequantize().to(**meta) if hasattr(param, 'dequantize') else torch.Tensor([]).to(**meta))
optimization_utils.py+28-3