Skip to content

Commit 1ec76dd

Browse files
cyyeverpytorchmergebot
authored andcommitted
Enable clang-tidy on torch/csrc/distributed (pytorch#139043)
Fixes #ISSUE_NUMBER Pull Request resolved: pytorch#139043 Approved by: https://github.com/Skylion007
1 parent 60d1c71 commit 1ec76dd

File tree

4 files changed

+39
-58
lines changed

4 files changed

+39
-58
lines changed

.lintrunner.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ include_patterns = [
214214
'torch/csrc/*.cpp',
215215
'torch/csrc/**/*.h',
216216
'torch/csrc/**/*.cpp',
217-
'torch/csrc/distributed/autograd/**/*.cpp',
218-
'torch/csrc/distributed/autograd/**/*.h',
219-
'torch/csrc/distributed/rpc/**/*.cpp',
220-
'torch/csrc/distributed/rpc/**/*.h',
221217
'torch/csrc/jit/serialization/*.h',
222218
'torch/csrc/jit/serialization/*.cpp',
223219
]
@@ -245,7 +241,11 @@ exclude_patterns = [
245241
'torch/csrc/api/include/torch/linalg.h',
246242
'torch/csrc/api/include/torch/nn/pimpl-inl.h',
247243
'torch/csrc/autograd/generated/**',
248-
'torch/csrc/distributed/**/*',
244+
'torch/csrc/distributed/**/*.cu',
245+
'torch/csrc/distributed/c10d/CUDASymmetricMemory-inl.h',
246+
'torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp',
247+
'torch/csrc/distributed/c10d/WinSockUtils.hpp',
248+
'torch/csrc/distributed/c10d/quantization/quantization_gpu.h',
249249
'torch/csrc/dynamo/eval_frame.h',
250250
'torch/csrc/inductor/aoti_torch/c/shim.h',
251251
'torch/csrc/jit/**/*',

torch/csrc/distributed/c10d/TCPStore.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,16 @@
55
#include <torch/csrc/distributed/c10d/Backoff.hpp>
66
#include <torch/csrc/distributed/c10d/TCPStore.hpp>
77
#include <torch/csrc/distributed/c10d/TCPStoreBackend.hpp>
8+
#include <torch/csrc/distributed/c10d/Utils.hpp>
89
#include <torch/csrc/distributed/c10d/logging.h>
910

10-
#include <fcntl.h>
1111
#include <chrono>
1212
#include <fstream>
13-
#include <random>
13+
#include <optional>
1414
#include <thread>
1515
#include <unordered_map>
1616
#include <utility>
1717

18-
#ifdef _WIN32
19-
#include <io.h>
20-
#include <winsock2.h>
21-
#else
22-
#include <poll.h>
23-
#include <unistd.h>
24-
#endif
25-
26-
#ifdef _WIN32
27-
#include <torch/csrc/distributed/c10d/WinSockUtils.hpp>
28-
#else
29-
#include <torch/csrc/distributed/c10d/UnixSockUtils.hpp>
30-
#endif
31-
32-
#include <torch/csrc/distributed/c10d/socket.h>
33-
3418
namespace c10d {
3519
namespace detail {
3620

@@ -143,11 +127,10 @@ class TCPClient {
143127
}
144128
}
145129
template <typename T>
146-
bool receiveValueWithTimeout(T& t, std::chrono::milliseconds timeout) {
130+
std::optional<T> receiveValueWithTimeout(std::chrono::milliseconds timeout) {
147131
if (!socket_.waitForInput(timeout))
148-
return false;
149-
t = tcputil::recvValue<T>(socket_.handle());
150-
return true;
132+
return {};
133+
return tcputil::recvValue<T>(socket_.handle());
151134
}
152135
void setTimeout(std::chrono::milliseconds value);
153136

@@ -200,8 +183,10 @@ void TCPClient::setTimeout(std::chrono::milliseconds value) {
200183

201184
class SendBuffer {
202185
// ethernet mtu 1500 - 40 (ip v6 header) - 20 (tcp header)
186+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
203187
const size_t FLUSH_WATERMARK = 1440;
204188
std::vector<uint8_t> buffer;
189+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
205190
detail::TCPClient& client;
206191

207192
void maybeFlush() {
@@ -557,10 +542,10 @@ void TCPStore::doWait(
557542
buffer.flush();
558543
}
559544

560-
detail::WaitResponseType response;
561-
if (client_->receiveValueWithTimeout<detail::WaitResponseType>(
562-
response, timeout)) {
563-
if (response != detail::WaitResponseType::STOP_WAITING) {
545+
auto response_opt =
546+
client_->receiveValueWithTimeout<detail::WaitResponseType>(timeout);
547+
if (response_opt.has_value()) {
548+
if (response_opt != detail::WaitResponseType::STOP_WAITING) {
564549
TORCH_CHECK(false, "Stop_waiting response is expected");
565550
}
566551
return;
@@ -572,7 +557,7 @@ void TCPStore::doWait(
572557
buffer.flush();
573558
}
574559

575-
response = client_->receiveValue<detail::WaitResponseType>();
560+
auto response = client_->receiveValue<detail::WaitResponseType>();
576561
// this can happen if the server responds before we cancel, just ignore it
577562
if (response != detail::WaitResponseType::WAIT_CANCELED) {
578563
if (response != detail::WaitResponseType::STOP_WAITING) {
@@ -639,7 +624,7 @@ void TCPStore::multiSet(
639624
const std::lock_guard<std::mutex> lock(activeOpLock_);
640625

641626
detail::SendBuffer buffer(*client_, detail::QueryType::MULTI_SET);
642-
buffer.appendValue<std::int64_t>(keys.size());
627+
buffer.appendValue<std::int64_t>(static_cast<int64_t>(keys.size()));
643628
for (auto i : c10::irange(keys.size())) {
644629
buffer.appendString(keyPrefix_ + keys[i]);
645630
buffer.appendBytes(values[i]);

torch/csrc/distributed/c10d/TCPStoreBackend.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11

22
#include <c10/util/irange.h>
3-
#include <fcntl.h>
43
#include <algorithm>
54
#include <array>
6-
#include <system_error>
75
#include <unordered_map>
86
#include <utility>
97

10-
#ifdef _WIN32
11-
#include <io.h>
12-
#include <winsock2.h>
13-
#else
14-
#include <poll.h>
15-
#include <unistd.h>
16-
#endif
17-
188
#include <c10/util/thread_name.h>
199
#include <torch/csrc/distributed/c10d/TCPStoreBackend.hpp>
2010
#include <torch/csrc/distributed/c10d/logging.h>
@@ -111,7 +101,7 @@ class TCPStoreMasterDaemon : public BackgroundThread {
111101
const std::chrono::milliseconds checkTimeout_ = std::chrono::milliseconds{10};
112102
HANDLE ghStopEvent_{};
113103
#else
114-
std::array<int, 2> controlPipeFd_{{-1, -1}};
104+
std::array<int, 2> controlPipeFd_{-1, -1};
115105
#endif
116106
};
117107

@@ -217,8 +207,10 @@ void TCPStoreMasterDaemon::queryFds(std::vector<struct pollfd>& fds) {
217207
// we hit an exception here.
218208
clearSocketWaitState(fds[fdIdx].fd);
219209

220-
fds.erase(fds.begin() + fdIdx);
221-
sockets_.erase(sockets_.begin() + fdIdx - CONNECT_SOCKET_OFFSET);
210+
fds.erase(fds.begin() + static_cast<std::ptrdiff_t>(fdIdx));
211+
sockets_.erase(
212+
sockets_.begin() + static_cast<std::ptrdiff_t>(fdIdx) -
213+
CONNECT_SOCKET_OFFSET);
222214
--fdIdx;
223215
continue;
224216
}
@@ -256,7 +248,7 @@ void TCPStoreMasterDaemon::clearSocketWaitState(int socket) {
256248
// or, in the case of wait
257249
// type of query | number of args | size of arg1 | arg1 | ...
258250
void TCPStoreMasterDaemon::query(int socket) {
259-
QueryType qt;
251+
QueryType qt{};
260252
tcputil::recvBytes<QueryType>(socket, &qt, 1);
261253

262254
if (isMiscellaneousSocket(socket)) {
@@ -401,13 +393,13 @@ void TCPStoreMasterDaemon::getHandler(int socket) const {
401393
}
402394

403395
void TCPStoreMasterDaemon::getNumKeysHandler(int socket) const {
404-
tcputil::sendValue<int64_t>(socket, tcpStore_.size());
396+
tcputil::sendValue<size_t>(socket, tcpStore_.size());
405397
}
406398

407399
void TCPStoreMasterDaemon::deleteHandler(int socket) {
408400
std::string key = tcputil::recvString(socket);
409401
auto numDeleted = tcpStore_.erase(key);
410-
tcputil::sendValue<int64_t>(socket, numDeleted);
402+
tcputil::sendValue<size_t>(socket, numDeleted);
411403
}
412404

413405
void TCPStoreMasterDaemon::checkHandler(int socket) const {

torch/csrc/distributed/c10d/TCPStoreLibUvBackend.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <deque>
33
#include <exception>
44
#include <memory>
5-
#include <ostream>
65
#include <unordered_map>
76
#include <unordered_set>
87
#include <utility>
@@ -35,15 +34,15 @@ Other callbacks don't provide exception safety so avoid there.
3534
// This controls how many un-accepted TCP connections can be waiting in the
3635
// backlog. This should be at least world size to avoid issues on init. We set
3736
// it to -1 to use the host max value which is controlled by `soconnmax`.
38-
#define DEFAULT_BACKLOG -1
39-
#define MAX_KEY_COUNT (128 * 1024)
40-
#define MAX_STRING_LEN (8 * 1024)
41-
#define MAX_PAYLOAD_LEN (8 * 1024 * 1024)
37+
auto constexpr DEFAULT_BACKLOG = -1;
38+
auto constexpr MAX_KEY_COUNT = size_t(128 * 1024);
39+
auto constexpr MAX_STRING_LEN = 8 * 1024;
40+
auto constexpr MAX_PAYLOAD_LEN = 8 * 1024 * 1024;
4241

4342
// This controls the preferred size for buffers.
4443
// Too small and we'll need multiple buffers for one request
4544
// Too big and we might taxing malloc
46-
#define ALLOC_BUFFER_SIZE ((size_t)4000)
45+
auto constexpr ALLOC_BUFFER_SIZE = size_t(4000);
4746
class UvHandle : public c10::intrusive_ptr_target {
4847
public:
4948
~UvHandle() override = default;
@@ -105,7 +104,8 @@ class UvTcpSocket : public UvHandle {
105104
uv_handle_t* handle,
106105
size_t suggested_size,
107106
uv_buf_t* buf) {
108-
suggested_size = std::min(suggested_size, (size_t)ALLOC_BUFFER_SIZE);
107+
suggested_size = std::min(suggested_size, ALLOC_BUFFER_SIZE);
108+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
109109
buf->base = (char*)malloc(suggested_size);
110110
buf->len = suggested_size;
111111
}
@@ -486,6 +486,7 @@ class ChunkedStream {
486486

487487
void append(uv_buf_t buf) {
488488
if (buf.len == 0) {
489+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
489490
free(buf.base);
490491
} else {
491492
capacity += buf.len;
@@ -597,6 +598,7 @@ class ChunkedStream {
597598
}
598599

599600
for (size_t i = 0; i < buff_idx; ++i) {
601+
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
600602
free(buffers[0].base);
601603
capacity -= buffers[0].len;
602604
buffers.pop_front();
@@ -1259,12 +1261,14 @@ const std::vector<uint8_t>& LibUVStoreDaemon::compareAndSet(
12591261
if (expectedValue.empty()) {
12601262
tcpStore_[key] = newValue;
12611263
wakeupWaitingClients(key);
1264+
// NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
12621265
return newValue;
12631266
} else {
12641267
// TODO: This code path is not ideal as we are "lying" to the caller in
12651268
// case the key does not exist. We should come up with a working solution.
12661269
// It might make more sense to return ""
12671270
wakeupWaitingClients(key);
1271+
// NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
12681272
return expectedValue;
12691273
}
12701274
} else {
@@ -1326,11 +1330,11 @@ bool LibUVStoreDaemon::waitKeys(
13261330
}
13271331

13281332
int64_t LibUVStoreDaemon::size() {
1329-
return tcpStore_.size();
1333+
return static_cast<int64_t>(tcpStore_.size());
13301334
}
13311335

13321336
int64_t LibUVStoreDaemon::deleteKey(const std::string& key) {
1333-
return tcpStore_.erase(key);
1337+
return static_cast<int64_t>(tcpStore_.erase(key));
13341338
}
13351339

13361340
void LibUVStoreDaemon::append(

0 commit comments

Comments
 (0)