From f37a6523efe1b9bf7f6b5b5d0f36dc461a3fda2a Mon Sep 17 00:00:00 2001 From: Mikayla Gawarecki Date: Mon, 6 Oct 2025 18:29:20 -0700 Subject: [PATCH] Move version.h to torch/headeronly (#164381) Differential Revision: [D83685392](https://our.internmc.facebook.com/intern/diff/D83685392) Pull Request resolved: https://github.com/pytorch/pytorch/pull/164381 Approved by: https://github.com/janeyx99 --- .gitignore | 2 +- BUILD.bazel | 7 ++++- buckbuild.bzl | 21 ++++++++++---- build.bzl | 12 -------- caffe2/CMakeLists.txt | 4 +-- tools/bazel.bzl | 1 + tools/setup_helpers/BUILD.bazel | 5 +++- tools/setup_helpers/build.bzl | 5 +++- torch/csrc/api/include/torch/version.h | 1 + torch/headeronly/build.bzl | 28 ++++++++++++++++++- torch/headeronly/ovrsource_defs.bzl | 1 + .../include/torch => headeronly}/version.h.in | 0 12 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 torch/csrc/api/include/torch/version.h rename torch/{csrc/api/include/torch => headeronly}/version.h.in (100%) diff --git a/.gitignore b/.gitignore index 91d6c9f71550..c5ff1e969964 100644 --- a/.gitignore +++ b/.gitignore @@ -88,7 +88,7 @@ torch_compile_debug/ # Listed manually because some files in this directory are not generated torch/testing/_internal/generated/annotated_fn_args.py torch/testing/_internal/data/*.pt -torch/csrc/api/include/torch/version.h +torch/headeronly/version.h torch/csrc/cudnn/cuDNN.cpp torch/csrc/generated torch/csrc/generic/TensorMethods.cpp diff --git a/BUILD.bazel b/BUILD.bazel index aef5365b04da..4737a2a0c486 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -13,6 +13,9 @@ load(":build_variables.bzl", "jit_core_sources", "lazy_tensor_ts_sources", "libt load(":ufunc_defs.bzl", "aten_ufunc_generated_cpu_kernel_sources", "aten_ufunc_generated_cpu_sources", "aten_ufunc_generated_cuda_sources") load("//:tools/bazel.bzl", "rules") +# Export files for use by torch/headeronly (where version.h generation now lives) +exports_files(["version.txt"]) + define_targets(rules = rules) COMMON_COPTS = [ @@ -690,7 +693,9 @@ cc_library( "torch/csrc/*/generated/*.h", "torch/csrc/jit/serialization/mobile_bytecode_generated.h", ] + torch_cuda_headers, - ) + GENERATED_AUTOGRAD_CPP + [":version_h"], + ) + GENERATED_AUTOGRAD_CPP + [ + "//torch/headeronly:version_h", + ], includes = [ "third_party/kineto/libkineto/include", "torch/csrc", diff --git a/buckbuild.bzl b/buckbuild.bzl index 0c168b132619..e60c02cd2ade 100644 --- a/buckbuild.bzl +++ b/buckbuild.bzl @@ -1038,7 +1038,8 @@ def define_buck_targets( name = "generated-version-header", header_namespace = "torch", exported_headers = { - "version.h": ":generate-version-header[version.h]", + "headeronly/version.h": ":generate-version-header[version.h]", + "version.h": "torch/csrc/api/include/torch/version.h" }, labels = labels, ) @@ -1047,19 +1048,27 @@ def define_buck_targets( fb_native.genrule( name = "generate-version-header", srcs = [ - "torch/csrc/api/include/torch/version.h.in", + "torch/headeronly/version.h.in", "version.txt", ], - cmd = "$(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([ + cmd = "mkdir -p $OUT/torch/headeronly && $(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([ "--template-path", - "torch/csrc/api/include/torch/version.h.in", + "torch/headeronly/version.h.in", "--version-path", "version.txt", "--output-path", - "$OUT/version.h", + "$OUT/torch/headeronly/version.h", + ]), + cmd_exe = "md $OUT\\torch\\headeronly 2>nul & $(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([ + "--template-path", + "torch/headeronly/version.h.in", + "--version-path", + "version.txt", + "--output-path", + "$OUT\\torch\\headeronly\\version.h", ]), outs = { - "version.h": ["version.h"], + "version.h": ["torch/headeronly/version.h"], }, default_outs = ["."], ) diff --git a/build.bzl b/build.bzl index 91529e75c9f0..0ce5b63e7b6c 100644 --- a/build.bzl +++ b/build.bzl @@ -142,18 +142,6 @@ def define_targets(rules): visibility = ["//visibility:public"], ) - rules.genrule( - name = "version_h", - srcs = [ - ":torch/csrc/api/include/torch/version.h.in", - ":version.txt", - ], - outs = ["torch/csrc/api/include/torch/version.h"], - cmd = "$(execpath //tools/setup_helpers:gen_version_header) " + - "--template-path $(location :torch/csrc/api/include/torch/version.h.in) " + - "--version-path $(location :version.txt) --output-path $@ ", - tools = ["//tools/setup_helpers:gen_version_header"], - ) # # ATen generated code diff --git a/caffe2/CMakeLists.txt b/caffe2/CMakeLists.txt index 9f929150de4a..b844e41a6163 100644 --- a/caffe2/CMakeLists.txt +++ b/caffe2/CMakeLists.txt @@ -243,8 +243,8 @@ configure_file("${TORCH_SRC_DIR}/_utils_internal.py" COPYONLY) # Generate header with version info -configure_file("${TORCH_SRC_DIR}/csrc/api/include/torch/version.h.in" - "${TORCH_SRC_DIR}/csrc/api/include/torch/version.h" +configure_file("${TORCH_SRC_DIR}/headeronly/version.h.in" + "${TORCH_SRC_DIR}/headeronly/version.h" @ONLY) set(GENERATED_CXX_TORCH diff --git a/tools/bazel.bzl b/tools/bazel.bzl index 9b662859adb4..147990c2e84b 100644 --- a/tools/bazel.bzl +++ b/tools/bazel.bzl @@ -16,6 +16,7 @@ def _is_cpu_static_dispatch_build(): # build structure aims to replicate Bazel as much as possible, most of # the rules simply forward to the Bazel definitions. rules = struct( + alias = native.alias, cc_binary = cc_binary, cc_library = cc_library, cc_test = cc_test, diff --git a/tools/setup_helpers/BUILD.bazel b/tools/setup_helpers/BUILD.bazel index 28dcd1b5b47c..8a02a12d0845 100644 --- a/tools/setup_helpers/BUILD.bazel +++ b/tools/setup_helpers/BUILD.bazel @@ -11,5 +11,8 @@ py_binary( py_binary( name = "gen_version_header", srcs = ["gen_version_header.py"], - visibility = ["//:__pkg__"], + visibility = [ + "//:__pkg__", + "//torch/headeronly:__pkg__", + ], ) diff --git a/tools/setup_helpers/build.bzl b/tools/setup_helpers/build.bzl index c5be13e4603b..ab2fa27f9f31 100644 --- a/tools/setup_helpers/build.bzl +++ b/tools/setup_helpers/build.bzl @@ -13,5 +13,8 @@ def define_targets(rules): rules.py_binary( name = "gen_version_header", srcs = ["gen_version_header.py"], - visibility = ["//:__pkg__"], + visibility = [ + "//:__pkg__", + "//torch/headeronly:__pkg__", + ], ) diff --git a/torch/csrc/api/include/torch/version.h b/torch/csrc/api/include/torch/version.h new file mode 100644 index 000000000000..44362d302fa9 --- /dev/null +++ b/torch/csrc/api/include/torch/version.h @@ -0,0 +1 @@ +#include diff --git a/torch/headeronly/build.bzl b/torch/headeronly/build.bzl index 6ec9a843e884..c44a8dc563d0 100644 --- a/torch/headeronly/build.bzl +++ b/torch/headeronly/build.bzl @@ -1,9 +1,35 @@ def define_targets(rules): + # workaround issue where open source bazel requires $(location ...) + # for filepaths but the buck conversion requires no $(location ...) + # for filepaths. + is_buck = hasattr(native, "read_config") + template_arg = "version.h.in" if is_buck else "$(location version.h.in)" + + genrule_args = { + "name": "version_h", + "srcs": [ + "version.h.in", + "//:version.txt", + ], + "outs": ["version.h"], + "cmd": "$(execpath //tools/setup_helpers:gen_version_header) " + + "--template-path " + template_arg + " " + + "--version-path $(location //:version.txt) --output-path $@ ", + "tools": ["//tools/setup_helpers:gen_version_header"], + } + + # Add visibility only for Bazel, buck genrule in fbcode.bzl does not + # support this argument + if not is_buck: + genrule_args["visibility"] = ["//visibility:public"] + + rules.genrule(**genrule_args) + rules.cc_library( name = "torch_headeronly", hdrs = rules.glob([ "**/*.h" - ]), + ]) + ["version.h.in"], visibility = ["//visibility:public"], deps = [ "//torch/headeronly/macros", diff --git a/torch/headeronly/ovrsource_defs.bzl b/torch/headeronly/ovrsource_defs.bzl index 3c3030c048b1..56fa9596585d 100644 --- a/torch/headeronly/ovrsource_defs.bzl +++ b/torch/headeronly/ovrsource_defs.bzl @@ -40,6 +40,7 @@ def define_torch_headeronly_ovrsource(name, is_mobile): ], exported_deps = [ ":ovrsource_torch_headeronly_cmake_macros.h", + ":generate-version-header", ], ) diff --git a/torch/csrc/api/include/torch/version.h.in b/torch/headeronly/version.h.in similarity index 100% rename from torch/csrc/api/include/torch/version.h.in rename to torch/headeronly/version.h.in