Skip to content

Enable Training Pybindings in OSS #8073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ cmake_dependent_option(
"NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
)


if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
set(EXECUTORCH_BUILD_EXTENSION_TENSOR ON)
set(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER ON)
set(EXECUTORCH_BUILD_EXTENSION_MODULE ON)
endif()

if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
set(EXECUTORCH_BUILD_EXTENSION_TENSOR ON)
set(EXECUTORCH_BUILD_KERNELS_CUSTOM ON)
Expand Down Expand Up @@ -791,6 +798,35 @@ if(EXECUTORCH_BUILD_PYBIND)
install(TARGETS portable_lib
LIBRARY DESTINATION executorch/extension/pybindings
)

if(EXECUTORCH_BUILD_EXTENSION_TRAINING)

set(_pybind_training_dep_libs
${TORCH_PYTHON_LIBRARY}
etdump
executorch
util
torch
extension_training
)

if(EXECUTORCH_BUILD_XNNPACK)
# need to explicitly specify XNNPACK and microkernels-prod
# here otherwise uses XNNPACK and microkernel-prod symbols from libtorch_cpu
list(APPEND _pybind_training_dep_libs xnnpack_backend XNNPACK microkernels-prod)
endif()

# pybind training
pybind11_add_module(_training_lib SHARED extension/training/pybindings/_training_lib.cpp)

target_include_directories(_training_lib PRIVATE ${TORCH_INCLUDE_DIRS})
target_compile_options(_training_lib PUBLIC ${_pybind_compile_options})
target_link_libraries(_training_lib PRIVATE ${_pybind_training_dep_libs})

install(TARGETS _training_lib
LIBRARY DESTINATION executorch/extension/training/pybindings
)
endif()
endif()

if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
Expand Down
8 changes: 6 additions & 2 deletions install_executorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def clean():
print("Done cleaning build artifacts.")


VALID_PYBINDS = ["coreml", "mps", "xnnpack"]
VALID_PYBINDS = ["coreml", "mps", "xnnpack", "training"]


def main(args):
Expand Down Expand Up @@ -78,8 +78,12 @@ def main(args):
raise Exception(
f"Unrecognized pybind argument {pybind_arg}; valid options are: {', '.join(VALID_PYBINDS)}"
)
if pybind_arg == "training":
CMAKE_ARGS += " -DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON"
os.environ["EXECUTORCH_BUILD_TRAINING"] = "ON"
else:
CMAKE_ARGS += f" -DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON"
EXECUTORCH_BUILD_PYBIND = "ON"
CMAKE_ARGS += f" -DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON"

if args.clean:
clean()
Expand Down
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def _is_env_enabled(env_var: str, default: bool = False) -> bool:
def pybindings(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_PYBIND", default=False)

@classmethod
def training(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_TRAINING", default=False)

@classmethod
def llama_custom_ops(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT", default=True)
Expand Down Expand Up @@ -575,6 +579,11 @@ def run(self):
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON", # add quantized ops to pybindings.
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON",
]
if ShouldBuild.training():
cmake_args += [
"-DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON",
]
build_args += ["--target", "_training_lib"]
build_args += ["--target", "portable_lib"]
# To link backends into the portable_lib target, callers should
# add entries like `-DEXECUTORCH_BUILD_XNNPACK=ON` to the CMAKE_ARGS
Expand Down Expand Up @@ -677,6 +686,14 @@ def get_ext_modules() -> List[Extension]:
"_portable_lib.*", "executorch.extension.pybindings._portable_lib"
)
)
if ShouldBuild.training():
ext_modules.append(
# Install the prebuilt pybindings extension wrapper for training
BuiltExtension(
"_training_lib.*",
"executorch.extension.training.pybindings._training_lib",
)
)
if ShouldBuild.llama_custom_ops():
ext_modules.append(
BuiltFile(
Expand Down
Loading