Unverified Commit fdadc90e authored by Pascal Roth's avatar Pascal Roth Committed by GitHub

Replaces torch_utils `set_seed` method with IsaacLab implementation (#3920)

# Description

Replace torch_utils `set_seed` method with IsaacLab implementation

## Type of change

- Dependency removal

## Checklist

- [ ] I have read and understood the [contribution
guidelines](https://isaac-sim.github.io/IsaacLab/main/source/refs/contributing.html)
- [ ] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [ ] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

---------
Signed-off-by: 's avatarPascal Roth <57946385+pascal-roth@users.noreply.github.com>
Co-authored-by: 's avatarMayank Mittal <12863862+Mayankm96@users.noreply.github.com>
Co-authored-by: 's avatarOcti Zhang <zhengyuz@nvidia.com>
Co-authored-by: 's avatargreptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
parent c32db68e
...@@ -10,7 +10,7 @@ or soft bodies. For more information, please refer to the `PhysX Determinism doc ...@@ -10,7 +10,7 @@ or soft bodies. For more information, please refer to the `PhysX Determinism doc
Based on above, Isaac Lab provides a deterministic simulation that ensures consistent simulation Based on above, Isaac Lab provides a deterministic simulation that ensures consistent simulation
results across different runs. This is achieved by using the same random seed for the results across different runs. This is achieved by using the same random seed for the
simulation environment and the physics engine. At construction of the environment, the random seed simulation environment and the physics engine. At construction of the environment, the random seed
is set to a fixed value using the :meth:`~isaacsim.core.utils.torch.set_seed` method. This method sets the is set to a fixed value using the :meth:`~isaaclab.utils.seed.configure_seed` method. This method sets the
random seed for both the CPU and GPU globally across different libraries, including PyTorch and random seed for both the CPU and GPU globally across different libraries, including PyTorch and
NumPy. NumPy.
......
[package] [package]
# Note: Semantic Versioning is used: https://semver.org/ # Note: Semantic Versioning is used: https://semver.org/
version = "0.48.0" version = "0.48.2"
# Description # Description
title = "Isaac Lab framework for Robot Learning" title = "Isaac Lab framework for Robot Learning"
......
Changelog Changelog
--------- ---------
0.48.2 (2025-11-13)
~~~~~~~~~~~~~~~~~~~
Changed
^^^^^^^
* Changed from using :meth:`isaacsim.core.utils.torch.set_seed` to :meth:`~isaaclab.utils.seed.configure_seed`
0.48.1 (2025-11-10) 0.48.1 (2025-11-10)
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
......
...@@ -18,7 +18,6 @@ from collections.abc import Sequence ...@@ -18,7 +18,6 @@ from collections.abc import Sequence
from dataclasses import MISSING from dataclasses import MISSING
from typing import Any, ClassVar from typing import Any, ClassVar
import isaacsim.core.utils.torch as torch_utils
import omni.kit.app import omni.kit.app
import omni.physx import omni.physx
from isaacsim.core.version import get_version from isaacsim.core.version import get_version
...@@ -28,6 +27,7 @@ from isaaclab.scene import InteractiveScene ...@@ -28,6 +27,7 @@ from isaaclab.scene import InteractiveScene
from isaaclab.sim import SimulationContext from isaaclab.sim import SimulationContext
from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage
from isaaclab.utils.noise import NoiseModel from isaaclab.utils.noise import NoiseModel
from isaaclab.utils.seed import configure_seed
from isaaclab.utils.timer import Timer from isaaclab.utils.timer import Timer
from .common import ActionType, AgentID, EnvStepReturn, ObsType, StateType from .common import ActionType, AgentID, EnvStepReturn, ObsType, StateType
...@@ -465,7 +465,7 @@ class DirectMARLEnv(gym.Env): ...@@ -465,7 +465,7 @@ class DirectMARLEnv(gym.Env):
except ModuleNotFoundError: except ModuleNotFoundError:
pass pass
# set seed for torch and other libraries # set seed for torch and other libraries
return torch_utils.set_seed(seed) return configure_seed(seed)
def render(self, recompute: bool = False) -> np.ndarray | None: def render(self, recompute: bool = False) -> np.ndarray | None:
"""Run rendering without stepping through the physics. """Run rendering without stepping through the physics.
......
...@@ -19,7 +19,6 @@ from collections.abc import Sequence ...@@ -19,7 +19,6 @@ from collections.abc import Sequence
from dataclasses import MISSING from dataclasses import MISSING
from typing import Any, ClassVar from typing import Any, ClassVar
import isaacsim.core.utils.torch as torch_utils
import omni.kit.app import omni.kit.app
import omni.physx import omni.physx
from isaacsim.core.simulation_manager import SimulationManager from isaacsim.core.simulation_manager import SimulationManager
...@@ -30,6 +29,7 @@ from isaaclab.scene import InteractiveScene ...@@ -30,6 +29,7 @@ from isaaclab.scene import InteractiveScene
from isaaclab.sim import SimulationContext from isaaclab.sim import SimulationContext
from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage
from isaaclab.utils.noise import NoiseModel from isaaclab.utils.noise import NoiseModel
from isaaclab.utils.seed import configure_seed
from isaaclab.utils.timer import Timer from isaaclab.utils.timer import Timer
from .common import VecEnvObs, VecEnvStepReturn from .common import VecEnvObs, VecEnvStepReturn
...@@ -434,7 +434,7 @@ class DirectRLEnv(gym.Env): ...@@ -434,7 +434,7 @@ class DirectRLEnv(gym.Env):
except ModuleNotFoundError: except ModuleNotFoundError:
pass pass
# set seed for torch and other libraries # set seed for torch and other libraries
return torch_utils.set_seed(seed) return configure_seed(seed)
def render(self, recompute: bool = False) -> np.ndarray | None: def render(self, recompute: bool = False) -> np.ndarray | None:
"""Run rendering without stepping through the physics. """Run rendering without stepping through the physics.
......
...@@ -10,7 +10,6 @@ import warnings ...@@ -10,7 +10,6 @@ import warnings
from collections.abc import Sequence from collections.abc import Sequence
from typing import Any from typing import Any
import isaacsim.core.utils.torch as torch_utils
import omni.physx import omni.physx
from isaacsim.core.simulation_manager import SimulationManager from isaacsim.core.simulation_manager import SimulationManager
from isaacsim.core.version import get_version from isaacsim.core.version import get_version
...@@ -20,6 +19,7 @@ from isaaclab.scene import InteractiveScene ...@@ -20,6 +19,7 @@ from isaaclab.scene import InteractiveScene
from isaaclab.sim import SimulationContext from isaaclab.sim import SimulationContext
from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage
from isaaclab.ui.widgets import ManagerLiveVisualizer from isaaclab.ui.widgets import ManagerLiveVisualizer
from isaaclab.utils.seed import configure_seed
from isaaclab.utils.timer import Timer from isaaclab.utils.timer import Timer
from .common import VecEnvObs from .common import VecEnvObs
...@@ -515,7 +515,7 @@ class ManagerBasedEnv: ...@@ -515,7 +515,7 @@ class ManagerBasedEnv:
except ModuleNotFoundError: except ModuleNotFoundError:
pass pass
# set seed for torch and other libraries # set seed for torch and other libraries
return torch_utils.set_seed(seed) return configure_seed(seed)
def close(self): def close(self):
"""Cleanup for the environment.""" """Cleanup for the environment."""
......
# Copyright (c) 2022-2025, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
import numpy as np
import os
import random
import torch
import warp as wp
def configure_seed(seed: int | None, torch_deterministic: bool = False) -> int:
"""Set seed across all random number generators (torch, numpy, random, warp).
Args:
seed: The random seed value. If None, generates a random seed.
torch_deterministic: If True, enables deterministic mode for torch operations.
Returns:
The seed value that was set.
"""
if seed is None or seed == -1:
seed = 42 if torch_deterministic else 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 torch_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
...@@ -17,11 +17,11 @@ import os ...@@ -17,11 +17,11 @@ import os
import shutil import shutil
import torch import torch
import isaacsim.core.utils.torch as torch_utils
import pytest import pytest
from isaaclab.terrains import FlatPatchSamplingCfg, TerrainGenerator, TerrainGeneratorCfg from isaaclab.terrains import FlatPatchSamplingCfg, TerrainGenerator, TerrainGeneratorCfg
from isaaclab.terrains.config.rough import ROUGH_TERRAINS_CFG from isaaclab.terrains.config.rough import ROUGH_TERRAINS_CFG
from isaaclab.utils.seed import configure_seed
@pytest.fixture @pytest.fixture
...@@ -65,7 +65,7 @@ def test_generation_reproducibility(use_global_seed, seed): ...@@ -65,7 +65,7 @@ def test_generation_reproducibility(use_global_seed, seed):
Setting only locally is not tested as it is not supported. Setting only locally is not tested as it is not supported.
""" """
# set initial seed # set initial seed
torch_utils.set_seed(seed) configure_seed(seed)
# create terrain generator # create terrain generator
cfg = ROUGH_TERRAINS_CFG cfg = ROUGH_TERRAINS_CFG
...@@ -77,7 +77,7 @@ def test_generation_reproducibility(use_global_seed, seed): ...@@ -77,7 +77,7 @@ def test_generation_reproducibility(use_global_seed, seed):
terrain_mesh_1 = terrain_generator.terrain_mesh.copy() terrain_mesh_1 = terrain_generator.terrain_mesh.copy()
# set seed again # set seed again
torch_utils.set_seed(seed) configure_seed(seed)
# create terrain generator # create terrain generator
terrain_generator = TerrainGenerator(cfg=cfg) terrain_generator = TerrainGenerator(cfg=cfg)
...@@ -116,7 +116,7 @@ def test_generation_cache(output_dir, curriculum): ...@@ -116,7 +116,7 @@ def test_generation_cache(output_dir, curriculum):
# set a random seed to disturb the process # set a random seed to disturb the process
# this is to ensure that the seed inside the terrain generator makes deterministic results # this is to ensure that the seed inside the terrain generator makes deterministic results
torch_utils.set_seed(12456) configure_seed(12456)
# create terrain generator with cache enabled # create terrain generator with cache enabled
terrain_generator = TerrainGenerator(cfg=cfg) terrain_generator = TerrainGenerator(cfg=cfg)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment