Skip to content

Commit 352685c

Browse files
metascroyweifengpy
authored andcommitted
Add executorch parallel
Differential Revision: D62711909 Pull Request resolved: pytorch#953
1 parent e7270f1 commit 352685c

File tree

9 files changed

+58
-9
lines changed

9 files changed

+58
-9
lines changed

torchao/experimental/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ if(NOT TORCHAO_INCLUDE_DIRS)
2424
set(TORCHAO_INCLUDE_DIRS ${TORCHAO_ROOT}/../..)
2525
endif()
2626

27+
if (NOT TORCHAO_OP_TARGET)
28+
message(FATAL_ERROR "TORCHAO_OP_TARGET is not set. Set it to ATEN or EXECUTORCH.")
29+
endif()
30+
2731
if (NOT TORCHAO_PARALLEL_BACKEND)
2832
if (TORCHAO_OP_TARGET STREQUAL "ATEN")
2933
set(TORCHAO_PARALLEL_BACKEND "ATEN_OPENMP")
3034
elseif(TORCHAO_OP_TARGET STREQUAL "EXECUTORCH")
31-
set(TORCHAO_PARALLEL_BACKEND "PTHREADPOOL")
35+
set(TORCHAO_PARALLEL_BACKEND "EXECUTORCH")
3236
else()
3337
message(TORCHAO_PARALLEL_BACKEND "TORCHAO_PARALLEL_BACKEND is not set. Please set it directly or set TORCHAO_OP_TARGET to get a default.")
3438
endif()

torchao/experimental/Utils.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ function(target_link_torchao_parallel_backend target_name torchao_parallel_backe
2323
target_compile_definitions(${target_name} PRIVATE TORCHAO_PARALLEL_ATEN=1 AT_PARALLEL_OPENMP=1 INTRA_OP_PARALLEL=1)
2424
target_link_libraries(${target_name} PRIVATE ${TORCH_INSTALL_PREFIX}/lib/libomp${CMAKE_SHARED_LIBRARY_SUFFIX})
2525

26+
elseif(TORCHAO_PARALLEL_BACKEND_TOUPPER STREQUAL "EXECUTORCH")
27+
message(STATUS "Building with TORCHAO_PARALLEL_BACKEND=TORCHAO_PARALLEL_EXECUTORCH")
28+
message(STATUS "EXECUTORCH_INCLUDE_DIRS: ${EXECUTORCH_INCLUDE_DIRS}")
29+
message(STATUS "EXECUTORCH_LIBRARIES: ${EXECUTORCH_LIBRARIES}")
30+
target_include_directories(${target_name} PRIVATE "${EXECUTORCH_INCLUDE_DIRS}")
31+
target_link_libraries(${target_name} PRIVATE "${EXECUTORCH_LIBRARIES}")
32+
target_compile_definitions(${target_name} PRIVATE TORCHAO_PARALLEL_EXECUTORCH=1)
33+
2634
elseif(TORCHAO_PARALLEL_BACKEND_TOUPPER STREQUAL "OPENMP")
2735
message(STATUS "Building with TORCHAO_PARALLEL_BACKEND=OPENMP. You must set the CMake variable OpenMP_ROOT to the OMP library location before compiling. Do not use this option if Torch was built with OPENMP; use ATEN_OPENMP instead.")
2836
find_package(OpenMP REQUIRED)

torchao/experimental/build_torchao_ops.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export CMAKE_OUT=/tmp/cmake-out/torchao
1111
cmake -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH} \
1212
-DCMAKE_INSTALL_PREFIX=${CMAKE_OUT} \
1313
-DTORCHAO_OP_TARGET="$1" \
14-
-DEXECUTORCH_LIBRARIES=${EXECUTORCH_LIBRARIES} \
15-
-DEXECUTORCH_INCLUDE_DIRS=${EXECUTORCH_INCLUDE_DIRS} \
14+
-DEXECUTORCH_LIBRARIES="${EXECUTORCH_LIBRARIES}" \
15+
-DEXECUTORCH_INCLUDE_DIRS="${EXECUTORCH_INCLUDE_DIRS}" \
1616
-S . \
1717
-B ${CMAKE_OUT}
1818
cmake --build ${CMAKE_OUT} --target install --config Release

torchao/experimental/kernels/cpu/aarch64/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ if (CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
1313
${TORCHAO_INCLUDE_DIRS}/torchao/experimental/kernels/cpu/aarch64/valpacking/interleave.cpp
1414
)
1515
endif()
16+
17+
install(
18+
TARGETS torchao_kernels_aarch64
19+
DESTINATION lib
20+
)

torchao/experimental/ops/linear/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ include(${TORCHAO_ROOT}/Utils.cmake)
1010

1111
add_library(torchao_ops_linear_${TORCHAO_PARALLEL_BACKEND} STATIC channelwise_8bit_activation_groupwise_lowbit_weight.cpp)
1212
target_link_torchao_parallel_backend(torchao_ops_linear_${TORCHAO_PARALLEL_BACKEND} "${TORCHAO_PARALLEL_BACKEND}")
13+
14+
install(
15+
TARGETS torchao_ops_linear_${TORCHAO_PARALLEL_BACKEND}
16+
DESTINATION lib
17+
)

torchao/experimental/ops/linear/channelwise_8bit_activation_groupwise_lowbit_weight.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LinearTilingParams get_default_linear_tiling_params(
9393

9494
LinearTilingParams tiling_params;
9595
auto num_threads = torchao::get_num_threads();
96-
assert(num_threads >= 1);
96+
TORCHAO_CHECK(num_threads >= 1, "num_threads must be >= 1");
9797

9898
tiling_params.mc_by_mr = 1;
9999
int mc = tiling_params.mc_by_mr * ukernel_config.mr;

torchao/experimental/ops/linear/linear_a8wxdq_op/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if(TORCHAO_OP_TARGET STREQUAL "ATEN")
1919
target_compile_definitions(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE USE_ATEN=1)
2020
elseif(TORCHAO_OP_TARGET STREQUAL "EXECUTORCH")
2121
message(STATUS "Building with TORCHAO_OP_TARGET=EXECUTORCH")
22-
add_library(linear_a8wxdq_${TORCHAO_OP_TARGET} SHARED
22+
add_library(linear_a8wxdq_${TORCHAO_OP_TARGET} STATIC
2323
linear_a8wxdq_executorch/w2s.cpp
2424
linear_a8wxdq_executorch/w2sz.cpp
2525
linear_a8wxdq_executorch/w3s.cpp
@@ -29,9 +29,9 @@ elseif(TORCHAO_OP_TARGET STREQUAL "EXECUTORCH")
2929
linear_a8wxdq_executorch/w5s.cpp
3030
linear_a8wxdq_executorch/w5sz.cpp
3131
)
32-
target_include_directories(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE ${EXECUTORCH_INCLUDE_DIRS})
32+
target_include_directories(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE "${EXECUTORCH_INCLUDE_DIRS}")
3333
target_compile_definitions(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE USE_EXECUTORCH=1)
34-
target_link_libraries(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE ${EXECUTORCH_LIBRARIES})
34+
target_link_libraries(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE "${EXECUTORCH_LIBRARIES}")
3535
target_link_libraries(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE torchao_kernels_aarch64)
3636
target_link_libraries(linear_a8wxdq_${TORCHAO_OP_TARGET} PRIVATE torchao_ops_linear_${TORCHAO_PARALLEL_BACKEND})
3737
else()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// All rights reserved.
3+
//
4+
// This source code is licensed under the license found in the
5+
// LICENSE file in the root directory of this source tree.
6+
7+
#pragma once
8+
9+
#include <executorch/extension/threadpool/threadpool.h>
10+
11+
template <typename F>
12+
void torchao::parallel_1d(const int64_t begin, const int64_t end, const F& f) {
13+
torch::executorch::threadpool::get_threadpool()->run(
14+
[&](size_t i) {
15+
int64_t idx = begin + i;
16+
f(idx);
17+
},
18+
end - begin);
19+
}
20+
21+
inline void torchao::set_num_threads(int num_threads) {
22+
torch::executorch::threadpool::get_threadpool()->_unsafe_reset_threadpool(
23+
num_threads);
24+
}
25+
26+
inline int torchao::get_num_threads() {
27+
return torch::executorch::threadpool::get_threadpool()->get_thread_count();
28+
}

torchao/experimental/ops/parallel.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ int get_num_threads();
3434
#ifdef TORCHAO_PARALLEL_EXECUTORCH
3535
#pragma message( \
3636
"TORCHAO_PARALLEL_EXECUTORCH is set. Using ExecuTorch parallel backend.")
37-
38-
#error "TORCHAO_PARALLEL_EXECUTORCH is not implemented yet"
37+
#include <torchao/experimental/ops/parallel-executorch-impl.h>
3938

4039
#else
4140
#ifdef TORCHAO_PARALLEL_PTHREADPOOL

0 commit comments

Comments
 (0)