mirror of
https://github.com/saymrwulf/pytorch.git
synced 2026-05-14 20:57:59 +00:00
Summary: Breaking this out of #8338 This takes care of failures we saw on Mac CUDA builds when BUILD_CAFFE2 and BUILD_ATEN were removed. Specifically, smessmer fixed `std::hash` being handled in a weird way by nvcc and I fixed an nvcc template issue by moving `SparseNormalizeOp::RunOnDevice` implementation into the cc file. cc mingzhe09088 smessmer Pull Request resolved: https://github.com/pytorch/pytorch/pull/9269 Reviewed By: mingzhe09088 Differential Revision: D8767984 Pulled By: orionr fbshipit-source-id: 550686bfcef6d331f16d593859c99169216c5c2e
67 lines
2.3 KiB
C++
67 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <functional>
|
|
|
|
namespace c10 { namespace guts {
|
|
|
|
/**
|
|
* This template simplifies generation of simple classes that wrap an id
|
|
* in a typesafe way. Namely, you can use it to create a very lightweight
|
|
* type that only offers equality comparators and hashing. Example:
|
|
*
|
|
* struct MyIdType final : IdWrapper<MyIdType, uint32_t> {
|
|
* constexpr explicit MyIdType(uint32_t id): IdWrapper(id) {}
|
|
* };
|
|
*
|
|
* Then in the global top level namespace:
|
|
*
|
|
* C10_DEFINE_IDWRAPPER(MyIdType);
|
|
*
|
|
* That's it - equality operators and hash functions are automatically defined
|
|
* for you, given the underlying type supports it.
|
|
*/
|
|
template <class ConcreteType, class UnderlyingType>
|
|
class IdWrapper {
|
|
public:
|
|
using underlying_type = UnderlyingType;
|
|
using concrete_type = ConcreteType;
|
|
|
|
protected:
|
|
constexpr explicit IdWrapper(underlying_type id) noexcept(noexcept(underlying_type(std::declval<underlying_type>())))
|
|
: id_(id) {}
|
|
|
|
constexpr underlying_type underlyingId() const noexcept(noexcept(underlying_type(std::declval<underlying_type>()))) {
|
|
return id_;
|
|
}
|
|
|
|
private:
|
|
friend size_t hash_value(const concrete_type& v) {
|
|
return std::hash<underlying_type>()(v.id_);
|
|
}
|
|
|
|
// TODO Making operator== noexcept if underlying type is noexcept equality comparable doesn't work with GCC 4.8.
|
|
// Fix this once we don't need GCC 4.8 anymore.
|
|
friend constexpr bool operator==(const concrete_type& lhs, const concrete_type& rhs) {
|
|
return lhs.id_ == rhs.id_;
|
|
}
|
|
|
|
// TODO Making operator!= noexcept if operator== is noexcept doesn't work with GCC 4.8.
|
|
// Fix this once we don't need GCC 4.8 anymore.
|
|
friend constexpr bool operator!=(const concrete_type& lhs, const concrete_type& rhs) {
|
|
return !(lhs == rhs);
|
|
}
|
|
|
|
underlying_type id_;
|
|
};
|
|
|
|
}}
|
|
|
|
#define C10_DEFINE_HASH_FOR_IDWRAPPER(ClassName) \
|
|
namespace std { \
|
|
template <> \
|
|
struct hash<ClassName> { \
|
|
size_t operator()(ClassName x) const { \
|
|
return hash_value(x); \
|
|
} \
|
|
}; \
|
|
}
|