Add const reference in opportunities detected by clang-tidy (#105931)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/105931
Approved by: https://github.com/Skylion007
This commit is contained in:
cyy
2023-07-26 21:38:06 +00:00
committed by PyTorch MergeBot
parent b69e5302b5
commit 646fa36875
19 changed files with 140 additions and 157 deletions

View File

@ -431,7 +431,7 @@ Tensor get_clamped_target_length(
}
Tensor get_clamped_target_length(
Tensor target_lengths,
const Tensor & target_lengths,
const TensorOptions& options) {
return target_lengths.clamp_min(1);
}

View File

@ -134,7 +134,7 @@ std::string get_privateuse1_backend(bool lower_case) {
return backend_name;
}
void register_privateuse1_backend(std::string backend_name) {
void register_privateuse1_backend(const std::string& backend_name) {
std::lock_guard<std::mutex> guard(privateuse1_lock);
TORCH_CHECK(
!privateuse1_backend_name_set.load() ||

View File

@ -98,7 +98,7 @@ C10_API bool isValidDeviceType(DeviceType d);
C10_API std::ostream& operator<<(std::ostream& stream, DeviceType type);
C10_API void register_privateuse1_backend(std::string backend_name);
C10_API void register_privateuse1_backend(const std::string& backend_name);
C10_API std::string get_privateuse1_backend(bool lower_case = true);
} // namespace c10

View File

@ -503,7 +503,7 @@ DEFINE_SYMBOOL_COMPUTE(compute_non_overlapping_and_dense, is_non_overlapping_and
// test_aot_autograd_symbolic_exhaustive_nn_functional_unfold_cpu_float32 to run
// very slowly.
static bool definitely_true(SymBool b) {
static bool definitely_true(const SymBool& b) {
return b.has_hint() && b.guard_bool(__FILE__, __LINE__);
}

View File

@ -6,7 +6,7 @@ namespace c10 {
ThreadPool::ThreadPool(
int pool_size,
int numa_node_id,
std::function<void()> init_thread)
const std::function<void()>& init_thread)
: threads_(pool_size < 0 ? defaultNumThreads() : pool_size),
running_(true),
complete_(true),

View File

@ -76,7 +76,7 @@ class C10_API ThreadPool : public c10::TaskThreadPoolBase {
explicit ThreadPool(
int pool_size,
int numa_node_id = -1,
std::function<void()> init_thread = nullptr);
const std::function<void()>& init_thread = nullptr);
~ThreadPool() override;

View File

@ -43,7 +43,7 @@ c10::IValue readArchive(
std::shared_ptr<mobile::CompilationUnit> mobile_compilation_unit =
std::make_shared<mobile::CompilationUnit>();
auto obj_loader = [&](at::StrongTypePtr type, IValue input) {
auto obj_loader = [&](const at::StrongTypePtr& type, IValue input) {
return objLoaderMobile(type, input, *mobile_compilation_unit);
};
bool bytecode_tensor_in_constants_archive =

View File

@ -117,7 +117,7 @@ TypePtr resolveTypeNameMobile(
c10::StrongTypePtr typeResolverMobile(
const c10::QualifiedName& qn,
std::shared_ptr<CompilationUnit> compilation_unit) {
const std::shared_ptr<CompilationUnit>& compilation_unit) {
return c10::StrongTypePtr(
compilation_unit, resolveTypeNameMobile(qn, compilation_unit));
}
@ -426,9 +426,7 @@ void BytecodeDeserializer::deserialize_only_extra(
for (const auto& kv : extra_files) {
const std::string& key = "extra/" + kv.first;
if (reader_->hasRecord(key)) {
at::DataPtr meta_ptr;
size_t meta_size = 0;
std::tie(meta_ptr, meta_size) = reader_->getRecord(key);
auto [meta_ptr, meta_size] = reader_->getRecord(key);
extra_files[kv.first] =
std::string(static_cast<char*>(meta_ptr.get()), meta_size);
}
@ -486,7 +484,7 @@ c10::IValue BytecodeDeserializer::readArchive(
return typeResolverMobile(qn, compilation_unit_);
};
auto obj_loader = [&](at::StrongTypePtr type, IValue input) {
auto obj_loader = [&](const at::StrongTypePtr& type, const IValue& input) {
return objLoaderMobile(type, input, *mcu);
};
@ -507,128 +505,6 @@ c10::IValue BytecodeDeserializer::readArchive(
return ivalues;
}
} // namespace
// Forward declare so that _load_for_mobile() overloads can
// call this method directly.
mobile::Module _load_for_mobile_impl(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options);
mobile::Module _load_mobile_from_bytes(
std::shared_ptr<char> data,
size_t size,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options);
mobile::Module _load_for_mobile(
std::istream& in,
c10::optional<at::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(in, device, extra_files);
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(filename, device, extra_files);
}
mobile::Module _load_for_mobile(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(std::move(rai), device, extra_files);
}
mobile::Module _load_for_mobile(
std::istream& in,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
if (getFileFormat(in) == FileFormat::FlatbufferFileFormat) {
std::shared_ptr<char> data;
size_t size = 0;
std::tie(data, size) = get_stream_content(in);
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
std::unique_ptr<IStreamAdapter> rai = std::make_unique<IStreamAdapter>(&in);
auto module = _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
return module;
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files) {
return _load_for_mobile(
filename, device, extra_files, kDefaultMobileLoadOptions);
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
auto format = getFileFormat(filename);
if (format == FileFormat::FlatbufferFileFormat) {
std::shared_ptr<char> data;
size_t size = 0;
std::tie(data, size) = get_file_content(filename.c_str());
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
std::unique_ptr<FileAdapter> rai = std::make_unique<FileAdapter>(filename);
return _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
}
TORCH_API mobile::Module _load_for_mobile(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
// TODO optimize file read for non-flatbuffer models
std::shared_ptr<char> data;
size_t size = 0;
std::tie(data, size) = get_rai_content(rai.get());
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
mobile::Module _load_mobile_from_bytes(
std::shared_ptr<char> data,
size_t size,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
TORCH_CHECK(size >= kFileFormatHeaderSize, "Format error");
auto format = getFileFormat(data.get());
switch (format) {
case FileFormat::ZipFileFormat: {
std::unique_ptr<ReadAdapterInterface> rai =
std::make_unique<MemoryReadAdapter>(data.get(), size);
return _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
}
case FileFormat::FlatbufferFileFormat: {
return parse_and_initialize_mobile_module(
data, size, device, &extra_files);
}
default: {
TORCH_CHECK(false, "Format error");
}
}
}
mobile::Module _load_for_mobile_impl(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device,
@ -701,6 +577,107 @@ mobile::Module _load_for_mobile_impl(
}
}
mobile::Module _load_mobile_from_bytes(
const std::shared_ptr<char>& data,
size_t size,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
TORCH_CHECK(size >= kFileFormatHeaderSize, "Format error");
auto format = getFileFormat(data.get());
switch (format) {
case FileFormat::ZipFileFormat: {
std::unique_ptr<ReadAdapterInterface> rai =
std::make_unique<MemoryReadAdapter>(data.get(), size);
return _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
}
case FileFormat::FlatbufferFileFormat: {
return parse_and_initialize_mobile_module(
data, size, device, &extra_files);
}
default: {
TORCH_CHECK(false, "Format error");
}
}
}
} // namespace
mobile::Module _load_for_mobile(
std::istream& in,
c10::optional<at::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(in, device, extra_files);
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(filename, device, extra_files);
}
mobile::Module _load_for_mobile(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device) {
ExtraFilesMap extra_files;
return _load_for_mobile(std::move(rai), device, extra_files);
}
mobile::Module _load_for_mobile(
std::istream& in,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
if (getFileFormat(in) == FileFormat::FlatbufferFileFormat) {
auto [data, size] = get_stream_content(in);
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
std::unique_ptr<IStreamAdapter> rai = std::make_unique<IStreamAdapter>(&in);
auto module = _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
return module;
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files) {
return _load_for_mobile(
filename, device, extra_files, kDefaultMobileLoadOptions);
}
mobile::Module _load_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
auto format = getFileFormat(filename);
if (format == FileFormat::FlatbufferFileFormat) {
auto [data, size] = get_file_content(filename.c_str());
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
std::unique_ptr<FileAdapter> rai = std::make_unique<FileAdapter>(filename);
return _load_for_mobile_impl(
std::move(rai), device, extra_files, module_load_options);
}
TORCH_API mobile::Module _load_for_mobile(
std::unique_ptr<ReadAdapterInterface> rai,
c10::optional<c10::Device> device,
ExtraFilesMap& extra_files,
uint64_t module_load_options) {
// TODO optimize file read for non-flatbuffer models
auto [data, size] = get_rai_content(rai.get());
return _load_mobile_from_bytes(
data, size, device, extra_files, module_load_options);
}
void _load_extra_only_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,

View File

@ -80,7 +80,7 @@ at::TypePtr resolveTypeNameMobile(
std::shared_ptr<CompilationUnit> compilation_unit);
c10::StrongTypePtr typeResolverMobile(
const c10::QualifiedName& qn,
std::shared_ptr<CompilationUnit> compilation_unit);
const std::shared_ptr<CompilationUnit>& compilation_unit);
c10::intrusive_ptr<c10::ivalue::Object> objLoaderMobile(
const at::StrongTypePtr& type,
const at::IValue& input,

View File

@ -14,7 +14,7 @@ using TypeResolver =
std::function<c10::StrongTypePtr(const c10::QualifiedName&)>;
using ObjLoader = std::function<
c10::intrusive_ptr<c10::ivalue::Object>(at::StrongTypePtr, IValue)>;
c10::intrusive_ptr<c10::ivalue::Object>(const at::StrongTypePtr&, IValue)>;
class DeserializationStorageContext;

View File

@ -114,7 +114,7 @@ class Completion::Data {
}
static std::function<void()> GetCompleter(
std::shared_ptr<Data> data,
const std::shared_ptr<Data>& data,
std::function<void()> closure) {
auto closure_wrapper = [closure = std::move(closure), data]() {
std::exception_ptr exptr;

View File

@ -16,6 +16,7 @@
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
int THPUtils_getCallable(PyObject* arg, PyObject** result) {
@ -216,13 +217,13 @@ void THPPointer<THPStorage>::free() {
Py_DECREF(ptr);
}
void storage_fill(at::Storage self, uint8_t value) {
void storage_fill(const at::Storage& self, uint8_t value) {
auto options = c10::TensorOptions().device(self.device()).dtype(at::kByte);
auto self_t = at::empty({0}, {}, options).set_(self);
self_t.fill_(value);
}
void storage_set(at::Storage self, ptrdiff_t idx, uint8_t value) {
void storage_set(const at::Storage& self, ptrdiff_t idx, uint8_t value) {
TORCH_CHECK(
(idx >= 0) && (idx < static_cast<ptrdiff_t>(self.nbytes())),
"out of bounds");
@ -231,7 +232,7 @@ void storage_set(at::Storage self, ptrdiff_t idx, uint8_t value) {
self_t[idx].fill_(value);
}
uint8_t storage_get(at::Storage self, ptrdiff_t idx) {
uint8_t storage_get(const at::Storage& self, ptrdiff_t idx) {
TORCH_CHECK(
(idx >= 0) && (idx < static_cast<ptrdiff_t>(self.nbytes())),
"out of bounds");
@ -267,7 +268,7 @@ char* tensor_repr(at::Tensor tensor) {
const char* buf = nullptr;
char* result = nullptr;
pytensor = THPVariable_Wrap(at::Tensor(tensor));
pytensor = THPVariable_Wrap(at::Tensor(std::move(tensor)));
if (!pytensor)
// NOLINTNEXTLINE(cppcoreguidelines-avoid-goto,hicpp-avoid-goto)
goto error;

View File

@ -220,8 +220,8 @@ std::vector<c10::optional<at::cuda::CUDAStream>>
THPUtils_PySequence_to_CUDAStreamList(PyObject* obj);
#endif
void storage_fill(at::Storage self, uint8_t value);
void storage_set(at::Storage self, ptrdiff_t idx, uint8_t value);
uint8_t storage_get(at::Storage self, ptrdiff_t idx);
void storage_fill(const at::Storage& self, uint8_t value);
void storage_set(const at::Storage& self, ptrdiff_t idx, uint8_t value);
uint8_t storage_get(const at::Storage& self, ptrdiff_t idx);
#endif

View File

@ -27,7 +27,7 @@ bool type_caster<c10::SymInt>::load(py::handle src, bool) {
}
py::handle type_caster<c10::SymInt>::cast(
c10::SymInt si,
const c10::SymInt& si,
return_value_policy /* policy */,
handle /* parent */) {
if (auto m = si.maybe_as_int()) {
@ -65,7 +65,7 @@ bool type_caster<c10::SymFloat>::load(py::handle src, bool) {
}
py::handle type_caster<c10::SymFloat>::cast(
c10::SymFloat si,
const c10::SymFloat& si,
return_value_policy /* policy */,
handle /* parent */) {
if (si.is_symbolic()) {
@ -95,7 +95,7 @@ bool type_caster<c10::SymBool>::load(py::handle src, bool) {
}
py::handle type_caster<c10::SymBool>::cast(
c10::SymBool si,
const c10::SymBool& si,
return_value_policy /* policy */,
handle /* parent */) {
if (si.is_symbolic()) {

View File

@ -267,7 +267,7 @@ struct TORCH_PYTHON_API type_caster<c10::SymInt> {
bool load(py::handle src, bool);
static py::handle cast(
c10::SymInt si,
const c10::SymInt& si,
return_value_policy /* policy */,
handle /* parent */);
};
@ -279,7 +279,7 @@ struct TORCH_PYTHON_API type_caster<c10::SymFloat> {
bool load(py::handle src, bool);
static py::handle cast(
c10::SymFloat si,
const c10::SymFloat& si,
return_value_policy /* policy */,
handle /* parent */);
};
@ -291,7 +291,7 @@ struct TORCH_PYTHON_API type_caster<c10::SymBool> {
bool load(py::handle src, bool);
static py::handle cast(
c10::SymBool si,
const c10::SymBool& si,
return_value_policy /* policy */,
handle /* parent */);
};

View File

@ -1488,7 +1488,9 @@ bool FunctionSignature::parse(
return true;
}
PythonArgParser::PythonArgParser(std::vector<std::string> fmts, bool traceable)
PythonArgParser::PythonArgParser(
const std::vector<std::string>& fmts,
bool traceable)
: max_args(0), traceable(traceable) {
int index = 0;
for (auto& fmt : fmts) {

View File

@ -137,7 +137,7 @@ struct ParsedArgs {
struct PYBIND11_EXPORT PythonArgParser {
explicit PythonArgParser(
std::vector<std::string> fmts,
const std::vector<std::string>& fmts,
bool traceable = false);
// meant only for `torch` functions.

View File

@ -22,6 +22,7 @@
#include <torch/csrc/utils/python_raii.h>
#include <iostream>
#include <utility>
namespace py = pybind11;
@ -192,10 +193,10 @@ static torch::_RegisterOrVerify register_or_verify() {
static py::object ophandle_call_boxed(
const c10::OperatorHandle& handle,
py::args args,
py::kwargs kwargs) {
const py::kwargs& kwargs) {
auto stack = torch::jit::createStackForSchema(
handle.schema(),
args,
std::move(args),
kwargs,
/*self=*/c10::nullopt);
{
@ -306,7 +307,7 @@ void initDispatchBindings(PyObject* module) {
py::arg("debug") = "impl_t_t")
.def(
"impl",
[](py::object self,
[](const py::object& self,
const char* name,
// TODO: empty string no longer works
c10::DispatchKey dispatch,
@ -332,7 +333,9 @@ void initDispatchBindings(PyObject* module) {
py::arg("func"))
.def(
"define",
[](py::object self, const char* schema, const char* alias_analysis) {
[](const py::object& self,
const char* schema,
const char* alias_analysis) {
auto parsed_schema =
torch::schema(schema, parseAliasAnalysisKind(alias_analysis));
self.cast<torch::Library&>().def(

View File

@ -837,7 +837,7 @@ class CheckSparseTensorInvariantsContext {
};
static Tensor sparse_compressed_tensor_ctor_worker(
std::string name,
const std::string& name,
c10::DispatchKey dispatch_key,
at::ScalarType scalar_type,
PythonArgs& r,