MCPcopy Create free account
hub / github.com/MegaScenes/nvs / CheckpointFunction

Class CheckpointFunction

ldm/modules/diffusionmodules/util.py:119–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

117
118
119class CheckpointFunction(torch.autograd.Function):
120 @staticmethod
121 def forward(ctx, run_function, length, *args):
122 ctx.run_function = run_function
123 ctx.input_tensors = list(args[:length])
124 ctx.input_params = list(args[length:])
125
126 with torch.no_grad():
127 output_tensors = ctx.run_function(*ctx.input_tensors)
128 return output_tensors
129
130 @staticmethod
131 def backward(ctx, *output_grads):
132 ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors]
133 with torch.enable_grad():
134 # Fixes a bug where the first op in run_function modifies the
135 # Tensor storage in place, which is not allowed for detach()'d
136 # Tensors.
137 shallow_copies = [x.view_as(x) for x in ctx.input_tensors]
138 output_tensors = ctx.run_function(*shallow_copies)
139 input_grads = torch.autograd.grad(
140 output_tensors,
141 ctx.input_tensors + ctx.input_params,
142 output_grads,
143 allow_unused=True,
144 )
145 del ctx.input_tensors
146 del ctx.input_params
147 del output_tensors
148 return (None, None) + input_grads
149
150
151def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected