pytorch/c10/util/IdWrapper.h
Gao, Xiang 15b560a5c4 Fix missing include for size_t (#84088)
Fixes the following issue:

```C++
In file included from /home/gaoxiang/pytorch-ucc/c10/test/util/ConstexprCrc_test.cpp:1:
In file included from /home/gaoxiang/pytorch-ucc/c10/util/ConstexprCrc.h:3:
/home/gaoxiang/pytorch-ucc/c10/util/IdWrapper.h:42:10: error: unknown type name 'size_t'; did you mean 'std::size_t'?
  friend size_t hash_value(const concrete_type& v) {
         ^~~~~~
         std::size_t
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/x86_64-pc-linux-gnu/bits/c++config.h:298:26: note: 'std::size_t' declared here
  typedef __SIZE_TYPE__         size_t;
                                ^
1 error generated.
[111/2069] Generating /home/gaoxiang/pytorch-ucc/torch/csrc/a...ch-ucc/torch/testing/_internal/generated/annotated_fn_args.py
ninja: build stopped: subcommand failed.
```

This error happens with my GCC 12.2.0 + Clang 14.0.6.

Full environment:
```
Collecting environment information...
PyTorch version: 1.13.0a0+git14a53e6
Is debug build: True
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Arch Linux (x86_64)
GCC version: (GCC) 12.2.0
Clang version: 14.0.6
CMake version: version 3.24.1
Libc version: glibc-2.36

Python version: 3.10.6 (main, Aug  3 2022, 17:39:45) [GCC 12.1.1 20220730] (64-bit runtime)
Python platform: Linux-5.19.3-arch1-1-x86_64-with-glibc2.36
Is CUDA available: True
CUDA runtime version: 11.7.99
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 2080 Ti

Nvidia driver version: 515.65.01
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.4.1
/usr/lib/libcudnn_adv_infer.so.8.4.1
/usr/lib/libcudnn_adv_train.so.8.4.1
/usr/lib/libcudnn_cnn_infer.so.8.4.1
/usr/lib/libcudnn_cnn_train.so.8.4.1
/usr/lib/libcudnn_ops_infer.so.8.4.1
/usr/lib/libcudnn_ops_train.so.8.4.1
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.23.1
[pip3] torch==1.13.0a0+gitbcc6f6c
[pip3] torch-ucc==1.0.0
[pip3] torchani==2.2
[pip3] torchvision==0.2.2.post3
[conda] Could not collect
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/84088
Approved by: https://github.com/ezyang
2022-08-26 03:11:46 +00:00

79 lines
2.3 KiB
C++

#pragma once
#include <c10/macros/Macros.h>
#include <cstddef>
#include <functional>
#include <utility>
namespace c10 {
/**
* 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_HASH_FOR_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) noexcept {
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) noexcept {
return !(lhs == rhs);
}
underlying_type id_;
};
} // namespace c10
#define C10_DEFINE_HASH_FOR_IDWRAPPER(ClassName) \
namespace std { \
template <> \
struct hash<ClassName> { \
size_t operator()(ClassName x) const { \
return hash_value(x); \
} \
}; \
}