Repository: pytorch
Issue: Enable UFMT on all files in PyTorch
Labels: module: lint,triaged,better-engineering,actionable

### 🐛 Describe the bug

Right now, around 1.5k files are grandfathered in as "not UFMT'ed" in the PyTorch codebase. Let's make them all formatted!

To format a file, remove its name from `exclude_patterns` of `UFMT` section in `.lintrunner.toml`. Then, run `lintrunner -a --take UFMT --all-files`. You'll end up with a pull request like this: https://github.com/pytorch/pytorch/pull/123061

~~For ease of review, the UFMT diff should be 100% generatable from the `.lintrunner.toml` change. In particular, if you are code reviewing a UFMT change from an untrusted source, clear all changes except the `.lintrunner.toml` and manually rerun `lintrunner` to regenerate the changes. To verify that there are no changes, a maintainer can use this script:~~ You may need to make fixup changes. This means maintainers MUST verify the additional changes on top of lint. This can be done with this script:

```
#!/bin/bash
set -ex
gh pr checkout $1
REV=$(git rev-parse HEAD)
git fetch origin
git checkout $(git merge-base origin/main HEAD)
git checkout $REV .lintrunner.toml
lintrunner init
lintrunner -a --take UFMT --all-files
git diff $REV
```

In some cases, preparatory work needs to be done.  Here are some known problems:

* torch/cuda/_memory_viz.py - mypy type: ignore annotations get moved in such a way that an ignore is no longer applied to the correct line
* test/functorch/test_control_flow.py - flake8: reformatting moves flake8 ignore to the wrong line, and now a multiline string no longer has flake8 suppressed
* torch/masked/ - the import reordering causes an import cycle
* ... probably more ...

These problems should be resolved in preparatory commits BEFORE the UFMT commit, for ease of review.

How to split the work? I decided to bundle things up by directory, so long as there were not too many changes in one directory that necessitated splitting it up. With https://gist.github.com/ezyang/6c10a63d5795cbd7787beeea94497948 I did a sweep to find an optimal threshold:

<img width="679" alt="image" src="https://github.com/pytorch/pytorch/assets/13564/3d9a1774-2b70-4dfd-8999-3bd2c48e1ec0">

13k seems like a good splitting point. Here is the worklist:

- [ ] `    7 - test/_test_bazel.py`
- [x] ` 1831 - test/ao`
- [x] `  635 - test/autograd`
- [x] `   33 - test/backends`
- [ ] `  231 - test/benchmark_utils`
- [ ] `    9 - test/bottleneck_test`
- [ ] `   49 - test/conftest.py`
- [ ] `  127 - test/cpp`
- [x] `  660 - test/cpp_api_parity`
- [x] `  108 - test/cpp_extensions`
- [x] `    4 - test/create_dummy_torchscript_model.py`
- [x] `   14 - test/custom_backend`
- [x] `   32 - test/custom_operator`
- [ ] ` 6342 - test/distributed` https://github.com/pytorch/pytorch/pull/123539
- [x] ` 5766 - test/distributions` https://github.com/pytorch/pytorch/pull/123527
- [x] `   91 - test/error_messages` https://github.com/pytorch/pytorch/pull/123527
- [ ] `  837 - test/export` https://github.com/pytorch/pytorch/pull/123520 https://github.com/pytorch/pytorch/pull/123527
- [x] `   40 - test/forward_backward_compatibility` https://github.com/pytorch/pytorch/pull/123527
- [ ] `11846 - test/functorch` https://github.com/pytorch/pytorch/pull/123541 BLOCKED
- [ ] ` 1784 - test/fx` https://github.com/pytorch/pytorch/issues/123062
- [x] ` 9432 - test/jit` https://github.com/pytorch/pytorch/pull/123623
- [ ] `   59 - test/jit_hooks`
- [ ] `  348 - test/lazy`
- [ ] `    3 - test/linear.py`
- [ ] `    3 - test/load_torchscript_model.py`
- [ ] `    5 - test/mkl_verbose.py`
- [ ] `    6 - test/mkldnn_verbose.py`
- [x] `  665 - test/mobile` https://github.com/pytorch/pytorch/pull/123521
- [ ] ` 7397 - test/nn`
- [ ] `    1 - test/onnx_caffe2` https://github.com/pytorch/pytorch/pull/123901
- [ ] `  346 - test/optim` https://github.com/pytorch/pytorch/pull/123901
- [ ] `   41 - test/package` https://github.com/pytorch/pytorch/pull/123901
- [ ] ` 1019 - test/profiler` https://github.com/pytorch/pytorch/pull/123901
- [ ] `  875 - test/quantization/ao_migration` https://github.com/pytorch/pytorch/pull/123994
- [ ] `  339 - test/quantization/bc`
- [ ] `12315 - test/quantization/core`
- [ ] ` 2321 - test/quantization/eager`
- [ ] ` 7054 - test/quantization/fx`
- [ ] `  544 - test/quantization/jit`
- [ ] `  697 - test/quantization/pt2e`
- [ ] `  130 - test/scripts`
- [ ] `   42 - test/simulate_nccl_errors.py`
- [ ] `   63 - test/test_ao_sparsity.py`
- [ ] `  134 - test/test_autocast.py`
- [ ] ` 2917 - test/test_autograd.py`
- [ ] `  204 - test/test_binary_ufuncs.py`
- [ ] `   16 - test/test_bundled_images.py`
- [ ] `  114 - test/test_bundled_inputs.py`
- [ ] `    5 - test/test_comparison_utils.py`
- [ ] `   33 - test/test_compile_benchmark_util.py`
- [ ] `  381 - test/test_complex.py`
- [ ] `   55 - test/test_cpp_api_parity.py`
- [ ] `   57 - test/test_cpp_extensions_aot.py`
- [ ] `  166 - test/test_cpp_extensions_jit.py`
- [ ] `  190 - test/test_cpp_extensions_open_device_registration.py`
- [ ] ` 1811 - test/test_cuda.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `    7 - test/test_cuda_expandable_segments.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `  423 - test/test_cuda_multigpu.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `   94 - test/test_cuda_nvml_based_avail.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `   41 - test/test_cuda_primary_ctx.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `    4 - test/test_cuda_sanitizer.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] `    2 - test/test_cuda_trace.py` https://github.com/pytorch/pytorch/pull/124352
- [ ] ` 1468 - test/test_dataloader.py`
- [ ] ` 1431 - test/test_datapipe.py`
- [ ] `  468 - test/test_decomp.py`
- [ ] `    3 - test/test_deploy.py`
- [ ] `   17 - test/test_determination.py`
- [x] `  743 - test/test_dispatch.py` https://github.com/pytorch/pytorch/pull/123644
- [ ] `   68 - test/test_dlpack.py`
- [ ] `  362 - test/test_dynamic_shapes.py`
- [ ] `  713 - test/test_expanded_weights.py`
- [ ] `  359 - test/test_fake_tensor.py`
- [ ] `  200 - test/test_flop_counter.py`
- [x] `  741 - test/test_foreach.py` https://github.com/pytorch/pytorch/pull/123718
- [ ] `  252 - test/test_function_schema.py`
- [ ] `   57 - test/test_functional_autograd_benchmark.py`
- [ ] `   24 - test/test_functional_optim.py`
- [ ] `  846 - test/test_functionalization.py` https://github.com/pytorch/pytorch/pull/123926
- [ ] `   51 - test/test_functionalization_of_rng_ops.py`
- [ ] `   36 - test/test_futures.py`
- [ ] ` 1435 - test/test_fx.py`
- [ ] `  130 - test/test_fx_experimental.py`
- [ ] `  410 - test/test_fx_passes.py`
- [ ] `   91 - test/test_fx_reinplace_pass.py`
- [ ] `  179 - test/test_hub.py`
- [ ] `    2 - test/test_import_stats.py`
- [ ] `  893 - test/test_indexing.py`
- [ ] `    9 - test/test_itt.py`
- [ ] ` 5290 - test/test_jit.py`
- [ ] `  182 - test/test_jit_autocast.py`
- [ ] `   20 - test/test_jit_disabled.py`
- [ ] `  530 - test/test_jit_fuser.py`
- [ ] `    3 - test/test_jit_fuser_legacy.py`
- [ ] `  891 - test/test_jit_fuser_te.py`
- [ ] `    3 - test/test_jit_legacy.py`
- [ ] `  343 - test/test_jit_llga_fuser.py`
- [ ] `    3 - test/test_jit_profiling.py`
- [ ] `    3 - test/test_jit_simple.py`
- [ ] `  229 - test/test_jit_string.py`
- [ ] `   87 - test/test_jiterator.py`
- [ ] `   34 - test/test_kernel_launch_checks.py`
- [x] `  904 - test/test_legacy_vmap.py`
- [ ] `   28 - test/test_license.py`
- [ ] ` 4375 - test/test_linalg.py`
- [ ] `    8 - test/test_logging.py`
- [ ] `  289 - test/test_masked.py`
- [ ] `  153 - test/test_maskedtensor.py`
- [ ] `  151 - test/test_matmul_cuda.py`
- [ ] ` 1041 - test/test_meta.py`
- [ ] `  150 - test/test_metal.py`
- [ ] `   36 - test/test_mkl_verbose.py`
- [ ] `  827 - test/test_mkldnn.py`
- [ ] `  298 - test/test_mkldnn_fusion.py`
- [ ] `   36 - test/test_mkldnn_verbose.py`
- [ ] `  336 - test/test_mobile_optimizer.py`
- [ ] `   70 - test/test_model_dump.py`
- [ ] `  767 - test/test_modules.py`
- [ ] `   29 - test/test_monitor.py`
- [ ] ` 8262 - test/test_mps.py`
- [x] `  442 - test/test_multiprocessing.py` https://github.com/pytorch/pytorch/pull/123840
- [ ] `   59 - test/test_multiprocessing_spawn.py`
- [ ] ` 1990 - test/test_namedtensor.py`
- [ ] `  393 - test/test_namedtuple_return_api.py`
- [ ] `   45 - test/test_native_functions.py`
- [ ] `  138 - test/test_native_mha.py`
- [ ] ` 1951 - test/test_nestedtensor.py`
- [ ] ` 9457 - test/test_nn.py`
- [ ] `  244 - test/test_nnapi.py`
- [ ] `   74 - test/test_numba_integration.py`
- [ ] `  179 - test/test_numpy_interop.py`
- [ ] `   20 - test/test_openmp.py`
- [x] `  755 - test/test_ops.py` https://github.com/pytorch/pytorch/pull/123935
- [x] `   71 - test/test_ops_fwd_gradients.py`
- [x] `   42 - test/test_ops_gradients.py`
- [x] `  190 - test/test_ops_jit.py`
- [ ] `  442 - test/test_optim.py`
- [ ] `  115 - test/test_out_dtype_op.py`
- [ ] `  226 - test/test_overrides.py`
- [ ] `   29 - test/test_package.py`
- [ ] `   27 - test/test_per_overload_api.py`
- [ ] `  132 - test/test_prims.py`
- [ ] ` 1016 - test/test_proxy_tensor.py`
- [ ] `   80 - test/test_pruning_op.py`
- [ ] `  159 - test/test_public_bindings.py`
- [x] `  737 - test/test_python_dispatch.py`
- [ ] `  234 - test/test_quantization.py`
- [ ] ` 2343 - test/test_reductions.py`
- [ ] `  296 - test/test_scatter_gather_ops.py`
- [ ] `  209 - test/test_schema_check.py`
- [ ] `  206 - test/test_segment_reductions.py`
- [ ] ` 4013 - test/test_serialization.py`
- [ ] `    6 - test/test_set_default_mobile_cpu_allocator.py`
- [ ] `  239 - test/test_shape_ops.py`
- [ ] `   13 - test/test_show_pickle.py`
- [ ] `  646 - test/test_sort_and_select.py`
- [ ] ` 3657 - test/test_sparse.py`
- [ ] ` 3338 - test/test_sparse_csr.py`
- [ ] `  234 - test/test_sparse_semi_structured.py`
- [ ] ` 1132 - test/test_spectral_ops.py`
- [ ] `  713 - test/test_stateless.py`
- [ ] `   44 - test/test_static_runtime.py`
- [ ] `   65 - test/test_subclass.py`
- [ ] `  101 - test/test_sympy_utils.py`
- [ ] ` 2166 - test/test_tensor_creation_ops.py`
- [ ] `  580 - test/test_tensorboard.py`
- [ ] `  208 - test/test_tensorexpr.py`
- [ ] `    5 - test/test_tensorexpr_pybind.py`
- [ ] ` 1189 - test/test_testing.py`
- [ ] `    8 - test/test_throughput_benchmark.py`
- [ ] ` 4817 - test/test_torch.py`
- [ ] ` 3285 - test/test_transformers.py`
- [ ] `   36 - test/test_type_hints.py`
- [ ] `   50 - test/test_type_info.py`
- [ ] `  606 - test/test_type_promotion.py`
- [ ] `  100 - test/test_unary_ufuncs.py`
- [ ] `  458 - test/test_utils.py`
- [ ] `  551 - test/test_view_ops.py`
- [ ] `  155 - test/test_vulkan.py`
- [ ] ` 1365 - test/test_xnnpack_integration.py`
- [ ] `  175 - test/typing`
- [ ] `    6 - torch/_awaits`
- [ ] `  315 - torch/_custom_op`
- [ ] `  541 - torch/_export`
- [x] ` 2427 - torch/_functorch` #123723
- [ ] `   87 - torch/_higher_order_ops`
- [ ] `    2 - torch/_vendor`
- [x] `    2 - torch/ao/__init__.py`
- [x] ` 7593 - torch/ao/nn`
- [x] ` 2134 - torch/ao/ns`
- [x] ` 1442 - torch/ao/pruning`
- [x] `10772 - torch/ao/quantization`
- [ ] `   14 - torch/compiler`
- [ ] `   76 - torch/contrib`
- [ ] `  503 - torch/cuda`
- [x] ` 4071 - torch/distributed`
- [ ] `  233 - torch/fft`
- [ ] `   17 - torch/func`
- [ ] `  430 - torch/functional.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `   22 - torch/futures`
- [ ] ` 7711 - torch/fx`
- [ ] `  262 - torch/hub.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `  133 - torch/library.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `  395 - torch/linalg`
- [x] `  337 - torch/masked`
- [ ] `    1 - torch/monitor`
- [ ] `  291 - torch/nested`
- [x] `10425 - torch/nn`
- [x] ` 3774 - torch/optim`
- [ ] `  476 - torch/overrides.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `   84 - torch/quasirandom.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `   51 - torch/random.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `    9 - torch/return_types.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `  729 - torch/serialization.py` https://github.com/pytorch/pytorch/pull/127707
- [ ] `  507 - torch/signal`
- [ ] ` 1190 - torch/sparse`
- [ ] `  904 - torch/special`
- [ ] `  675 - torch/storage.py` https://github.com/pytorch/pytorch/pull/127706
- [ ] `  640 - torch/testing/_internal/autocast_test_lists.py`
- [ ] `  103 - torch/testing/_internal/autograd_function_db.py`
- [ ] `   32 - torch/testing/_internal/check_kernel_launches.py`
- [ ] `  173 - torch/testing/_internal/common_cuda.py`
- [ ] `  614 - torch/testing/_internal/common_device_type.py`
- [ ] `  124 - torch/testing/_internal/common_distributed.py`
- [ ] `   81 - torch/testing/_internal/common_dtype.py`
- [ ] `  251 - torch/testing/_internal/common_jit.py`
- [ ] ` 9191 - torch/testing/_internal/common_modules.py`
- [ ] ` 2889 - torch/testing/_internal/common_nn.py`
- [ ] `   17 - torch/testing/_internal/common_pruning.py`
- [ ] ` 1072 - torch/testing/_internal/common_quantization.py`
- [ ] `  101 - torch/testing/_internal/common_quantized.py`
- [ ] `   68 - torch/testing/_internal/common_subclass.py`
- [ ] ` 2289 - torch/testing/_internal/common_utils.py`
- [ ] `  327 - torch/testing/_internal/composite_compliance.py`
- [ ] `  259 - torch/testing/_internal/custom_op_db.py`
- [ ] `    2 - torch/testing/_internal/data`
- [ ] `    9 - torch/testing/_internal/dist_utils.py`
- [ ] ` 2052 - torch/testing/_internal/distributed`
- [ ] `   40 - torch/testing/_internal/hop_db.py`
- [ ] `  160 - torch/testing/_internal/hypothesis_utils.py`
- [ ] `   24 - torch/testing/_internal/inductor_utils.py`
- [ ] ` 1215 - torch/testing/_internal/jit_metaprogramming_utils.py`
- [ ] `  361 - torch/testing/_internal/jit_utils.py`
- [ ] `   88 - torch/testing/_internal/logging_tensor.py`
- [ ] `   32 - torch/testing/_internal/logging_utils.py`
- [ ] `  100 - torch/testing/_internal/optests`
- [ ] `    1 - torch/testing/_internal/quantization_torch_package_models.py`
- [ ] `    1 - torch/testing/_internal/test_module`
- [x] `   14 - torch/torch_version.py` https://github.com/pytorch/pytorch/pull/123131
- [x] `   27 - torch/types.py` https://github.com/pytorch/pytorch/pull/123131
- [ ] ` 9910 - torch/utils`
- [ ] `   12 - torch/xpu`

This list does not cover torch/testing/_internal/common_methods_invocations.py, which has a whopping 35341 lines of changes and will likely have to be handled in some other way (e.g., splitting it first).

To claim an entry, put your name (and eventually PR) next to it by editing this post. Feel free to bundle multiple small but related changes together if necessary.

### Versions

main