The type Place should be a class hierarchy other than boost::variant
Created by: wangkuiyi
The current implementation of TensorContainsNAN and TensorContainsInf are super complicated, as explained in https://github.com/PaddlePaddle/Paddle/issues/11949. Part of the reason is that Place is currently a boost::variant -- so that we cannot determine the real place type (CPU, CUDA, etc) at runtime; instead, we'd have to define boost::static_visitor classes to determine online. If we are going to access a Tensor, we might need three visitor classes, each for a specific type of Place!
For example
struct IsCUDAPlace : public boost::static_visitor<bool> {
bool operator()(const CPUPlace &) const { return false; }
bool operator()(const CUDAPlace &gpu) const { return true; }
bool operator()(const CUDAPinnedPlace &) const { return false; }
};
struct IsCPUPlace : public boost::static_visitor<bool> {
bool operator()(const CPUPlace &cpu) const { return true; }
bool operator()(const CUDAPlace &) const { return false; }
bool operator()(const CUDAPinnedPlace &) const { return false; }
};
struct IsCUDAPinnedPlace : public boost::static_visitor<bool> {
bool operator()(const CPUPlace &) const { return false; }
bool operator()(const CUDAPlace &) const { return false; }
bool operator()(const CUDAPinnedPlace &cuda_pinned) const { return true; }
};
However, if we switch back to use the old school style class hierarchy --
- the base class is Place, and
- CPUPlace, CUDAPlace, etc all derive from Place
then we will be able to check the real type using dynamic_cast:
Place * p = ...;
if (dynamic_cast<const CPUPlace*>(p) != nullptr) {
std::cout << "This is the type CPUPlace!";
}