MCPcopy Create free account
hub / github.com/Sin3DM/Sin3DM / CheckpointFunction

Class CheckpointFunction

src/diffusion/nn.py:142–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

140
141
142class CheckpointFunction(th.autograd.Function):
143 @staticmethod
144 def forward(ctx, run_function, length, *args):
145 ctx.run_function = run_function
146 ctx.input_tensors = list(args[:length])
147 ctx.input_params = list(args[length:])
148 with th.no_grad():
149 output_tensors = ctx.run_function(*ctx.input_tensors)
150 return output_tensors
151
152 @staticmethod
153 def backward(ctx, *output_grads):
154 ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors]
155 with th.enable_grad():
156 # Fixes a bug where the first op in run_function modifies the
157 # Tensor storage in place, which is not allowed for detach()'d
158 # Tensors.
159 shallow_copies = [x.view_as(x) for x in ctx.input_tensors]
160 output_tensors = ctx.run_function(*shallow_copies)
161 input_grads = th.autograd.grad(
162 output_tensors,
163 ctx.input_tensors + ctx.input_params,
164 output_grads,
165 allow_unused=True,
166 )
167 del ctx.input_tensors
168 del ctx.input_params
169 del output_tensors
170 return (None, None) + input_grads

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected