[Code Clean] Replace std::runtime_error with TORCH_CHECK (#165305)

Fixes part of #148114

Including:

- torch/csrc/distributed

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165305
Approved by: https://github.com/FFFrog, https://github.com/albanD
This commit is contained in:
orangeH25
2025-10-18 01:08:40 +00:00
committed by PyTorch MergeBot
parent 29b029648e
commit e9f4999985
4 changed files with 19 additions and 22 deletions

View File

@ -361,13 +361,12 @@ class UvTcpServer : public UvTcpSocket {
int addr_len = sizeof(addr_s); int addr_len = sizeof(addr_s);
if (uv_tcp_getsockname( TORCH_CHECK(
uv_tcp_getsockname(
(uv_tcp_t*)unsafeGetStream(), (uv_tcp_t*)unsafeGetStream(),
reinterpret_cast<::sockaddr*>(&addr_s), reinterpret_cast<::sockaddr*>(&addr_s),
&addr_len) != 0) { &addr_len) == 0,
throw std::runtime_error(
"The port number of the socket cannot be retrieved."); "The port number of the socket cannot be retrieved.");
}
if (addr_s.ss_family == AF_INET) { if (addr_s.ss_family == AF_INET) {
portNum_ = ntohs(reinterpret_cast<sockaddr_in*>(&addr_s)->sin_port); portNum_ = ntohs(reinterpret_cast<sockaddr_in*>(&addr_s)->sin_port);

View File

@ -49,7 +49,7 @@ void StoreCollectives::barrier(
msg += fmt::format("{}, ", i); msg += fmt::format("{}, ", i);
} }
} }
throw std::runtime_error(msg + e.what()); TORCH_CHECK(false, msg, e.what());
} }
} }
} }
@ -118,7 +118,7 @@ std::vector<std::vector<uint8_t>> StoreCollectives::gatherRecv(
msg += fmt::format("{}, ", i); msg += fmt::format("{}, ", i);
} }
} }
throw std::runtime_error(msg + e.what()); TORCH_CHECK(false, msg, e.what());
} }
// insert local data // insert local data
@ -194,7 +194,7 @@ std::vector<std::vector<uint8_t>> StoreCollectives::allGather(
msg += fmt::format("{}, ", i); msg += fmt::format("{}, ", i);
} }
} }
throw std::runtime_error(msg + e.what()); TORCH_CHECK(false, msg, e.what());
} }
} }

View File

@ -4,6 +4,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <c10/util/Exception.h>
#include <c10/util/tempfile.h> #include <c10/util/tempfile.h>
#include <torch/csrc/distributed/c10d/exception.h> #include <torch/csrc/distributed/c10d/exception.h>
#include <torch/csrc/utils/pybind.h> #include <torch/csrc/utils/pybind.h>
@ -17,9 +18,7 @@ RegisterHandler tracebackHandler{
auto tmpfile = c10::make_tempfile("torch-dump_traceback"); auto tmpfile = c10::make_tempfile("torch-dump_traceback");
auto cfile = ::fopen(tmpfile.name.c_str(), "w"); auto cfile = ::fopen(tmpfile.name.c_str(), "w");
if (!cfile) { TORCH_CHECK(cfile, "failed to open file for writing");
throw std::runtime_error("failed to open file for writing");
}
{ {
py::gil_scoped_acquire guard{}; py::gil_scoped_acquire guard{};

View File

@ -2,6 +2,7 @@
#include <unordered_map> #include <unordered_map>
#include <ATen/core/interned_strings.h> #include <ATen/core/interned_strings.h>
#include <c10/util/Exception.h>
#include <c10/util/FileSystem.h> #include <c10/util/FileSystem.h>
#include <c10/util/thread_name.h> #include <c10/util/thread_name.h>
#include <caffe2/utils/threadpool/WorkersPool.h> #include <caffe2/utils/threadpool/WorkersPool.h>
@ -144,22 +145,20 @@ WorkerServer::WorkerServer(const std::string& hostOrFile, int port) {
if (port == -1) { if (port == -1) {
// using unix sockets // using unix sockets
server_.set_address_family(AF_UNIX); server_.set_address_family(AF_UNIX);
TORCH_CHECK(
if (c10::filesystem::exists(hostOrFile)) { !c10::filesystem::exists(hostOrFile),
throw std::runtime_error(fmt::format("{} already exists", hostOrFile)); fmt::format("{} already exists", hostOrFile));
}
C10D_WARNING("Server listening to UNIX {}", hostOrFile); C10D_WARNING("Server listening to UNIX {}", hostOrFile);
if (!server_.bind_to_port(hostOrFile, 80)) { TORCH_CHECK(
throw std::runtime_error(fmt::format("Error binding to {}", hostOrFile)); server_.bind_to_port(hostOrFile, 80),
} fmt::format("Error binding to {}", hostOrFile));
} else { } else {
C10D_WARNING("Server listening to TCP {}:{}", hostOrFile, port); C10D_WARNING("Server listening to TCP {}:{}", hostOrFile, port);
if (!server_.bind_to_port(hostOrFile, port)) { TORCH_CHECK(
throw std::runtime_error( server_.bind_to_port(hostOrFile, port),
fmt::format("Error binding to {}:{}", hostOrFile, port)); fmt::format("Error binding to {}:{}", hostOrFile, port));
} }
}
serverThread_ = std::thread([this]() { serverThread_ = std::thread([this]() {
c10::setThreadName("pt_workerserver"); c10::setThreadName("pt_workerserver");