Skip to content

Commit 9da5def

Browse files
zou3519pytorchmergebot
authored andcommitted
Package config/template files with torchgen (pytorch#78942)
Package config/template files with torchgen This PR packages native_functions.yaml, tags.yaml and ATen/templates with torchgen. This PR: - adds a step to setup.py to copy the relevant files over into torchgen - adds a docstring for torchgen (so `import torchgen; help(torchgen)` says something) - adds a helper function in torchgen so you can get the torchgen root directory (and figure out where the packaged files are) - changes some scripts to explicitly pass the location of torchgen, which will be helpful for the first item in the Future section. Future ====== - torchgen, when invoked from the command line, should use sources in torchgen/packaged instead of aten/src. I'm unable to do this because people (aka PyTorch CI) invokes `python -m torchgen.gen` without installing torchgen. - the source of truth for all of these files should be in torchgen. This is a bit annoying to execute on due to potential merge conflicts and dealing with merge systems - CI and testing. The way things are set up right now is really fragile, we should have a CI job for torchgen. Test Plan ========= I ran the following locally: ``` python -m torchgen.gen -s torchgen/packaged ``` and verified that it outputted files. Furthermore, I did a setup.py install and checked that the files are actually being packaged with torchgen. Pull Request resolved: pytorch#78942 Approved by: https://github.com/ezyang
1 parent 67badf0 commit 9da5def

File tree

7 files changed

+79
-2
lines changed

7 files changed

+79
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ env
122122
.circleci/scripts/COMMIT_MSG
123123
scripts/release_notes/*.json
124124

125+
# These files get copied over on invoking setup.py
126+
torchgen/packaged/*
127+
!torchgen/packaged/README.md
128+
125129
# IPython notebook checkpoints
126130
.ipynb_checkpoints
127131

.jenkins/pytorch/codegen-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rm -rf "$OUT"
2727

2828
# aten codegen
2929
python -m torchgen.gen \
30+
-s aten/src/ATen \
3031
-d "$OUT"/torch/share/ATen
3132

3233
# torch codegen

docs/cpp/source/check-doxygen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pushd "$(dirname "$0")/../../.."
1616

1717
cp torch/_utils_internal.py tools/shared
1818

19-
python -m torchgen.gen
19+
python -m torchgen.gen --source-path aten/src/ATen
2020

2121
python tools/setup_helpers/generate_code.py \
2222
--native-functions-path aten/src/ATen/native/native_functions.yaml \

setup.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,33 @@ def not_exists_or_empty(folder):
363363
'benchmark'), ['CMakeLists.txt'])
364364

365365

366+
# Windows has very bad support for symbolic links.
367+
# Instead of using symlinks, we're going to copy files over
368+
def mirror_files_into_torchgen():
369+
# (new_path, orig_path)
370+
# Directories are OK and are recursively mirrored.
371+
paths = [
372+
('torchgen/packaged/ATen/native/native_functions.yaml', 'aten/src/ATen/native/native_functions.yaml'),
373+
('torchgen/packaged/ATen/native/tags.yaml', 'aten/src/ATen/native/tags.yaml'),
374+
('torchgen/packaged/ATen/templates', 'aten/src/ATen/templates'),
375+
]
376+
for new_path, orig_path in paths:
377+
# Create the dirs involved in new_path if they don't exist
378+
if not os.path.exists(new_path):
379+
os.makedirs(os.path.dirname(new_path), exist_ok=True)
380+
381+
# Copy the files from the orig location to the new location
382+
if os.path.isfile(orig_path):
383+
shutil.copyfile(orig_path, new_path)
384+
continue
385+
if os.path.isdir(orig_path):
386+
if os.path.exists(new_path):
387+
# copytree fails if the tree exists already, so remove it.
388+
shutil.rmtree(new_path)
389+
shutil.copytree(orig_path, new_path)
390+
continue
391+
raise RuntimeError("Check the file paths in `mirror_files_into_torchgen()`")
392+
366393
# all the work we need to do _before_ setup runs
367394
def build_deps():
368395
report('-- Building version ' + version)
@@ -912,6 +939,7 @@ def print_box(msg):
912939
print(e)
913940
sys.exit(1)
914941

942+
mirror_files_into_torchgen()
915943
if RUN_BUILD_DEPS:
916944
build_deps()
917945

@@ -1081,7 +1109,15 @@ def print_box(msg):
10811109
'utils/model_dump/code.js',
10821110
'utils/model_dump/*.mjs',
10831111
],
1084-
'torchgen': [],
1112+
'torchgen': [
1113+
# Recursive glob doesn't work in setup.py,
1114+
# https://github.com/pypa/setuptools/issues/1806
1115+
# To make this robust we should replace it with some code that
1116+
# returns a list of everything under packaged/
1117+
'packaged/ATen/*',
1118+
'packaged/ATen/native/*',
1119+
'packaged/ATen/templates/*',
1120+
],
10851121
'caffe2': [
10861122
'python/serialized_test/data/operator_test/*.zip',
10871123
],

torchgen/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""torchgen
2+
3+
This module contains codegeneration utilities for PyTorch. It is used to
4+
build PyTorch from source, but may also be used for out-of-tree projects
5+
that extend PyTorch.
6+
7+
Note well that we provide no BC guarantees for torchgen. If you're interested
8+
in using torchgen and want the PyTorch team to be aware, please reach out
9+
on GitHub.
10+
"""

torchgen/gen.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,14 @@ def gen_declarations_yaml(
22972297
)
22982298

22992299

2300+
def get_torchgen_root() -> pathlib.Path:
2301+
"""
2302+
If you're depending on torchgen out-of-tree, you can use the root to figure
2303+
out the path to native_functions.yaml
2304+
"""
2305+
return pathlib.Path(__file__).parent.resolve()
2306+
2307+
23002308
def main() -> None:
23012309
parser = argparse.ArgumentParser(description="Generate ATen source files")
23022310
parser.add_argument(

torchgen/packaged/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
What is torchgen/packaged?
2+
--------------------------
3+
4+
This directory is a collection of files that have been mirrored from their
5+
original locations. setup.py is responsible for performing the mirroring.
6+
7+
These files are necessary config files (e.g. `native_functions.yaml`) for
8+
torchgen to do its job; we mirror them over so that they can be packaged
9+
and distributed with torchgen.
10+
11+
Ideally the source of truth of these files exists just in torchgen (and not
12+
elsewhere), but getting to that point is a bit difficult due to needing to
13+
deal with merge conflicts, multiple build systems, etc. We aspire towards
14+
this for the future, though.
15+
16+
Note well that although we bundle torchgen with PyTorch, there are NO
17+
BC guarantees: use it at your own risk. If you're a user and want to use it,
18+
please reach out to us on GitHub.

0 commit comments

Comments
 (0)