Repository navigation
[doc][tune][DOC-1131] Update deprecated spec.config sample_from examples to config-dict style - #63804
Conversation
…les to config-dict style sample_from callables have received the trial config dict directly since the placeholder resolution mechanism landed (befad81, Feb 2023); the spec.config access pattern hits the deprecation fallback in Function._CallSampler.__try_fn and logs a warning on every trial. Update user-facing examples to the config-dict style. The tune-resources.rst with_resources example was broken outright: resource callables receive the bare config dict via default_resource_request() with no spec fallback, so `lambda spec: ... spec.config.use_gpu` raises AttributeError. Deliberately left unchanged: - test_sample.py / test_var.py sites that drive generate_variants / BasicVariantGenerator directly (legacy full-spec path, where spec.config is still the operative contract) - test_placeholder.py testFunction (deliberate both-styles coverage) - test_controller_resume_integration.py (legacy-style restore coverage) - variant_generator.py docstring (documents the DeveloperAPI legacy contract and the {"eval": ...} syntax) Verified on ray 2.55.1: the config-dict conditional space runs clean with no deprecation warning (old style logs one per trial), and the fixed with_resources lambda resolves while the old one reproduces the AttributeError. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates Ray Tune documentation, examples, and tests to reflect an API change where lambda functions in tune.sample_from and tune.with_resources receive a config dictionary directly instead of a spec object. Feedback is provided for tune-resources.rst suggesting the use of config.get("use_gpu", False) instead of direct dictionary access to prevent potential KeyErrors when the key is missing.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| # If you want to allocate gpu resources to trials based on a setting in your config | ||
| trainable_with_resources = tune.with_resources(trainable, | ||
| resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}) | ||
| resources=lambda config: {"gpu": 1} if config["use_gpu"] else {"gpu": 0}) |
There was a problem hiding this comment.
Using direct dictionary access like config["use_gpu"] can raise a KeyError if the use_gpu key is not explicitly provided in the configuration. It is safer and more robust to use config.get("use_gpu", False) to provide a default fallback value.
| resources=lambda config: {"gpu": 1} if config["use_gpu"] else {"gpu": 0}) | |
| resources=lambda config: {"gpu": 1} if config.get("use_gpu", False) else {"gpu": 0}) |
…les to config-dict style (ray-project#63804) ## Why are these changes needed? `tune.sample_from` callables receive the trial `config` dict directly; the legacy `spec.config` access pattern has been deprecated since the placeholder-resolution mechanism landed in Feb 2023 (befad81). Every Tune doc example still written as `lambda spec: spec.config.x` logs this warning on each trial when users copy it: > sample_from functions that take a spec dict are deprecated. Please update your function to work with the config dict directly. Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring itself. ### What changed Updated to config-dict style (`lambda config: config["x"]`): - `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the prose describing the `spec` object and `spec.config` namespace - `doc/source/tune/api/search_space.rst` - `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search examples - `doc/source/tune/doc_code/key_concepts.py` - `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` — 3 cells - `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py` - `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through `Tuner`, so it exercised the deprecated fallback on every run One of these is a bug fix rather than deprecation cleanup: the `tune-resources.rst` `with_resources` example was broken as written. Resource callables receive the bare config dict via `default_resource_request()` with no spec fallback, so `resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises `AttributeError: 'dict' object has no attribute 'config'`. ### Deliberately left unchanged - `test_sample.py` and `test_var.py` `sample_from` sites — these drive `generate_variants` / `BasicVariantGenerator` directly, the legacy full-spec path where `spec.config` is still the operative contract (config-dict style would break them). - `test_placeholder.py::testFunction` — deliberate coverage that both styles work. - `test_controller_resume_integration.py` — restore-path coverage for legacy-style functions. - `variant_generator.py` docstring — documents the DeveloperAPI legacy contract and the `{"eval": ...}` syntax. ### Verification Ran the updated patterns end-to-end on ray 2.55.1: - Config-dict conditional space (`b` sampled from a range depending on `a`): 4 trials, conditional held, zero deprecation warnings. Control with the old spec style logs one warning per trial. - Fixed `with_resources` callable resolves; control with the old spec style reproduces the `AttributeError`. `ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the edited Python files. ## Related issue number Internal tracking: DOC-1131. ## Checks - [x] I've signed off every commit (by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run pre-commit equivalent lint (ruff + black at pinned versions) on the changed files. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference (n/a — no API changes). - [x] I've made sure the tests are passing (changed examples validated against ray 2.55.1; `test_trial_scheduler_pbt.py` change is parameter-name-only under the `Tuner` path). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…les to config-dict style (ray-project#63804) ## Why are these changes needed? `tune.sample_from` callables receive the trial `config` dict directly; the legacy `spec.config` access pattern has been deprecated since the placeholder-resolution mechanism landed in Feb 2023 (befad81). Every Tune doc example still written as `lambda spec: spec.config.x` logs this warning on each trial when users copy it: > sample_from functions that take a spec dict are deprecated. Please update your function to work with the config dict directly. Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring itself. ### What changed Updated to config-dict style (`lambda config: config["x"]`): - `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the prose describing the `spec` object and `spec.config` namespace - `doc/source/tune/api/search_space.rst` - `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search examples - `doc/source/tune/doc_code/key_concepts.py` - `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` — 3 cells - `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py` - `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through `Tuner`, so it exercised the deprecated fallback on every run One of these is a bug fix rather than deprecation cleanup: the `tune-resources.rst` `with_resources` example was broken as written. Resource callables receive the bare config dict via `default_resource_request()` with no spec fallback, so `resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises `AttributeError: 'dict' object has no attribute 'config'`. ### Deliberately left unchanged - `test_sample.py` and `test_var.py` `sample_from` sites — these drive `generate_variants` / `BasicVariantGenerator` directly, the legacy full-spec path where `spec.config` is still the operative contract (config-dict style would break them). - `test_placeholder.py::testFunction` — deliberate coverage that both styles work. - `test_controller_resume_integration.py` — restore-path coverage for legacy-style functions. - `variant_generator.py` docstring — documents the DeveloperAPI legacy contract and the `{"eval": ...}` syntax. ### Verification Ran the updated patterns end-to-end on ray 2.55.1: - Config-dict conditional space (`b` sampled from a range depending on `a`): 4 trials, conditional held, zero deprecation warnings. Control with the old spec style logs one warning per trial. - Fixed `with_resources` callable resolves; control with the old spec style reproduces the `AttributeError`. `ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the edited Python files. ## Related issue number Internal tracking: DOC-1131. ## Checks - [x] I've signed off every commit (by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run pre-commit equivalent lint (ruff + black at pinned versions) on the changed files. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference (n/a — no API changes). - [x] I've made sure the tests are passing (changed examples validated against ray 2.55.1; `test_trial_scheduler_pbt.py` change is parameter-name-only under the `Tuner` path). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
…les to config-dict style (ray-project#63804) ## Why are these changes needed? `tune.sample_from` callables receive the trial `config` dict directly; the legacy `spec.config` access pattern has been deprecated since the placeholder-resolution mechanism landed in Feb 2023 (befad81). Every Tune doc example still written as `lambda spec: spec.config.x` logs this warning on each trial when users copy it: > sample_from functions that take a spec dict are deprecated. Please update your function to work with the config dict directly. Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring itself. ### What changed Updated to config-dict style (`lambda config: config["x"]`): - `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the prose describing the `spec` object and `spec.config` namespace - `doc/source/tune/api/search_space.rst` - `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search examples - `doc/source/tune/doc_code/key_concepts.py` - `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` — 3 cells - `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py` - `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through `Tuner`, so it exercised the deprecated fallback on every run One of these is a bug fix rather than deprecation cleanup: the `tune-resources.rst` `with_resources` example was broken as written. Resource callables receive the bare config dict via `default_resource_request()` with no spec fallback, so `resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises `AttributeError: 'dict' object has no attribute 'config'`. ### Deliberately left unchanged - `test_sample.py` and `test_var.py` `sample_from` sites — these drive `generate_variants` / `BasicVariantGenerator` directly, the legacy full-spec path where `spec.config` is still the operative contract (config-dict style would break them). - `test_placeholder.py::testFunction` — deliberate coverage that both styles work. - `test_controller_resume_integration.py` — restore-path coverage for legacy-style functions. - `variant_generator.py` docstring — documents the DeveloperAPI legacy contract and the `{"eval": ...}` syntax. ### Verification Ran the updated patterns end-to-end on ray 2.55.1: - Config-dict conditional space (`b` sampled from a range depending on `a`): 4 trials, conditional held, zero deprecation warnings. Control with the old spec style logs one warning per trial. - Fixed `with_resources` callable resolves; control with the old spec style reproduces the `AttributeError`. `ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the edited Python files. ## Related issue number Internal tracking: DOC-1131. ## Checks - [x] I've signed off every commit (by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run pre-commit equivalent lint (ruff + black at pinned versions) on the changed files. - [x] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference (n/a — no API changes). - [x] I've made sure the tests are passing (changed examples validated against ray 2.55.1; `test_trial_scheduler_pbt.py` change is parameter-name-only under the `Tuner` path). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: 400Ping <[email protected]>
Why are these changes needed?
tune.sample_fromcallables receive the trialconfigdict directly; the legacyspec.configaccess pattern has been deprecated since the placeholder-resolution mechanism landed in Feb 2023 (befad81). Every Tune doc example still written aslambda spec: spec.config.xlogs this warning on each trial when users copy it:Follow-up to #63443, which fixed the
tune.sample_fromdocstring itself.What changed
Updated to config-dict style (
lambda config: config["x"]):doc/source/tune/tutorials/tune-search-spaces.rst— examples plus the prose describing thespecobject andspec.confignamespacedoc/source/tune/api/search_space.rstdoc/source/tune/doc_code/faq.py— conditional-spaces and grid-search examplesdoc/source/tune/doc_code/key_concepts.pydoc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb— 3 cellspython/ray/tune/examples/pbt_tune_cifar10_with_keras.pypython/ray/tune/tests/test_trial_scheduler_pbt.py— runs throughTuner, so it exercised the deprecated fallback on every runOne of these is a bug fix rather than deprecation cleanup: the
tune-resources.rstwith_resourcesexample was broken as written. Resource callables receive the bare config dict viadefault_resource_request()with no spec fallback, soresources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}raisesAttributeError: 'dict' object has no attribute 'config'.Deliberately left unchanged
test_sample.pyandtest_var.pysample_fromsites — these drivegenerate_variants/BasicVariantGeneratordirectly, the legacy full-spec path wherespec.configis still the operative contract (config-dict style would break them).test_placeholder.py::testFunction— deliberate coverage that both styles work.test_controller_resume_integration.py— restore-path coverage for legacy-style functions.variant_generator.pydocstring — documents the DeveloperAPI legacy contract and the{"eval": ...}syntax.Verification
Ran the updated patterns end-to-end on ray 2.55.1:
bsampled from a range depending ona): 4 trials, conditional held, zero deprecation warnings. Control with the old spec style logs one warning per trial.with_resourcescallable resolves; control with the old spec style reproduces theAttributeError.ruff==0.8.4andblack==22.10.0(the pre-commit pins) pass on the edited Python files.Related issue number
Internal tracking: DOC-1131.
Checks
git commit -s) in this PR.test_trial_scheduler_pbt.pychange is parameter-name-only under theTunerpath).🤖 Generated with Claude Code