Skip to content

Commit e9cfb19

Browse files
mfbalinlijialin03
authored andcommitted
[GraphBolt][CUDA] Use better memory allocation algorithm to avoid OOM. (dmlc#7618)
1 parent 97c73d2 commit e9cfb19

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

python/dgl/graphbolt/__init__.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22
import os
33
import sys
44

5+
from .internal_utils import *
6+
7+
CUDA_ALLOCATOR_ENV_WARNING_STR = """
8+
An experimental feature for CUDA allocations is turned on for better allocation
9+
pattern resulting in better memory usage for minibatch GNN training workloads.
10+
See https://pytorch.org/docs/stable/notes/cuda.html#optimizing-memory-usage-with-pytorch-cuda-alloc-conf,
11+
and set the environment variable `PYTORCH_CUDA_ALLOC_CONF=expandable_segments:False`
12+
if you want to disable it.
13+
"""
14+
cuda_allocator_env = os.getenv("PYTORCH_CUDA_ALLOC_CONF")
15+
if cuda_allocator_env is None:
16+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
17+
gb_warning(CUDA_ALLOCATOR_ENV_WARNING_STR)
18+
else:
19+
configs = {
20+
kv_pair.split(":")[0]: kv_pair.split(":")[1]
21+
for kv_pair in cuda_allocator_env.split(",")
22+
}
23+
if "expandable_segments" in configs:
24+
if configs["expandable_segments"] != "True":
25+
gb_warning(
26+
"You should consider `expandable_segments:True` in the"
27+
" environment variable `PYTORCH_CUDA_ALLOC_CONF` for lower"
28+
" memory usage. See "
29+
"https://pytorch.org/docs/stable/notes/cuda.html"
30+
"#optimizing-memory-usage-with-pytorch-cuda-alloc-conf"
31+
)
32+
else:
33+
configs["expandable_segments"] = "True"
34+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = ",".join(
35+
[k + ":" + v for k, v in configs.items()]
36+
)
37+
gb_warning(CUDA_ALLOCATOR_ENV_WARNING_STR)
38+
39+
40+
# pylint: disable=wrong-import-position, wrong-import-order
541
import torch
642

743
### FROM DGL @todo
@@ -47,7 +83,6 @@ def load_graphbolt():
4783
from .itemset import *
4884
from .item_sampler import *
4985
from .minibatch_transformer import *
50-
from .internal_utils import *
5186
from .negative_sampler import *
5287
from .sampled_subgraph import *
5388
from .subgraph_sampler import *

tests/python/pytorch/graphbolt/test_base.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import unittest
34
from collections.abc import Iterable, Mapping
@@ -12,6 +13,13 @@
1213
from . import gb_test_utils
1314

1415

16+
def test_pytorch_cuda_allocator_conf():
17+
env = os.getenv("PYTORCH_CUDA_ALLOC_CONF")
18+
assert env is not None
19+
config_list = env.split(",")
20+
assert "expandable_segments:True" in config_list
21+
22+
1523
@unittest.skipIf(F._default_context_str != "gpu", "CopyTo needs GPU to test")
1624
@pytest.mark.parametrize("non_blocking", [False, True])
1725
def test_CopyTo(non_blocking):

0 commit comments

Comments
 (0)