Skip to content

Commit

Permalink
release np array in main thread to eliminate the time-consuming gil r…
Browse files Browse the repository at this point in the history
…equest (#10050)

Signed-off-by: daquexian <daquexian566@gmail.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
daquexian and mergify[bot] authored Apr 1, 2023
1 parent 6514a17 commit b7aac72
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
4 changes: 1 addition & 3 deletions ci/fixed-dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
numpy==1.22.1 ; python_version >= "3.10"
numpy==1.20.0 ; python_version >= "3.9" and python_version < "3.10"
numpy==1.18.0 ; python_version >= "3.8" and python_version < "3.9"
numpy==1.17.0 ; python_version >= "3.6" and python_version < "3.8"
numpy==1.21.6 ; python_version >= "3.7" and python_version < "3.10"
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
black==19.10b0; python_version >= "3.6"
click==8.0.0; python_version >= "3.6" # https://github.com/psf/black/issues/2964
numpy>=1.17.0
numpy>=1.21.6
protobuf>=3.9.2, <4.0
wheel
tqdm
Expand Down
6 changes: 3 additions & 3 deletions oneflow/api/python/utils/tensor_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ inline Maybe<void> CopyBetweenLocalTensorAndNumpy(
} else {
Py_INCREF(array);
NumPyArrayPtr array_ptr(array, [array]() {
CHECK_JUST(Singleton<ForeignLockHelper>::Get()->WithScopedAcquire([&]() -> Maybe<void> {
// release array in main thread to eliminate the time-consuming gil request
CHECK_JUST(SingletonMaybe<VirtualMachine>())->add_main_thread_pending_task([array]() {
Py_DECREF(array);
return Maybe<void>::Ok();
}));
});
});

JUST(PhysicalRun([&](InstructionsBuilder* builder) -> Maybe<void> {
Expand Down
10 changes: 10 additions & 0 deletions oneflow/core/vm/virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Maybe<void> VirtualMachine::ShrinkAllMem() {

VirtualMachine::~VirtualMachine() {
if (!threads_closed_) { CHECK_JUST(CloseVMThreads()); }
RunMainThreadPendingTasks();
CHECK(engine_->SchedulerEmpty());
engine_.Reset();
}
Expand Down Expand Up @@ -238,8 +239,17 @@ std::string VirtualMachine::GetBlockingDebugString() {
return engine_->GetLivelyInstructionListDebugString(limit);
}

void VirtualMachine::RunMainThreadPendingTasks() {
std::unique_lock lock(main_thread_pending_tasks_mutex_);
for (const auto& main_thread_pending_task : main_thread_pending_tasks_) {
main_thread_pending_task();
}
main_thread_pending_tasks_.clear();
}

Maybe<void> VirtualMachine::Receive(vm::InstructionList* instruction_list) {
SyncVmModeGuard guard(SyncVmMode::kEnable);
RunMainThreadPendingTasks();
if (unlikely(pthread_fork::IsForkedSubProcess())) {
INTRUSIVE_FOR_EACH_PTR(instruction, instruction_list) {
const auto& device = instruction->stream().device();
Expand Down
10 changes: 10 additions & 0 deletions oneflow/core/vm/virtual_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class VirtualMachine final {

size_t flying_instruction_cnt() const { return engine().flying_instruction_cnt(); }

void add_main_thread_pending_task(std::function<void()> task) {
std::unique_lock lock(main_thread_pending_tasks_mutex_);
main_thread_pending_tasks_.push_back(std::move(task));
}

private:
friend class InstructionsBuilder;

Expand Down Expand Up @@ -80,6 +85,8 @@ class VirtualMachine final {

Maybe<void> CloseWorkerThreads();

void RunMainThreadPendingTasks();

bool multi_thread_;
bool threads_closed_;
bool scheduler_stopped_;
Expand All @@ -100,6 +107,9 @@ class VirtualMachine final {

std::thread schedule_thread_;
Notifier pending_notifier_;

std::mutex main_thread_pending_tasks_mutex_;
std::vector<std::function<void()>> main_thread_pending_tasks_;
};

} // namespace oneflow
Expand Down

0 comments on commit b7aac72

Please sign in to comment.