Download diffsynth/core/gradient/gradient_checkpoint.py from ViTeX-Bench/ViTeX-Edit-14B: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/ViTeX-Bench/ViTeX-Edit-14B/resolve/c5afbf279998b75da844c19fb4182e8bf58df875/diffsynth/core/gradient/gradient_checkpoint.py
- Command line
-
hf download hf://ViTeX-Bench/ViTeX-Edit-14B@c5afbf279998b75da844c19fb4182e8bf58df875/diffsynth/core/gradient/gradient_checkpoint.py
-
curl -L -o gradient_checkpoint.py https://huggingface.co/ViTeX-Bench/ViTeX-Edit-14B/resolve/c5afbf279998b75da844c19fb4182e8bf58df875/diffsynth/core/gradient/gradient_checkpoint.py
1.1 kB
| import torch | |
| import warnings | |
| # Suppress checkpoint requires_grad warning - gradients flow through model params, not inputs | |
| warnings.filterwarnings("ignore", message=".*None of the inputs have requires_grad.*") | |
| def create_custom_forward(module): | |
| def custom_forward(*inputs, **kwargs): | |
| return module(*inputs, **kwargs) | |
| return custom_forward | |
| def gradient_checkpoint_forward( | |
| model, | |
| use_gradient_checkpointing, | |
| use_gradient_checkpointing_offload, | |
| *args, | |
| **kwargs, | |
| ): | |
| if use_gradient_checkpointing_offload: | |
| with torch.autograd.graph.save_on_cpu(): | |
| model_output = torch.utils.checkpoint.checkpoint( | |
| create_custom_forward(model), | |
| *args, | |
| **kwargs, | |
| use_reentrant=True, | |
| ) | |
| elif use_gradient_checkpointing: | |
| model_output = torch.utils.checkpoint.checkpoint( | |
| create_custom_forward(model), | |
| *args, | |
| **kwargs, | |
| use_reentrant=True, | |
| ) | |
| else: | |
| model_output = model(*args, **kwargs) | |
| return model_output | |