Source code for embodichain.utils
# ----------------------------------------------------------------------------
# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""Shared utilities used across EmbodiChain.
The ``@configclass`` decorator, ``CfgNode`` configuration system, logging, math/tensor helpers, file/string/device/image utilities, non-maximum suppression, and legacy computation import aliases. Domain computations live in ``embodichain.compute``.
"""
from __future__ import annotations
from .configclass import configclass, is_configclass
from .config_paths import resolve_config_path
__all__ = [
"GLOBAL_SEED",
"configclass",
"is_configclass",
"resolve_config_path",
"set_seed",
]
GLOBAL_SEED = 1024
[docs]
def set_seed(seed: int, deterministic: bool = False) -> int:
"""Set the random seed for reproducibility.
Args:
seed (int): The seed value to set. If -1, a random seed will be generated.
deterministic (bool): If True, sets the environment to deterministic mode for reproducibility.
"""
import random
import numpy as np
import torch
import os
import warp as wp
if seed == -1 and deterministic:
seed = GLOBAL_SEED
elif seed == -1:
seed = np.random.randint(0, 10000)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
wp.rand_init(seed)
if deterministic:
# refer to https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
torch.use_deterministic_algorithms(True)
else:
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = False
return seed