pytorch/test
Filip 2b6a74abf1 [optim] prevent unintended aliasing in lr_scheduler; update type annotations/docs (#163120)
1. Prevents unintended aliasing of `self._last_lr`/`get_last_lr(...)` with `group["lr"]` when `group["lr"]` is a tensor.
2. Prevents unintended aliasing of `LRScheduler.base_lrs` with the `group["initial_lr"]`s.
3. Updates `test/optim/test_lrscheduler.py` to test tensor LRs.
4. Changes type annotations for `_last_lr`, `get_last_lr()`, `base_lrs`, `get_lr()`, and `_get_closed_form_lr()` from `list[float]` to `list[float | Tensor]`; adds documentation.

Fixes #163103

LR schedulers can behave in unexpected ways when using a tensor LR due to patterns like this:
```python
self._last_lr: list[float] = [group["lr"] for group in self.optimizer.param_groups]
```

This PR adds a helper to address this:
```python
def _param_groups_val_list(optimizer: Optimizer, key: str) -> list[Any]:
    """Create a list containing group[key] for each optimizer param_group.
    Prevents aliasing when group[key] could be a Tensor.
    Raises a KeyError when group[key] does not exist.
    """
    return [
        group[key].clone() if isinstance(group[key], Tensor) else group[key]
        for group in optimizer.param_groups
    ]
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/163120
Approved by: https://github.com/janeyx99
2025-09-25 06:58:58 +00:00
..
ao/sparsity [testing] Add test owner labels for some ao sparse tests (#163203) 2025-09-18 16:08:13 +00:00
autograd
backends/xeon
benchmark_utils [BE][3/6] fix typos in test/ (#157637) 2025-07-17 12:08:33 +00:00
compiled_autograd_skips [ca] skip on some PYTORCH_TEST_WITH_DYNAMO=1 autograd tests (#156374) 2025-06-21 18:33:38 +00:00
cpp [RELAND] Always build USE_DISTRIBUTED (#160449) and Make distributed modules importable even when backend not built (#159889) (#162594) 2025-09-22 21:12:18 +00:00
cpp_api_parity [BE][PYFMT] migrate PYFMT for test/[a-h]*/ to ruff format (#144555) 2025-06-24 04:53:54 +00:00
cpp_extensions [Code Clean] Remove deadcodes about Python3.9 [2/N] (#163627) 2025-09-24 07:30:50 +00:00
custom_backend [Cmake] Make PyTorch buildable by CMake-4.x (#150203) 2025-03-29 01:39:13 +00:00
custom_operator Using the latest torch.library.register_fake API instead of torch.library.impl_abstract (#158839) 2025-07-25 02:37:30 +00:00
distributed Shortcut redistribution when num_shards == 1 (#163742) 2025-09-24 23:49:08 +00:00
distributions [Reland][2/N]Port several test files under test/distributed to Intel GPU (#159473) 2025-09-17 06:42:27 +00:00
dynamo [Code Clean] Remove deadcodes about Python3.9 [8/N] (#163728) 2025-09-25 05:12:46 +00:00
dynamo_expected_failures [dynamo] Graph break on on user-defined class in compiled region (#161670) 2025-09-10 04:39:20 +00:00
dynamo_skips [dynamo] Graph break on on user-defined class in compiled region (#161670) 2025-09-10 04:39:20 +00:00
error_messages
expect [MPS] Move sparsemps testing from test_mps to test_sparse (#161852) 2025-09-02 19:04:11 +00:00
export Update tests to check for more robust pattern (#163107) 2025-09-23 21:11:48 +00:00
forward_backward_compatibility Fused RMSNorm implementation (#153666) 2025-07-22 22:25:44 +00:00
functorch [inductor] Fix divmod error in decomp (#163482) 2025-09-24 02:52:36 +00:00
fx [doc]: Small typos (#162982) 2025-09-16 17:42:19 +00:00
higher_order_ops [hop] support local_map + SAC (#163322) 2025-09-24 04:57:40 +00:00
inductor [Code Clean] Remove deadcodes about Python3.9 [8/N] (#163728) 2025-09-25 05:12:46 +00:00
inductor_expected_failures [dynamo] Graph break on on user-defined class in compiled region (#161670) 2025-09-10 04:39:20 +00:00
inductor_skips
jit [Code Clean] Remove deadcodes about Python3.9 [7/N] (#163646) 2025-09-24 07:30:50 +00:00
jit_hooks [Cmake] Make PyTorch buildable by CMake-4.x (#150203) 2025-03-29 01:39:13 +00:00
lazy [BE][PYFMT] migrate PYFMT for test/[i-z]*/ to ruff format (#144556) 2025-07-29 03:26:09 +00:00
mobile [BE][PYFMT] migrate PYFMT for test/[i-z]*/ to ruff format (#144556) 2025-07-29 03:26:09 +00:00
nn [BUG] MaxUnpool2d/3d should check output dim before accessing its elements (#163507) 2025-09-22 21:36:48 +00:00
onnx [ONNX] Fix rotary_embedding_23 implementation (#162865) 2025-09-16 03:30:05 +00:00
optim [optim] prevent unintended aliasing in lr_scheduler; update type annotations/docs (#163120) 2025-09-25 06:58:58 +00:00
package [Torch Package] Make get names of OrderedImporters support fallback to importers (#155743) 2025-08-06 02:26:10 +00:00
profiler [ARM] Add test_memory_profiler to aarch64 tests (#145260) 2025-09-24 09:29:13 +00:00
quantization [6/n] Quantization with min & max bounds support - using fbgemm changes in ATen (#162924) 2025-09-25 02:52:04 +00:00
scripts [BE][PYFMT] migrate PYFMT for test/[i-z]*/ to ruff format (#144556) 2025-07-29 03:26:09 +00:00
strobelight/examples
test_img
torch_np [BE][PYFMT] migrate PYFMT for test/[i-z]*/ to ruff format (#144556) 2025-07-29 03:26:09 +00:00
typing Revert "Add __init__.pyi to torch/linalg (#160750)" 2025-09-02 16:53:55 +00:00
xpu [WOQ] Add XPU kernel for _weight_int8pack_mm (#160938) 2025-09-19 07:37:14 +00:00
_test_bazel.py
allowlist_for_publicAPI.json Clean up 'torch.onnx' entries from public API allowlist (#162850) 2025-09-15 16:14:43 +00:00
bench_mps_ops.py [BE] Remove macos-13 guard from bench_mps_ops (#159732) 2025-08-03 20:53:58 +00:00
conftest.py [CI][run_test] Fix rerun logic for failing at exit (#155853) 2025-06-17 17:51:40 +00:00
create_dummy_torchscript_model.py
HowToWriteTestsUsingFileCheck.md
linear.py
load_torchscript_model.py
minioptest_failures_dict.json
mkl_verbose.py
mkldnn_verbose.py
pytest_shard_custom.py
run_doctests.sh
run_test.py update test_quantization tests to run weekly (#163077) 2025-09-24 11:31:11 +00:00
simulate_nccl_errors.py [BE]: Update ruff to 0.11.8 (#153249) 2025-05-12 18:30:52 +00:00
slow_tests.json Update slow tests (#162946) 2025-09-15 11:31:37 +00:00
test_accelerator.py Add UT for torch.accelerator memory-related API (#155200) 2025-08-08 17:41:22 +00:00
test_ao_sparsity.py [testing] Add test owner labels for some ao sparse tests (#163203) 2025-09-18 16:08:13 +00:00
test_appending_byte_serializer.py Check integrity of bytes in AppendingByteSerializer (#152139) 2025-04-26 18:10:58 +00:00
test_autocast.py
test_autograd_fallback.py Fix TestAutogradFallback flaky tests under Dynamo: migrate to lib._destroy() (#159443) 2025-07-30 19:30:55 +00:00
test_autograd.py Clear custom autograd Function ctx.to_save earlier (#161171) 2025-09-02 03:26:31 +00:00
test_autoload.py
test_binary_ufuncs.py [TEST][CUDA] Use proper dtype in test_cuda_tensor_pow_scalar_tensor_cuda (#163070) 2025-09-16 18:28:50 +00:00
test_bundled_images.py Add __main__ guards to tests (#154716) 2025-06-04 14:38:13 +00:00
test_bundled_inputs.py
test_ci_sanity_check_fail.py
test_comparison_utils.py
test_compile_benchmark_util.py
test_complex.py
test_content_store.py [ROCm] Enabling several UTs (#161715) 2025-09-09 15:49:21 +00:00
test_cpp_api_parity.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_cpp_extensions_aot.py [build] modernize build-frontend: python setup.py develop/install -> [uv ]pip install --no-build-isolation [-e ]. (#156027) 2025-07-09 11:24:27 +00:00
test_cpp_extensions_jit.py Add ScalarType -> shim conversion, add stable::Tensor.scalar_type (#160557) 2025-08-19 22:13:47 +00:00
test_cpp_extensions_mtia_backend.py Revert "Generalize poison fork logic for each device backend (#144664)" 2025-04-10 21:02:14 +00:00
test_cpp_extensions_stream_and_event.py [Easy] Add more check for elapsedTime of torch.xxx.Event and torch.Event (#151404) 2025-04-25 20:15:04 +00:00
test_cuda_expandable_segments.py
test_cuda_multigpu.py [ROCm] Enabling several UTs (#161715) 2025-09-09 15:49:21 +00:00
test_cuda_nvml_based_avail.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_cuda_primary_ctx.py [ROCm][CI] skip TestCudaPrimaryCtx.test_set_device_0 (#163693) 2025-09-23 22:15:10 +00:00
test_cuda_sanitizer.py
test_cuda_trace.py
test_cuda.py [ROCm][CI] adjust tf32 tolerance for test_compile_kernel_advanced (#163783) 2025-09-24 19:39:15 +00:00
test_custom_ops.py Add utility to get computed kernel in torch.library (#158393) 2025-08-13 21:00:59 +00:00
test_dataloader.py Skip test_ind_worker_queue on Windows and macOS (flaky) (#162555) 2025-09-10 07:05:14 +00:00
test_datapipe.py [BC breaking] Remove deprecated imports for torch.utils.data.datapipes.iter.grouping (#163438) 2025-09-23 05:02:06 +00:00
test_decomp.py write conv1d decomposition (#163080) 2025-09-17 19:22:38 +00:00
test_determination.py
test_dispatch.py Typo fixes for "overridden" in comments and function names (#155944) 2025-06-14 03:37:38 +00:00
test_dlpack.py Upgrades dlpack to v1.1 to include fp8/fp4 (#162195) 2025-09-17 16:39:11 +00:00
test_dynamic_shapes.py [dynamic shapes] DynamicInts prototype (#162194) 2025-09-18 23:26:28 +00:00
test_expanded_weights.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_extension_utils.py
test_fake_tensor.py [2/n] Support module.to("cuda:0") in FakeTensorMode on cuda-less machine (#163433) 2025-09-22 20:16:32 +00:00
test_file_check.py
test_flop_counter.py PYFMT lint grandfathered files 1 (#154261) 2025-05-25 17:36:14 +00:00
test_foreach.py Fix requires_cuda to requires_cuda_and_triton (#160222) 2025-08-10 07:05:52 +00:00
test_function_schema.py
test_functional_autograd_benchmark.py
test_functional_optim.py
test_functionalization_of_rng_ops.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_functionalization.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_futures.py
test_fx_experimental.py [fx] fix split_module with symint (#160093) 2025-08-13 05:50:15 +00:00
test_fx_passes.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_fx_reinplace_pass.py [dynamic shapes] avoid unnecessary slices (#157528) 2025-07-10 06:34:46 +00:00
test_fx.py [Code Clean] Remove deadcodes about Python3.9 [8/N] (#163728) 2025-09-25 05:12:46 +00:00
test_hop_infra.py
test_hub.py Add __main__ guards to tests (#154716) 2025-06-04 14:38:13 +00:00
test_import_stats.py
test_indexing.py Revert "Fix index_add for int64 input + zerodim index (#161511)" 2025-08-27 15:38:11 +00:00
test_itt.py
test_jit_autocast.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_jit_disabled.py
test_jit_fuser_legacy.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_jit_fuser_te.py Remove tensorexpr tests (#158928) 2025-08-09 02:21:22 +00:00
test_jit_fuser.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_jit_legacy.py Revert "Stop parsing command line arguments every time common_utils is imported. (#156703)" 2025-08-04 20:37:39 +00:00
test_jit_llga_fuser.py
test_jit_profiling.py
test_jit_simple.py
test_jit_string.py
test_jit.py [Code Clean] Remove deadcodes about Python3.9 [8/N] (#163728) 2025-09-25 05:12:46 +00:00
test_jiterator.py
test_kernel_launch_checks.py
test_legacy_vmap.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_license.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_linalg.py Remove test conditions for CUDA<12 (#163495) 2025-09-23 07:52:00 +00:00
test_logging.py
test_masked.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_maskedtensor.py Fix MaskedTensor to device ignored mask (#151205) 2025-07-21 21:44:49 +00:00
test_matmul_cuda.py Skip on sm100 later since Tests are non determinisitic (#163552) 2025-09-23 15:45:05 +00:00
test_meta.py [Inductor] support mixed dtype in the native_layer_norm_backward meta function (#159830) 2025-09-17 20:29:12 +00:00
test_metal.py
test_mkl_verbose.py
test_mkldnn_fusion.py
test_mkldnn_verbose.py
test_mkldnn.py Enable TF32 as fp32 internal precision for matmul/linear/conv (#157520) 2025-07-17 08:57:34 +00:00
test_mobile_optimizer.py
test_model_exports_to_core_aten.py Replace export_for_training with export (#162396) 2025-09-10 14:19:34 +00:00
test_module_tracker.py
test_modules.py [ROCm][CI] unskip some test_memory_format tests (#162766) 2025-09-12 15:02:40 +00:00
test_monitor.py
test_mps.py [MPS] Compute offset2bag/bag_size/max_indices in _embedding_bag (#163281) 2025-09-23 22:30:48 +00:00
test_multiprocessing_spawn.py Test multiprocessing spawn timing fix (#160672) 2025-08-15 00:11:55 +00:00
test_multiprocessing.py Revert "[CI] Use setup-python from for Mac tests (#155698)" 2025-06-12 14:42:32 +00:00
test_namedtensor.py
test_namedtuple_return_api.py
test_native_functions.py
test_native_mha.py
test_nestedtensor.py Add NestedTensor dispatch for _is_any_true/_is_all_true (#162096) 2025-09-22 20:22:44 +00:00
test_nn.py Handling overflow for long int overflow for the product of kernel_hei… (#155989) 2025-09-19 18:15:01 +00:00
test_nnapi.py
test_numa_binding.py [RELAND] Always build USE_DISTRIBUTED (#160449) and Make distributed modules importable even when backend not built (#159889) (#162594) 2025-09-22 21:12:18 +00:00
test_numba_integration.py Fix broken URLs (#152237) 2025-04-27 09:56:42 +00:00
test_numpy_interop.py Throw invalid_argument instead of RuntimeError when parameters exceed… (#158267) 2025-07-25 23:49:46 +00:00
test_opaque_obj.py [opaque_obj] Add set_payload + docs (#163276) 2025-09-22 20:02:29 +00:00
test_openmp.py
test_ops_fwd_gradients.py
test_ops_gradients.py Enable more nightly tests on s390x (#148452) 2025-03-18 16:09:39 +00:00
test_ops_jit.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_ops.py Don't register wrong overload to prim decomp (#163138) 2025-09-18 17:01:19 +00:00
test_optim.py Fix LBFGS wolfe max iteration (#161488) 2025-09-16 12:07:50 +00:00
test_out_dtype_op.py
test_overrides.py Revert "[Reland] Use std::string_view in torchgen (#158625)" 2025-09-13 07:52:50 +00:00
test_package.py
test_per_overload_api.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_prims.py [Bugfix] Match eager stride semantics for cloned tensors with preserve_format in compile (#163017) 2025-09-19 19:41:33 +00:00
test_proxy_tensor.py Revert "[dynamic shapes] unbacked-safe slicing (#157944)" 2025-08-22 20:48:46 +00:00
test_pruning_op.py
test_public_bindings.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_python_dispatch.py Remove guard_size_oblivious from default contiguity python check, and add aten.sym_is_contiguous. [attempt2] (#160869) 2025-09-08 22:59:13 +00:00
test_pytree.py [BE][pytree] cleanup parameterized pytree tests (#160842) 2025-09-05 20:15:29 +00:00
test_quantization.py Remove pytorch quant docs since we are moving to torchao (#157766) 2025-07-11 03:21:47 +00:00
test_reductions.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_rename_privateuse1_to_existing_device.py [Device] Add support for PrivateUse1 device type in parse_type function (#157609) 2025-07-17 01:27:44 +00:00
test_scatter_gather_ops.py fix deterministic scatter_add path for multi-d tensors (#162866) 2025-09-15 06:50:00 +00:00
test_schema_check.py [inductor] slow test some Windows UTs. (#160267) 2025-08-10 18:35:42 +00:00
test_segment_reductions.py [ROCm] Enabling several UTs (#161715) 2025-09-09 15:49:21 +00:00
test_serialization.py added class or module info for functions blocked by weight-only load (#159935) 2025-08-12 20:52:25 +00:00
test_set_default_mobile_cpu_allocator.py
test_shape_ops.py
test_show_pickle.py
test_sort_and_select.py Add dtype checks in meta dispatch for various ordering ops (#159556) 2025-08-14 17:06:27 +00:00
test_sparse_csr.py [ROCm][CI] skip test_sparse_triangular_solve (#163651) 2025-09-23 15:55:51 +00:00
test_sparse_semi_structured.py Remove test conditions for CUDA<12 (#163495) 2025-09-23 07:52:00 +00:00
test_sparse.py Remove C++ and test branches for CUDA<12 (#163443) 2025-09-22 18:20:08 +00:00
test_spectral_ops.py fix numpy compatibility for 2d small list indices (#154806) 2025-06-04 01:58:52 +00:00
test_stateless.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_static_runtime.py
test_subclass.py
test_sympy_utils.py fix pickling for BitwiseFn (#163571) 2025-09-25 04:52:11 +00:00
test_tensor_creation_ops.py Reland #161649, vectorize stored in cat for all dtypes (#162440) 2025-09-18 13:50:44 +00:00
test_tensorboard.py Fix protobuf test comparison by parsing proto instead of raw strings (#162644) 2025-09-12 16:26:54 +00:00
test_tensorexpr_pybind.py
test_tensorexpr.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_testing.py [Fix XPU CI][Inductor UT] Fix test cases broken by community. (#160403) 2025-08-19 00:54:51 +00:00
test_throughput_benchmark.py
test_torch.py [BE] Move indexing tests to test_indexing (#160994) 2025-08-21 00:42:55 +00:00
test_transformers.py [ROCm] Remove HIPBLASLT_ALLOW_TF32 from codebase (#162998) 2025-09-18 13:53:48 +00:00
test_type_hints.py
test_type_info.py
test_type_promotion.py [BE] Raise ValueError from torch.cat meta func (#158249) 2025-07-20 23:49:18 +00:00
test_typing.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_unary_ufuncs.py [BUG] Fix nonzero_static crash on CUDA when the input is a empty tensor (#162578) 2025-09-15 05:44:15 +00:00
test_utils_config_module.py
test_utils_filelock.py
test_utils.py [BE] Remove bottleneck (#163210) 2025-09-18 12:08:13 +00:00
test_view_ops.py unify broadcast_shapes functions and avoid duplicates (#160251) 2025-08-16 00:54:32 +00:00
test_vulkan.py
test_weak.py [BE][2/6] fix typos in test/ (test/test_*.py) (#157636) 2025-07-09 11:02:23 +00:00
test_xnnpack_integration.py
test_xpu.py Add a new API torch.xpu.can_device_access_peer for Intel GPU (#162705) 2025-09-16 18:00:22 +00:00