Skip to content

Commit

Permalink
Fix some code issues (pytorch#92760)
Browse files Browse the repository at this point in the history
Fixes #ISSUE_NUMBER

Pull Request resolved: pytorch#92760
Approved by: https://github.com/Skylion007, https://github.com/albanD
  • Loading branch information
cyyever authored and pytorchmergebot committed Jan 24, 2023
1 parent 3f64c96 commit 045d1de
Show file tree
Hide file tree
Showing 28 changed files with 49 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ cppcoreguidelines-*,
-facebook-hte-RelativeInclude,
hicpp-exception-baseclass,
hicpp-avoid-goto,
misc-unused-alias-decls,
misc-unused-using-decls,
modernize-*,
-modernize-concat-nested-namespaces,
-modernize-return-braced-init-list,
Expand Down
2 changes: 0 additions & 2 deletions aten/src/ATen/code_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ namespace jit {
// in the top level environment, and then recurses into a parent
// environment if the key is not found.)
struct TemplateEnv {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
TemplateEnv() : parent(nullptr) {}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
TemplateEnv(TemplateEnv& parent) : parent(&parent) {}

using string_list = std::vector<std::string>;
Expand Down
12 changes: 7 additions & 5 deletions aten/src/ATen/cuda/CUDAEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace at { namespace cuda {
struct TORCH_CUDA_CPP_API CUDAEvent {
// Constructors
// Default value for `flags` is specified below - it's cudaEventDisableTiming
CUDAEvent() {}
CUDAEvent(unsigned int flags) : flags_{flags} {}
CUDAEvent() noexcept = default;
CUDAEvent(unsigned int flags) noexcept : flags_{flags} {}

CUDAEvent(
DeviceIndex device_index, const cudaIpcEventHandle_t* handle) {
Expand Down Expand Up @@ -58,9 +58,11 @@ struct TORCH_CUDA_CPP_API CUDAEvent {
CUDAEvent(const CUDAEvent&) = delete;
CUDAEvent& operator=(const CUDAEvent&) = delete;

CUDAEvent(CUDAEvent&& other) { moveHelper(std::move(other)); }
CUDAEvent& operator=(CUDAEvent&& other) {
moveHelper(std::move(other));
CUDAEvent(CUDAEvent&& other) noexcept { moveHelper(std::move(other)); }
CUDAEvent& operator=(CUDAEvent&& other) noexcept {
if (this != &other) {
moveHelper(std::move(other));
}
return *this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PackBMatrix::PackBMatrix(
output_channels_ = output_channels;
packed_weights_ =
malloc(n_stride * (k_stride * sizeof(uint8_t) + sizeof(int32_t)));
if (packed_weights_ == NULL) {
if (packed_weights_ == nullptr) {
pytorch_qnnp_log_error(
"failed to allocate %zu bytes for packed weights",
n_stride * (k_stride * sizeof(uint8_t) + sizeof(int32_t)));
Expand Down
1 change: 0 additions & 1 deletion c10/core/CPUAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ template <uint32_t PreGuardBytes, uint32_t PostGuardBytes>
class DefaultMobileCPUAllocator final : public at::Allocator {
public:
DefaultMobileCPUAllocator() = default;
// NOLINTNEXTLINE(modernize-use-override)
~DefaultMobileCPUAllocator() override = default;

static void deleter(void* const pointer) {
Expand Down
5 changes: 2 additions & 3 deletions c10/core/GeneratorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ namespace detail {
#if !defined(_WIN32)
static uint64_t readURandomLong() {
int randDev = open("/dev/urandom", O_RDONLY);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
uint64_t randValue;
TORCH_CHECK(randDev >= 0, "Unable to open /dev/urandom");
uint64_t randValue{};
ssize_t readBytes = read(randDev, &randValue, sizeof(randValue));
close(randDev);
TORCH_CHECK(
readBytes >= (ssize_t)sizeof(randValue),
"Unable to read from /dev/urandom");
close(randDev);
return randValue;
}
#endif // _WIN32
Expand Down
2 changes: 0 additions & 2 deletions c10/core/TensorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ TensorImpl::TensorImpl(
// the Python and PythonTLSSnapshot dispatch keys will be set and all is well.
// The point is to delay the dispatch key setting until that point.

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
TensorImpl::TensorImpl(
ImplType type,
Storage&& storage,
Expand All @@ -129,7 +128,6 @@ TensorImpl::TensorImpl(
c10::optional<c10::Device> device_opt)
: TensorImpl({}, key_set, data_type, device_opt) {}

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
TensorImpl::TensorImpl(
Storage&& storage,
DispatchKeySet key_set,
Expand Down
6 changes: 3 additions & 3 deletions c10/util/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::function<string(void)>* GetFetchStackTrace() {
} // namespace

void SetStackTraceFetcher(std::function<string(void)> fetcher) {
*GetFetchStackTrace() = fetcher;
*GetFetchStackTrace() = std::move(fetcher);
}

void ThrowEnforceNotMet(
Expand Down Expand Up @@ -113,13 +113,13 @@ DDPUsageLoggerType* GetDDPUsageLogger() {

void SetAPIUsageLogger(std::function<void(const std::string&)> logger) {
TORCH_CHECK(logger);
*GetAPIUsageLogger() = logger;
*GetAPIUsageLogger() = std::move(logger);
}

void SetPyTorchDDPUsageLogger(
std::function<void(const DDPLoggingData&)> logger) {
TORCH_CHECK(logger);
*GetDDPUsageLogger() = logger;
*GetDDPUsageLogger() = std::move(logger);
}

void LogAPIUsage(const std::string& event) try {
Expand Down
3 changes: 1 addition & 2 deletions c10/util/Type_demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ std::string demangle(const char* name) {
abi::__cxa_demangle(
name,
/*__output_buffer=*/nullptr,
// NOLINTNEXTLINE(modernize-use-nullptr)
/*__length=*/0,
/*__length=*/nullptr,
&status),
/*deleter=*/free);

Expand Down
3 changes: 1 addition & 2 deletions caffe2/operators/pow_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ struct EigenPowFunctor {
template <int b_is_scalar, typename T1, typename T2, typename R>
inline void
Run(size_t n, const T1* a, const T2* b, T2 e, R* out, CPUContext*) {
// NOLINTNEXTLINE(modernize-use-nullptr)
if (b == NULL) {
if (b == nullptr) {
EigenVectorArrayMap<R>(out, n) =
EIGEN_POW((ConstEigenVectorArrayMap<T1>(a, n)), (e));
} else {
Expand Down
3 changes: 1 addition & 2 deletions caffe2/share/contrib/nnpack/conv_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ bool NNPACKConvOp::RunOnDeviceWithOrderNCHW() {
ConvPoolOpBase<CPUContext>::SetOutputSize(X, Y, filter.dim32(0));
const int oH = Y->dim32(2), oW = Y->dim32(3);

// NOLINTNEXTLINE(modernize-use-nullptr)
const float* biasData = NULL;
const float* biasData = nullptr;
if (InputSize() == 3) {
/* Convolution with bias */
auto& bias = Input(2);
Expand Down
1 change: 0 additions & 1 deletion torch/csrc/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <ATen/Device.h>

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct TORCH_API THPDevice {
PyObject_HEAD at::Device device;
};
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/api/include/torch/nn/modules/embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TORCH_API EmbeddingImpl : public torch::nn::Cloneable<EmbeddingImpl> {
public:
EmbeddingImpl(int64_t num_embeddings, int64_t embedding_dim)
: EmbeddingImpl(EmbeddingOptions(num_embeddings, embedding_dim)) {}
explicit EmbeddingImpl(const EmbeddingOptions& options_);
explicit EmbeddingImpl(EmbeddingOptions options_);

void reset() override;

Expand Down Expand Up @@ -110,7 +110,7 @@ class TORCH_API EmbeddingBagImpl
public:
EmbeddingBagImpl(int64_t num_embeddings, int64_t embedding_dim)
: EmbeddingBagImpl(EmbeddingBagOptions(num_embeddings, embedding_dim)) {}
explicit EmbeddingBagImpl(const EmbeddingBagOptions& options_);
explicit EmbeddingBagImpl(EmbeddingBagOptions options_);

void reset() override;

Expand Down
2 changes: 0 additions & 2 deletions torch/csrc/api/src/nn/modules/batchnorm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <utility>
#include <vector>

namespace F = torch::nn::functional;

namespace torch {
namespace nn {

Expand Down
8 changes: 4 additions & 4 deletions torch/csrc/api/src/nn/modules/embedding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace F = torch::nn::functional;

namespace torch {
namespace nn {
EmbeddingImpl::EmbeddingImpl(const EmbeddingOptions& options_)
: options(options_) { // NOLINT(modernize-pass-by-value)
EmbeddingImpl::EmbeddingImpl(EmbeddingOptions options_)
: options(std::move(options_)) {
// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
reset();
}
Expand Down Expand Up @@ -89,8 +89,8 @@ torch::Tensor EmbeddingImpl::forward(const Tensor& input) {
options.sparse());
}

EmbeddingBagImpl::EmbeddingBagImpl(const EmbeddingBagOptions& options_)
: options(options_) { // NOLINT(modernize-pass-by-value)
EmbeddingBagImpl::EmbeddingBagImpl(EmbeddingBagOptions options_)
: options(std::move(options_)) {
// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
reset();
}
Expand Down
2 changes: 0 additions & 2 deletions torch/csrc/api/src/nn/modules/instancenorm.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <torch/nn/functional/instancenorm.h>
#include <torch/nn/modules/instancenorm.h>

namespace F = torch::nn::functional;

namespace torch {
namespace nn {

Expand Down
2 changes: 0 additions & 2 deletions torch/csrc/autograd/functions/accumulate_grad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <stdexcept>
#include <utility>

using at::Tensor;

namespace torch {
namespace autograd {

Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/autograd/profiler_kineto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ struct KinetoThreadLocalState : public ProfilerStateBase {
std::set<torch::profiler::impl::ActivityType> activities)
: ProfilerStateBase(config),
start_time_(getTimeUs()),
record_queue_(config, activities) {}
record_queue_(config, std::move(activities)) {}
~KinetoThreadLocalState() override = default;

static KinetoThreadLocalState* get(bool global) {
Expand Down
3 changes: 0 additions & 3 deletions torch/csrc/cuda/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ static PyObject* THCPEvent_pynew(
return nullptr;
}

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
THCPEvent* self = (THCPEvent*)ptr.get();
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
unsigned int flags = (blocking ? cudaEventBlockingSync : cudaEventDefault) |
(enable_timing ? cudaEventDefault : cudaEventDisableTiming) |
(interprocess ? cudaEventInterprocess : cudaEventDefault);
Expand Down Expand Up @@ -88,7 +86,6 @@ static PyObject* THCPEvent_from_ipc_handle(
if (!ptr) {
return nullptr;
}
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
THCPEvent* self = (THCPEvent*)ptr.get();

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
Expand Down
5 changes: 1 addition & 4 deletions torch/csrc/cuda/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ static PyObject* THCPStream_pynew(
: at::cuda::getStreamFromPool(
/* isHighPriority */ priority < 0 ? true : false);

// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
THCPStream* self = (THCPStream*)ptr.get();
self->stream_id = static_cast<int64_t>(stream.id());
self->device_index = static_cast<int64_t>(stream.device_index());
Expand Down Expand Up @@ -104,9 +103,7 @@ static PyObject* THCPStream_priority_range(
PyObject* _unused,
PyObject* noargs) {
HANDLE_TH_ERRORS
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int least_priority, greatest_priority;
std::tie(least_priority, greatest_priority) =
auto [least_priority, greatest_priority] =
at::cuda::CUDAStream::priority_range();
return Py_BuildValue("(ii)", least_priority, greatest_priority);
END_HANDLE_TH_ERRORS
Expand Down
1 change: 0 additions & 1 deletion torch/csrc/cuda/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <torch/csrc/Stream.h>
#include <torch/csrc/python_headers.h>

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
struct THCPStream : THPStream {
at::cuda::CUDAStream cuda_stream;
};
Expand Down
1 change: 0 additions & 1 deletion torch/csrc/distributed/autograd/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ using torch::distributed::rpc::JitFuture;
using torch::distributed::rpc::Message;
using torch::distributed::rpc::MessageType;
using torch::distributed::rpc::RpcAgent;
using torch::distributed::rpc::RpcCommandBase;
using torch::distributed::rpc::WorkerInfo;

void addSendRpcBackward(
Expand Down
7 changes: 0 additions & 7 deletions torch/csrc/distributed/rpc/script_resp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ namespace torch {
namespace distributed {
namespace rpc {

namespace {

using torch::jit::Pickler;
using torch::jit::Unpickler;

} // namespace

ScriptResp::ScriptResp(at::IValue&& value) : value_(value) {}

const at::IValue& ScriptResp::value() {
Expand Down
6 changes: 2 additions & 4 deletions torch/csrc/jit/passes/onnx/shape_type_inference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,10 +2061,8 @@ void ONNXShapeTypeInference(
const char shape_err[] = "ShapeInferenceError";
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
const char type_err[] = "TypeInferenceError";
// NOLINTNEXTLINE(modernize-use-nullptr)
if ((strstr(ex.what(), shape_err) == NULL) &&
// NOLINTNEXTLINE(modernize-use-nullptr)
(strstr(ex.what(), type_err) == NULL)) {
if ((strstr(ex.what(), shape_err) == nullptr) &&
(strstr(ex.what(), type_err) == nullptr)) {
throw;
}
}
Expand Down
22 changes: 13 additions & 9 deletions torch/csrc/profiler/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ RawTensorMetadata::RawTensorMetadata(const at::Tensor& t)

TensorMetadata::TensorMetadata(
const RawTensorMetadata& r,
const std::vector<int64_t>& sizes,
const std::vector<int64_t>& strides)
std::vector<int64_t> sizes,
std::vector<int64_t> strides)
: RawTensorMetadataBase(r),
weak_self_{r.weak_self_.value_or(WeakTensor(at::Tensor()))},
device_{r.device_type_, r.device_index_},
sizes_{sizes},
strides_{strides} {
sizes_{std::move(sizes)},
strides_{std::move(strides)} {
SOFT_ASSERT(r.weak_self_.has_value());
}

Expand Down Expand Up @@ -1129,12 +1129,16 @@ RecordQueue::getRecords(
auto& queue = *subqueue_it.second;
auto materialize = [&](auto& events) {
for (auto& i : events) {
time_t start_time_ns;
if constexpr (std::is_same<
std::remove_reference_t<decltype(i)>,
ExtraFields<EventType::Backend>>::value) {
start_time_ns = i.start_time_us_ * 1000;
} else {
start_time_ns = converter(i.start_time_);
}
out.emplace_back(Result::create(
/*start_time_ns_=*/c10::guts::if_constexpr<std::is_same<
typename std::remove_reference<decltype(i)>::type,
ExtraFields<EventType::Backend>>::value>(
[&](auto _) { return _(i).start_time_us_ * 1000; },
[&](auto _) { return converter(_(i).start_time_); }),
/*start_time_ns_=*/start_time_ns,
/*start_tid_=*/queue.tid(),
/*kineto_info_=*/queue.kineto_info(),
/*extra_fields_=*/std::move(i)));
Expand Down
4 changes: 2 additions & 2 deletions torch/csrc/profiler/collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ struct TORCH_API RawTensorMetadata : RawTensorMetadataBase {
struct TORCH_API TensorMetadata : public RawTensorMetadataBase {
TensorMetadata(
const RawTensorMetadata& r,
const std::vector<int64_t>& sizes,
const std::vector<int64_t>& strides);
std::vector<int64_t> sizes,
std::vector<int64_t> strides);

TensorImplAddress impl() const {
return weak_self_.get();
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/profiler/orchestration/python_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MakeFn make_fn;

struct NoOpPythonTracer : public PythonTracerBase {
NoOpPythonTracer() = default;
~NoOpPythonTracer() = default;
~NoOpPythonTracer() override = default;

void stop() override {}
std::vector<std::shared_ptr<Result>> getEvents(
Expand Down
Loading

0 comments on commit 045d1de

Please sign in to comment.