From fca0f34b834f5614a3c6999c28c529a17f093986 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Wed, 27 Nov 2024 01:41:16 +0000 Subject: [PATCH] Switch c10::string_view to std::string_view (#139635) Shortens `string_view_starts_with` to `starts_with`. Adds some missing headers. Isolates `c10_string_view` to use with `get_fully_qualified_name`. Test Plan: Sandcastle Reviewed By: ezyang Differential Revision: D64833558 Pull Request resolved: https://github.com/pytorch/pytorch/pull/139635 Approved by: https://github.com/Skylion007, https://github.com/ezyang --- aten/src/ATen/core/dynamic_type.cpp | 10 ------ aten/src/ATen/core/dynamic_type.h | 1 - aten/src/ATen/core/ivalue.cpp | 5 --- aten/src/ATen/core/ivalue.h | 1 - aten/src/ATen/core/ivalue_inl.h | 2 -- aten/src/ATen/core/jit_type.h | 6 ---- aten/src/ATen/core/operator_name.h | 2 ++ aten/src/ATen/core/register_symbols.cpp | 2 ++ c10/core/Allocator.cpp | 2 ++ c10/core/DeviceType.cpp | 2 ++ c10/test/util/string_view_test.cpp | 2 +- c10/util/ConstexprCrc.h | 1 + c10/util/NetworkFlow.cpp | 3 +- c10/util/StringUtil.cpp | 2 +- c10/util/StringUtil.h | 4 +-- c10/util/TypeIndex.h | 23 ++++++------ c10/util/string_view.h | 40 ++++++++++++++------- caffe2/serialize/inline_container.cc | 15 ++++---- torch/csrc/jit/mobile/debug_info.cpp | 2 +- torch/csrc/jit/mobile/flatbuffer_loader.cpp | 17 ++++----- torch/csrc/lazy/core/hash.h | 4 --- torch/csrc/profiler/perf-inl.h | 2 ++ 22 files changed, 73 insertions(+), 75 deletions(-) diff --git a/aten/src/ATen/core/dynamic_type.cpp b/aten/src/ATen/core/dynamic_type.cpp index 5b780944415..9f1a9db2e42 100644 --- a/aten/src/ATen/core/dynamic_type.cpp +++ b/aten/src/ATen/core/dynamic_type.cpp @@ -69,16 +69,6 @@ DynamicType::Arguments::Arguments( } } -DynamicType::Arguments::Arguments( - const std::vector& names, - c10::ArrayRef args) - : Arguments(args) { - TORCH_INTERNAL_ASSERT(names.size() == args.size()); - for (size_t i = 0; i < args.size(); i++) { - elems[i].label = std::string{names[i]}; - } -} - DynamicType::~DynamicType() { if (tag_ == Tag::Class) { class_.~ClassTypePtr(); diff --git a/aten/src/ATen/core/dynamic_type.h b/aten/src/ATen/core/dynamic_type.h index 697fcec39e3..2e7b7cbc5d3 100644 --- a/aten/src/ATen/core/dynamic_type.h +++ b/aten/src/ATen/core/dynamic_type.h @@ -138,7 +138,6 @@ class DynamicType : public SharedType { struct Arguments { Arguments() = default; Arguments(c10::ArrayRef); - Arguments(const std::vector&, c10::ArrayRef); Arguments(const std::vector&, c10::ArrayRef); std::vector elems; }; diff --git a/aten/src/ATen/core/ivalue.cpp b/aten/src/ATen/core/ivalue.cpp index 5a49b16387a..c6087f0a68e 100644 --- a/aten/src/ATen/core/ivalue.cpp +++ b/aten/src/ATen/core/ivalue.cpp @@ -44,11 +44,6 @@ TORCH_API c10::intrusive_ptr ConstantString::create( return c10::make_intrusive(std::move(str_)); } -TORCH_API c10::intrusive_ptr ConstantString::create( - c10::string_view str_) { - return c10::make_intrusive(std::string(str_)); -} - TORCH_API c10::intrusive_ptr ConstantString::create( std::string_view str_) { return c10::make_intrusive(std::string(str_)); diff --git a/aten/src/ATen/core/ivalue.h b/aten/src/ATen/core/ivalue.h index 5258de15beb..b808b280391 100644 --- a/aten/src/ATen/core/ivalue.h +++ b/aten/src/ATen/core/ivalue.h @@ -690,7 +690,6 @@ struct TORCH_API IValue final { IValue(c10::intrusive_ptr v); IValue(std::string v); IValue(const char* v) : IValue(std::string(v)) {} - IValue(c10::string_view v) : IValue(std::string(v)){} IValue(std::string_view v) : IValue(std::string(v)){} bool isString() const { return Tag::String == tag; diff --git a/aten/src/ATen/core/ivalue_inl.h b/aten/src/ATen/core/ivalue_inl.h index 2b8358646c0..788980d16e8 100644 --- a/aten/src/ATen/core/ivalue_inl.h +++ b/aten/src/ATen/core/ivalue_inl.h @@ -301,10 +301,8 @@ struct TORCH_API ConstantString final : c10::intrusive_ptr_target { public: ConstantString(std::string str) : str_(std::move(str)) {} - ConstantString(c10::string_view str) : str_(std::string(str)) {} ConstantString(std::string_view str) : str_(std::string(str)) {} static c10::intrusive_ptr create(std::string str_); - static c10::intrusive_ptr create(c10::string_view str_); static c10::intrusive_ptr create(std::string_view str_); static c10::intrusive_ptr create(const char* str_); diff --git a/aten/src/ATen/core/jit_type.h b/aten/src/ATen/core/jit_type.h index 39c92909956..91031be176e 100644 --- a/aten/src/ATen/core/jit_type.h +++ b/aten/src/ATen/core/jit_type.h @@ -1955,12 +1955,6 @@ struct getTypePtr_ final { } }; template <> -struct getTypePtr_ final { - static decltype(auto) call() { - return StringType::get(); - } -}; -template <> struct getTypePtr_ final { static decltype(auto) call() { return StringType::get(); diff --git a/aten/src/ATen/core/operator_name.h b/aten/src/ATen/core/operator_name.h index f96e09a6086..6ef5e860633 100644 --- a/aten/src/ATen/core/operator_name.h +++ b/aten/src/ATen/core/operator_name.h @@ -3,6 +3,8 @@ #include #include #include + +#include #include #include #include diff --git a/aten/src/ATen/core/register_symbols.cpp b/aten/src/ATen/core/register_symbols.cpp index a2607d8e6d2..ce4119edcda 100644 --- a/aten/src/ATen/core/register_symbols.cpp +++ b/aten/src/ATen/core/register_symbols.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace c10 { namespace { diff --git a/c10/core/Allocator.cpp b/c10/core/Allocator.cpp index 8284b433395..7ec50e94393 100644 --- a/c10/core/Allocator.cpp +++ b/c10/core/Allocator.cpp @@ -3,6 +3,8 @@ #include +#include + namespace c10 { DataPtr Allocator::clone(const void* data, std::size_t n) { diff --git a/c10/core/DeviceType.cpp b/c10/core/DeviceType.cpp index 70d3d7489e5..daf94245c1e 100644 --- a/c10/core/DeviceType.cpp +++ b/c10/core/DeviceType.cpp @@ -1,5 +1,7 @@ #include #include + +#include #include #include #include diff --git a/c10/test/util/string_view_test.cpp b/c10/test/util/string_view_test.cpp index e54fa13a43f..8f6bde0dce4 100644 --- a/c10/test/util/string_view_test.cpp +++ b/c10/test/util/string_view_test.cpp @@ -3,7 +3,7 @@ #include // NOLINTBEGIN(modernize*, readability*, bugprone-string-constructor) -using c10::string_view; +using string_view = c10::c10_string_view; namespace { namespace testutils { diff --git a/c10/util/ConstexprCrc.h b/c10/util/ConstexprCrc.h index d0092eda9e3..66873dc6761 100644 --- a/c10/util/ConstexprCrc.h +++ b/c10/util/ConstexprCrc.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/c10/util/NetworkFlow.cpp b/c10/util/NetworkFlow.cpp index 17eb4037198..fe9073b31c0 100644 --- a/c10/util/NetworkFlow.cpp +++ b/c10/util/NetworkFlow.cpp @@ -2,8 +2,7 @@ #include -#include -#include +#include #include #include #include diff --git a/c10/util/StringUtil.cpp b/c10/util/StringUtil.cpp index d3c9794c01d..708b6ee10cb 100644 --- a/c10/util/StringUtil.cpp +++ b/c10/util/StringUtil.cpp @@ -83,7 +83,7 @@ std::ostream& operator<<(std::ostream& out, const SourceLocation& loc) { return out; } -size_t ReplaceAll(std::string& s, c10::string_view from, c10::string_view to) { +size_t ReplaceAll(std::string& s, std::string_view from, std::string_view to) { if (from.empty()) { return 0; } diff --git a/c10/util/StringUtil.h b/c10/util/StringUtil.h index 41f80496f7a..601535e42dc 100644 --- a/c10/util/StringUtil.h +++ b/c10/util/StringUtil.h @@ -139,7 +139,7 @@ inline std::string Join(const std::string& delimiter, const Container& v) { // Replace all occurrences of "from" substring to "to" string. // Returns number of replacements size_t C10_API -ReplaceAll(std::string& s, c10::string_view from, c10::string_view to); +ReplaceAll(std::string& s, std::string_view from, std::string_view to); /// Represents a location in source code (for debugging). struct C10_API SourceLocation { @@ -155,7 +155,7 @@ inline bool isPrint(char s) { return s > 0x1f && s < 0x7f; } -inline void printQuotedString(std::ostream& stmt, const string_view str) { +inline void printQuotedString(std::ostream& stmt, const std::string_view str) { stmt << "\""; for (auto s : str) { switch (s) { diff --git a/c10/util/TypeIndex.h b/c10/util/TypeIndex.h index 543c472a015..e560fc6869d 100644 --- a/c10/util/TypeIndex.h +++ b/c10/util/TypeIndex.h @@ -32,13 +32,13 @@ struct type_index final : IdWrapper { namespace detail { -inline constexpr string_view extract( - string_view prefix, - string_view suffix, - string_view str) { +inline constexpr c10::c10_string_view extract( + c10::c10_string_view prefix, + c10::c10_string_view suffix, + c10::c10_string_view str) { #if !defined(__CUDA_ARCH__) // CUDA doesn't like std::logic_error in device code return (!str.starts_with(prefix) || !str.ends_with(suffix)) - ? (throw std::logic_error("Invalid pattern"), string_view()) + ? (throw std::logic_error("Invalid pattern"), c10::c10_string_view()) : str.substr(prefix.size(), str.size() - prefix.size() - suffix.size()); #else return str.substr(prefix.size(), str.size() - prefix.size() - suffix.size()); @@ -46,7 +46,7 @@ inline constexpr string_view extract( } template -inline constexpr c10::string_view fully_qualified_type_name_impl() { +inline constexpr c10::c10_string_view fully_qualified_type_name_impl() { #if defined(_MSC_VER) && !defined(__clang__) #if defined(__NVCC__) return extract( @@ -61,13 +61,13 @@ inline constexpr c10::string_view fully_qualified_type_name_impl() { #endif #elif defined(__clang__) return extract( - "c10::string_view c10::util::detail::fully_qualified_type_name_impl() [T = ", + "c10::c10_string_view c10::util::detail::fully_qualified_type_name_impl() [T = ", "]", __PRETTY_FUNCTION__); #elif defined(__GNUC__) return extract( - "constexpr c10::string_view c10::util::detail::fully_qualified_type_name_impl() [with T = ", - "; c10::string_view = c10::basic_string_view]", + "constexpr c10::c10_string_view c10::util::detail::fully_qualified_type_name_impl() [with T = ", + "; c10::c10_string_view = c10::basic_string_view]", __PRETTY_FUNCTION__); #endif } @@ -122,8 +122,9 @@ inline constexpr type_index get_type_index() { #endif template -inline constexpr string_view get_fully_qualified_type_name() noexcept { - constexpr string_view name = detail::fully_qualified_type_name_impl(); +inline constexpr c10::c10_string_view get_fully_qualified_type_name() noexcept { + constexpr c10::c10_string_view name = + detail::fully_qualified_type_name_impl(); return name; } } // namespace c10::util diff --git a/c10/util/string_view.h b/c10/util/string_view.h index d0c88fe3a37..ae38c55656e 100644 --- a/c10/util/string_view.h +++ b/c10/util/string_view.h @@ -596,22 +596,38 @@ constexpr inline void swap( basic_string_view& rhs) noexcept { lhs.swap(rhs); } -using string_view = basic_string_view; +using string_view = std::string_view; +using c10_string_view = basic_string_view; -// NOTE: In C++20, this function should be replaced by str.starts_with -constexpr bool string_view_starts_with( - std::string_view str, - std::string_view prefix) noexcept { - return str.size() >= prefix.size() && str.substr(0, prefix.size()) == prefix; +// NOTE: In C++20, this function should be replaced by string_view.starts_with +constexpr bool starts_with( + const std::string_view s, + const std::string_view prefix) noexcept { + return (prefix.size() > s.size()) ? false + : prefix == s.substr(0, prefix.size()); } -// NOTE: In C++20, this function should be replaced by str.ends_with -constexpr bool string_view_ends_with( - std::string_view str, - std::string_view suffix) noexcept { - return str.size() >= suffix.size() && - str.substr(str.size() - suffix.size()) == suffix; +// NOTE: In C++20, this function should be replaced by string_view.starts_with +constexpr bool starts_with( + const std::string_view s, + const char prefix) noexcept { + return !s.empty() && prefix == s.front(); } + +// NOTE: In C++20, this function should be replaced by string_view.ends_with +constexpr bool ends_with( + const std::string_view s, + const std::string_view suffix) noexcept { + return (suffix.size() > s.size()) + ? false + : suffix == s.substr(s.size() - suffix.size(), suffix.size()); +} + +// NOTE: In C++20, this function should be replaced by string_view.ends_with +constexpr bool ends_with(const std::string_view s, const char prefix) noexcept { + return !s.empty() && prefix == s.back(); +} + } // namespace c10 namespace std { diff --git a/caffe2/serialize/inline_container.cc b/caffe2/serialize/inline_container.cc index 989340756d1..ed2c5eeb690 100644 --- a/caffe2/serialize/inline_container.cc +++ b/caffe2/serialize/inline_container.cc @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -283,7 +282,7 @@ size_t getPadding( bool PyTorchStreamReader::hasRecord(const std::string& name) { std::lock_guard guard(reader_lock_); - if ((!load_debug_symbol_) && c10::string_view_ends_with(std::string_view(name), kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(std::string_view(name), kDebugPklSuffix)) { return false; } std::string ss = archive_name_plus_slash_ + name; @@ -320,7 +319,7 @@ std::vector PyTorchStreamReader::getAllRecords() { buf); } if ((load_debug_symbol_) || - (!c10::string_view_ends_with(std::string_view(buf + archive_name_plus_slash_.size()),kDebugPklSuffix))) { + (!c10::ends_with(std::string_view(buf + archive_name_plus_slash_.size()),kDebugPklSuffix))) { // NOLINTNEXTLINE(modernize-use-emplace) out.push_back(buf + archive_name_plus_slash_.size()); } @@ -343,7 +342,7 @@ size_t PyTorchStreamReader::getRecordID(const std::string& name) { // return dataptr, size std::tuple PyTorchStreamReader::getRecord(const std::string& name) { std::lock_guard guard(reader_lock_); - if ((!load_debug_symbol_) && c10::string_view_ends_with(name, kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(name, kDebugPklSuffix)) { at::DataPtr retval; return std::make_tuple(std::move(retval), 0); } @@ -424,7 +423,7 @@ PyTorchStreamReader::getRecord(const std::string& name, return getRecord(name); } - if ((!load_debug_symbol_) && c10::string_view_ends_with(name, kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(name, kDebugPklSuffix)) { at::DataPtr retval; return std::make_tuple(std::move(retval), 0); } @@ -448,7 +447,7 @@ PyTorchStreamReader::getRecord(const std::string& name, size_t PyTorchStreamReader::getRecord(const std::string& name, void* dst, size_t n) { std::lock_guard guard(reader_lock_); - if ((!load_debug_symbol_) && c10::string_view_ends_with(name, kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(name, kDebugPklSuffix)) { return 0; } size_t key = getRecordID(name); @@ -477,7 +476,7 @@ PyTorchStreamReader::getRecord(const std::string& name, void* dst, size_t n, return getRecord(name, dst, n); } - if ((!load_debug_symbol_) && c10::string_view(name).ends_with(kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(std::string_view(name), kDebugPklSuffix)) { return 0; } size_t key = getRecordID(name); @@ -508,7 +507,7 @@ size_t PyTorchStreamReader::getRecord( void* buf, const std::function& memcpy_func) { std::lock_guard guard(reader_lock_); - if ((!load_debug_symbol_) && c10::string_view_ends_with(name, kDebugPklSuffix)) { + if ((!load_debug_symbol_) && c10::ends_with(name, kDebugPklSuffix)) { return 0; } if (chunk_size <= 0) { diff --git a/torch/csrc/jit/mobile/debug_info.cpp b/torch/csrc/jit/mobile/debug_info.cpp index 0b8e2e66263..eeb8f48ee74 100644 --- a/torch/csrc/jit/mobile/debug_info.cpp +++ b/torch/csrc/jit/mobile/debug_info.cpp @@ -117,7 +117,7 @@ MobileDebugTable::MobileDebugTable( const std::vector& record_names = reader->getAllRecords(); constexpr std::string_view suffix(".debug_pkl"); for (const auto& record_name : record_names) { - if (c10::string_view_ends_with(std::string_view(record_name), suffix)) { + if (c10::ends_with(std::string_view(record_name), suffix)) { auto [debug_data, debug_size] = reader->getRecord(record_name); auto ivalueTuple = jit::unpickle( reinterpret_cast(debug_data.get()), diff --git a/torch/csrc/jit/mobile/flatbuffer_loader.cpp b/torch/csrc/jit/mobile/flatbuffer_loader.cpp index f56b5818eca..4d9505ee21a 100644 --- a/torch/csrc/jit/mobile/flatbuffer_loader.cpp +++ b/torch/csrc/jit/mobile/flatbuffer_loader.cpp @@ -70,9 +70,10 @@ static_assert( namespace { -static constexpr auto kCustomClassPrefix = "__torch__.torch.classes"; -static constexpr auto kTorchPrefix = "__torch__"; -static constexpr auto kJitPrefix = "torch.jit"; +static constexpr std::string_view kCustomClassPrefix = + "__torch__.torch.classes"; +static constexpr std::string_view kTorchPrefix = "__torch__"; +static constexpr std::string_view kJitPrefix = "torch.jit"; class FlatbufferLoader final { public: @@ -188,13 +189,13 @@ TypePtr resolveType( const std::shared_ptr& cu) { TypePtr type; std::string_view type_str(type_string); - if (c10::string_view_starts_with(type_str, kCustomClassPrefix)) { + if (c10::starts_with(type_str, kCustomClassPrefix)) { type = getCustomClass(type_string); TORCH_CHECK( type, "The implementation of class ", type_string, " cannot be found."); } else if ( - c10::string_view_starts_with(type_str, kTorchPrefix) || - c10::string_view_starts_with(type_str, kJitPrefix)) { + c10::starts_with(type_str, kTorchPrefix) || + c10::starts_with(type_str, kJitPrefix)) { c10::QualifiedName qn(type_string); if (cu->get_class(qn) == nullptr) { auto classtype = ClassType::create(qn, cu, true); @@ -609,8 +610,8 @@ ClassTypePtr FlatbufferLoader::getOrCreateClassTypeForObject( if (cls == nullptr) { std::string_view qn_str( obj_type->type_name()->c_str(), obj_type->type_name()->size()); - if (c10::string_view_starts_with(qn_str, kTorchPrefix) || - c10::string_view_starts_with(qn_str, kJitPrefix)) { + if (c10::starts_with(qn_str, kTorchPrefix) || + c10::starts_with(qn_str, kJitPrefix)) { c10::QualifiedName qn(obj_type->type_name()->str()); cls = cu_->get_class(qn); if (cls == nullptr) { diff --git a/torch/csrc/lazy/core/hash.h b/torch/csrc/lazy/core/hash.h index 2def30dcc69..0ea3022f269 100644 --- a/torch/csrc/lazy/core/hash.h +++ b/torch/csrc/lazy/core/hash.h @@ -149,10 +149,6 @@ static inline hash_t Hash(const std::string& value) { return DataHash(value.data(), value.size()); } -static inline hash_t Hash(const c10::string_view& value) { - return DataHash(value.data(), value.size()); -} - static inline hash_t Hash(const std::string_view& value) { return DataHash(value.data(), value.size()); } diff --git a/torch/csrc/profiler/perf-inl.h b/torch/csrc/profiler/perf-inl.h index 7a2a70efa1a..c92b3f427fb 100644 --- a/torch/csrc/profiler/perf-inl.h +++ b/torch/csrc/profiler/perf-inl.h @@ -13,6 +13,8 @@ #include +#include + namespace torch::profiler::impl::linux_perf { /*