importtorchimportwarningsfromtypingimportAny,Iterable,List,Tupledefdetach_variable(inputs:Tuple[Any,...])->Tuple[torch.Tensor,...]:ifisinstance(inputs,tuple):out=[]forinpininputs:ifnotisinstance(inp,torch.Tensor):out.append(inp)continuex=inp.detach()x.requires_grad=inp.requires_gradout.append(x)returntuple(out)else:raiseRuntimeError("Only tuple of tensors is supported. Got Unsupported input type: ",type(inputs).__name__)defcheck_backward_validity(inputs:Iterable[Any])->None:ifnotany(inp.requires_gradforinpininputsifisinstance(inp,torch.Tensor)):warnings.warn("None of the inputs have requires_grad=True. Gradients will be None")# We can't know if the run_fn will internally move some args to different devices,# which would require logic to preserve rng states for those devices as well.# We could paranoically stash and restore ALL the rng states for all visible devices,# but that seems very wasteful for most cases. Compromise: Stash the RNG state for# the device of all Tensor args.## To consider: maybe get_device_states and set_device_states should reside in torch/random.py?defget_device_states(*args)->Tuple[List[int],List[torch.Tensor]]:# This will not error out if "arg" is a CPU tensor or a non-tensor type because# the conditionals short-circuit.fwd_gpu_devices=list(set(arg.get_device()forarginargsifisinstance(arg,torch.Tensor)andarg.is_cuda))fwd_gpu_states=[]fordeviceinfwd_gpu_devices:withtorch.cuda.device(device):fwd_gpu_states.append(torch.cuda.get_rng_state())returnfwd_gpu_devices,fwd_gpu_statesdefset_device_states(devices,states)->None:fordevice,stateinzip(devices,states):withtorch.cuda.device(device):torch.cuda.set_rng_state(state)classCheckpointFunction(torch.autograd.Function):@staticmethoddefforward(ctx,run_function,preserve_rng_state,*args):check_backward_validity(args)ctx.run_function=run_functionctx.preserve_rng_state=preserve_rng_statectx.had_autocast_in_fwd=torch.is_autocast_enabled()ifpreserve_rng_state:ctx.fwd_cpu_state=torch.get_rng_state()# Don't eagerly initialize the cuda context by accident.# (If the user intends that the context is initialized later, within their# run_function, we SHOULD actually stash the cuda state here. Unfortunately,# we have no way to anticipate this will happen before we run the function.)ctx.had_cuda_in_fwd=Falseiftorch.cuda._initialized:ctx.had_cuda_in_fwd=Truectx.fwd_gpu_devices,ctx.fwd_gpu_states=get_device_states(*args)# Save non-tensor inputs in ctx, keep a placeholder None for tensors# to be filled out during the backward.ctx.inputs=[]ctx.tensor_indices=[]tensor_inputs=[]fori,arginenumerate(args):iftorch.is_tensor(arg):tensor_inputs.append(arg)ctx.tensor_indices.append(i)ctx.inputs.append(None)else:ctx.inputs.append(arg)ctx.save_for_backward(*tensor_inputs)withtorch.no_grad():outputs=run_function(*args)returnoutputs@staticmethoddefbackward(ctx,*args):ifnottorch.autograd._is_checkpoint_valid():raiseRuntimeError("Checkpointing is not compatible with .grad() or when an `inputs` parameter"" is passed to .backward(). Please use .backward() and do not pass its `inputs`"" argument.")# Copy the list to avoid modifying original list.inputs=list(ctx.inputs)tensor_indices=ctx.tensor_indicestensors=ctx.saved_tensors# Fill in inputs with appropriate saved tensors.fori,idxinenumerate(tensor_indices):inputs[idx]=tensors[i]# Stash the surrounding rng state, and mimic the state that was# present at this time during forward. Restore the surrounding state# when we're done.rng_devices=[]ifctx.preserve_rng_stateandctx.had_cuda_in_fwd:rng_devices=ctx.fwd_gpu_deviceswithtorch.random.fork_rng(devices=rng_devices,enabled=ctx.preserve_rng_state):ifctx.preserve_rng_state:torch.set_rng_state(ctx.fwd_cpu_state)ifctx.had_cuda_in_fwd:set_device_states(ctx.fwd_gpu_devices,ctx.fwd_gpu_states)detached_inputs=detach_variable(tuple(inputs))withtorch.enable_grad(),torch.cuda.amp.autocast(ctx.had_autocast_in_fwd):outputs=ctx.run_function(*detached_inputs)ifisinstance(outputs,torch.Tensor):outputs=(outputs,)# run backward() with only tensor that requires gradoutputs_with_grad=[]args_with_grad=[]foriinrange(len(outputs)):iftorch.is_tensor(outputs[i])andoutputs[i].requires_grad:outputs_with_grad.append(outputs[i])args_with_grad.append(args[i])iflen(outputs_with_grad)==0:raiseRuntimeError("none of output has requires_grad=True,"" this checkpoint() is not necessary")torch.autograd.backward(outputs_with_grad,args_with_grad)grads=tuple(inp.gradifisinstance(inp,torch.Tensor)elseNoneforinpindetached_inputs)return(None,None)+grads
[docs]defcheckpoint(function,*args,**kwargs):r"""Checkpoint a model or part of the model Checkpointing works by trading compute for memory. Rather than storing all intermediate activations of the entire computation graph for computing backward, the checkpointed part does **not** save intermediate activations, and instead recomputes them in backward pass. It can be applied on any part of a model. Specifically, in the forward pass, :attr:`function` will run in :func:`torch.no_grad` manner, i.e., not storing the intermediate activations. Instead, the forward pass saves the inputs tuple and the :attr:`function` parameter. In the backwards pass, the saved inputs and :attr:`function` is retrieved, and the forward pass is computed on :attr:`function` again, now tracking the intermediate activations, and then the gradients are calculated using these activation values. The output of :attr:`function` can contain non-Tensor values and gradient recording is only performed for the Tensor values. Note that if the output consists of nested structures (ex: custom objects, lists, dicts etc.) consisting of Tensors, these Tensors nested in custom structures will not be considered as part of autograd. .. warning:: Checkpointing currently only supports :func:`torch.autograd.backward` and only if its `inputs` argument is not passed. :func:`torch.autograd.grad` is not supported. .. warning:: If :attr:`function` invocation during backward does anything different than the one during forward, e.g., due to some global variable, the checkpointed version won't be equivalent, and unfortunately it can't be detected. .. warning:: If checkpointed segment contains tensors detached from the computational graph by `detach()` or `torch.no_grad()`, the backward pass will raise an error. This is because `checkpoint` makes all the outputs require gradients which causes issues when a tensor is defined to have no gradient in the model. To circumvent this, detach the tensors outside of the `checkpoint` function. .. warning:: At least one of the inputs needs to have :code:`requires_grad=True` if grads are needed for model inputs, otherwise the checkpointed part of the model won't have gradients. At least one of the outputs needs to have :code:`requires_grad=True` as well. Args: function: describes what to run in the forward pass of the model or part of the model. It should also know how to handle the inputs passed as the tuple. For example, in LSTM, if user passes ``(activation, hidden)``, :attr:`function` should correctly use the first input as ``activation`` and the second input as ``hidden`` preserve_rng_state(bool, optional, default=True): Omit stashing and restoring the RNG state during each checkpoint. args: tuple containing inputs to the :attr:`function` Returns: Output of running :attr:`function` on :attr:`*args` """# Hack to mix *args with **kwargs in a python 2.7-compliant waypreserve=kwargs.pop('preserve_rng_state',True)ifkwargs:raiseValueError("Unexpected keyword arguments: "+",".join(argforarginkwargs))returnCheckpointFunction.apply(function,preserve,*args)
[docs]defcheckpoint_sequential(functions,segments,input,**kwargs):r"""A helper function for checkpointing sequential models. Sequential models execute a list of modules/functions in order (sequentially). Therefore, we can divide such a model in various segments and checkpoint each segment. All segments except the last will run in :func:`torch.no_grad` manner, i.e., not storing the intermediate activations. The inputs of each checkpointed segment will be saved for re-running the segment in the backward pass. See :func:`~torch.utils.checkpoint.checkpoint` on how checkpointing works. .. warning:: Checkpointing currently only supports :func:`torch.autograd.backward` and only if its `inputs` argument is not passed. :func:`torch.autograd.grad` is not supported. .. warning: At least one of the inputs needs to have :code:`requires_grad=True` if grads are needed for model inputs, otherwise the checkpointed part of the model won't have gradients. .. warning: Since PyTorch 1.4, it allows only one Tensor as the input and intermediate outputs, just like :class:`torch.nn.Sequential`. Args: functions: A :class:`torch.nn.Sequential` or the list of modules or functions (comprising the model) to run sequentially. segments: Number of chunks to create in the model input: A Tensor that is input to :attr:`functions` preserve_rng_state(bool, optional, default=True): Omit stashing and restoring the RNG state during each checkpoint. Returns: Output of running :attr:`functions` sequentially on :attr:`*inputs` Example: >>> model = nn.Sequential(...) >>> input_var = checkpoint_sequential(model, chunks, input_var) """# Hack for keyword-only parameter in a python 2.7-compliant waypreserve=kwargs.pop('preserve_rng_state',True)ifkwargs:raiseValueError("Unexpected keyword arguments: "+",".join(argforarginkwargs))defrun_function(start,end,functions):defforward(input):forjinrange(start,end+1):input=functions[j](input)returninputreturnforwardifisinstance(functions,torch.nn.Sequential):functions=list(functions.children())segment_size=len(functions)//segments# the last chunk has to be non-volatileend=-1forstartinrange(0,segment_size*(segments-1),segment_size):end=start+segment_size-1input=checkpoint(run_function(start,end,functions),input,preserve_rng_state=preserve)returnrun_function(end+1,len(functions)-1,functions)(input)
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.