Skip to content

Commit 4f56e7c

Browse files
authored
[BugFix]Fix TypeInfo errors in MacOS (#54279)
* fix mac typeinfo bugs * add file * move code to cc * fix compile bugs
1 parent 85d5f26 commit 4f56e7c

16 files changed

+105
-119
lines changed

paddle/fluid/framework/type_info.cc

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,26 @@ limitations under the License. */
1919
#include "paddle/fluid/prim/utils/static/desc_tensor.h"
2020

2121
namespace phi {
22-
template <>
23-
const TypeInfo<phi::TensorBase>
24-
TypeInfoTraits<phi::TensorBase, paddle::framework::RawTensor>::kType =
25-
RegisterStaticType<phi::TensorBase>(
26-
paddle::framework::RawTensor::name());
27-
28-
template <>
29-
const TypeInfo<phi::TensorBase>
30-
TypeInfoTraits<phi::TensorBase, paddle::framework::Vocab>::kType =
31-
RegisterStaticType<phi::TensorBase>(paddle::framework::Vocab::name());
32-
33-
template <>
34-
const TypeInfo<phi::TensorBase>
35-
TypeInfoTraits<phi::TensorBase, paddle::framework::Strings>::kType =
36-
RegisterStaticType<phi::TensorBase>(paddle::framework::Strings::name());
37-
38-
template <>
39-
const TypeInfo<phi::TensorBase>
40-
TypeInfoTraits<phi::TensorBase, paddle::framework::FeedList>::kType =
41-
RegisterStaticType<phi::TensorBase>(
42-
paddle::framework::FeedList::name());
43-
44-
template <>
45-
const TypeInfo<phi::TensorBase>
46-
TypeInfoTraits<phi::TensorBase, egr::VariableCompatTensor>::kType =
47-
RegisterStaticType<phi::TensorBase>(egr::VariableCompatTensor::name());
48-
49-
template <>
50-
const TypeInfo<phi::TensorBase>
51-
TypeInfoTraits<phi::TensorBase, paddle::prim::DescTensor>::kType =
52-
RegisterStaticType<phi::TensorBase>(paddle::prim::DescTensor::name());
22+
23+
template <typename BaseT, typename DerivedT>
24+
TypeInfoTraits<BaseT, DerivedT>::TypeInfoTraits() {
25+
static_cast<BaseT*>(static_cast<DerivedT*>(this))->type_info_ = kType;
26+
}
27+
28+
template <typename BaseT, typename DerivedT>
29+
const TypeInfo<BaseT> TypeInfoTraits<BaseT, DerivedT>::kType =
30+
RegisterStaticType<BaseT>(DerivedT::name());
31+
32+
template <typename BaseT, typename DerivedT>
33+
bool TypeInfoTraits<BaseT, DerivedT>::classof(const BaseT* obj) {
34+
return obj->type_info() == kType;
35+
}
36+
37+
template class TypeInfoTraits<phi::TensorBase, paddle::framework::RawTensor>;
38+
template class TypeInfoTraits<phi::TensorBase, paddle::framework::Vocab>;
39+
template class TypeInfoTraits<phi::TensorBase, paddle::framework::Strings>;
40+
template class TypeInfoTraits<phi::TensorBase, paddle::framework::FeedList>;
41+
template class TypeInfoTraits<phi::TensorBase, egr::VariableCompatTensor>;
42+
template class TypeInfoTraits<phi::TensorBase, paddle::prim::DescTensor>;
5343

5444
} // namespace phi

paddle/phi/backends/cpu/cpu_context.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
namespace phi {
2626

27-
template <>
28-
const TypeInfo<DeviceContext> TypeInfoTraits<DeviceContext, CPUContext>::kType =
29-
RegisterStaticType<DeviceContext>(CPUContext::name());
30-
3127
struct CPUContext::Impl {
3228
Impl() : place_(CPUPlace()) {}
3329

paddle/phi/backends/custom/custom_context.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ limitations under the License. */
1919

2020
namespace phi {
2121

22-
template <>
23-
const TypeInfo<DeviceContext>
24-
TypeInfoTraits<DeviceContext, CustomContext>::kType =
25-
RegisterStaticType<DeviceContext>(CustomContext::name());
26-
2722
struct CustomContext::Impl {
2823
explicit Impl(const CustomPlace& place) : place_(place) {}
2924

paddle/phi/backends/gpu/gpu_context.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ limitations under the License. */
5959

6060
namespace phi {
6161

62-
template <>
63-
const TypeInfo<DeviceContext> TypeInfoTraits<DeviceContext, GPUContext>::kType =
64-
RegisterStaticType<DeviceContext>(GPUContext::name());
65-
66-
template <>
67-
const TypeInfo<DeviceContext>
68-
TypeInfoTraits<DeviceContext, GPUPinnedContext>::kType =
69-
RegisterStaticType<DeviceContext>(GPUPinnedContext::name());
70-
7162
namespace internal {
7263

7364
class EigenGpuStreamDevice : public Eigen::StreamInterface {

paddle/phi/backends/xpu/xpu_context.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ namespace xpu = baidu::xpu::api;
3030

3131
namespace phi {
3232

33-
template <>
34-
const TypeInfo<DeviceContext> TypeInfoTraits<DeviceContext, XPUContext>::kType =
35-
RegisterStaticType<DeviceContext>(XPUContext::name());
3633
struct XPUContext::Impl {
3734
void SetL3Cache(int l3_size = 14155776) {
3835
const int MAX_XPU_NUM = 16;

paddle/phi/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ collect_srcs(
3737
generator.cc
3838
kernel_factory.cc
3939
tensor_utils.cc
40-
storage_properties.cc)
40+
utils/type_info.cc)

paddle/phi/core/dense_tensor.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ limitations under the License. */
4242

4343
namespace phi {
4444

45-
template <>
46-
const TypeInfo<phi::TensorBase>
47-
TypeInfoTraits<phi::TensorBase, DenseTensor>::kType =
48-
RegisterStaticType<phi::TensorBase>(DenseTensor::name());
49-
5045
DenseTensor::DenseTensor(Allocator* a, const DenseTensorMeta& meta)
5146
: meta_(meta), holder_(a->Allocate(SizeOf(dtype()) * numel())) {}
5247

paddle/phi/core/selected_rows.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ limitations under the License. */
1616

1717
namespace phi {
1818

19-
template <>
20-
const TypeInfo<phi::TensorBase>
21-
TypeInfoTraits<phi::TensorBase, SelectedRows>::kType =
22-
RegisterStaticType<phi::TensorBase>(SelectedRows::name());
23-
2419
SelectedRows::SelectedRows(const std::vector<int64_t>& rows,
2520
const int64_t& height)
2621
: impl_(std::make_shared<phi::SelectedRowsImpl>(rows, height)) {}

paddle/phi/core/sparse_coo_tensor.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ limitations under the License. */
1616

1717
namespace phi {
1818

19-
template <>
20-
const TypeInfo<phi::TensorBase>
21-
TypeInfoTraits<phi::TensorBase, SparseCooTensor>::kType =
22-
RegisterStaticType<phi::TensorBase>(SparseCooTensor::name());
23-
2419
SparseCooTensor::SparseCooTensor() {
2520
DenseTensor non_zero_indices, non_zero_elements;
2621
this->SetMember(non_zero_indices, non_zero_elements, {1}, true);

paddle/phi/core/sparse_csr_tensor.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ limitations under the License. */
1616

1717
namespace phi {
1818

19-
template <>
20-
const TypeInfo<phi::TensorBase>
21-
TypeInfoTraits<phi::TensorBase, SparseCsrTensor>::kType =
22-
RegisterStaticType<phi::TensorBase>(SparseCsrTensor::name());
23-
2419
SparseCsrTensor::SparseCsrTensor() {
2520
DenseTensor crows, cols, values;
2621
this->non_zero_crows_ = crows;

paddle/phi/core/storage_properties.cc

Lines changed: 0 additions & 32 deletions
This file was deleted.

paddle/phi/core/string_tensor.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ limitations under the License. */
2121

2222
namespace phi {
2323

24-
template <>
25-
const TypeInfo<phi::TensorBase>
26-
TypeInfoTraits<phi::TensorBase, StringTensor>::kType =
27-
RegisterStaticType<phi::TensorBase>(StringTensor::name());
28-
2924
StringTensor::StringTensor() { meta_.offset = 0; }
3025

3126
StringTensor::StringTensor(Allocator* a, const StringTensorMeta& meta)

paddle/phi/core/tensor_array.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ limitations under the License. */
1616

1717
namespace phi {
1818

19-
template <>
20-
const TypeInfo<phi::TensorBase>
21-
TypeInfoTraits<phi::TensorBase, TensorArray>::kType =
22-
RegisterStaticType<phi::TensorBase>(TensorArray::name());
23-
2419
TensorArray::TensorArray(const std::vector<DenseTensor>& vec) {
2520
tensors_ = vec;
2621
}

paddle/phi/core/utils/type_info.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <string>
16+
17+
#include "paddle/phi/backends/cpu/cpu_context.h"
18+
#include "paddle/phi/backends/custom/custom_context.h"
19+
#include "paddle/phi/backends/gpu/gpu_context.h"
20+
#include "paddle/phi/backends/xpu/xpu_context.h"
21+
#include "paddle/phi/core/selected_rows.h"
22+
#include "paddle/phi/core/sparse_coo_tensor.h"
23+
#include "paddle/phi/core/sparse_csr_tensor.h"
24+
#include "paddle/phi/core/storage_properties.h"
25+
#include "paddle/phi/core/string_tensor.h"
26+
#include "paddle/phi/core/tensor_array.h"
27+
#include "paddle/phi/core/utils/type_info.h"
28+
29+
namespace phi {
30+
31+
template <typename BaseT, typename DerivedT>
32+
TypeInfoTraits<BaseT, DerivedT>::TypeInfoTraits() {
33+
static_cast<BaseT*>(static_cast<DerivedT*>(this))->type_info_ = kType;
34+
}
35+
36+
template <typename BaseT, typename DerivedT>
37+
bool TypeInfoTraits<BaseT, DerivedT>::classof(const BaseT* obj) {
38+
return obj->type_info() == kType;
39+
}
40+
41+
template <typename BaseT, typename DerivedT>
42+
const TypeInfo<BaseT> TypeInfoTraits<BaseT, DerivedT>::kType =
43+
RegisterStaticType<BaseT>(DerivedT::name());
44+
45+
template class TypeInfoTraits<phi::TensorBase, DenseTensor>;
46+
template class TypeInfoTraits<phi::TensorBase, SelectedRows>;
47+
template class TypeInfoTraits<phi::TensorBase, SparseCooTensor>;
48+
template class TypeInfoTraits<phi::TensorBase, SparseCsrTensor>;
49+
template class TypeInfoTraits<phi::TensorBase, StringTensor>;
50+
template class TypeInfoTraits<phi::TensorBase, TensorArray>;
51+
52+
template class TypeInfoTraits<phi::DeviceContext, CPUContext>;
53+
template class TypeInfoTraits<phi::DeviceContext, CustomContext>;
54+
55+
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) || \
56+
defined(PADDLE_WITH_XPU_KP)
57+
template class TypeInfoTraits<phi::DeviceContext, GPUContext>;
58+
template class TypeInfoTraits<phi::DeviceContext, GPUPinnedContext>;
59+
#endif
60+
61+
#ifdef PADDLE_WITH_XPU
62+
template class TypeInfoTraits<phi::DeviceContext, XPUContext>;
63+
#endif
64+
65+
#ifdef PADDLE_WITH_MKLDNN
66+
template class TypeInfoTraits<phi::StorageProperties, OneDNNStorageProperties>;
67+
#endif
68+
69+
template class TypeInfoTraits<phi::StorageProperties, NPUStorageProperties>;
70+
71+
} // namespace phi

paddle/phi/core/utils/type_info.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ template <typename BaseT, typename DerivedT>
4343
class TypeInfoTraits {
4444
public:
4545
static const TypeInfo<BaseT> kType;
46-
TypeInfoTraits() {
47-
static_cast<BaseT*>(static_cast<DerivedT*>(this))->type_info_ = kType;
48-
}
49-
static bool classof(const BaseT* obj) { return obj->type_info() == kType; }
46+
TypeInfoTraits();
47+
static bool classof(const BaseT* obj);
5048
};
5149

5250
template <typename BaseT>

test/cpp/phi/core/test_type_info.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ template <typename BaseT, typename DerivedT>
2222
const TypeInfo<BaseT> TypeInfoTraits<BaseT, DerivedT>::kType =
2323
RegisterStaticType<BaseT>(DerivedT::name());
2424

25+
template <typename BaseT, typename DerivedT>
26+
bool TypeInfoTraits<BaseT, DerivedT>::classof(const BaseT* obj) {
27+
return obj->type_info() == kType;
28+
}
29+
30+
template <typename BaseT, typename DerivedT>
31+
TypeInfoTraits<BaseT, DerivedT>::TypeInfoTraits() {
32+
static_cast<BaseT*>(static_cast<DerivedT*>(this))->type_info_ = kType;
33+
}
34+
2535
namespace tests {
2636

2737
template <typename T>

0 commit comments

Comments
 (0)