[Reland 1] Cleanup header inclusions in torch_cpu by iwyu (#112311)

Reland https://github.com/pytorch/pytorch/pull/101178 to use IWYU on torch_cpu. The header file changes are excluded to avoid breaking internal jobs.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/112311
Approved by: https://github.com/ezyang
This commit is contained in:
cyy 2023-11-19 04:06:33 +00:00 committed by PyTorch MergeBot
parent 68ab458fe3
commit bae61ecb96
58 changed files with 74 additions and 102 deletions

View File

@ -1,15 +1,15 @@
#include <ATen/Dispatch.h>
#if defined ENABLE_RECORD_KERNEL_FUNCTION_DTYPE
#include <ATen/record_function.h>
namespace at::detail {
#if defined ENABLE_RECORD_KERNEL_FUNCTION_DTYPE
void record_kernel_function_dtype(std::string name) {
RECORD_FUNCTION_WITH_SCOPE(
at::RecordScope::KERNEL_FUNCTION_DTYPE,
std::move(name),
c10::ArrayRef<const c10::IValue>{});
}
#endif
} // namespace at::detail
#endif

View File

@ -1,9 +1,9 @@
#include <torch/library.h>
#include <ATen/RedispatchFunctions.h>
#include <ATen/ATen.h>
#include <ATen/LegacyVmapTransforms.h>
#include <ATen/LegacyBatchedFallback.h>
#include <ATen/RedispatchFunctions.h>
#include <ATen/native/ResizeCommon.h>
#include <ATen/ATen.h>
#include <ATen/core/IListRef.h>
#include <c10/util/irange.h>
#include <c10/core/SymIntArrayRef.h>

View File

@ -8,11 +8,9 @@
#endif
#include <c10/core/CPUAllocator.h>
#include <c10/util/Unicode.h>
/* stuff for mapped files */
#ifdef _WIN32
#include <c10/util/win32-headers.h>
#include <c10/util/Unicode.h>
#endif
#if defined(HAVE_MMAP)

View File

@ -1,7 +1,6 @@
#include <ATen/ATen.h>
#include <ATen/NamedTensorUtils.h>
#include <ATen/WrapDimUtils.h>
#include <ATen/core/op_registration/op_registration.h>
#include <ATen/NestedTensorImpl.h>
#include <c10/core/DispatchKey.h>
#include <c10/core/DispatchKeySet.h>

View File

@ -22,6 +22,7 @@
#endif
#include <c10/util/irange.h>
#include <c10/util/string_utils.h>
#include <c10/util/SmallBuffer.h>
#include <array>

View File

@ -5,8 +5,8 @@
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#include <ATen/Operators.h>
#include <ATen/FunctionalTensorWrapper.h>
#include <ATen/Operators.h>
#include <ATen/core/dispatch/Dispatcher.h>
#include <ATen/functorch/BatchRulesHelper.h>
#include <ATen/functorch/BatchedFallback.h>

View File

@ -79,7 +79,6 @@
#include <ATen/ops/zeros_like.h>
#include <utility>
#include <vector>
#endif
namespace at {

View File

@ -7,7 +7,6 @@
#include <c10/util/irange.h>
#include <algorithm>
#include <climits>
#include <iostream>
#include <limits>
namespace {

View File

@ -5,7 +5,6 @@
#include <ATen/Config.h>
#include <c10/util/SmallBuffer.h>
#include <c10/util/C++17.h>
#include <c10/util/irange.h>
#include <climits>

View File

@ -5,7 +5,6 @@
#endif
#include <c10/util/Exception.h>
#include <ATen/native/TensorTransformations.h>
#include <ATen/native/cpu/ChannelShuffleKernel.h>
#ifndef AT_PER_OPERATOR_HEADERS

View File

@ -1,7 +1,6 @@
#include <ATen/core/TensorBase.h>
#include <algorithm>
#include <vector>
#include <ATen/core/TensorBody.h>
#include <algorithm>
#include <c10/util/OptionalArrayRef.h>
#ifdef AT_PER_OPERATOR_HEADERS

View File

@ -11,7 +11,6 @@
#else
#include <ATen/ops/max_pool2d_with_indices_backward_native.h>
#include <ATen/ops/max_pool2d_with_indices_native.h>
#include <ATen/ops/zeros_like_ops.h>
#endif
namespace at {

View File

@ -12,7 +12,6 @@
#include <ATen/ops/empty.h>
#include <ATen/ops/max_pool3d_with_indices_backward_native.h>
#include <ATen/ops/max_pool3d_with_indices_native.h>
#include <ATen/ops/zeros_like.h>
#endif
namespace at {

View File

@ -1,8 +1,8 @@
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/native/DispatchStub.h>
#include <c10/core/DeviceType.h>
#include <c10/util/Exception.h>
#include <c10/macros/Macros.h>
#if !defined(__s390x__) && !defined(__powerpc__)
#include <cpuinfo.h>

View File

@ -58,7 +58,6 @@
#include <ATen/ops/maximum.h>
#include <ATen/ops/minimum.h>
#include <ATen/ops/pow.h>
#include <ATen/ops/zeros_like_ops.h>
#endif
namespace at::native {

View File

@ -13,7 +13,6 @@
#include <ATen/ops/full.h>
#include <ATen/ops/meshgrid.h>
#include <ATen/ops/stack.h>
#include <ATen/ops/zeros_like_ops.h>
#endif
#include <vector>

View File

@ -4,7 +4,6 @@
#include <ATen/native/xnnpack/Engine.h>
#include <ATen/WrapDimUtilsMulti.h>
#include <ATen/TensorOperators.h>
#include <ATen/native/xnnpack/Engine.h>
#include <c10/util/irange.h>
#include <c10/core/SymInt.h>
#include <c10/util/MaybeOwned.h>
@ -28,11 +27,9 @@
#include <ATen/ops/mul.h>
#include <ATen/ops/tensordot_native.h>
#include <ATen/ops/zeros.h>
#include <ATen/ops/zeros_like_ops.h>
#endif
#include <cctype>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

View File

@ -3,7 +3,6 @@
#include <ATen/core/Tensor.h>
#include <ATen/ScalarOps.h>
#include <ATen/native/Resize.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>

View File

@ -116,8 +116,10 @@
#include <ATen/ops/masked_scatter_native.h>
#include <ATen/ops/masked_select_backward_native.h>
#include <ATen/ops/masked_select_native.h>
#include <ATen/ops/nested_to_padded_tensor_native.h>
#include <ATen/ops/nonzero_native.h>
#include <ATen/ops/nonzero_numpy_native.h>
#include <ATen/ops/nonzero_static_native.h>
#include <ATen/ops/ones_like.h>
#include <ATen/ops/put_native.h>
#include <ATen/ops/quantize_per_tensor.h>

View File

@ -20,6 +20,7 @@
#include <ATen/ops/_sparse_bsc_tensor_unsafe_native.h>
#include <ATen/ops/_sparse_bsr_tensor_unsafe_native.h>
#include <ATen/ops/_sparse_compressed_tensor_unsafe_native.h>
#include <ATen/ops/_sparse_coo_tensor_with_dims_native.h>
#include <ATen/ops/_sparse_coo_tensor_unsafe_native.h>
#include <ATen/ops/_sparse_csc_tensor_unsafe_native.h>
#include <ATen/ops/_sparse_csr_tensor_unsafe_native.h>
@ -27,6 +28,11 @@
#include <ATen/ops/_to_copy_native.h>
#include <ATen/ops/_to_cpu_native.h>
#include <ATen/ops/_to_dense_native.h>
#include <ATen/ops/_to_sparse_bsc_native.h>
#include <ATen/ops/_to_sparse_bsr_native.h>
#include <ATen/ops/_to_sparse_csc_native.h>
#include <ATen/ops/_to_sparse_csr_native.h>
#include <ATen/ops/_to_sparse_native.h>
#include <ATen/ops/arange_native.h>
#include <ATen/ops/empty.h>
#include <ATen/ops/empty_like.h>

View File

@ -1,7 +1,7 @@
#include <ATen/native/mkldnn/ConvPrepack.h>
#include <ATen/native/mkldnn/OpContext.h>
#if AT_MKLDNN_ENABLED()
#include <ATen/native/mkldnn/ConvPrepack.h>
namespace at {
namespace native {

View File

@ -15,6 +15,8 @@
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/mkldnn_convolution_native.h>
#include <ATen/ops/mkldnn_rnn_layer_backward_native.h>
#include <ATen/ops/mkldnn_rnn_layer_native.h>
#endif
#if !AT_MKLDNN_ENABLED()

View File

@ -1,12 +1,14 @@
#include <ATen/Tensor.h>
#include <ATen/native/mkldnn/ConvPrepack.h>
#include <ATen/native/mkldnn/OpContext.h>
#include <torch/custom_class.h>
#include <torch/library.h>
#include <ATen/Config.h>
#if AT_MKLDNN_ENABLED()
#include <ATen/Tensor.h>
#include <ATen/native/mkldnn/ConvPrepack.h>
#include <ATen/native/mkldnn/OpContext.h>
#include <ATen/native/mkldnn/Utils.h>
#include <torch/custom_class.h>
#include <torch/library.h>
namespace at {
namespace native {
namespace mkldnn {

View File

@ -1,7 +1,6 @@
#define TORCH_ASSERT_ONLY_METHOD_OPERATORS
#include <ATen/Config.h>
#include <ATen/InferSize.h>
#include <ATen/WrapDimUtils.h>
#include <ATen/core/Tensor.h>
#include <c10/core/SymIntArrayRef.h>

View File

@ -10,7 +10,8 @@
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/_nested_from_padded.h>
#include <ATen/ops/_nested_from_padded_native.h>
#include <ATen/ops/narrow_native.h>
#endif
#include <ATen/native/NonSymbolicBC.h>

View File

@ -35,6 +35,7 @@
#endif
#include <c10/util/irange.h>
#include <c10/util/string_utils.h>
namespace {
// To have a sanity check for maximum matrix size.

View File

@ -1,9 +1,5 @@
#include <torch/library.h>
#include <ATen/native/quantized/PackedParams.h>
#include <ATen/native/quantized/cpu/EmbeddingPackedParams.h>
#include <torch/custom_class.h>
int register_linear_params();
template <int kSpatialDim = 2>

View File

@ -15,7 +15,6 @@
#include <c10/core/SymInt.h>
#include <c10/core/SymIntArrayRef.h>
#include <c10/util/Logging.h>
#include <c10/util/Exception.h>
#include <c10/core/DispatchKey.h>
#include <c10/core/DispatchKeySet.h>

View File

@ -50,7 +50,7 @@
#include <ATen/ops/scalar_tensor.h>
#include <ATen/ops/scaled_dot_product_attention.h>
#include <ATen/ops/split_native.h>
#include <ATen/ops/narrow_native.h>
#endif
#include <c10/cuda/CUDAMathCompat.h>

View File

@ -2,7 +2,6 @@
#include <ATen/core/Tensor.h>
#include <ATen/Dispatch.h>
#include <ATen/NestedTensorImpl.h>
#include <c10/util/Exception.h>
#include <torch/library.h>

View File

@ -7,14 +7,12 @@
#include <ATen/native/quantized/AffineQuantizer.h>
#include <ATen/native/TensorFactories.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Parallel.h>
#include <ATen/quantized/QTensorImpl.h>
#include <ATen/quantized/Quantizer.h>
#include <c10/core/CPUAllocator.h>
#include <c10/util/accumulate.h>
#include <cmath>
#include <typeinfo>
#include <utility>
namespace at {

View File

@ -146,7 +146,7 @@ std::string Device::str() const {
std::string str = DeviceTypeName(type(), /* lower case */ true);
if (has_index()) {
str.push_back(':');
str.append(to_string(index()));
str.append(std::to_string(index()));
}
return str;
}

View File

@ -1,8 +1,5 @@
#include <c10/core/ConstantSymNodeImpl.h>
#include <c10/core/SymBool.h>
#include <c10/core/SymNodeImpl.h>
#include <array>
#include <utility>
namespace c10 {

View File

@ -2,8 +2,6 @@
#include <c10/util/Logging.h>
#include <c10/util/Type.h>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <utility>

View File

@ -7,9 +7,7 @@
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <numeric>
// Common code that we use regardless of whether we use glog or not.

View File

@ -1,6 +1,5 @@
#include <c10/util/StringUtil.h>
#include <cstring>
#include <string>
namespace c10 {

View File

@ -1,11 +1,11 @@
#include <c10/util/Type.h>
#if HAS_DEMANGLE
#include <cstdlib>
#include <functional>
#include <memory>
#if HAS_DEMANGLE
#include <cxxabi.h>
namespace c10 {

View File

@ -1,6 +1,8 @@
#include <c10/util/Type.h>
#if HAS_DEMANGLE == 0
#include <string>
namespace c10 {
std::string demangle(const char* name) {
return std::string(name);

View File

@ -850,6 +850,25 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND NOT USE_VULKAN AND NOT USE_IOS
endforeach()
endif()
option(TORCH_USE_IWYU "Use include-what-you-use to clean up header inclusion" OFF)
if(TORCH_USE_IWYU)
find_program(iwyu NAMES include-what-you-use)
if(iwyu)
set(iwyu_cmd
"include-what-you-use"
"-Xiwyu"
"--transitive_includes_only"
"-Xiwyu"
"--no_fwd_decls"
"-Xiwyu"
"--prefix_header_includes=keep"
"-Xiwyu"
"--mapping_file=${CMAKE_CURRENT_LIST_DIR}/../tools/iwyu/all.imp"
)
set_property(TARGET torch_cpu PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_cmd})
endif()
endif()
set_property(SOURCE ${ATen_CORE_SRCS} APPEND
PROPERTY COMPILE_DEFINITIONS "TORCH_ASSERT_ONLY_METHOD_OPERATORS")
set_property(SOURCE ${ATen_ATTENTION_KERNEL_SRCS} APPEND

View File

@ -2,7 +2,6 @@
{ ref: "system.imp" },
{ ref: "c10.imp" },
{ ref: "aten.imp" },
{ ref: "qnnpack.imp" },
{ ref: "torch.imp" },
{ ref: "gtest.imp" },
]

View File

@ -14,31 +14,12 @@
{ include: [ "<c10/util/logging_is_google_glog.h>", private, "<c10/util/Logging.h>", public ] },
{ include: [ "<c10/util/logging_is_not_google_glog.h>", private, "<c10/util/Logging.h>", public ] },
{ include: [ "<c10/util/qint32.h>", public, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/qint8.h>", public, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint2x4.h>", public, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint4x4.h>", public, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint8.h>", public, "<c10/core/ScalarType.h>", public ] },
{ include: ["<c10/core/Backend.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/Device.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/DefaultDtype.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/Layout.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/MemoryFormat.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/ScalarType.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/util/Optional.h>", public, "<c10/core/TensorOptions.h>", public] },
{ include: ["<c10/core/Allocator.h>", public, "<c10/core/StorageImpl.h>", public] },
{ include: ["<c10/core/Device.h>", public, "<c10/core/StorageImpl.h>", public] },
{ include: ["<c10/core/ScalarType.h>", public, "<c10/core/StorageImpl.h>", public] },
{ include: ["<c10/core/StorageImpl.h>", public, "<c10/core/Storage.h>", public] },
{ include: ["<c10/util/ArrayRef.h>", public, "<c10/core/TensorImpl.h>", public] },
{ include: ["<c10/core/DispatchKeySet.h>", public, "<c10/core/TensorImpl.h>", public] },
{ include: ["<c10/core/Layout.h>", public, "<c10/core/TensorImpl.h>", public] },
{ include: ["<c10/core/Storage.h>", public, "<c10/core/TensorImpl.h>", public] },
{ include: ["<c10/core/MemoryFormat.h>", public, "<c10/core/TensorImpl.h>", public] },
{ include: [ "<c10/util/qint32.h>", private, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/qint8.h>", private, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint2x4.h>", private, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint4x4.h>", private, "<c10/core/ScalarType.h>", public ] },
{ include: [ "<c10/util/quint8.h>", private, "<c10/core/ScalarType.h>", public ] },
{ include: ["<c10/core/StorageImpl.h>", private, "<c10/core/Storage.h>", public ] },
{ symbol: [ "C10_API", private, "<c10/macros/Export.h>", public ] },
{ symbol: [ "c10::complex", private, "<c10/util/complex.h>", public ] },
]

View File

@ -1,2 +0,0 @@
[
]

View File

@ -1,14 +1,8 @@
# Toolchain-specific mappings. These ones seem to be missing from iwyu defaults
[
{ include: [ "<bits/struct_stat.h>", private, "<sys/stat.h>", public ] },
{ include: [ "<bits/stdint-uintn.h>", private, "<cstdint>", public ] },
{ include: [ "<bits/stdint-intn.h>", private, "<cstdint>", public ] },
{ include: [ "<bits/pthreadtypes.h>", private, "<pthread.h>", public ] },
{ include: [ "<bits/exception.h>", private, "<stdexcept>", public ] },
{ include: [ "<bits/refwrap.h>", private, "<reference_wrapper>", public ] },
{ include: [ "<bits/shared_ptr.h>", private, "<memory>", public ] },
{ include: [ "<bits/std_function.h>", private, "<functional>", public ] },
{ symbol: [ "size_t", private, "<cstddef>", public ] },
{ symbol: [ "ptrdiff_t", private, "<cstddef>", public ] },
]

View File

@ -1,7 +1,6 @@
#include <torch/optim/optimizer.h>
#include <torch/csrc/autograd/generated/variable_factories.h>
#include <torch/ordered_dict.h>
#include <torch/types.h>
#include <string>

View File

@ -1,7 +1,6 @@
#include <torch/optim/sgd.h>
#include <torch/csrc/autograd/variable.h>
#include <torch/nn/pimpl.h>
#include <torch/optim/optimizer.h>
#include <torch/optim/serialize.h>
#include <torch/types.h>

View File

@ -9,8 +9,6 @@
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
namespace torch {

View File

@ -4,7 +4,6 @@
#include <torch/csrc/jit/frontend/ir_emitter.h>
#include <torch/csrc/jit/frontend/mini_environment.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/ir_views.h>
namespace torch::jit {

View File

@ -1,6 +1,5 @@
#include <torch/csrc/jit/frontend/error_report.h>
#include <c10/util/Optional.h>
#include <torch/csrc/jit/frontend/tree.h>
namespace torch::jit {

View File

@ -2,10 +2,8 @@
#include <ATen/core/jit_type.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/frontend/error_report.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/ir/ir_views.h>
#include <torch/csrc/jit/passes/dead_code_elimination.h>
#include <torch/csrc/jit/runtime/graph_iterator.h>
namespace torch::jit {

View File

@ -15,7 +15,6 @@
#include <torch/csrc/jit/api/compilation_unit.h>
#include <torch/csrc/jit/mobile/file_format.h>
#include <torch/csrc/jit/mobile/flatbuffer_loader.h>
#include <torch/csrc/jit/mobile/interpreter.h>
#include <torch/csrc/jit/mobile/observer.h>
#include <torch/csrc/jit/mobile/type_parser.h>
#include <torch/csrc/jit/mobile/upgrader_mobile.h>
@ -24,8 +23,6 @@
#include <torch/csrc/jit/serialization/import_export_functions.h>
#include <torch/csrc/jit/serialization/import_read.h>
#include <torch/custom_class.h>
#include <exception>
#include <fstream>
#include <string>
#include <vector>

View File

@ -7,7 +7,6 @@
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/device_type_analysis.h>
#include <torch/csrc/jit/passes/shape_analysis.h>
#include <torch/library.h>
#include <memory>
#include <utility>

View File

@ -1,5 +1,6 @@
#include <ATen/ATen.h>
#include <ATen/Config.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Utils.h>
#include <ATen/core/symbol.h>
#include <ATen/native/layer_norm.h>

View File

@ -13,7 +13,6 @@
#include <torch/csrc/jit/passes/remove_mutation.h>
#include <memory>
#include <regex>
#include <stack>
#include <string>
#include <utility>

View File

@ -2,10 +2,8 @@
#include <ATen/core/jit_type.h>
#include <c10/util/irange.h>
#include <torch/csrc/autograd/autograd.h>
#include <torch/csrc/jit/ir/constants.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/runtime/operator.h>
#include <vector>

View File

@ -2,6 +2,7 @@
#include <torch/library.h>
#include <ATen/ExpandUtils.h>
#include <ATen/NativeFunctions.h>
#include <ATen/core/jit_type.h>
#include <c10/core/DefaultDtype.h>
#include <c10/util/irange.h>

View File

@ -16,6 +16,14 @@
#include <torch/csrc/jit/tensorexpr/ir_simplifier.h>
#include <torch/csrc/jit/tensorexpr/registerizer.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/NativeFunctions.h>
#else
#include <ATen/ops/empty_strided_native.h>
#endif
#include <unordered_map>
namespace torch::jit::tensorexpr {
// A RAII wrapper to manage a variable and name pair in the look-up table.

View File

@ -2,6 +2,7 @@
#include <torch/csrc/jit/tensorexpr/llvm_codegen.h>
#include <ATen/NativeFunctions.h>
#include <ATen/Parallel.h>
#include <c10/util/Exception.h>
#include <c10/util/irange.h>

View File

@ -20,6 +20,7 @@
#include <ATen/DLConvertor.h>
#include <ATen/InitialTensorOptions.h>
#include <ATen/NamedTensorUtils.h>
#include <ATen/NativeFunctions.h>
#include <ATen/SparseCsrTensorUtils.h>
#include <ATen/TracerMode.h>
#include <ATen/dlpack.h>