Skip to content

Commit 0766c39

Browse files
larryliu0820cyyever
authored andcommitted
[PyTorch] Add codegen unboxing ability (#69881)
Summary: RFC: pytorch/rfcs#40 This PR (re)introduces python codegen for unboxing wrappers. Given an entry of `native_functions.yaml` the codegen should be able to generate the corresponding C++ code to convert ivalues from the stack to their proper types. To trigger the codegen, run ``` tools/jit/gen_unboxing.py -d cg/torch/share/ATen ``` Merged changes on CI test. In pytorch/pytorch#71782 I added an e2e test for static dispatch + codegen unboxing. The test exports a mobile model of mobilenetv2, load and run it on a new binary for lite interpreter: `test/mobile/custom_build/lite_predictor.cpp`. ## Lite predictor build specifics 1. Codegen: `gen.py` generates `RegisterCPU.cpp` and `RegisterSchema.cpp`. Now with this PR, once `static_dispatch` mode is enabled, `gen.py` will not generate `TORCH_LIBRARY` API calls in those cpp files, hence avoids interaction with the dispatcher. Once `USE_LIGHTWEIGHT_DISPATCH` is turned on, `cmake/Codegen.cmake` calls `gen_unboxing.py` which generates `UnboxingFunctions.h`, `UnboxingFunctions_[0-4].cpp` and `RegisterCodegenUnboxedKernels_[0-4].cpp`. 2. Build: `USE_LIGHTWEIGHT_DISPATCH` adds generated sources into `all_cpu_cpp` in `aten/src/ATen/CMakeLists.txt`. All other files remain unchanged. In reality all the `Operators_[0-4].cpp` are not necessary but we can rely on linker to strip them off. ## Current CI job test coverage update Created a new CI job `linux-xenial-py3-clang5-mobile-lightweight-dispatch-build` that enables the following build options: * `USE_LIGHTWEIGHT_DISPATCH=1` * `BUILD_LITE_INTERPRETER=1` * `STATIC_DISPATCH_BACKEND=CPU` This job triggers `test/mobile/lightweight_dispatch/build.sh` and builds `libtorch`. Then the script runs C++ tests written in `test_lightweight_dispatch.cpp` and `test_codegen_unboxing.cpp`. Recent commits added tests to cover as many C++ argument type as possible: in `build.sh` we installed PyTorch Python API so that we can export test models in `tests_setup.py`. Then we run C++ test binary to run these models on lightweight dispatch enabled runtime. Pull Request resolved: pytorch/pytorch#69881 Reviewed By: iseeyuan Differential Revision: D33692299 Pulled By: larryliu0820 fbshipit-source-id: 211e59f2364100703359b4a3d2ab48ca5155a023 (cherry picked from commit 58e1c9a25e3d1b5b656282cf3ac2f548d98d530b)
1 parent bb18259 commit 0766c39

28 files changed

+1351
-31
lines changed

.github/generated-ciflow-ruleset.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/scripts/generate_ci_workflows.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,17 @@ def generate_workflow_file(self, workflow_template: jinja2.Template) -> None:
527527
labels={LABEL_CIFLOW_LINUX, LABEL_CIFLOW_MOBILE, LABEL_CIFLOW_DEFAULT},
528528
),
529529
),
530+
CIWorkflow(
531+
arch="linux",
532+
build_environment="linux-xenial-py3.7-gcc5.4-mobile-lightweight-dispatch-build",
533+
docker_image_base=f"{DOCKER_REGISTRY}/pytorch/pytorch-linux-xenial-py3.7-gcc5.4",
534+
test_runner_type=LINUX_CPU_TEST_RUNNER,
535+
build_generates_artifacts=False,
536+
exclude_test=True,
537+
ciflow_config=CIFlowConfig(
538+
labels={LABEL_CIFLOW_LINUX, LABEL_CIFLOW_MOBILE, LABEL_CIFLOW_DEFAULT, LABEL_CIFLOW_LIBTORCH, LABEL_CIFLOW_CPU},
539+
),
540+
),
530541
CIWorkflow(
531542
arch="linux",
532543
build_environment="linux-xenial-py3.7-clang7-asan",

.github/workflows/generated-linux-xenial-py3.7-gcc5.4-mobile-lightweight-dispatch-build.yml

Lines changed: 243 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.jenkins/pytorch/build-mobile.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ retry pip install --pre torch torchvision \
2626
# binary, and running forward pass with a real model.
2727
if [[ "$BUILD_ENVIRONMENT" == *-mobile-custom-build-static* ]]; then
2828
TEST_CUSTOM_BUILD_STATIC=1 test/mobile/custom_build/build.sh
29+
elif [[ "$BUILD_ENVIRONMENT" == *-mobile-lightweight-dispatch* ]]; then
30+
test/mobile/lightweight_dispatch/build.sh
2931
else
3032
TEST_DEFAULT_BUILD=1 test/mobile/custom_build/build.sh
3133
fi

.jenkins/pytorch/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,8 @@ elif [[ "${BUILD_ENVIRONMENT}" == *-bazel-* ]]; then
570570
elif [[ "${BUILD_ENVIRONMENT}" == *distributed* || "${JOB_BASE_NAME}" == *distributed* ]]; then
571571
test_distributed
572572
test_rpc
573+
elif [[ "${BUILD_ENVIRONMENT}" == *-mobile-lightweight-dispatch* ]]; then
574+
test_libtorch
573575
elif [[ "${TEST_CONFIG}" = docs_test ]]; then
574576
test_docs_test
575577
else

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,14 @@ else()
394394
endif()
395395
set(SELECTED_OP_LIST "" CACHE STRING
396396
"Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default.")
397-
set(STATIC_DISPATCH_BACKEND "" CACHE STRING
398-
"Name of the backend for which static dispatch code is generated, e.g.: CPU.")
397+
option(
398+
STATIC_DISPATCH_BACKEND
399+
"Name of the backend for which static dispatch code is generated, e.g.: CPU."
400+
"")
401+
option(USE_LIGHTWEIGHT_DISPATCH "Enable codegen unboxing for ATen ops, need to work with static dispatch in order to work properly." OFF)
402+
if(USE_LIGHTWEIGHT_DISPATCH AND NOT STATIC_DISPATCH_BACKEND)
403+
message(FATAL_ERROR "Need to enable static dispatch after enabling USE_LIGHTWEIGHT_DISPATCH.")
404+
endif()
399405
option(
400406
TRACING_BASED
401407
"Master flag to build Lite Interpreter with tracing build option"

aten/src/ATen/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ else()
162162
)
163163
endif()
164164

165+
if(USE_LIGHTWEIGHT_DISPATCH)
166+
set(all_cpu_cpp ${all_cpu_cpp} ${generated_unboxing_sources})
167+
endif()
165168
if(AT_MKL_ENABLED)
166169
set(all_cpu_cpp ${all_cpu_cpp} ${mkl_cpp})
167170
endif()

0 commit comments

Comments
 (0)