Skip to content

Commit d7121fd

Browse files
committed
Replaces torch_utils set_seed method with IsaacLab implementation
1 parent 69394d1 commit d7121fd

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

source/isaaclab/isaaclab/envs/direct_rl_env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from dataclasses import MISSING
1818
from typing import Any, ClassVar
1919

20-
import isaacsim.core.utils.torch as torch_utils
2120
import omni.kit.app
2221
import omni.log
2322
import omni.physx
@@ -29,6 +28,7 @@
2928
from isaaclab.sim import SimulationContext
3029
from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage
3130
from isaaclab.utils.noise import NoiseModel
31+
from isaaclab.utils.seed import configure_seed
3232
from isaaclab.utils.timer import Timer
3333

3434
from .common import VecEnvObs, VecEnvStepReturn
@@ -417,7 +417,7 @@ def seed(seed: int = -1) -> int:
417417
except ModuleNotFoundError:
418418
pass
419419
# set seed for torch and other libraries
420-
return torch_utils.set_seed(seed)
420+
return configure_seed(seed)
421421

422422
def render(self, recompute: bool = False) -> np.ndarray | None:
423423
"""Run rendering without stepping through the physics.

source/isaaclab/isaaclab/envs/manager_based_env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from collections.abc import Sequence
99
from typing import Any
1010

11-
import isaacsim.core.utils.torch as torch_utils
1211
import omni.log
1312
import omni.physx
1413
from isaacsim.core.simulation_manager import SimulationManager
@@ -19,6 +18,7 @@
1918
from isaaclab.sim import SimulationContext
2019
from isaaclab.sim.utils import attach_stage_to_usd_context, use_stage
2120
from isaaclab.ui.widgets import ManagerLiveVisualizer
21+
from isaaclab.utils.seed import configure_seed
2222
from isaaclab.utils.timer import Timer
2323

2424
from .common import VecEnvObs
@@ -447,7 +447,7 @@ def seed(seed: int = -1) -> int:
447447
except ModuleNotFoundError:
448448
pass
449449
# set seed for torch and other libraries
450-
return torch_utils.set_seed(seed)
450+
return configure_seed(seed)
451451

452452
def close(self):
453453
"""Cleanup for the environment."""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2022-2025, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
2+
# All rights reserved.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
6+
import numpy as np
7+
import os
8+
import random
9+
import torch
10+
11+
import warp as wp
12+
13+
14+
def configure_seed(seed: int | None, torch_deterministic: bool = False) -> int:
15+
"""Set seed across all random number generators (torch, numpy, random, warp).
16+
Args:
17+
seed: The random seed value. If None, generates a random seed.
18+
torch_deterministic: If True, enables deterministic mode for torch operations.
19+
Returns:
20+
The seed value that was set.
21+
"""
22+
if seed is None or seed == -1:
23+
seed = 42 if torch_deterministic else random.randint(0, 10000)
24+
25+
random.seed(seed)
26+
np.random.seed(seed)
27+
torch.manual_seed(seed)
28+
os.environ["PYTHONHASHSEED"] = str(seed)
29+
torch.cuda.manual_seed(seed)
30+
torch.cuda.manual_seed_all(seed)
31+
wp.rand_init(seed)
32+
33+
if torch_deterministic:
34+
# refer to https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
35+
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
36+
torch.backends.cudnn.benchmark = False
37+
torch.backends.cudnn.deterministic = True
38+
torch.use_deterministic_algorithms(True)
39+
else:
40+
torch.backends.cudnn.benchmark = True
41+
torch.backends.cudnn.deterministic = False
42+
43+
return seed

source/isaaclab/test/terrains/test_terrain_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import shutil
1818
import torch
1919

20-
import isaacsim.core.utils.torch as torch_utils
2120
import pytest
2221

2322
from isaaclab.terrains import FlatPatchSamplingCfg, TerrainGenerator, TerrainGeneratorCfg
2423
from isaaclab.terrains.config.rough import ROUGH_TERRAINS_CFG
24+
from isaaclab.utils.seed import configure_seed
2525

2626

2727
@pytest.fixture
@@ -65,7 +65,7 @@ def test_generation_reproducibility(use_global_seed, seed):
6565
Setting only locally is not tested as it is not supported.
6666
"""
6767
# set initial seed
68-
torch_utils.set_seed(seed)
68+
configure_seed(seed)
6969

7070
# create terrain generator
7171
cfg = ROUGH_TERRAINS_CFG
@@ -77,7 +77,7 @@ def test_generation_reproducibility(use_global_seed, seed):
7777
terrain_mesh_1 = terrain_generator.terrain_mesh.copy()
7878

7979
# set seed again
80-
torch_utils.set_seed(seed)
80+
configure_seed(seed)
8181

8282
# create terrain generator
8383
terrain_generator = TerrainGenerator(cfg=cfg)
@@ -116,7 +116,7 @@ def test_generation_cache(output_dir, curriculum):
116116

117117
# set a random seed to disturb the process
118118
# this is to ensure that the seed inside the terrain generator makes deterministic results
119-
torch_utils.set_seed(12456)
119+
configure_seed(12456)
120120

121121
# create terrain generator with cache enabled
122122
terrain_generator = TerrainGenerator(cfg=cfg)

0 commit comments

Comments
 (0)