diff --git a/paddle/fluid/framework/type_info.cc b/paddle/fluid/framework/type_info.cc index b24e7fa53a3cca951bfbf8df68707358e9919a80..e7082e82ef8e83fb854b93c63621b2863b9b55d4 100644 --- a/paddle/fluid/framework/type_info.cc +++ b/paddle/fluid/framework/type_info.cc @@ -19,36 +19,26 @@ limitations under the License. */ #include "paddle/fluid/prim/utils/static/desc_tensor.h" namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType( - paddle::framework::RawTensor::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(paddle::framework::Vocab::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(paddle::framework::Strings::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType( - paddle::framework::FeedList::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(egr::VariableCompatTensor::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(paddle::prim::DescTensor::name()); + +template +TypeInfoTraits::TypeInfoTraits() { + static_cast(static_cast(this))->type_info_ = kType; +} + +template +const TypeInfo TypeInfoTraits::kType = + RegisterStaticType(DerivedT::name()); + +template +bool TypeInfoTraits::classof(const BaseT* obj) { + return obj->type_info() == kType; +} + +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; } // namespace phi diff --git a/paddle/phi/backends/cpu/cpu_context.cc b/paddle/phi/backends/cpu/cpu_context.cc index 4538a96dc99b0caf971b26fd11127928e4c2da54..d42189e00eeb836d61838ec3bb96ec52b6bbb0b1 100644 --- a/paddle/phi/backends/cpu/cpu_context.cc +++ b/paddle/phi/backends/cpu/cpu_context.cc @@ -24,10 +24,6 @@ namespace phi { -template <> -const TypeInfo TypeInfoTraits::kType = - RegisterStaticType(CPUContext::name()); - struct CPUContext::Impl { Impl() : place_(CPUPlace()) {} diff --git a/paddle/phi/backends/custom/custom_context.cc b/paddle/phi/backends/custom/custom_context.cc index d382c766cfd774a857d397c9beb77f285d60466d..ddba0baea7e062782a435070871d6e94029c3696 100644 --- a/paddle/phi/backends/custom/custom_context.cc +++ b/paddle/phi/backends/custom/custom_context.cc @@ -19,11 +19,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(CustomContext::name()); - struct CustomContext::Impl { explicit Impl(const CustomPlace& place) : place_(place) {} diff --git a/paddle/phi/backends/gpu/gpu_context.cc b/paddle/phi/backends/gpu/gpu_context.cc index 5ab7019e601ada71b5145fc954c3bb9a07f2d6d4..5c9c010d365e4e6256713fe9db8d57417172b794 100644 --- a/paddle/phi/backends/gpu/gpu_context.cc +++ b/paddle/phi/backends/gpu/gpu_context.cc @@ -59,15 +59,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo TypeInfoTraits::kType = - RegisterStaticType(GPUContext::name()); - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(GPUPinnedContext::name()); - namespace internal { class EigenGpuStreamDevice : public Eigen::StreamInterface { diff --git a/paddle/phi/backends/xpu/xpu_context.cc b/paddle/phi/backends/xpu/xpu_context.cc index 0c554270b51e7f6579a61cfbeb28abc4caf5e697..44f247ff2596783a1c80f837bd5769a97f8bd6fe 100644 --- a/paddle/phi/backends/xpu/xpu_context.cc +++ b/paddle/phi/backends/xpu/xpu_context.cc @@ -30,9 +30,6 @@ namespace xpu = baidu::xpu::api; namespace phi { -template <> -const TypeInfo TypeInfoTraits::kType = - RegisterStaticType(XPUContext::name()); struct XPUContext::Impl { void SetL3Cache(int l3_size = 14155776) { const int MAX_XPU_NUM = 16; diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index 3500d88090722ebb482915b4c2127660d4c75473..fefc9ff0433afce8fdf2ac0fae4734b928792117 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -37,4 +37,4 @@ collect_srcs( generator.cc kernel_factory.cc tensor_utils.cc - storage_properties.cc) + utils/type_info.cc) diff --git a/paddle/phi/core/dense_tensor.cc b/paddle/phi/core/dense_tensor.cc index 4346cc6f32b7d58ad12374e5562b1866560fb834..ad0a5ba00957917e9b2030312a7c6e37a5e2b532 100644 --- a/paddle/phi/core/dense_tensor.cc +++ b/paddle/phi/core/dense_tensor.cc @@ -42,11 +42,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(DenseTensor::name()); - DenseTensor::DenseTensor(Allocator* a, const DenseTensorMeta& meta) : meta_(meta), holder_(a->Allocate(SizeOf(dtype()) * numel())) {} diff --git a/paddle/phi/core/selected_rows.cc b/paddle/phi/core/selected_rows.cc index 3ececdfc0bb58920997bb9999e34eb608a4a6899..ec2d0d61fae287418b032a1a7c16f2f014ea6c50 100644 --- a/paddle/phi/core/selected_rows.cc +++ b/paddle/phi/core/selected_rows.cc @@ -16,11 +16,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(SelectedRows::name()); - SelectedRows::SelectedRows(const std::vector& rows, const int64_t& height) : impl_(std::make_shared(rows, height)) {} diff --git a/paddle/phi/core/sparse_coo_tensor.cc b/paddle/phi/core/sparse_coo_tensor.cc index d76064b5a3d3e7d174a1b8c0f24b4c48786fd7f1..6e7543016f0a08415d38653653f7a41262cbeada 100644 --- a/paddle/phi/core/sparse_coo_tensor.cc +++ b/paddle/phi/core/sparse_coo_tensor.cc @@ -16,11 +16,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(SparseCooTensor::name()); - SparseCooTensor::SparseCooTensor() { DenseTensor non_zero_indices, non_zero_elements; this->SetMember(non_zero_indices, non_zero_elements, {1}, true); diff --git a/paddle/phi/core/sparse_csr_tensor.cc b/paddle/phi/core/sparse_csr_tensor.cc index 156a324f8b64a9be86ee8c30fb406fc8bbccb0f5..ab31a63f4a0f8fb1ec16532b651d5fddd8f9a917 100644 --- a/paddle/phi/core/sparse_csr_tensor.cc +++ b/paddle/phi/core/sparse_csr_tensor.cc @@ -16,11 +16,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(SparseCsrTensor::name()); - SparseCsrTensor::SparseCsrTensor() { DenseTensor crows, cols, values; this->non_zero_crows_ = crows; diff --git a/paddle/phi/core/storage_properties.cc b/paddle/phi/core/storage_properties.cc deleted file mode 100644 index f05a3572f5e7d39f74161a73c30c623d74add8e3..0000000000000000000000000000000000000000 --- a/paddle/phi/core/storage_properties.cc +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. */ - -#include "paddle/phi/core/storage_properties.h" - -namespace phi { - -#ifdef PADDLE_WITH_MKLDNN -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(OneDNNStorageProperties::name()); - -#endif - -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(NPUStorageProperties::name()); - -} // namespace phi diff --git a/paddle/phi/core/string_tensor.cc b/paddle/phi/core/string_tensor.cc index 428c890c1f2c872c55ca2ebb002155818dc588a0..0e465982ba429a4ffdf0c861f1d1ca8388a64573 100644 --- a/paddle/phi/core/string_tensor.cc +++ b/paddle/phi/core/string_tensor.cc @@ -21,11 +21,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(StringTensor::name()); - StringTensor::StringTensor() { meta_.offset = 0; } StringTensor::StringTensor(Allocator* a, const StringTensorMeta& meta) diff --git a/paddle/phi/core/tensor_array.cc b/paddle/phi/core/tensor_array.cc index f30b17251cde5c56f93e24d18cb99bfb755a8067..e774bd0da448a76512bcba2db706d8aa3fe03bd9 100644 --- a/paddle/phi/core/tensor_array.cc +++ b/paddle/phi/core/tensor_array.cc @@ -16,11 +16,6 @@ limitations under the License. */ namespace phi { -template <> -const TypeInfo - TypeInfoTraits::kType = - RegisterStaticType(TensorArray::name()); - TensorArray::TensorArray(const std::vector& vec) { tensors_ = vec; } diff --git a/paddle/phi/core/utils/type_info.cc b/paddle/phi/core/utils/type_info.cc new file mode 100644 index 0000000000000000000000000000000000000000..dd43e672ea34682223103a13a2039733b0cac02b --- /dev/null +++ b/paddle/phi/core/utils/type_info.cc @@ -0,0 +1,71 @@ +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#include + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/backends/custom/custom_context.h" +#include "paddle/phi/backends/gpu/gpu_context.h" +#include "paddle/phi/backends/xpu/xpu_context.h" +#include "paddle/phi/core/selected_rows.h" +#include "paddle/phi/core/sparse_coo_tensor.h" +#include "paddle/phi/core/sparse_csr_tensor.h" +#include "paddle/phi/core/storage_properties.h" +#include "paddle/phi/core/string_tensor.h" +#include "paddle/phi/core/tensor_array.h" +#include "paddle/phi/core/utils/type_info.h" + +namespace phi { + +template +TypeInfoTraits::TypeInfoTraits() { + static_cast(static_cast(this))->type_info_ = kType; +} + +template +bool TypeInfoTraits::classof(const BaseT* obj) { + return obj->type_info() == kType; +} + +template +const TypeInfo TypeInfoTraits::kType = + RegisterStaticType(DerivedT::name()); + +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; +template class TypeInfoTraits; + +template class TypeInfoTraits; +template class TypeInfoTraits; + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \ + defined(PADDLE_WITH_XPU_KP) +template class TypeInfoTraits; +template class TypeInfoTraits; +#endif + +#ifdef PADDLE_WITH_XPU +template class TypeInfoTraits; +#endif + +#ifdef PADDLE_WITH_MKLDNN +template class TypeInfoTraits; +#endif + +template class TypeInfoTraits; + +} // namespace phi diff --git a/paddle/phi/core/utils/type_info.h b/paddle/phi/core/utils/type_info.h index 1b3d0f8683b596aea784975a24ef8473096eb0da..b4d908e2c1d9c099d7f2d9c231ae040ca181b701 100644 --- a/paddle/phi/core/utils/type_info.h +++ b/paddle/phi/core/utils/type_info.h @@ -43,10 +43,8 @@ template class TypeInfoTraits { public: static const TypeInfo kType; - TypeInfoTraits() { - static_cast(static_cast(this))->type_info_ = kType; - } - static bool classof(const BaseT* obj) { return obj->type_info() == kType; } + TypeInfoTraits(); + static bool classof(const BaseT* obj); }; template diff --git a/test/cpp/phi/core/test_type_info.cc b/test/cpp/phi/core/test_type_info.cc index 40e89f6203d387a47cfb3ebff1014df54cd165ce..efa0fa228b986129ccd7bc249ab07a1feb7a220f 100644 --- a/test/cpp/phi/core/test_type_info.cc +++ b/test/cpp/phi/core/test_type_info.cc @@ -22,6 +22,16 @@ template const TypeInfo TypeInfoTraits::kType = RegisterStaticType(DerivedT::name()); +template +bool TypeInfoTraits::classof(const BaseT* obj) { + return obj->type_info() == kType; +} + +template +TypeInfoTraits::TypeInfoTraits() { + static_cast(static_cast(this))->type_info_ = kType; +} + namespace tests { template