Spaces:
Running on Zero
Running on Zero
File size: 8,156 Bytes
9368ee7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
from typing import List, Dict
import contextlib
import attrs
from collections.abc import Mapping, Iterable
from contextlib import contextmanager
import random
from typing import TYPE_CHECKING, Any
import numpy as np
import torch
from omegaconf import DictConfig, OmegaConf
from lipforcing.utils.distributed import world_size, get_rank
import lipforcing.utils.logging_utils as logger
if TYPE_CHECKING:
from lipforcing.configs.config import BaseConfig
PRECISION_MAP = {
"float16": torch.float16,
"bfloat16": torch.bfloat16,
"float32": torch.float32,
"float64": torch.float64,
}
def get_batch_size_total(config: BaseConfig):
# accumulated batch size per GPU
batch_size = config.dataloader_train.batch_size * config.trainer.grad_accum_rounds
return batch_size * world_size()
def to_str(obj: Any) -> str | Dict[Any, str]:
"""Print the object in a readable format. Typically used for batches of data."""
if isinstance(obj, torch.Tensor):
return f"Tensor{list(obj.shape)}"
elif isinstance(obj, str):
dots = "..." if len(obj) > 10 else ""
return f"{dots}{obj[-10:]}"
elif isinstance(obj, Mapping):
return {k: to_str(v) for k, v in obj.items()}
elif isinstance(obj, Iterable):
return str([to_str(v) for v in obj])
return str(obj)
@contextmanager
def inference_mode(*modules: torch.nn.Module, precision_amp: torch.dtype | None = None, device_type: str = "cuda"):
"""
Wraps torch.inference_mode() and temporarily sets the provided modules
to .eval() mode. If precision_amp is not None, it also wraps the context in torch.autocast().
Args:
*modules: Modules to set temporarily to eval mode.
precision_amp: If not None, wraps the context in torch.autocast().
device_type: Device type to use for autocast.
Returns:
Generator that yields the context manager.
Upon exit, it restores the original .training state of each module.
"""
# 1. Capture the original training state of each module
# (True if in train mode, False if in eval mode)
modules = [mod for mod in modules if isinstance(mod, torch.nn.Module)]
previous_states = [mod.training for mod in modules]
try:
# 2. Set all specific modules to eval mode
# This is crucial for layers like Dropout and BatchNorm
for mod in modules:
mod.eval()
# 3. Enter strict inference mode (disables gradients, etc.) and autocast if needed
with torch.inference_mode(), torch.autocast(
dtype=precision_amp, device_type=device_type, enabled=precision_amp is not None
):
yield
finally:
# 4. Restore the original state of each module
for mod, was_training in zip(modules, previous_states):
mod.train(was_training)
def set_random_seed(
seed: int, iteration: int = 0, by_rank: bool = False, devices: List[torch.device | str | int] | None = None
) -> int:
"""Set random seed for `random, numpy, Pytorch, cuda`.
Args:
seed (int): Random seed.
by_rank (bool): if set to true, each GPU will use a different random seed.
devices (List[torch.device] | None): devices to set the seed on. If None, will set the seed on all devices.
Returns:
The final random seed for the current rank.
"""
seed += iteration
if by_rank:
seed += get_rank()
seed %= 1 << 31
logger.info(f"Using random seed {seed}.")
random.seed(seed)
np.random.seed(seed)
if devices is None:
# sets seed on the current CPU & all GPUs
torch.manual_seed(seed)
else:
# set the seed on cpu
torch.default_generator.manual_seed(seed)
# set the seed on devices
for device in devices:
# get device index (as in torch.cuda.set_rng_state)
if isinstance(device, str):
device = torch.device(device)
elif isinstance(device, int):
device = torch.device("cuda", device)
idx = device.index
if idx is None:
idx = torch.cuda.current_device()
torch.cuda.default_generators[idx].manual_seed(seed)
return seed
@contextlib.contextmanager
def set_tmp_random_seed(
seed, iteration: int = 0, by_rank: bool = False, devices: List[torch.device | str | int] | None = None
):
"""A context manager to temporarily set the random seeds.
Args:
seed (int): Random seed.
iteration (int): Iteration number.
by_rank (bool): if set to true, each GPU will use a different random seed.
devices (List[torch.device] | None): devices to set the seed on. If None, will set the seed on all devices.
"""
if seed is None:
yield
return
# Save the original random states
np_state = np.random.get_state()
py_state = random.getstate()
try:
# Fork torch state
with torch.random.fork_rng(devices=devices):
# Set the new seeds
set_random_seed(seed, iteration=iteration, by_rank=by_rank, devices=devices)
yield
finally:
# Restore the original random states
np.random.set_state(np_state)
random.setstate(py_state)
def to(
data: Any,
device: str | torch.device | None = None,
dtype: torch.dtype | None = None,
) -> Any:
"""Recursively cast data into the specified device, dtype, and/or memory_format.
The input data can be a tensor, a list of tensors, a dict of tensors.
See the documentation for torch.Tensor.to() for details.
Args:
data (Any): Input data.
device (str | torch.device): GPU device (default: None).
dtype (torch.dtype): data type (default: None).
Returns:
data (Any): Data cast to the specified device, dtype, and/or memory_format.
"""
assert device is not None or dtype is not None, "at least one of device, dtype should be specified"
if isinstance(data, torch.Tensor):
is_cpu = (isinstance(device, str) and device == "cpu") or (
isinstance(device, torch.device) and device.type == "cpu"
)
if data.dtype == torch.int64:
# t variable is int64 for some networks (e.g. CogVideoX, Stable Diffusion)
dtype = torch.int64
data = data.to(
device=device,
dtype=dtype,
non_blocking=(not is_cpu),
)
return data
elif isinstance(data, (list, tuple)):
return type(data)(to(d, device, dtype) for d in data)
elif isinstance(data, dict):
return {k: to(v, device, dtype) for k, v in data.items()}
else:
return data
def convert_cfg_to_dict(cfg) -> dict:
"""Convert config to dictionary, handling both OmegaConf and attrs cases.
Args:
cfg: Either a DictConfig (from OmegaConf/Hydra) or Config (attrs class)
Returns:
Dictionary representation of the config
"""
if isinstance(cfg, DictConfig):
# Production case: OmegaConf DictConfig
return OmegaConf.to_container(cfg, resolve=True)
else:
# Test case: attrs SampleTConfig class
return attrs.asdict(cfg)
def detach(
data: Any,
) -> Any:
"""Recursively detach data if it is a tensor.
Args:
data (Any): Input data.
Returns:
data (Any): Data detached from the computation graph.
"""
if isinstance(data, torch.Tensor):
return data.detach()
elif isinstance(data, (list, tuple)):
return type(data)(detach(d) for d in data)
elif isinstance(data, dict):
return {k: detach(v) for k, v in data.items()}
else:
return data
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "1"):
return True
elif v.lower() in ("no", "false", "f", "0"):
return False
else:
raise ValueError("Boolean value expected.")
|