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

Bye bye env vars, keep everything as configs #34886

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions src/transformers/integrations/fsdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
from __future__ import annotations

import os
from typing import TYPE_CHECKING

from ..utils import is_torch_available
Expand All @@ -31,3 +32,24 @@ def is_fsdp_managed_module(module: nn.Module) -> bool:
return isinstance(module, torch.distributed.fsdp.FullyShardedDataParallel) or getattr(
module, "_is_fsdp_managed_module", False
)


def enable_cpu_ram_efficient_loading():
"""
Enable CPU RAM efficient loading of model weights by setting `FSDP_CPU_RAM_EFFICIENT_LOADING`.
"""
os.environ["FSDP_CPU_RAM_EFFICIENT_LOADING"] = "true"


def disable_cpu_ram_efficient_loading():
"""
Disable CPU RAM efficient loading of model weights by unsetting `FSDP_CPU_RAM_EFFICIENT_LOADING`.
"""
os.environ["FSDP_CPU_RAM_EFFICIENT_LOADING"] = "false"


def set_cpu_ram_efficient_loading(value: bool):
"""
Set CPU RAM efficient loading of model weights by setting `FSDP_CPU_RAM_EFFICIENT_LOADING`.
"""
os.environ["FSDP_CPU_RAM_EFFICIENT_LOADING"] = str(bool(value)).lower()
33 changes: 33 additions & 0 deletions src/transformers/trainer_pt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .integrations.deepspeed import is_deepspeed_zero3_enabled
from .tokenization_utils_base import BatchEncoding
from .utils import (
is_accelerate_available,
is_sagemaker_mp_enabled,
is_torch_available,
is_torch_xla_available,
Expand All @@ -49,6 +50,10 @@
)


if is_accelerate_available():
from accelerate.utils import FullyShardedDataParallelPlugin, TorchDynamoPlugin


if is_training_run_on_sagemaker():
logging.add_handler(StreamHandler(sys.stdout))

Expand Down Expand Up @@ -1312,6 +1317,27 @@ class AcceleratorConfig:
" The [`accelerate.utils.GradientAccumulationPlugin`] default is `False`."
},
)
mixed_precision: str = field(
default=None,
metadata={
"help": "The mixed precision policy to use. If not set, the policy will be determined by the `ACCELERATE_MIXED_PRECISION` environment variable. "
"Should not be passed in through a config file."
},
)
dynamo_plugin: Optional["TorchDynamoPlugin"] = field( # noqa: F821
default=None,
metadata={
"help": "The dynamo config to use. If not set, the config will be determined by the `ACCELERATE_DYNAMO_CONFIG` environment variable. "
"Should not be passed in through a config file."
},
)
fsdp_plugin: Optional["FullyShardedDataParallelPlugin"] = field( # noqa: F821
default=None,
metadata={
"help": "The FSDP config to use. If not set, the config will be determined by environmental variables set during `accelerate launch`. "
"Should not be passed in through a config file."
},
)
use_configured_state: bool = field(
default=False,
metadata={
Expand All @@ -1333,6 +1359,13 @@ def from_json_file(cls, json_file):
f"The config file at {json_file} had unknown keys ({extra_keys}), please try upgrading your `transformers`"
" version or fix (and potentially remove these keys) from your config file."
)
# Check for fields that should not be set in config file
invalid_fields = ["mixed_precision", "fsdp_plugin", "dynamo_plugin"]
for field in invalid_fields:
if config_dict.get(field) is not None:
raise ValueError(
f"The `{field}` field should not be set in a config file. It is determined by the TrainingArguments."
)
return cls(**config_dict)

def to_dict(self):
Expand Down
Loading
Loading