[2/N] Remove unnecessary once flag usage (#145057)

Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/145057
Approved by: https://github.com/albanD
This commit is contained in:
cyy 2025-01-23 09:48:44 +00:00 committed by PyTorch MergeBot
parent b6941d4e42
commit 29f52e3972
11 changed files with 32 additions and 50 deletions

View file

@ -48,24 +48,18 @@ bool _nnpack_available() {
namespace at::native {
static bool init_nnpack() {
static c10::once_flag once_;
static bool nnpack_successfully_initialized_ = false;
const static nnp_status nnpack_status = nnp_initialize();
auto nnpack_successfully_initialized_ = (nnp_status_success == nnpack_status);
c10::call_once(once_, []() {
const nnp_status nnpack_status = nnp_initialize();
nnpack_successfully_initialized_ = (nnp_status_success == nnpack_status);
if (nnpack_status != nnp_status_success) {
if (nnpack_status == nnp_status_out_of_memory) {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Out of memory.";
} else if (nnpack_status == nnp_status_unsupported_hardware) {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Unsupported hardware.";
} else {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Unknown error!";
}
if (nnpack_status != nnp_status_success) {
if (nnpack_status == nnp_status_out_of_memory) {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Out of memory.";
} else if (nnpack_status == nnp_status_unsupported_hardware) {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Unsupported hardware.";
} else {
LOG(WARNING) << "Could not initialize NNPACK! Reason: Unknown error!";
}
});
}
return nnpack_successfully_initialized_;
}

View file

@ -4,15 +4,10 @@
#include <c10/util/Exception.h>
#include <pytorch_qnnpack.h>
#include <c10/util/CallOnce.h>
namespace at::native {
void initQNNPACK() {
static c10::once_flag once;
static enum pytorch_qnnp_status qnnpackStatus =
pytorch_qnnp_status_uninitialized;
c10::call_once(once, []() { qnnpackStatus = pytorch_qnnp_initialize(); });
static enum pytorch_qnnp_status qnnpackStatus = pytorch_qnnp_initialize();
TORCH_CHECK(
qnnpackStatus == pytorch_qnnp_status_success,
"failed to initialize QNNPACK");

View file

@ -59,9 +59,8 @@ static void forked_autograd_child() {
// Should be called before unsafe for forks (thread pool) calls
static void track_bad_autograd_forks() {
#if !defined(WIN32)
static c10::once_flag flag;
c10::call_once(
flag, [&] { pthread_atfork(nullptr, nullptr, forked_autograd_child); });
static auto result [[maybe_unused]] =
pthread_atfork(nullptr, nullptr, forked_autograd_child);
#endif
}

View file

@ -73,8 +73,8 @@ static void forked_child() {
// has some working functions (e.g. device_count) but cannot fully initialize.
static void poison_fork() {
#ifndef WIN32
static c10::once_flag flag;
c10::call_once(flag, [] { pthread_atfork(nullptr, nullptr, forked_child); });
static auto result [[maybe_unused]] =
pthread_atfork(nullptr, nullptr, forked_child);
#endif
}

View file

@ -98,8 +98,7 @@ CapturedTraceback* getFromContext(
}
void _initRecordAnnotations() {
static c10::once_flag ra_init;
c10::call_once(ra_init, [&] {
static auto init_placeholder [[maybe_unused]] = [&] {
// Save user annotations to CCA memory snapshot tool
at::addThreadLocalCallback(
at::RecordFunctionCallback(
@ -114,7 +113,8 @@ void _initRecordAnnotations() {
{{"name", fn.name()}, {"stage", "END"}});
})
.scopes({at::RecordScope::USER_SCOPE}));
});
return true;
}();
}
} // namespace

View file

@ -390,11 +390,9 @@ std::unordered_map<std::string, std::string> NCCLComm::ncclCommDump() {
#endif
std::string getNcclVersion() {
static c10::once_flag ncclGetVersionFlag;
static std::string versionString;
c10::call_once(ncclGetVersionFlag, []() {
static std::string versionString = []() {
int version = 0;
std::string versionString;
ncclResult_t status = ncclGetVersion(&version);
// can't compute the version if call did not return successfully or version
// code < 100 (corresponding to 0.1.0)
@ -417,7 +415,8 @@ std::string getNcclVersion() {
}
#endif
}
});
return versionString;
}();
return versionString;
}

View file

@ -226,8 +226,6 @@ void ProcessGroupMPI::AsyncWork::populateException() {
// Static global states
int ProcessGroupMPI::mpiThreadSupport_ = 0;
std::mutex ProcessGroupMPI::pgGlobalMutex_;
// We only want to initialize once
c10::once_flag ProcessGroupMPI::onceFlagInitMPI;
void ProcessGroupMPI::mpiExit() {
std::unique_lock<std::mutex> globalLock(pgGlobalMutex_);
@ -235,8 +233,8 @@ void ProcessGroupMPI::mpiExit() {
}
void ProcessGroupMPI::initMPIOnce() {
// Initialize MPI environment
c10::call_once(onceFlagInitMPI, []() {
// Initialize MPI environment. We only want to initialize once.
static bool init_mpi_flag [[maybe_unused]] = []() {
int mpi_was_initialized = 0;
MPI_CHECK(MPI_Initialized(&mpi_was_initialized));
if (mpi_was_initialized == 0) {
@ -256,7 +254,8 @@ void ProcessGroupMPI::initMPIOnce() {
} else {
TORCH_WARN_ONCE("MPI was previously initialized.");
}
});
return true;
}();
}
c10::intrusive_ptr<ProcessGroupMPI> ProcessGroupMPI::createProcessGroupMPI(

View file

@ -258,7 +258,6 @@ class TORCH_API ProcessGroupMPI : public Backend {
// Global states
static void initMPIOnce();
static void mpiExit();
static c10::once_flag onceFlagInitMPI;
static std::mutex pgGlobalMutex_;
static int mpiThreadSupport_;

View file

@ -1,7 +1,6 @@
#define PYBIND11_DETAILED_ERROR_MESSAGES
#include <ATen/ATen.h>
#include <c10/util/CallOnce.h>
#include <pybind11/pytypes.h>
#include <torch/csrc/Generator.h>
#include <torch/csrc/THP.h>
@ -35,9 +34,8 @@ static void forked_mps_child() {
// Should be called before the first mps call.
static void track_bad_mps_fork() {
#ifndef WIN32
static c10::once_flag flag;
c10::call_once(
flag, [] { pthread_atfork(nullptr, nullptr, forked_mps_child); });
static auto result [[maybe_unused]] =
pthread_atfork(nullptr, nullptr, forked_mps_child);
#endif
}
} // namespace

View file

@ -1,7 +1,6 @@
#include <ATen/ATen.h>
#include <c10/core/DeviceType.h>
#include <c10/core/Stream.h>
#include <c10/util/CallOnce.h>
#include <torch/csrc/Generator.h>
#include <torch/csrc/Stream.h>
#include <torch/csrc/python_headers.h>
@ -28,8 +27,8 @@ static void forked_child() {
// has some working functions (e.g. device_count) but cannot fully initialize.
static void poison_fork() {
#ifndef WIN32
static c10::once_flag flag;
c10::call_once(flag, [] { pthread_atfork(nullptr, nullptr, forked_child); });
static auto result [[maybe_unused]] =
pthread_atfork(nullptr, nullptr, forked_child);
#endif
}

View file

@ -32,8 +32,8 @@ static void forked_child() {
// has some working functions (e.g. device_count) but cannot fully initialize.
static void poison_fork() {
#ifndef WIN32
static c10::once_flag flag;
c10::call_once(flag, [] { pthread_atfork(nullptr, nullptr, forked_child); });
static auto result [[maybe_unused]] =
pthread_atfork(nullptr, nullptr, forked_child);
#endif
}