-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Differential Revision: D60867909 Pull Request resolved: #774
- Loading branch information
Showing
9 changed files
with
266 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#ifdef TORCHAO_PARALLEL_ATEN | ||
#pragma message("TORCHAO_PARALLEL_ATEN is set. Using ATen parallel backend.") | ||
|
||
// TODO(T200106949): reconcile at::parallel_for's grain_size with what is needed | ||
// in torchao::parallel_for | ||
#error "TORCHAO_PARALLEL_ATEN is not implemented yet" | ||
|
||
#else | ||
#ifdef TORCHAO_PARALLEL_EXECUTORCH | ||
#pragma message( \ | ||
"TORCHAO_PARALLEL_EXECUTORCH is set. Using ExecuTorch parallel backend.") | ||
|
||
#error "TORCHAO_PARALLEL_EXECUTORCH is not implemented yet" | ||
|
||
#else | ||
#ifdef TORCHAO_PARALLEL_PTHREADPOOL | ||
#pragma message( \ | ||
"TORCHAO_PARALLEL_PTHREADPOOL is set. Using pthreadpool parallel backend.") | ||
#include <torchao/experimental/kernels/cpu/parallel-pthreadpool-impl.h> | ||
|
||
#else | ||
#ifdef TORCHAO_PARALLEL_OMP | ||
#pragma message("TORCHAO_PARALLEL_OMP is set. Using OMP parallel backend.") | ||
#include <torchao/experimental/kernels/cpu/parallel-omp-impl.h> | ||
|
||
#else | ||
#if defined TORCHAO_PARALLEL_SINGLE_THREADED | ||
#pragma message( \ | ||
"TORCHAO_PARALLEL_SINGLE_THREADED is set. Using single-threaded parallel backend.") | ||
#include <torchao/experimental/kernels/cpu/parallel-single_threaded-impl.h> | ||
|
||
#else | ||
#if defined TORCHAO_PARALLEL_TEST_DUMMY | ||
#pragma message( \ | ||
"TORCHAO_PARALLEL_TEST_DUMMY is set. Using test dummy parallel backend.") | ||
#include <torchao/experimental/kernels/cpu/parallel-test_dummy-impl.h> | ||
|
||
#else | ||
#error \ | ||
"Set parallel backend by defining one of the following: \ | ||
TORCHAO_PARALLEL_ATEN, \ | ||
TORCHAO_PARALLEL_EXECUTORCH, \ | ||
TORCHAO_PARALLEL_PTHREADPOOL, \ | ||
TORCHAO_PARALLEL_OMP, \ | ||
TORCHAO_PARALLEL_SINGLE_THREADED, \ | ||
TORCHAO_PARALLEL_TEST_DUMMY" | ||
|
||
#endif // TORCHAO_PARALLEL_TEST_DUMMY | ||
#endif // TORCHAO_PARALLEL_SINGLE_THREADED | ||
#endif // TORCHAO_PARALLEL_OMP | ||
#endif // TORCHAO_PARALLEL_PTHREADPOOL | ||
#endif // TORCHAO_PARALLEL_EXECUTORCH | ||
#endif // TORCHAO_PARALLEL_ATEN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
#include <omp.h> | ||
|
||
template <typename F> | ||
void torchao::parallel_for( | ||
const int64_t begin, | ||
const int64_t end, | ||
const int64_t grain_size, | ||
const F& f) { | ||
#pragma omp parallel | ||
{ | ||
#pragma omp for | ||
for (int i = begin; i < end; i += grain_size) { | ||
f(i, i + grain_size); | ||
} | ||
} | ||
} | ||
|
||
void torchao::set_num_threads(int num_threads) { | ||
omp_set_num_threads(num_threads); | ||
} | ||
int torchao::get_num_threads() { | ||
// omp_get_num_threads returns the number of threads | ||
// in the current code section, which will be 1 in the routines | ||
// that select tiling params | ||
return omp_get_max_threads(); | ||
} |
83 changes: 83 additions & 0 deletions
83
torchao/experimental/kernels/cpu/parallel-pthreadpool-impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
#include <pthreadpool.h> | ||
#include <stdexcept> | ||
|
||
namespace torchao::parallel::internal { | ||
class Threadpool { | ||
private: | ||
pthreadpool_t pthreadpool_{nullptr}; | ||
|
||
public: | ||
Threadpool(size_t num_threads = 0) { | ||
pthreadpool_ = pthreadpool_create(num_threads); | ||
if (pthreadpool_ == nullptr) { | ||
throw std::runtime_error("Failed to create pthreadpool."); | ||
} | ||
} | ||
~Threadpool() { | ||
pthreadpool_destroy(pthreadpool_); | ||
pthreadpool_ = nullptr; | ||
} | ||
pthreadpool_t get() { | ||
return pthreadpool_; | ||
} | ||
size_t get_num_threads() { | ||
if (pthreadpool_ == nullptr) { | ||
return 0; | ||
} | ||
return pthreadpool_get_threads_count(pthreadpool_); | ||
} | ||
void set_num_threads(size_t num_threads) { | ||
if (num_threads == get_num_threads()) { | ||
return; | ||
} | ||
pthreadpool_destroy(pthreadpool_); | ||
pthreadpool_ = pthreadpool_create(num_threads); | ||
} | ||
}; | ||
|
||
template <typename F> | ||
struct Context { | ||
const F& f; | ||
int grain_size; | ||
Context(const F& f, int grain_size) : f{f}, grain_size{grain_size} {} | ||
}; | ||
|
||
template <typename F> | ||
static void task(Context<F>* context, size_t grain_idx) { | ||
int i = grain_idx * context->grain_size; | ||
context->f(i, i + context->grain_size); | ||
} | ||
|
||
static Threadpool threadpool; | ||
} // namespace torchao::parallel::internal | ||
|
||
int torchao::get_num_threads() { | ||
return torchao::parallel::internal::threadpool.get_num_threads(); | ||
} | ||
|
||
void torchao::set_num_threads(int num_threads) { | ||
torchao::parallel::internal::threadpool.set_num_threads(num_threads); | ||
} | ||
|
||
template <typename F> | ||
void torchao::parallel_for( | ||
const int64_t begin, | ||
const int64_t end, | ||
const int64_t grain_size, | ||
const F& f) { | ||
int grain_idx_end = end / grain_size; | ||
auto context = torchao::parallel::internal::Context<F>(f, grain_size); | ||
pthreadpool_parallelize_1d( | ||
torchao::parallel::internal::threadpool.get(), | ||
(pthreadpool_task_1d_t)torchao::parallel::internal::task<F>, | ||
(void**)&context, | ||
grain_idx_end, | ||
0 /* flags */); | ||
} |
23 changes: 23 additions & 0 deletions
23
torchao/experimental/kernels/cpu/parallel-single_threaded-impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Meta Platforms, Inc. and affiliates. | ||
// All rights reserved. | ||
// | ||
// This source code is licensed under the license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
template <typename F> | ||
void torchao::parallel_for( | ||
const int64_t begin, | ||
const int64_t end, | ||
const int64_t grain_size, | ||
const F& f) { | ||
for (int i = begin; i < end; i += grain_size) { | ||
f(i, i + grain_size); | ||
} | ||
} | ||
|
||
void torchao::set_num_threads(int num_threads) {} | ||
int torchao::get_num_threads() { | ||
return 1; | ||
} |
Oops, something went wrong.