Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set use_reentrant to False for gradient checkpointing #3882

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ludwig/trainers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from typing import Callable, Dict, List, Optional, Tuple

import numpy as np
import packaging
import pandas as pd
import psutil
import torch
Expand Down Expand Up @@ -89,6 +90,9 @@
logger = logging.getLogger(__name__)


_TORCH210 = packaging.version.parse(torch.__version__) >= packaging.version.parse("2.1.0")


@register_trainer(MODEL_ECD, default=True)
class Trainer(BaseTrainer):
"""Trainer is a class that trains a model."""
Expand Down Expand Up @@ -252,7 +256,16 @@ def prepare(self):
):
logger.warning("Gradient checkpointing is not supported by this model. Skipping...")
elif hasattr(self.compiled_model.model, "gradient_checkpointing_enable"):
self.compiled_model.model.gradient_checkpointing_enable()
if _TORCH210:
# https://pytorch.org/docs/stable/checkpoint.html
# https://github.com/huggingface/transformers/blob/02f8738ef8c674300c314d004ba436cb5aaca165/src/transformers/modeling_utils.py#L2094 # noqa: E501
self.compiled_model.model.gradient_checkpointing_enable(
gradient_checkpointing_kwargs={"use_reentrant": False}
)
else:
self.compiled_model.model.gradient_checkpointing_enable()
# `use_cache=True` is incompatible with gradient checkpointing.
self.compiled_model.model.config.use_cache = False
self.compiled_model.model.enable_input_require_grads()
logger.info("Gradient checkpointing enabled for training.")
else:
Expand Down
Loading