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

add expandable segments var #775

Merged
merged 6 commits into from
Jul 22, 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
13 changes: 13 additions & 0 deletions src/fairchem/core/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
from torch.nn.modules.module import _IncompatibleKeys


DEFAULT_ENV_VARS = {
# Expandable segments is a new cuda feature that helps with memory fragmentation during frequent allocations (ie: in the case of variable batch sizes).
# see https://pytorch.org/docs/stable/notes/cuda.html.
"PYTORCH_CUDA_ALLOC_CONF" : "expandable_segments:True",
}

# copied from https://stackoverflow.com/questions/33490870/parsing-yaml-in-python-detect-duplicated-keys
# prevents loading YAMLS where keys have been overwritten
class UniqueKeyLoader(yaml.SafeLoader):
Expand Down Expand Up @@ -953,6 +959,12 @@ def check_traj_files(batch, traj_dir) -> bool:
return all(fl.exists() for fl in traj_files)


def setup_env_vars() -> None:
for k, v in DEFAULT_ENV_VARS.items():
os.environ[k] = v
logging.info(f"Setting env {k}={v}")


@contextmanager
def new_trainer_context(*, config: dict[str, Any], distributed: bool = False):
from fairchem.core.common import distutils, gp_utils
Expand All @@ -969,6 +981,7 @@ class _TrainingContext:
trainer: BaseTrainer

setup_logging()
setup_env_vars()
original_config = config
config = copy.deepcopy(original_config)

Expand Down