MCPcopy Create free account
hub / github.com/TencentARC/MotionCtrl / CheckpointFunction

Class CheckpointFunction

lvdm/common.py:106–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104
105
106class CheckpointFunction(torch.autograd.Function):
107 @staticmethod
108 @torch.cuda.amp.custom_fwd
109 def forward(ctx, run_function, length, *args):
110 ctx.run_function = run_function
111 ctx.input_tensors = list(args[:length])
112 ctx.input_params = list(args[length:])
113
114 with torch.no_grad():
115 output_tensors = ctx.run_function(*ctx.input_tensors)
116 return output_tensors
117
118 @staticmethod
119 @torch.cuda.amp.custom_bwd # add this
120 def backward(ctx, *output_grads):
121 '''
122 for x in ctx.input_tensors:
123 if isinstance(x, int):
124 print('-----------------', ctx.run_function)
125 '''
126 ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors]
127 with torch.enable_grad():
128 # Fixes a bug where the first op in run_function modifies the
129 # Tensor storage in place, which is not allowed for detach()'d
130 # Tensors.
131 shallow_copies = [x.view_as(x) for x in ctx.input_tensors]
132 output_tensors = ctx.run_function(*shallow_copies)
133 input_grads = torch.autograd.grad(
134 output_tensors,
135 ctx.input_tensors + ctx.input_params,
136 output_grads,
137 allow_unused=True,
138 )
139 del ctx.input_tensors
140 del ctx.input_params
141 del output_tensors
142 return (None, None) + input_grads

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected