[docs]defset_module(obj,mod):""" Set the module attribute on a python object for a given object for nicer printing """ifnotisinstance(mod,str):raiseTypeError("The mod argument should be a string")obj.__module__=mod
iftorch._running_with_deploy():# not valid inside torch_deploy interpreter, no paths exists for frozen modulescmake_prefix_path=Noneelse:cmake_prefix_path=_osp.join(_osp.dirname(_osp.dirname(__file__)),'share','cmake')
[docs]defswap_tensors(t1,t2):""" This function swaps the content of the two Tensor objects. At a high level, this will make t1 have the content of t2 while preserving its identity. This will not work if t1 and t2 have different slots. """# Ensure there are no weakrefsifweakref.getweakrefs(t1):raiseRuntimeError("Cannot swap t1 because it has weakref associated with it")ifweakref.getweakrefs(t2):raiseRuntimeError("Cannot swap t2 because it has weakref associated with it")t1_slots=set(copyreg._slotnames(t1.__class__))# type: ignore[attr-defined]t2_slots=set(copyreg._slotnames(t2.__class__))# type: ignore[attr-defined]ift1_slots!=t2_slots:raiseRuntimeError("Cannot swap t1 and t2 if they have different slots")defswap_attr(name):tmp=getattr(t1,name)setattr(t1,name,(getattr(t2,name)))setattr(t2,name,tmp)# Swap the types# Note that this will fail if there are mismatched slotsswap_attr("__class__")# Swap the dynamic attributesswap_attr("__dict__")# Swap the slotsforslotint1_slots:ifhasattr(t1,slot)andhasattr(t2,slot):swap_attr(slot)elifhasattr(t1,slot):setattr(t2,slot,(getattr(t1,slot)))delattr(t1,slot)elifhasattr(t2,slot):setattr(t1,slot,(getattr(t2,slot)))delattr(t2,slot)# Swap the at::Tensor they point totorch._C._swap_tensor_impl(t1,t2)
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.