[torchgen] reference generated comment to actual location of the generator and template (#130020)

As per title.

```diff
# torch/_VF.pyi

- # @generated from torch/_C/_VariableFunctions.pyi.in
+ # @generated by tools/pyi/gen_pyi.py from torch/_C/_VariableFunctions.pyi.in
```

```diff
# torch/return_types.pyi

- # @generated from torch/_C/return_types.pyi
+ # @generated by tools/pyi/gen_pyi.py from torch/_C/return_types.pyi.in
```

```diff
# torch/_C/__init__.pyi

- # @generated from torch/_C/__init__.pyi.in
+ # @generated by tools/pyi/gen_pyi.py from torch/_C/__init__.pyi.in
```

```diff
# torch/_C/_nn.pyi

+ # @generated by tools/pyi/gen_pyi.py from torch/_C/_nn.pyi.in
```

```diff
# torch/_C/_VariableFunctions.pyi

- # @generated from torch/_C/_VariableFunctions.pyi.in
+ # @generated by tools/pyi/gen_pyi.py from torch/_C/_VariableFunctions.pyi.in
```

```diff
# torch/nn/functional.pyi

+ # @generated by tools/pyi/gen_pyi.py from torch/nn/functional.pyi.in
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/130020
Approved by: https://github.com/ezyang
This commit is contained in:
Xuehai Pan 2024-07-05 21:47:12 +00:00 committed by PyTorch MergeBot
parent 6fc771d19b
commit d1d0a7080f
5 changed files with 32 additions and 23 deletions

View File

@ -1426,36 +1426,22 @@ def gen_pyi(
fm.write_with_template(
"torch/_C/__init__.pyi",
"torch/_C/__init__.pyi.in",
lambda: {
"generated_comment": "@" + "generated from torch/_C/__init__.pyi.in",
**env,
},
lambda: env,
)
fm.write_with_template(
"torch/_C/_VariableFunctions.pyi",
"torch/_C/_VariableFunctions.pyi.in",
lambda: {
"generated_comment": "@"
+ "generated from torch/_C/_VariableFunctions.pyi.in",
**env,
},
lambda: env,
)
fm.write_with_template(
"torch/_VF.pyi",
"torch/_C/_VariableFunctions.pyi.in",
lambda: {
"generated_comment": "@"
+ "generated from torch/_C/_VariableFunctions.pyi.in",
**env,
},
lambda: env,
)
fm.write_with_template(
"torch/return_types.pyi",
"torch/_C/return_types.pyi.in",
lambda: {
"generated_comment": "@" + "generated from torch/_C/return_types.pyi",
**env,
},
lambda: env,
)
gen_nn_functional(fm)

View File

@ -1,4 +1,6 @@
# ${generated_comment}
# mypy: disable-error-code="type-arg"
from typing import List, Optional, overload, Sequence, Tuple, Union
from torch import memory_format, Tensor

View File

@ -1,5 +1,5 @@
# mypy: allow-untyped-defs
# ${generated_comment}
# mypy: allow-untyped-defs
from typing import (
Any,

View File

@ -1,4 +1,6 @@
# ${generated_comment}
# mypy: allow-untyped-defs
from typing import (
Any,
Callable,

View File

@ -9,6 +9,7 @@ import sys
import textwrap
from dataclasses import fields, is_dataclass
from enum import auto, Enum
from pathlib import Path
from typing import (
Any,
Callable,
@ -30,6 +31,9 @@ if TYPE_CHECKING:
from argparse import Namespace
REPO_ROOT = Path(__file__).absolute().parent.parent
# Many of these functions share logic for defining both the definition
# and declaration (for example, the function signature is the same), so
# we organize them into one function that takes a Target to say which
@ -155,11 +159,26 @@ class FileManager:
template_path = os.path.join(self.template_dir, template_fn)
env = env_callable()
if isinstance(env, dict):
# TODO: Update the comment reference to the correct location
if "generated_comment" not in env:
comment = "@" + "generated by torchgen/gen.py"
comment += f" from {os.path.basename(template_path)}"
env["generated_comment"] = comment
generator_default = REPO_ROOT / "torchgen" / "gen.py"
try:
generator = Path(
sys.modules["__main__"].__file__ or generator_default
).absolute()
except (KeyError, AttributeError):
generator = generator_default.absolute()
try:
generator_path = generator.relative_to(REPO_ROOT).as_posix()
except ValueError:
generator_path = generator.name
env = {
**env, # copy the original dict instead of mutating it
"generated_comment": (
"@" + f"generated by {generator_path} from {template_fn}"
),
}
template = _read_template(template_path)
return template.substitute(env)
elif isinstance(env, str):