mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 00:21:07 +01:00
backport std::ssize to c10 Summary: Now that we have -Werror=sign-compare enabled, we encounter a lot of friction comparing standard containers and our tensors which are signed. std::ssize will make it easier and safer to succinctly convert container sizes to a signed type. Test Plan: Added a unit test. Reviewers: ezyang Subscribers: Tasks: Tags: --- Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/pytorch/pytorch/pull/97442). * #97443 * __->__ #97442 Pull Request resolved: https://github.com/pytorch/pytorch/pull/97442 Approved by: https://github.com/ezyang
46 lines
1.3 KiB
C++
46 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <c10/util/Exception.h>
|
|
#include <c10/util/TypeSafeSignMath.h>
|
|
|
|
#include <cstddef>
|
|
#include <type_traits>
|
|
|
|
namespace c10 {
|
|
|
|
// Implementations of std::ssize() from C++ 20.
|
|
//
|
|
// This is useful in particular for avoiding -Werror=sign-compare
|
|
// issues.
|
|
//
|
|
// Use this with argument-dependent lookup, e.g.:
|
|
// use c10::ssize;
|
|
// auto size = ssize(container);
|
|
//
|
|
// As with the standard library version, containers are permitted to
|
|
// specialize this with a free function defined in the same namespace.
|
|
//
|
|
// See https://en.cppreference.com/w/cpp/iterator/size for more
|
|
// information as well as the source of our implementations.
|
|
//
|
|
// We augment the implementation by adding an assert() if an overflow
|
|
// would occur.
|
|
|
|
template <typename C>
|
|
constexpr auto ssize(const C& c) -> std::
|
|
common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>> {
|
|
using R = std::
|
|
common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
|
|
// We expect this to be exceedingly rare to fire and don't wish to
|
|
// pay a performance hit in release mode.
|
|
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(!greater_than_max<R>(c.size()));
|
|
return static_cast<R>(c.size());
|
|
}
|
|
|
|
template <typename T, std::ptrdiff_t N>
|
|
constexpr auto ssize(const T (&array)[N]) noexcept -> std::ptrdiff_t {
|
|
return N;
|
|
}
|
|
|
|
} // namespace c10
|