Skip to content

Commit 68f0768

Browse files
authored
Fix pthreadpool subset corruption + executor deadlock; de-nest ExecuTorch SDPA under OpenMP MKL (pytorch#20267)
Differential Revision: D108226589 Pull Request resolved: pytorch#20267
1 parent 15b0d73 commit 68f0768

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎extension/llm/custom_ops/op_sdpa_impl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ void cpu_flash_attention(
805805
is_reduced_type ? reinterpret_cast<scalar_t*>(buf_reduced) : nullptr;
806806

807807
auto compute_lambda = [&](int64_t begin, int64_t end) {
808+
// Blocks are parallelized over the threadpool; keep each block's gemms
809+
// single-threaded so an OpenMP-threaded BLAS doesn't nest a second layer.
810+
::executorch::cpublas::SingleThreadedGemmGuard gemm_guard;
808811
int64_t i = 0, j = 0, k = 0;
809812
data_index_init(begin, i, batchSize, j, num_head, k, qSlice);
810813
int ompIdx = torch::executor::get_thread_num();

‎kernels/optimized/blas/CPUBlas.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,33 @@ extern "C" void zgemm_(char *transa, char *transb, int *m, int *n, int *k, void
2323
#endif // ET_BUILD_FOR_APPLE
2424
#endif // ET_BUILD_WITH_BLAS
2525

26+
#ifdef ET_CPUBLAS_MKL_OMP
27+
// MKL's thread-local thread-count setter. The C name aliases the Fortran
28+
// by-reference entry point in this MKL build, so the argument is int*. Only
29+
// referenced when linked against OpenMP MKL, so the strong ref always resolves.
30+
extern "C" int mkl_set_num_threads_local(int* nt);
31+
#endif // ET_CPUBLAS_MKL_OMP
32+
2633
namespace executorch {
2734
namespace cpublas {
2835

2936
using executorch::aten::BFloat16;
3037
using executorch::aten::complex;
3138
using executorch::aten::Half;
3239

40+
SingleThreadedGemmGuard::SingleThreadedGemmGuard() : prev_num_threads_(0) {
41+
#ifdef ET_CPUBLAS_MKL_OMP
42+
int one = 1;
43+
prev_num_threads_ = mkl_set_num_threads_local(&one);
44+
#endif // ET_CPUBLAS_MKL_OMP
45+
}
46+
47+
SingleThreadedGemmGuard::~SingleThreadedGemmGuard() {
48+
#ifdef ET_CPUBLAS_MKL_OMP
49+
mkl_set_num_threads_local(&prev_num_threads_);
50+
#endif // ET_CPUBLAS_MKL_OMP
51+
}
52+
3353
#ifdef ET_BUILD_WITH_BLAS
3454
#ifdef ET_BUILD_FOR_APPLE
3555
inline CBLAS_TRANSPOSE to_cblas_transpose(TransposeType trans) {

‎kernels/optimized/blas/CPUBlas.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ enum class TransposeType {
2323
ConjTranspose,
2424
};
2525

26+
// Forces gemm() in its scope to run single-threaded when this library is built
27+
// against OpenMP-threaded MKL (-DET_CPUBLAS_MKL_OMP), so a gemm called from
28+
// inside a threadpool parallel region doesn't nest a second OpenMP team. No-op
29+
// for any other BLAS backend.
30+
class SingleThreadedGemmGuard {
31+
public:
32+
SingleThreadedGemmGuard();
33+
~SingleThreadedGemmGuard();
34+
SingleThreadedGemmGuard(const SingleThreadedGemmGuard&) = delete;
35+
SingleThreadedGemmGuard& operator=(const SingleThreadedGemmGuard&) = delete;
36+
SingleThreadedGemmGuard(SingleThreadedGemmGuard&&) = delete;
37+
SingleThreadedGemmGuard& operator=(SingleThreadedGemmGuard&&) = delete;
38+
39+
private:
40+
[[maybe_unused]] int prev_num_threads_;
41+
};
42+
2643
// clang-format off
2744
void normalize_last_dims(
2845
TransposeType transa, TransposeType transb,

‎kernels/optimized/lib_defs.bzl‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def define_libs(is_fbcode=False):
175175
"//executorch/extension/threadpool:threadpool",
176176
]
177177

178-
for libblas_name, mkl_dep in [("libblas", "fbsource//third-party/mkl:mkl_lp64_omp"), ("libblas_mkl_noomp", "fbsource//third-party/mkl:mkl")]:
178+
for libblas_name, mkl_dep, mkl_omp_define in [("libblas", "fbsource//third-party/mkl:mkl_lp64_omp", ["-DET_CPUBLAS_MKL_OMP"]), ("libblas_mkl_noomp", "fbsource//third-party/mkl:mkl", [])]:
179179
# Merge platform-specific kwargs
180180
platform_kwargs = get_apple_framework_deps_kwargs(is_fbcode)
181181
if not is_fbcode:
@@ -217,7 +217,10 @@ def define_libs(is_fbcode=False):
217217
}),
218218
header_namespace = "executorch/kernels/optimized",
219219
visibility = ["PUBLIC"],
220-
preprocessor_flags = get_preprocessor_flags(),
220+
preprocessor_flags = get_preprocessor_flags() + select({
221+
":linux-x86_64": mkl_omp_define,
222+
"DEFAULT": [],
223+
}),
221224
fbobjc_exported_preprocessor_flags = [
222225
"-DET_BUILD_WITH_BLAS",
223226
"-DET_BUILD_FOR_APPLE",

0 commit comments

Comments
 (0)