| 46 | |
| 47 | @staticmethod |
| 48 | def backward(ctx, *output_grads): |
| 49 | ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors] |
| 50 | with torch.enable_grad(), \ |
| 51 | torch.cuda.amp.autocast(**ctx.gpu_autocast_kwargs): |
| 52 | # Fixes a bug where the first op in run_function modifies the |
| 53 | # Tensor storage in place, which is not allowed for detach()'d |
| 54 | # Tensors. |
| 55 | shallow_copies = [x.view_as(x) for x in ctx.input_tensors] |
| 56 | output_tensors = ctx.run_function(*shallow_copies) |
| 57 | input_grads = torch.autograd.grad( |
| 58 | output_tensors, |
| 59 | ctx.input_tensors + ctx.input_params, |
| 60 | output_grads, |
| 61 | allow_unused=True, |
| 62 | ) |
| 63 | del ctx.input_tensors |
| 64 | del ctx.input_params |
| 65 | del output_tensors |
| 66 | return (None, None) + input_grads |
| 67 | |
| 68 | |
| 69 | def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False): |