diff --git a/aten/src/ATen/ParallelNative.cpp b/aten/src/ATen/ParallelNative.cpp index 9dff0ee7f1d3..b0d060d9f538 100644 --- a/aten/src/ATen/ParallelNative.cpp +++ b/aten/src/ATen/ParallelNative.cpp @@ -178,10 +178,21 @@ void set_num_threads(int nthreads) { #ifndef C10_MOBILE TORCH_CHECK(nthreads > 0, "Expected positive number of threads"); int no_value = NOT_SET; - TORCH_CHECK(num_intraop_threads.compare_exchange_strong(no_value, nthreads), - "Error: cannot set number of intraop threads " - "after parallel work has started or after set_num_threads call " - "when using native parallel backend"); + if (!num_intraop_threads.compare_exchange_strong(no_value, nthreads)) { + // num_intraop_threads either stores a positive integer or CONSUMED, + // check that requested size is the same as the current one + int stored_nthreads = num_intraop_threads.load(); + if (stored_nthreads <= 0) { + // plus one because of master thread + stored_nthreads = _get_intraop_pool().size() + 1; + } + if (stored_nthreads != nthreads) { + TORCH_WARN( + "Cannot set number of intraop threads " + "after parallel work has started or after set_num_threads call " + "when using native parallel backend"); + } + } #else TORCH_CHECK(false, "set_num_threads is not supported for mobile."); #endif // C10_MOBILE diff --git a/aten/src/ATen/test/test_parallel.cpp b/aten/src/ATen/test/test_parallel.cpp index a6579ae5f083..7c1072c67225 100644 --- a/aten/src/ATen/test/test_parallel.cpp +++ b/aten/src/ATen/test/test_parallel.cpp @@ -69,12 +69,3 @@ TEST(TestParallel, IntraOpLaunchFuture) { ASSERT_TRUE(v1 == 1 && v2 == 2); } - -TEST(TestParallel, MultipleSetNumThreadsCalls) { -#if !AT_PARALLEL_NATIVE - set_num_threads(5); - ASSERT_TRUE(get_num_threads() == 5); - set_num_threads(10); - ASSERT_TRUE(get_num_threads() == 10); -#endif -} diff --git a/aten/src/ATen/test/thread_init_test.cpp b/aten/src/ATen/test/thread_init_test.cpp index 37ae3fa0454f..0650e9a3e6b4 100644 --- a/aten/src/ATen/test/thread_init_test.cpp +++ b/aten/src/ATen/test/thread_init_test.cpp @@ -29,6 +29,11 @@ int main() { }); t1.join(); + #if !AT_PARALLEL_NATIVE + at::set_num_threads(5); + ASSERT_TRUE(at::get_num_threads() == 5); + #endif + // test inter-op settings at::set_num_interop_threads(5); ASSERT_EQ(at::get_num_interop_threads(), 5); diff --git a/test/cpp/api/torch_include.cpp b/test/cpp/api/torch_include.cpp index d85e728de88e..1cc5335a1e31 100644 --- a/test/cpp/api/torch_include.cpp +++ b/test/cpp/api/torch_include.cpp @@ -8,7 +8,7 @@ TEST(TorchIncludeTest, GetSetNumThreads) { torch::init_num_threads(); torch::set_num_threads(2); - ASSERT_EQ(torch::get_num_threads(), 2); torch::set_num_interop_threads(2); - ASSERT_EQ(torch::get_num_interop_threads(), 2); + torch::get_num_threads(); + torch::get_num_interop_threads(); }