mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/76275 In preparation for addressing https://github.com/pytorch/pytorch/issues/73212 Diff was generated with: ``` git mv tools/codegen torchgen git grep -l 'tools.codegen' | xargs sed -i 's/tools.codegen/torchgen/g' sed -i "s/\${TOOLS_PATH}\/codegen/\${TORCH_ROOT}\/torchgen/g" caffe2/CMakeLists.txt ``` and a manual edits to: * tools/test/test_gen_backend_stubs.py * torchgen/build.bzl * torchgen/gen_backend_stubs.py aka this diff: ``` diff --git a/tools/test/test_gen_backend_stubs.py b/tools/test/test_gen_backend_stubs.py index 3dc26c6d2d..104054575e 100644 --- a/tools/test/test_gen_backend_stubs.py +++ b/tools/test/test_gen_backend_stubs.py @@ -9,7 +9,7 @@ from torchgen.gen_backend_stubs import run from torchgen.gen import _GLOBAL_PARSE_NATIVE_YAML_CACHE # noqa: F401 path = os.path.dirname(os.path.realpath(__file__)) -gen_backend_stubs_path = os.path.join(path, '../torchgen/gen_backend_stubs.py') +gen_backend_stubs_path = os.path.join(path, '../../torchgen/gen_backend_stubs.py') # gen_backend_stubs.py is an integration point that is called directly by external backends. # The tests here are to confirm that badly formed inputs result in reasonable error messages. diff --git a/torchgen/build.bzl b/torchgen/build.bzl index ed04e35a43..d00078a3cf 100644 --- a/torchgen/build.bzl +++ b/torchgen/build.bzl @@ -1,6 +1,6 @@ def define_targets(rules): rules.py_library( - name = "codegen", + name = "torchgen", srcs = rules.glob(["**/*.py"]), deps = [ rules.requirement("PyYAML"), @@ -11,6 +11,6 @@ def define_targets(rules): rules.py_binary( name = "gen", - srcs = [":codegen"], + srcs = [":torchgen"], visibility = ["//visibility:public"], ) diff --git a/torchgen/gen_backend_stubs.py b/torchgen/gen_backend_stubs.py index c1a672a655..beee7a15e0 100644 --- a/torchgen/gen_backend_stubs.py +++ b/torchgen/gen_backend_stubs.py @@ -474,7 +474,7 @@ def run( ) -> None: # Assumes that this file lives at PYTORCH_ROOT/torchgen/gen_backend_stubs.py - pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute() + pytorch_root = pathlib.Path(__file__).parent.parent.absolute() template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates") def make_file_manager(install_dir: str) -> FileManager: ``` run_all_fbandroid_tests Test Plan: sandcastle Reviewed By: albanD, ngimel Differential Revision: D35770317 fbshipit-source-id: 153ac4a7fef15b1e750812a90bfafdbc8f1ebcdf (cherry picked from commit c6d485d1d4648fa1c8a4c14c5bf3d8e899b9b4dd)
107 lines
3.2 KiB
Python
107 lines
3.2 KiB
Python
import re
|
|
from typing import Match, Optional, Sequence, Mapping
|
|
|
|
# match $identifier or ${identifier} and replace with value in env
|
|
# If this identifier is at the beginning of whitespace on a line
|
|
# and its value is a list then it is treated as
|
|
# block substitution by indenting to that depth and putting each element
|
|
# of the list on its own line
|
|
# if the identifier is on a line starting with non-whitespace and a list
|
|
# then it is comma separated ${,foo} will insert a comma before the list
|
|
# if this list is not empty and ${foo,} will insert one after.
|
|
|
|
|
|
class CodeTemplate:
|
|
# Python 2.7.5 has a bug where the leading (^[^\n\S]*)? does not work,
|
|
# workaround via appending another [^\n\S]? inside
|
|
|
|
substitution_str = r"(^[^\n\S]*[^\n\S]?)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})"
|
|
|
|
# older versions of Python have a bug where \w* does not work,
|
|
# so we need to replace with the non-shortened version [a-zA-Z0-9_]*
|
|
# https://bugs.python.org/issue18647
|
|
|
|
substitution_str = substitution_str.replace(r"\w", r"[a-zA-Z0-9_]")
|
|
|
|
substitution = re.compile(substitution_str, re.MULTILINE)
|
|
|
|
pattern: str
|
|
filename: str
|
|
|
|
@staticmethod
|
|
def from_file(filename: str) -> "CodeTemplate":
|
|
with open(filename, "r") as f:
|
|
return CodeTemplate(f.read(), filename)
|
|
|
|
def __init__(self, pattern: str, filename: str = "") -> None:
|
|
self.pattern = pattern
|
|
self.filename = filename
|
|
|
|
def substitute(
|
|
self, env: Optional[Mapping[str, object]] = None, **kwargs: object
|
|
) -> str:
|
|
if env is None:
|
|
env = {}
|
|
|
|
def lookup(v: str) -> object:
|
|
assert env is not None
|
|
return kwargs[v] if v in kwargs else env[v]
|
|
|
|
def indent_lines(indent: str, v: Sequence[object]) -> str:
|
|
return "".join(
|
|
[indent + l + "\n" for e in v for l in str(e).splitlines()]
|
|
).rstrip()
|
|
|
|
def replace(match: Match[str]) -> str:
|
|
indent = match.group(1)
|
|
key = match.group(2)
|
|
comma_before = ""
|
|
comma_after = ""
|
|
if key[0] == "{":
|
|
key = key[1:-1]
|
|
if key[0] == ",":
|
|
comma_before = ", "
|
|
key = key[1:]
|
|
if key[-1] == ",":
|
|
comma_after = ", "
|
|
key = key[:-1]
|
|
v = lookup(key)
|
|
if indent is not None:
|
|
if not isinstance(v, list):
|
|
v = [v]
|
|
return indent_lines(indent, v)
|
|
elif isinstance(v, list):
|
|
middle = ", ".join([str(x) for x in v])
|
|
if len(v) == 0:
|
|
return middle
|
|
return comma_before + middle + comma_after
|
|
else:
|
|
return str(v)
|
|
|
|
return self.substitution.sub(replace, self.pattern)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
c = CodeTemplate(
|
|
"""\
|
|
int foo($args) {
|
|
|
|
$bar
|
|
$bar
|
|
$a+$b
|
|
}
|
|
int commatest(int a${,stuff})
|
|
int notest(int a${,empty,})
|
|
"""
|
|
)
|
|
print(
|
|
c.substitute(
|
|
args=["hi", 8],
|
|
bar=["what", 7],
|
|
a=3,
|
|
b=4,
|
|
stuff=["things...", "others"],
|
|
empty=[],
|
|
)
|
|
)
|