Skip to content

Commit

Permalink
Canonicalize all includes in PyTorch. (#14849)
Browse files Browse the repository at this point in the history
Summary:
Anywhere we used #include "foo.h", we now say #include <foo.h>
Paths are adjusted to be rooted out of aten/src, torch/lib, or
the root level directory.

I modified CMakeLists.txt by hand to remove TH and THC from
the include paths.

I used the following script to do the canonicalization:

```
  import subprocess
  import re
  import os.path

  files = subprocess.check_output(['git', 'ls-files']).decode('utf-8').rstrip().split('\n')
  for fn in files:
      if not any(fn.endswith(suff) for suff in ['.cu', '.cpp', '.in', '.h', '.hpp', '.cu', '.cuh', '.cc']):
          continue
      if not any(fn.startswith(pref) for pref in ["aten/", "torch/"]):
          continue
      with open(fn, 'r') as f:
          c = f.read()
      def fmt(p):
          return "#include <{}>".format(p)
      def repl(m):
          p = m.group(1)
          if p in ["dlfcn.h", "unistd.h", "nvrtc.h", "cuda.h", "cuda_runtime.h", "cstdint", "cudnn.h", "Python.h", "cusparse.h", "cuda_runtime_api.h", "cuda_fp16.h", "cublas_v2.h", "stdint.h", "curand_kernel.h"]:
              return fmt(p)
          if any(p.startswith(pref) for pref in ["torch/csrc", "c10/", "ATen/", "caffe2/", "TH/", "THC/", "Eigen/", "gtest/", "zdl/", "gloo/", "onnx/", "miopen/"]):
              return fmt(p)
          for root in ["aten/src", "torch/lib", ""]:
              for bad_root in [os.path.dirname(fn), "aten/src/TH", "aten/src/THC", "torch/csrc"]:
                  new_p = os.path.relpath(os.path.join(bad_root, p), root)
                  if not new_p.startswith("../") and (os.path.exists(os.path.join(root, new_p)) or os.path.exists(os.path.join(root, new_p + ".in"))):
                      return fmt(new_p)
          print("ERROR: ", fn, p)
          return m.group(0)
      new_c = re.sub(r'#include "([^"]+)"', repl, c)
      if new_c != c:
          print(fn)
          with open(fn, 'w') as f:
              f.write(new_c)
```

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: pytorch/pytorch#14849

Reviewed By: dzhulgakov

Differential Revision: D13363445

Pulled By: ezyang

fbshipit-source-id: 52361f878a672785f9306c9e9ab2513128092b68
  • Loading branch information
ezyang authored and facebook-github-bot committed Dec 9, 2018
1 parent a7b3197 commit 517c7c9
Show file tree
Hide file tree
Showing 998 changed files with 3,770 additions and 3,806 deletions.
18 changes: 1 addition & 17 deletions aten/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,11 @@ endif()
set(TH_LINK_STYLE STATIC)
add_subdirectory(src/TH)
set(TH_CPU_INCLUDE
# dense
${CMAKE_CURRENT_SOURCE_DIR}/src/TH
${CMAKE_CURRENT_BINARY_DIR}/src/TH
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}/src
${CMAKE_BINARY_DIR}/aten/src)
list(APPEND ATen_CPU_INCLUDE ${TH_CPU_INCLUDE})

if(USE_CUDA OR USE_ROCM)
set(TH_CUDA_INCLUDE
# dense
${CMAKE_CURRENT_SOURCE_DIR}/src/THC
${CMAKE_CURRENT_BINARY_DIR}/src/THC)
list(APPEND ATen_CUDA_INCLUDE ${TH_CUDA_INCLUDE})
endif()

add_subdirectory(src/THNN)

# Find the HIP package, set the HIP paths, load the HIP CMake.
Expand Down Expand Up @@ -82,14 +71,9 @@ else()
SET(AT_CUDA_ENABLED 0)
endif()

list(APPEND ATen_CPU_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/src/THNN
${CMAKE_CURRENT_SOURCE_DIR}/src/THCUNN)

list(APPEND ATen_CPU_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/catch/single_include
${CMAKE_CURRENT_BINARY_DIR}/src/ATen)
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/catch/single_include)
add_subdirectory(src/ATen)

# Pass source, includes, and libs to parent
Expand Down
38 changes: 19 additions & 19 deletions aten/src/ATen/ATen.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#pragma once

#include "ATen/Allocator.h"
#include "ATen/CPUGeneral.h"
#include "ATen/Context.h"
#include "ATen/Device.h"
#include "ATen/DeviceGuard.h"
#include "ATen/DimVector.h"
#include "ATen/Dispatch.h"
#include "ATen/Formatting.h"
#include "ATen/Functions.h"
#include "ATen/ScalarOps.h"
#include "ATen/Tensor.h"
#include "ATen/TensorGeometry.h"
#include "ATen/TensorOperators.h"
#include "ATen/Type.h"
#include "ATen/core/ATenGeneral.h"
#include "ATen/core/Generator.h"
#include <ATen/Allocator.h>
#include <ATen/CPUGeneral.h>
#include <ATen/Context.h>
#include <ATen/Device.h>
#include <ATen/DeviceGuard.h>
#include <ATen/DimVector.h>
#include <ATen/Dispatch.h>
#include <ATen/Formatting.h>
#include <ATen/Functions.h>
#include <ATen/ScalarOps.h>
#include <ATen/Tensor.h>
#include <ATen/TensorGeometry.h>
#include <ATen/TensorOperators.h>
#include <ATen/Type.h>
#include <ATen/core/ATenGeneral.h>
#include <ATen/core/Generator.h>
#include <c10/core/Layout.h>
#include "ATen/core/Scalar.h"
#include <ATen/core/Scalar.h>
#include <c10/core/Storage.h>
#include "ATen/core/TensorMethods.h"
#include "c10/core/TensorOptions.h"
#include <ATen/core/TensorMethods.h>
#include <c10/core/TensorOptions.h>
#include <c10/util/Exception.h>
4 changes: 2 additions & 2 deletions aten/src/ATen/AccumulateType.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "ATen/Config.h"
#include "ATen/core/Half.h"
#include <ATen/Config.h>
#include <ATen/core/Half.h>

// Defines the accumulation type for a scalar type.
// Example:
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/CPUApplyUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ATen/Parallel.h"
#include "ATen/TensorUtils.h"
#include <ATen/Parallel.h>
#include <ATen/TensorUtils.h>
#include <limits>
#include <utility>
#include <cstring>
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/CPUFixedAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "TH/TH.h"
#include "c10/util/Exception.h"
#include <TH/TH.h>
#include <c10/util/Exception.h>

// This file creates a fake allocator that just throws exceptions if
// it is actually used.
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/CPUGeneral.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// linking errors using MSVC
// See https://msdn.microsoft.com/en-us/library/a90k134d.aspx
// This header adds this if using CAFFE2_API
#include "ATen/core/ATenGeneral.h"
#include <ATen/core/ATenGeneral.h>

namespace at {
CAFFE2_API void set_num_threads(int);
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/CPUGenerator.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ATen/CPUGenerator.h"
#include <ATen/CPUGenerator.h>

#define const_generator_cast(generator) \
dynamic_cast<const CPUGenerator&>(generator)
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/CheckGenerator.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "ATen/Utils.h"
#include "ATen/core/Generator.h"
#include "c10/util/Exception.h"
#include <ATen/Utils.h>
#include <ATen/core/Generator.h>
#include <c10/util/Exception.h>

namespace at {

Expand Down
12 changes: 6 additions & 6 deletions aten/src/ATen/Context.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "ATen/Config.h"
#include <ATen/Config.h>

#include "Context.h"
#include <ATen/Context.h>

#include <c10/core/TensorOptions.h>

Expand All @@ -10,12 +10,12 @@
#include <string>
#include <stdexcept>

#include "ATen/CPUGenerator.h"
#include "ATen/RegisterCPU.h"
#include "ATen/Tensor.h"
#include <ATen/CPUGenerator.h>
#include <ATen/RegisterCPU.h>
#include <ATen/Tensor.h>
#include <ATen/cpu/FlushDenormal.h>

#include "TH/TH.h" // for USE_LAPACK
#include <TH/TH.h> // for USE_LAPACK

namespace at {

Expand Down
26 changes: 13 additions & 13 deletions aten/src/ATen/Context.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#pragma once

#include <ATen/CPUGeneral.h>
#include "ATen/Type.h"
#include "ATen/TypeExtendedInterface.h"
#include "ATen/Utils.h"
#include "ATen/LegacyTHDispatch.h"
#include "ATen/LegacyTHDispatcher.h"
#include "ATen/core/ATenGeneral.h"
#include "ATen/core/Generator.h"
#include "ATen/core/LegacyTypeDispatch.h"
#include "ATen/core/VariableHooksInterface.h"
#include "ATen/detail/CUDAHooksInterface.h"
#include "ATen/detail/HIPHooksInterface.h"
#include "ATen/detail/ComplexHooksInterface.h"
#include "c10/util/Exception.h"
#include <ATen/Type.h>
#include <ATen/TypeExtendedInterface.h>
#include <ATen/Utils.h>
#include <ATen/LegacyTHDispatch.h>
#include <ATen/LegacyTHDispatcher.h>
#include <ATen/core/ATenGeneral.h>
#include <ATen/core/Generator.h>
#include <ATen/core/LegacyTypeDispatch.h>
#include <ATen/core/VariableHooksInterface.h>
#include <ATen/detail/CUDAHooksInterface.h>
#include <ATen/detail/HIPHooksInterface.h>
#include <ATen/detail/ComplexHooksInterface.h>
#include <c10/util/Exception.h>

#include <memory>
#include <mutex>
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/DLConvertor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ATen/DLConvertor.h"
#include "ATen/Functions.h"
#include <ATen/DLConvertor.h>
#include <ATen/Functions.h>

#include <iostream>
#include <sstream>
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/DLConvertor.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "ATen/Tensor.h"
#include "ATen/ATen.h"
#include "ATen/dlpack.h"
#include <ATen/Tensor.h>
#include <ATen/ATen.h>
#include <ATen/dlpack.h>

// this convertor will:
// 1) take a Tensor object and wrap it in the DLPack tensor
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/ExpandUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ATen/ExpandUtils.h"
#include <ATen/ExpandUtils.h>

namespace at {

Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/ExpandUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ATen/Tensor.h"
#include "c10/util/Exception.h"
#include <ATen/Tensor.h>
#include <c10/util/Exception.h>

#include <functional>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/ScalarOps.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <c10/core/Scalar.h>
#include "ATen/Tensor.h"
#include <ATen/Tensor.h>

// This is in the c10 namespace because we use ADL to find the functions in it.
namespace c10 {
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/SparseTensorImpl.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "ATen/Tensor.h"
#include "ATen/core/TensorImpl.h"
#include "c10/util/Exception.h"
#include <ATen/Tensor.h>
#include <ATen/core/TensorImpl.h>
#include <c10/util/Exception.h>

namespace at {
struct CAFFE2_API SparseTensorImpl : public TensorImpl {
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/TensorOperators.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <c10/core/Scalar.h>
#include "ATen/Tensor.h"
#include "ATen/Type.h"
#include <ATen/Tensor.h>
#include <ATen/Type.h>

#include <string>
#include <stdexcept>
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/TensorUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "ATen/Config.h"
#include "ATen/TensorUtils.h"
#include <ATen/Config.h>
#include <ATen/TensorUtils.h>

#include "ATen/ATen.h"
#include <ATen/ATen.h>

#include <ostream>
#include <sstream>
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/TensorUtils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "ATen/Tensor.h"
#include "ATen/TensorGeometry.h"
#include "ATen/Utils.h"
#include <ATen/Tensor.h>
#include <ATen/TensorGeometry.h>
#include <ATen/Utils.h>

// These functions are NOT in Utils.h, because this file has a dep on Tensor.h

Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/UndefinedType.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ATen/UndefinedType.h"
#include "c10/util/Exception.h"
#include <ATen/UndefinedType.h>
#include <c10/util/Exception.h>

namespace at {

Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/UndefinedType.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ATen/TypeDefault.h"
#include "ATen/CheckGenerator.h"
#include <ATen/TypeDefault.h>
#include <ATen/CheckGenerator.h>

#ifdef _MSC_VER
#ifdef Type
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ATen/Utils.h"
#include <ATen/Utils.h>
#include <stdarg.h>
#include <stdexcept>
#include <typeinfo>
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/Utils.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "ATen/core/ATenGeneral.h"
#include <ATen/core/ATenGeneral.h>
#include <c10/core/StorageImpl.h>
#include "ATen/core/UndefinedTensorImpl.h"
#include <ATen/core/UndefinedTensorImpl.h>

#include <c10/core/ScalarType.h>
#include "ATen/Formatting.h"
#include <ATen/Formatting.h>
#include <c10/util/ArrayRef.h>
#include <c10/util/Exception.h>

Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/WrapDimUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ATen/core/WrapDimMinimal.h"
#include "ATen/core/TensorImpl.h"
#include <ATen/core/WrapDimMinimal.h>
#include <ATen/core/TensorImpl.h>

namespace at {

Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/WrapDimUtilsMulti.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ATen/core/TensorImpl.h"
#include "ATen/WrapDimUtils.h"
#include <ATen/core/TensorImpl.h>
#include <ATen/WrapDimUtils.h>
#include <sstream>
#include <bitset>

Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/core/ATenGeneral.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#include "c10/macros/Macros.h"
#include <c10/macros/Macros.h>
4 changes: 2 additions & 2 deletions aten/src/ATen/core/Backtrace.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#include "c10/util/Backtrace.h"
#include "c10/util/Type.h"
#include <c10/util/Backtrace.h>
#include <c10/util/Type.h>
2 changes: 1 addition & 1 deletion aten/src/ATen/core/Formatting.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ATen/core/Formatting.h"
#include <ATen/core/Formatting.h>

#include <cmath>
#include <cstdint>
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/core/Half.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#pragma once
#include "c10/Half.h"
#include <c10/Half.h>
2 changes: 1 addition & 1 deletion aten/src/ATen/core/Macros.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#pragma once
#include "c10/macros/Macros.h"
#include <c10/macros/Macros.h>
10 changes: 5 additions & 5 deletions aten/src/ATen/core/Tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <c10/core/Layout.h>
#include <c10/core/Scalar.h>
#include <c10/core/ScalarType.h>
#include "ATen/core/SparseTensorRef.h"
#include <ATen/core/SparseTensorRef.h>
#include <c10/core/Storage.h>
#include "ATen/core/TensorAccessor.h"
#include "ATen/core/TensorImpl.h"
#include "ATen/core/UndefinedTensorImpl.h"
#include <ATen/core/TensorAccessor.h>
#include <ATen/core/TensorImpl.h>
#include <ATen/core/UndefinedTensorImpl.h>
#include <c10/util/Exception.h>
#include <c10/util/Optional.h>
#include <ATen/core/LegacyTypeDispatch.h>
Expand Down Expand Up @@ -738,4 +738,4 @@ Tensor make_tensor(Args&&... args) {

} // namespace at

#include "ATen/core/TensorMethods.h"
#include <ATen/core/TensorMethods.h>
Loading

0 comments on commit 517c7c9

Please sign in to comment.