reduce header file to boost cpp_wrapper build. (#107585)

1. Reduce cpp_wrapper un-used header files.
2. Clean pch cache, when use_pch is False.

The first change will reduce the build time from 7.35s to 4.94s.

Before change:
![image](https://github.com/pytorch/pytorch/assets/8433590/fc5c1d37-ec40-44f3-8d4d-bf26bdc674bb)
After change:
![image](https://github.com/pytorch/pytorch/assets/8433590/c7ccadd2-bf3a-4d30-bf56-6e3b0230a194)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/107585
Approved by: https://github.com/ezyang, https://github.com/jansel, https://github.com/jgong5
This commit is contained in:
Xu Han
2023-08-22 11:58:44 +00:00
committed by PyTorch MergeBot
parent 94d85f18c9
commit 3f3479e85e
3 changed files with 12 additions and 1 deletions

View File

@ -650,6 +650,10 @@ def cpp_flags():
return "-std=c++17 -Wno-unused-variable"
def cpp_wrapper_flags():
return "-DTORCH_INDUCTOR_CPP_WRAPPER"
def optimization_flags():
base_flags = "-O3 -ffast-math -fno-finite-math-only"
if config.is_fbcode():
@ -1138,8 +1142,10 @@ class CppWrapperCodeCache:
cuda=cuda,
)
_use_custom_generated_macros = use_custom_generated_macros()
_cpp_wrapper_flags = cpp_wrapper_flags()
extra_cflags = f"{_cpp_flags} {_opt_flags} {_warning_all_flag} {_macros} {_use_custom_generated_macros}"
extra_cflags = f"{_cpp_flags} {_opt_flags} {_warning_all_flag} {_macros} {_cpp_wrapper_flags} \
{_use_custom_generated_macros}"
# For CPP wrapper, add -ffast-math during linking to make CPU flush denormals.
# CPP wrapper leverages cpp_extension which will do the compilation and linking in two stages.
# We need to explicitly add -ffast-math as a linking flag.

View File

@ -1,6 +1,9 @@
#pragma once
#ifndef TORCH_INDUCTOR_CPP_WRAPPER
// All pure C++ headers for the C++ frontend.
#include <torch/all.h>
#endif
// Python bindings for the C++ frontend (includes Python.h).
#include <torch/python.h>

View File

@ -1593,6 +1593,8 @@ def load_inline(name,
if use_pch is True:
# Using PreCompile Header('torch/extension.h') to reduce compile time.
_check_and_build_extension_h_precompiler_headers(extra_cflags, extra_include_paths)
else:
remove_extension_h_precompiler_headers()
# If `functions` is supplied, we create the pybind11 bindings for the user.
# Here, `functions` is (or becomes, after some processing) a map from