Skip to content

[BugFix] Let test_trainer.py collect without Hydra so tests-optdeps runs again - #4679

Merged
theap06 merged 1 commit into
pytorch:mainfrom
sramireddy2:fix/optdeps-hydra-test-trainer
Nov 4, 2026
Merged

theap06 merged 1 commit into
pytorch:mainfrom
sramireddy2:fix/optdeps-hydra-test-trainer

Conversation

@sramireddy2

@sramireddy2 sramireddy2 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

tests-optdeps has failed on every push to main since Sept 25. The last green run was bc199cd1.
The job stops at collection, so it hasn't run any tests since then:

ERROR collecting test/test_trainer.py
E   ModuleNotFoundError: No module named 'hydra'
Interrupted: 1 error during collection

#4459 added module-level from hydra.core.config_store import ConfigStore and from hydra.utils import instantiate to test/test_trainer.py.
test_trainer.py also imports FQLLossConfig, FQLTrainerConfig from torchrl.trainers.algorithms.configs, which raises ImportError on purpose when Hydra is missing.
The optdeps environment doesn't install Hydra.

This PR follows the optional-dependency pattern from CLAUDE.md and test/test_configs.py. It adds _has_hydra = importlib.util.find_spec("hydra") is not None at module top, moves the Hydra and config imports into the tests that use them, and skips those tests when Hydra is absent:

  • test_fql_hydra_configs and test_fql_config_constructor_parity;
  • TestTdMpc2OptimizationStepper and TestTdMpc2Trainer, whose helpers already imported hydra.utils.instantiate lazily but weren't skipped. They were hidden behind the collection error and fail with ModuleNotFoundError once the file collects;
  • TestLogReward::test_log_reward_normalizes_hydra_nested_key, which imports omegaconf.

No library code changes. With Hydra installed, the same tests run as before.

Could a maintainer add the ci/optdeps label so the full optdeps suite runs on this PR? It may surface failures elsewhere that have been masked since Sept 25.

Motivation and Context

tests-optdeps checks that TorchRL works without optional dependencies, and it has been red on every main push since Sept 25 without running any tests.

close #4680

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Testing

CPU-only torch 2.14.1, tensordict 0.15.4, Python 3.12 on Windows, TorchRL from source (C++ extension not built):

  • Without Hydra, python -m pytest --collect-only -q test/test_trainer.py:
    • before: no tests collected, 1 error;
    • after: 278 tests collected.
  • Without Hydra, python -m pytest -q test/test_trainer.py -m "not gpu":
    • 229 passed, 36 skipped;
    • the Hydra-dependent tests above report "Hydra is not installed".
  • With hydra-core installed, the same command gives identical results on this branch and on main: 244 passed, 21 skipped.
  • Failures that are local to my setup reproduce on main with and without this change:
    • TestRB with prioritized=True needs the compiled extension (SumSegmentTreeFp32 is not available);
    • test_fql_update_matches_recipe[True-cpu] needs MSVC for torch.compile on Windows;
    • TestOnPolicyTelemetry::test_standard_telemetry_for_finite_ppo_update_with_nested_keys.
  • Collecting the optdeps test set (pytest --collect-only test --ignore test/test_rlhf.py --ignore test/test_distributed.py --ignore test/llm) without Hydra gives 29372 tests collected.
    The only remaining errors are local: scipy and the C++ extension, both of which the optdeps job installs.

I used an AI assistant (Claude Code) to help find the cause and run the checks. I've read and tested every changed line.

Checklist

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

…uns again

test_trainer.py imported hydra and torchrl.trainers.algorithms.configs at
module level (since pytorch#4459). The configs package raises ImportError on purpose
without Hydra, so in the tests-optdeps job pytest stopped at collection and
the job has run no tests since 2026-09-25.

Check for Hydra with _has_hydra at module top, import Hydra and the config
classes inside the tests that use them, and skip those tests when Hydra is
missing. This also skips the TdMpc2 trainer and stepper tests and the
omegaconf-based LogReward test, which were masked by the collection error
and fail with ModuleNotFoundError once the file collects.
@pytorch-bot

pytorch-bot Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4679

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Cancelled Job

As of commit 01d4006 with merge base 1084821 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 4, 2026
@theap06 theap06 added the ci/optdeps Run the full tests-optdeps suite on this PR label Nov 4, 2026
@theap06 theap06 closed this Nov 4, 2026
@theap06 theap06 reopened this Nov 4, 2026

@theap06 theap06 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! LGTM! This also resolves our failing windows optdeps job.

@sramireddy2

Copy link
Copy Markdown
Contributor Author

Thanks for the review and for adding ci/optdeps, @theap06!

A note on the cancelled tests-optdeps job, to save digging through the log: the suite now runs to the end (22,294 passed, 59 failed), but the job hit its 120-minute timeout while pytest was printing the final summary, so GitHub reports it as cancelled. test_trainer.py itself is clean there (196 passed, 31 skipped, 0 failed).

The 59 failures are all outside test_trainer.py and had been hidden since Sept 25 because collection stopped early:

  • test/modules/test_planners.py::TestTdMpc2Planner (6 tests): ModuleNotFoundError: No module named 'hydra'. Same pattern as this PR: _make_tdmpc2_planner imports Hydra without a skip. I'll send a small follow-up PR for it.
  • test/transforms/test_observation_transforms.py::TestObservationNorm::test_observationnorm (48 CUDA cases): RuntimeError: Trying to pass too many CPU scalars to non-CPU kernel!
  • Ray gloo/nccl transport tests (3 in test_inference_server.py, 2 in test/rb/test_rb_distributed.py): socket.gaierror: [Errno -2] Name or service not known from socket.gethostbyname(socket.gethostname()) in torchrl/_comm/distributed.py, which looks like a runner hostname issue.

The job also runs close to its time limit now that it gets past collection (about 111 minutes of test time), so it may need a higher timeout or a split.

@theap06

theap06 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the review and for adding ci/optdeps, @theap06!

A note on the cancelled tests-optdeps job, to save digging through the log: the suite now runs to the end (22,294 passed, 59 failed), but the job hit its 120-minute timeout while pytest was printing the final summary, so GitHub reports it as cancelled. test_trainer.py itself is clean there (196 passed, 31 skipped, 0 failed).

The 59 failures are all outside test_trainer.py and had been hidden since Sept 25 because collection stopped early:

  • test/modules/test_planners.py::TestTdMpc2Planner (6 tests): ModuleNotFoundError: No module named 'hydra'. Same pattern as this PR: _make_tdmpc2_planner imports Hydra without a skip. I'll send a small follow-up PR for it.
  • test/transforms/test_observation_transforms.py::TestObservationNorm::test_observationnorm (48 CUDA cases): RuntimeError: Trying to pass too many CPU scalars to non-CPU kernel!
  • Ray gloo/nccl transport tests (3 in test_inference_server.py, 2 in test/rb/test_rb_distributed.py): socket.gaierror: [Errno -2] Name or service not known from socket.gethostbyname(socket.gethostname()) in torchrl/_comm/distributed.py, which looks like a runner hostname issue.

The job also runs close to its time limit now that it gets past collection (about 111 minutes of test time), so it may need a higher timeout or a split.

Thanks! Just looked at the log! the device-parametrized tests aren't marked as gpu. We should increase the timeout for ci as well.

@theap06
theap06 merged commit e7214d7 into pytorch:main Nov 4, 2026
@sramireddy2
sramireddy2 deleted the fix/optdeps-hydra-test-trainer branch November 4, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix ci/optdeps Run the full tests-optdeps suite on this PR CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Trainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] tests-optdeps has failed at collection since Sept 25: test_trainer.py imports Hydra unconditionally

2 participants