[docs]defmove_to_device_pass(ep:ExportedProgram,location:Union[torch.device,str,Dict[str,str]])->ExportedProgram:""" Move the exported program to the given device. Args: ep (ExportedProgram): The exported program to move. location (Union[torch.device, str, Dict[str, str]]): The device to move the exported program to. If a string, it is interpreted as a device name. If a dict, it is interpreted as a mapping from the existing device to the intended one Returns: ExportedProgram: The moved exported program. """def_get_new_device(curr_device:torch.device,location:Union[torch.device,str,Dict[str,str]],)->str:ifisinstance(location,dict):ifstr(curr_device)inlocation.keys():returnlocation[str(curr_device)]else:returnstr(curr_device)else:returnstr(location)# move all the state_dictfork,vinep.state_dict.items():ifisinstance(v,torch.nn.Parameter):ep._state_dict[k]=torch.nn.Parameter(v.to(_get_new_device(v.device,location)),v.requires_grad,)else:ep._state_dict[k]=v.to(_get_new_device(v.device,location))# move all the constantsfork,vinep.constants.items():ifisinstance(v,torch.Tensor):ep._constants[k]=v.to(_get_new_device(v.device,location))fornodeinep.graph.nodes:# move all the nodes kwargs with burnt-in deviceif"device"innode.kwargs:kwargs=node.kwargs.copy()kwargs["device"]=_get_new_device(kwargs["device"],location)node.kwargs=kwargs# move all the tensor metadatanode.meta["val"]=pytree.tree_map(lambdav:v.to(_get_new_device(v.device,location))ifisinstance(v,torch.Tensor)elsev,node.meta.get("val"),)ep.validate()returnep
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.