Skip to content

Commit

Permalink
Enable kernel testing in OSS (pytorch#3795)
Browse files Browse the repository at this point in the history
Summary:
We need to generate the correct Functions.h for each kernel and install it, so that the test binary can use it in include path. We also need to generate the wrapper so that we use the same test source for different kernels.

So far we add two tests (abs, neg) for portable and optimized respectively to demonstrate how it works.

Next, we will generate header executorch/kernels/test/supported_features.h to allow more ops.

Pull Request resolved: pytorch#3795

Reviewed By: shoumikhin

Differential Revision: D58042604

Pulled By: kirklandsign

fbshipit-source-id: a442beb32cea8f622fba85ae7095bb993ceabf6c
  • Loading branch information
kirklandsign authored and facebook-github-bot committed Jun 1, 2024
1 parent 7bcdb0e commit fa3bf82
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
4 changes: 4 additions & 0 deletions build/Codegen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ function(gen_operators_lib)
endif()

target_link_options_shared_lib(${GEN_LIB_NAME})
set(_generated_headers ${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h)
set_target_properties(
${GEN_LIB_NAME} PROPERTIES PUBLIC_HEADER "${_generated_headers}"
)
endfunction()

# Merge two kernel yaml files, prioritizing functions from FUNCTIONS_YAML and
Expand Down
6 changes: 5 additions & 1 deletion kernels/optimized/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ gen_operators_lib(
LIB_NAME "optimized_ops_lib" KERNEL_LIBS optimized_kernels DEPS executorch
)

install(TARGETS cpublas optimized_kernels optimized_ops_lib DESTINATION lib)
install(
TARGETS cpublas optimized_kernels optimized_ops_lib
DESTINATION lib
PUBLIC_HEADER DESTINATION include/optimized
)

install(TARGETS cpublas DESTINATION lib)
6 changes: 5 additions & 1 deletion kernels/portable/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ gen_operators_lib(
LIB_NAME "portable_ops_lib" KERNEL_LIBS portable_kernels DEPS executorch
)

install(TARGETS portable_kernels portable_ops_lib DESTINATION lib)
install(
TARGETS portable_kernels portable_ops_lib
DESTINATION lib
PUBLIC_HEADER DESTINATION include/portable
)
69 changes: 69 additions & 0 deletions kernels/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)
project(kernels_test)

# Use C++17 for test.
set(CMAKE_CXX_STANDARD 17)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)

include(${EXECUTORCH_ROOT}/build/Test.cmake)

set(_kernels portable optimized)

foreach(kernel ${_kernels})
set(_wrapper_path
"${CMAKE_CURRENT_BINARY_DIR}/include/${kernel}/executorch/kernels/test/FunctionHeaderWrapper.h"
)
add_custom_command(
OUTPUT "${_wrapper_path}"
COMMAND mkdir -p include/${kernel}/executorch/kernels/test
COMMAND echo "#include <${kernel}/Functions.h>" > "${_wrapper_path}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Generating ${_wrapper_path}"
VERBATIM
)
endforeach()

add_custom_target(
generate_wrapper
DEPENDS
"${CMAKE_CURRENT_BINARY_DIR}/include/portable/executorch/kernels/test/FunctionHeaderWrapper.h"
"${CMAKE_CURRENT_BINARY_DIR}/include/optimized/executorch/kernels/test/FunctionHeaderWrapper.h"
)

set(_portable_kernels_test_sources op_abs_test.cpp)
set(_optimized_kernels_test_sources op_neg_test.cpp)

et_cxx_test(
portable_kernels_test SOURCES ${_portable_kernels_test_sources} EXTRA_LIBS
portable_kernels portable_ops_lib
)
add_dependencies(portable_kernels_test generate_wrapper)
target_include_directories(
portable_kernels_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include/portable"
"${CMAKE_INSTALL_PREFIX}/include"
)

et_cxx_test(
optimized_kernels_test SOURCES ${_optimized_kernels_test_sources} EXTRA_LIBS
optimized_kernels optimized_ops_lib
)
add_dependencies(optimized_kernels_test generate_wrapper)
# message(FATAL_ERROR "${CMAKE_INSTALL_PREFIX}/include")
target_include_directories(
optimized_kernels_test PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include/optimized"
"${CMAKE_INSTALL_PREFIX}/include"
)
5 changes: 4 additions & 1 deletion test/run_oss_cpp_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build_executorch() {
cmake . \
-DCMAKE_INSTALL_PREFIX=cmake-out \
-DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \
Expand All @@ -48,7 +49,9 @@ build_gtest() {
}

export_test_add_model() {
python3 -m examples.portable.scripts.export --model_name="add" --output_dir="cmake-out"
if ! test -f cmake-out/add.pte; then
python3 -m examples.portable.scripts.export --model_name="add" --output_dir="cmake-out"
fi
}

build_and_run_test() {
Expand Down

0 comments on commit fa3bf82

Please sign in to comment.