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 numa binding to init from env helper #396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions torchtnt/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def init_from_env(
pg_backend: T.Optional[str] = None,
pg_timeout: timedelta = default_pg_timeout,
float32_matmul_precision: str = "high",
bind_numa: T.Optional[bool] = None,
) -> torch.device:
"""Utility function that initializes the device and process group, if applicable.

Expand All @@ -57,6 +58,7 @@ def init_from_env(
pg_timeout (timedelta, optional): Timeout for operations executed against the process
group. Default value equals 30 minutes
float32_matmul_precision (str, optional): The setting for torch's precision of matrix multiplications.
bind_numa (bool, optional): Whether to bind CPU sockets to GPUs.

Returns:
The current device.
Expand Down Expand Up @@ -86,4 +88,18 @@ def init_from_env(
)
torch.distributed.init_process_group(backend=pg_backend, timeout=pg_timeout)
maybe_enable_tf32(float32_matmul_precision)
bind_numa = bind_numa if bind_numa is not None else (device.type == "cuda")
if bind_numa:
init_numa()

return device


def init_numa() -> None:
import numa

local_world_size = int(os.environ.get("LOCAL_WORLD_SIZE", 1))
num_sockets = numa.get_max_node() + 1
socket_id = torch.cuda.current_device() // (max(local_world_size // num_sockets, 1))
node_mask = {socket_id}
numa.bind(node_mask)