(ctx, *args)
| 296 | |
| 297 | @staticmethod |
| 298 | def backward(ctx, *args): |
| 299 | if not torch.autograd._is_checkpoint_valid(): |
| 300 | raise RuntimeError( |
| 301 | "Checkpointing is not compatible with .grad(), " |
| 302 | "please use .backward() if possible" |
| 303 | ) |
| 304 | inputs = ctx.saved_tensors |
| 305 | if _CHECKPOINTED_ACTIVATIONS_MEMORY_BUFFER is not None: |
| 306 | inputs[0].data = gather_split_1d_tensor(inputs[0].data) |
| 307 | inputs[0].data = inputs[0].data.view(ctx.input_0_shape) |
| 308 | |
| 309 | # Store the current states. |
| 310 | bwd_cpu_rng_state = torch.get_rng_state() |
| 311 | bwd_cuda_rng_state = torch.cuda.get_rng_state() |
| 312 | bwd_cuda_rng_state_tracker = get_cuda_rng_tracker().get_states() |
| 313 | |
| 314 | # Set the states to what it used to be before the forward pass. |
| 315 | torch.set_rng_state(ctx.fwd_cpu_rng_state) |
| 316 | _set_cuda_rng_state(ctx.fwd_cuda_rng_state) |
| 317 | get_cuda_rng_tracker().set_states(ctx.fwd_cuda_rng_state_tracker) |
| 318 | |
| 319 | # Compute the forward pass. |
| 320 | detached_inputs = detach_variable(inputs) |
| 321 | with torch.enable_grad(): |
| 322 | outputs = ctx.run_function(*detached_inputs) |
| 323 | |
| 324 | # Set the states back to what it was at the start of this function. |
| 325 | torch.set_rng_state(bwd_cpu_rng_state) |
| 326 | _set_cuda_rng_state(bwd_cuda_rng_state) |
| 327 | get_cuda_rng_tracker().set_states(bwd_cuda_rng_state_tracker) |
| 328 | |
| 329 | if isinstance(outputs, torch.Tensor): |
| 330 | outputs = (outputs,) |
| 331 | torch.autograd.backward(outputs, args) |
| 332 | grads = tuple( |
| 333 | inp.grad if isinstance(inp, torch.Tensor) else inp |
| 334 | for inp in detached_inputs |
| 335 | ) |
| 336 | return (None,) + grads |
| 337 | |
| 338 | |
| 339 | def checkpoint(function, *args): |
no test coverage detected