|
| 1 | +# Copyright (c) 2022-2026, 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 pytest |
| 7 | + |
| 8 | +from isaaclab_tasks.utils import get_checkpoint_path |
| 9 | + |
| 10 | + |
| 11 | +def test_get_checkpoint_path_prefers_requested_checkpoint(tmp_path): |
| 12 | + checkpoint_dir = tmp_path / "cartpole_camera_direct" / "2026-06-07_02-41-41" / "nn" |
| 13 | + checkpoint_dir.mkdir(parents=True) |
| 14 | + expected_checkpoint = checkpoint_dir / "cartpole_camera_direct.pth" |
| 15 | + expected_checkpoint.touch() |
| 16 | + (checkpoint_dir / "last_cartpole_camera_direct_ep_10_rew_1.0.pth").touch() |
| 17 | + |
| 18 | + checkpoint_path = get_checkpoint_path( |
| 19 | + str(tmp_path / "cartpole_camera_direct"), "2026-06-07_02-41-41", "cartpole_camera_direct.pth", other_dirs=["nn"] |
| 20 | + ) |
| 21 | + |
| 22 | + assert checkpoint_path == str(expected_checkpoint) |
| 23 | + |
| 24 | + |
| 25 | +def test_get_checkpoint_path_uses_latest_checkpoint_when_checkpoint_is_omitted(tmp_path): |
| 26 | + checkpoint_dir = tmp_path / "cartpole_camera_direct" / "2026-06-07_02-41-41" / "nn" |
| 27 | + checkpoint_dir.mkdir(parents=True) |
| 28 | + (checkpoint_dir / "last_cartpole_camera_direct_ep_5_rew_0.5.pth").touch() |
| 29 | + expected_checkpoint = checkpoint_dir / "last_cartpole_camera_direct_ep_10_rew_1.0.pth" |
| 30 | + expected_checkpoint.touch() |
| 31 | + |
| 32 | + checkpoint_path = get_checkpoint_path( |
| 33 | + str(tmp_path / "cartpole_camera_direct"), "2026-06-07_02-41-41", other_dirs=["nn"] |
| 34 | + ) |
| 35 | + |
| 36 | + assert checkpoint_path == str(expected_checkpoint) |
| 37 | + |
| 38 | + |
| 39 | +def test_get_checkpoint_path_requested_checkpoint_stays_strict(tmp_path): |
| 40 | + checkpoint_dir = tmp_path / "cartpole_camera_direct" / "2026-06-07_02-41-41" / "nn" |
| 41 | + checkpoint_dir.mkdir(parents=True) |
| 42 | + (checkpoint_dir / "last_cartpole_camera_direct_ep_10_rew_1.0.pth").touch() |
| 43 | + |
| 44 | + with pytest.raises(ValueError, match="cartpole_camera_direct.pth"): |
| 45 | + get_checkpoint_path( |
| 46 | + str(tmp_path / "cartpole_camera_direct"), |
| 47 | + "2026-06-07_02-41-41", |
| 48 | + "cartpole_camera_direct.pth", |
| 49 | + other_dirs=["nn"], |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def test_get_checkpoint_path_prefers_preferred_over_checkpoint(tmp_path): |
| 54 | + # rl_games: the best checkpoint must win over numbered ones when given as the preferred pattern |
| 55 | + checkpoint_dir = tmp_path / "cartpole_camera_direct" / "2026-06-07_02-41-41" / "nn" |
| 56 | + checkpoint_dir.mkdir(parents=True) |
| 57 | + expected_checkpoint = checkpoint_dir / "cartpole_camera_direct.pth" |
| 58 | + expected_checkpoint.touch() |
| 59 | + (checkpoint_dir / "last_cartpole_camera_direct_ep_10_rew_1.0.pth").touch() |
| 60 | + |
| 61 | + checkpoint_path = get_checkpoint_path( |
| 62 | + str(tmp_path / "cartpole_camera_direct"), |
| 63 | + "2026-06-07_02-41-41", |
| 64 | + ".*", |
| 65 | + other_dirs=["nn"], |
| 66 | + preferred_checkpoint="cartpole_camera_direct.pth", |
| 67 | + ) |
| 68 | + |
| 69 | + assert checkpoint_path == str(expected_checkpoint) |
| 70 | + |
| 71 | + |
| 72 | +def test_get_checkpoint_path_falls_back_when_preferred_missing(tmp_path): |
| 73 | + # rl_games short run: the best checkpoint is never written, so resolution falls back to the latest numbered one |
| 74 | + checkpoint_dir = tmp_path / "cartpole_camera_direct" / "2026-06-07_02-41-41" / "nn" |
| 75 | + checkpoint_dir.mkdir(parents=True) |
| 76 | + (checkpoint_dir / "last_cartpole_camera_direct_ep_9_rew_0.9.pth").touch() |
| 77 | + expected_checkpoint = checkpoint_dir / "last_cartpole_camera_direct_ep_10_rew_1.0.pth" |
| 78 | + expected_checkpoint.touch() |
| 79 | + |
| 80 | + checkpoint_path = get_checkpoint_path( |
| 81 | + str(tmp_path / "cartpole_camera_direct"), |
| 82 | + "2026-06-07_02-41-41", |
| 83 | + ".*", |
| 84 | + other_dirs=["nn"], |
| 85 | + preferred_checkpoint="cartpole_camera_direct.pth", |
| 86 | + ) |
| 87 | + |
| 88 | + assert checkpoint_path == str(expected_checkpoint) |
| 89 | + |
| 90 | + |
| 91 | +def test_get_checkpoint_path_prefers_final_model_over_steps(tmp_path): |
| 92 | + # sb3: the final ``model.zip`` must win over numbered ``model_<n>_steps.zip`` snapshots |
| 93 | + run_dir = tmp_path / "2026-06-07_02-41-41" |
| 94 | + run_dir.mkdir(parents=True) |
| 95 | + expected_checkpoint = run_dir / "model.zip" |
| 96 | + expected_checkpoint.touch() |
| 97 | + (run_dir / "model_2000_steps.zip").touch() |
| 98 | + |
| 99 | + checkpoint_path = get_checkpoint_path( |
| 100 | + str(tmp_path), ".*", r"model_.*\.zip", sort_alpha=False, preferred_checkpoint=r"model\.zip" |
| 101 | + ) |
| 102 | + |
| 103 | + assert checkpoint_path == str(expected_checkpoint) |
0 commit comments