2018-05-01 18:30:13 +00:00
|
|
|
import argparse
|
2017-11-29 21:10:51 +00:00
|
|
|
import os
|
2022-04-15 07:02:35 +00:00
|
|
|
import pathlib
|
2017-11-29 21:10:51 +00:00
|
|
|
import sys
|
2022-07-14 07:03:12 +00:00
|
|
|
from typing import Any, cast, Optional
|
|
|
|
|
|
2021-01-19 08:06:10 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# use faster C loader if available
|
2021-03-19 07:01:57 +00:00
|
|
|
from yaml import CSafeLoader as YamlLoader
|
2021-01-19 08:06:10 +00:00
|
|
|
except ImportError:
|
2021-06-10 07:02:43 +00:00
|
|
|
from yaml import SafeLoader as YamlLoader # type: ignore[misc]
|
2017-11-29 21:10:51 +00:00
|
|
|
|
2022-04-21 07:02:32 +00:00
|
|
|
NATIVE_FUNCTIONS_PATH = "aten/src/ATen/native/native_functions.yaml"
|
2022-04-29 07:02:33 +00:00
|
|
|
TAGS_PATH = "aten/src/ATen/native/tags.yaml"
|
2018-05-01 18:30:13 +00:00
|
|
|
|
2017-11-29 21:10:51 +00:00
|
|
|
|
2022-04-21 07:02:32 +00:00
|
|
|
def generate_code(
|
2022-05-05 07:02:34 +00:00
|
|
|
gen_dir: pathlib.Path,
|
2022-04-21 07:02:32 +00:00
|
|
|
native_functions_path: Optional[str] = None,
|
2022-04-29 07:02:33 +00:00
|
|
|
tags_path: Optional[str] = None,
|
2022-04-21 07:02:32 +00:00
|
|
|
install_dir: Optional[str] = None,
|
|
|
|
|
subset: Optional[str] = None,
|
|
|
|
|
disable_autograd: bool = False,
|
|
|
|
|
force_schema_registration: bool = False,
|
|
|
|
|
operator_selector: Any = None,
|
|
|
|
|
) -> None:
|
2022-04-25 07:02:36 +00:00
|
|
|
from torchgen.selective_build.selector import SelectiveBuilder
|
2020-10-19 07:03:03 +00:00
|
|
|
|
2022-07-14 07:03:12 +00:00
|
|
|
from tools.autograd.gen_annotated_fn_args import gen_annotated
|
|
|
|
|
from tools.autograd.gen_autograd import gen_autograd, gen_autograd_python
|
|
|
|
|
|
2017-11-29 21:10:51 +00:00
|
|
|
# Build ATen based Variable classes
|
2020-07-18 07:02:55 +00:00
|
|
|
if install_dir is None:
|
2022-05-05 07:02:34 +00:00
|
|
|
install_dir = os.fspath(gen_dir / "torch/csrc")
|
|
|
|
|
python_install_dir = os.fspath(gen_dir / "torch/testing/_internal/generated")
|
2020-07-18 07:02:55 +00:00
|
|
|
else:
|
|
|
|
|
python_install_dir = install_dir
|
2022-04-21 07:02:32 +00:00
|
|
|
autograd_gen_dir = os.path.join(install_dir, "autograd", "generated")
|
2022-04-18 07:02:31 +00:00
|
|
|
for d in (autograd_gen_dir, python_install_dir):
|
|
|
|
|
os.makedirs(d, exist_ok=True)
|
2022-04-15 07:02:35 +00:00
|
|
|
autograd_dir = os.fspath(pathlib.Path(__file__).parent.parent / "autograd")
|
2019-04-26 08:26:49 +00:00
|
|
|
|
|
|
|
|
if subset == "pybindings" or not subset:
|
2020-10-20 07:02:41 +00:00
|
|
|
gen_autograd_python(
|
|
|
|
|
native_functions_path or NATIVE_FUNCTIONS_PATH,
|
2022-04-29 07:02:33 +00:00
|
|
|
tags_path or TAGS_PATH,
|
2020-10-20 07:02:41 +00:00
|
|
|
autograd_gen_dir,
|
2022-04-21 07:02:32 +00:00
|
|
|
autograd_dir,
|
|
|
|
|
)
|
2019-04-26 08:26:49 +00:00
|
|
|
|
2020-10-19 07:03:03 +00:00
|
|
|
if operator_selector is None:
|
|
|
|
|
operator_selector = SelectiveBuilder.get_nop_selector()
|
|
|
|
|
|
2019-04-26 08:26:49 +00:00
|
|
|
if subset == "libtorch" or not subset:
|
2020-08-01 07:03:38 +00:00
|
|
|
|
2019-09-10 17:18:19 +00:00
|
|
|
gen_autograd(
|
2020-11-10 08:02:38 +00:00
|
|
|
native_functions_path or NATIVE_FUNCTIONS_PATH,
|
2022-04-29 07:02:33 +00:00
|
|
|
tags_path or TAGS_PATH,
|
2019-09-10 17:18:19 +00:00
|
|
|
autograd_gen_dir,
|
2020-04-04 07:02:09 +00:00
|
|
|
autograd_dir,
|
2019-09-10 17:18:19 +00:00
|
|
|
disable_autograd=disable_autograd,
|
2020-10-19 07:03:03 +00:00
|
|
|
operator_selector=operator_selector,
|
2019-09-10 17:18:19 +00:00
|
|
|
)
|
2018-05-01 18:30:13 +00:00
|
|
|
|
2020-07-18 07:02:55 +00:00
|
|
|
if subset == "python" or not subset:
|
|
|
|
|
gen_annotated(
|
2020-11-15 08:04:25 +00:00
|
|
|
native_functions_path or NATIVE_FUNCTIONS_PATH,
|
2022-04-29 07:02:33 +00:00
|
|
|
tags_path or TAGS_PATH,
|
2020-07-18 07:02:55 +00:00
|
|
|
python_install_dir,
|
2022-04-21 07:02:32 +00:00
|
|
|
autograd_dir,
|
|
|
|
|
)
|
2020-07-18 07:02:55 +00:00
|
|
|
|
2021-01-19 08:06:10 +00:00
|
|
|
|
2020-10-19 07:03:03 +00:00
|
|
|
def get_selector_from_legacy_operator_selection_list(
|
2022-04-21 07:02:32 +00:00
|
|
|
selected_op_list_path: str,
|
2021-06-10 07:02:43 +00:00
|
|
|
) -> Any:
|
2022-04-21 07:02:32 +00:00
|
|
|
with open(selected_op_list_path, "r") as f:
|
2021-01-19 08:06:10 +00:00
|
|
|
# strip out the overload part
|
|
|
|
|
# It's only for legacy config - do NOT copy this code!
|
|
|
|
|
selected_op_list = {
|
2022-04-21 07:02:32 +00:00
|
|
|
opname.split(".", 1)[0] for opname in yaml.load(f, Loader=YamlLoader)
|
2021-01-19 08:06:10 +00:00
|
|
|
}
|
2020-10-19 07:03:03 +00:00
|
|
|
|
|
|
|
|
# Internal build doesn't use this flag any more. Only used by OSS
|
|
|
|
|
# build now. Every operator should be considered a root operator
|
|
|
|
|
# (hence generating unboxing code for it, which is consistent with
|
|
|
|
|
# the current behaviour), and also be considered as used for
|
|
|
|
|
# training, since OSS doesn't support training on mobile for now.
|
|
|
|
|
#
|
|
|
|
|
is_root_operator = True
|
|
|
|
|
is_used_for_training = True
|
|
|
|
|
|
2022-04-25 07:02:36 +00:00
|
|
|
from torchgen.selective_build.selector import SelectiveBuilder
|
2022-04-21 07:02:32 +00:00
|
|
|
|
2021-01-19 08:06:10 +00:00
|
|
|
selector = SelectiveBuilder.from_legacy_op_registration_allow_list(
|
|
|
|
|
selected_op_list,
|
|
|
|
|
is_root_operator,
|
|
|
|
|
is_used_for_training,
|
|
|
|
|
)
|
2020-10-19 07:03:03 +00:00
|
|
|
|
|
|
|
|
return selector
|
|
|
|
|
|
|
|
|
|
|
2021-06-10 07:02:43 +00:00
|
|
|
def get_selector(
|
|
|
|
|
selected_op_list_path: Optional[str],
|
|
|
|
|
operators_yaml_path: Optional[str],
|
|
|
|
|
) -> Any:
|
2020-10-19 07:03:03 +00:00
|
|
|
# cwrap depends on pyyaml, so we can't import it earlier
|
|
|
|
|
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
sys.path.insert(0, root)
|
2022-04-25 07:02:36 +00:00
|
|
|
from torchgen.selective_build.selector import SelectiveBuilder
|
2020-10-19 07:03:03 +00:00
|
|
|
|
2022-04-21 07:02:32 +00:00
|
|
|
assert not (
|
|
|
|
|
selected_op_list_path is not None and operators_yaml_path is not None
|
|
|
|
|
), (
|
|
|
|
|
"Expected at most one of selected_op_list_path and "
|
|
|
|
|
+ "operators_yaml_path to be set."
|
|
|
|
|
)
|
2020-10-19 07:03:03 +00:00
|
|
|
|
|
|
|
|
if selected_op_list_path is None and operators_yaml_path is None:
|
|
|
|
|
return SelectiveBuilder.get_nop_selector()
|
|
|
|
|
elif selected_op_list_path is not None:
|
|
|
|
|
return get_selector_from_legacy_operator_selection_list(selected_op_list_path)
|
|
|
|
|
else:
|
2021-06-10 07:02:43 +00:00
|
|
|
return SelectiveBuilder.from_yaml_path(cast(str, operators_yaml_path))
|
2020-10-19 07:03:03 +00:00
|
|
|
|
2018-05-01 18:30:13 +00:00
|
|
|
|
2021-06-10 07:02:43 +00:00
|
|
|
def main() -> None:
|
2022-04-21 07:02:32 +00:00
|
|
|
parser = argparse.ArgumentParser(description="Autogenerate code")
|
|
|
|
|
parser.add_argument("--native-functions-path")
|
2022-04-29 07:02:33 +00:00
|
|
|
parser.add_argument("--tags-path")
|
2022-05-05 07:02:34 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
"--gen-dir",
|
|
|
|
|
type=pathlib.Path,
|
|
|
|
|
default=pathlib.Path("."),
|
|
|
|
|
help="Root directory where to install files. Defaults to the current working directory.",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--install_dir",
|
|
|
|
|
help=(
|
|
|
|
|
"Deprecated. Use --gen-dir instead. The semantics are different, do not change "
|
|
|
|
|
"blindly."
|
|
|
|
|
),
|
|
|
|
|
)
|
2019-04-26 08:26:49 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--subset",
|
|
|
|
|
help='Subset of source files to generate. Can be "libtorch" or "pybindings". Generates both when omitted.',
|
2019-04-26 08:26:49 +00:00
|
|
|
)
|
2019-09-10 17:18:19 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--disable-autograd",
|
2019-09-10 17:18:19 +00:00
|
|
|
default=False,
|
2022-04-21 07:02:32 +00:00
|
|
|
action="store_true",
|
|
|
|
|
help="It can skip generating autograd related code when the flag is set",
|
2019-09-10 17:18:19 +00:00
|
|
|
)
|
2019-11-21 08:06:35 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--selected-op-list-path",
|
|
|
|
|
help="Path to the YAML file that contains the list of operators to include for custom build.",
|
2019-11-21 08:06:35 +00:00
|
|
|
)
|
2020-04-11 07:02:06 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--operators_yaml_path",
|
|
|
|
|
help="Path to the model YAML file that contains the list of operators to include for custom build.",
|
2020-04-11 07:02:06 +00:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--force_schema_registration",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="force it to generate schema-only registrations for ops that are not"
|
|
|
|
|
"listed on --selected-op-list",
|
2020-04-11 07:02:06 +00:00
|
|
|
)
|
2022-03-18 07:03:00 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--gen_lazy_ts_backend",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Enable generation of the torch::lazy TorchScript backend",
|
2022-03-18 07:03:00 +00:00
|
|
|
)
|
2022-03-23 07:02:35 +00:00
|
|
|
parser.add_argument(
|
2022-04-21 07:02:32 +00:00
|
|
|
"--per_operator_headers",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Build lazy tensor ts backend with per-operator ATen headers, must match how ATen was built",
|
2022-03-23 07:02:35 +00:00
|
|
|
)
|
2018-05-01 18:30:13 +00:00
|
|
|
options = parser.parse_args()
|
2020-10-19 07:03:03 +00:00
|
|
|
|
2019-04-26 08:26:49 +00:00
|
|
|
generate_code(
|
2022-05-05 07:02:34 +00:00
|
|
|
options.gen_dir,
|
2020-10-20 07:02:41 +00:00
|
|
|
options.native_functions_path,
|
2022-04-29 07:02:33 +00:00
|
|
|
options.tags_path,
|
2019-04-26 08:26:49 +00:00
|
|
|
options.install_dir,
|
|
|
|
|
options.subset,
|
2019-09-10 17:18:19 +00:00
|
|
|
options.disable_autograd,
|
2020-04-11 07:02:06 +00:00
|
|
|
options.force_schema_registration,
|
2020-10-19 07:03:03 +00:00
|
|
|
# options.selected_op_list
|
2022-04-21 07:02:32 +00:00
|
|
|
operator_selector=get_selector(
|
|
|
|
|
options.selected_op_list_path, options.operators_yaml_path
|
|
|
|
|
),
|
2019-04-26 08:26:49 +00:00
|
|
|
)
|
2017-11-29 21:10:51 +00:00
|
|
|
|
2022-03-18 07:03:00 +00:00
|
|
|
if options.gen_lazy_ts_backend:
|
|
|
|
|
aten_path = os.path.dirname(os.path.dirname(options.native_functions_path))
|
2022-04-21 07:02:32 +00:00
|
|
|
ts_backend_yaml = os.path.join(aten_path, "native/ts_native_functions.yaml")
|
2022-03-24 07:03:02 +00:00
|
|
|
ts_native_functions = "torch/csrc/lazy/ts_backend/ts_native_functions.cpp"
|
|
|
|
|
ts_node_base = "torch/csrc/lazy/ts_backend/ts_node.h"
|
2022-05-05 07:02:34 +00:00
|
|
|
install_dir = options.install_dir or os.fspath(options.gen_dir / "torch/csrc")
|
|
|
|
|
lazy_install_dir = os.path.join(install_dir, "lazy/generated")
|
2022-04-18 07:02:31 +00:00
|
|
|
os.makedirs(lazy_install_dir, exist_ok=True)
|
2022-03-18 07:03:00 +00:00
|
|
|
|
2022-04-21 07:02:32 +00:00
|
|
|
assert os.path.isfile(
|
|
|
|
|
ts_backend_yaml
|
|
|
|
|
), f"Unable to access ts_backend_yaml: {ts_backend_yaml}"
|
|
|
|
|
assert os.path.isfile(
|
|
|
|
|
ts_native_functions
|
|
|
|
|
), f"Unable to access {ts_native_functions}"
|
2022-04-25 07:02:36 +00:00
|
|
|
from torchgen.dest.lazy_ir import GenTSLazyIR
|
2022-07-14 07:03:12 +00:00
|
|
|
from torchgen.gen_lazy_tensor import run_gen_lazy_tensor
|
2022-04-21 07:02:32 +00:00
|
|
|
|
|
|
|
|
run_gen_lazy_tensor(
|
|
|
|
|
aten_path=aten_path,
|
|
|
|
|
source_yaml=ts_backend_yaml,
|
|
|
|
|
backend_name="TorchScript",
|
|
|
|
|
output_dir=lazy_install_dir,
|
|
|
|
|
dry_run=False,
|
|
|
|
|
impl_path=ts_native_functions,
|
|
|
|
|
node_base="TsNode",
|
|
|
|
|
node_base_hdr=ts_node_base,
|
|
|
|
|
build_in_tree=True,
|
2022-04-23 07:02:38 +00:00
|
|
|
lazy_ir_generator=GenTSLazyIR,
|
2022-04-21 07:02:32 +00:00
|
|
|
per_operator_headers=options.per_operator_headers,
|
|
|
|
|
gen_forced_fallback_code=True,
|
|
|
|
|
)
|
2022-03-18 07:03:00 +00:00
|
|
|
|
2018-02-27 22:58:09 +00:00
|
|
|
|
2017-11-29 21:10:51 +00:00
|
|
|
if __name__ == "__main__":
|
2018-05-01 18:30:13 +00:00
|
|
|
main()
|