Skip to content

Commit ab1097c

Browse files
authored
Feature/template (#13093)
* remove template operator * "fix compile" * "fix ci" * "fix ci"
1 parent d0c65bf commit ab1097c

15 files changed

+26
-61
lines changed

paddle/fluid/framework/data_layout_transform.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct CastDataLayout {
4646
const std::vector<int> axis_;
4747

4848
template <typename T>
49-
void operator()() {
49+
void apply() {
5050
auto place = ctx_->GetPlace();
5151

5252
if (platform::is_cpu_place(place)) {

paddle/fluid/framework/data_type.h

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,75 +26,40 @@ namespace framework {
2626
extern proto::VarType::Type ToDataType(std::type_index type);
2727
extern std::type_index ToTypeIndex(proto::VarType::Type type);
2828

29-
#if !defined(_WIN32)
3029
template <typename Visitor>
3130
inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
3231
switch (type) {
3332
case proto::VarType::FP16:
34-
visitor.template operator()<platform::float16>();
33+
visitor.template apply<platform::float16>();
3534
break;
3635
case proto::VarType::FP32:
37-
visitor.template operator()<float>();
36+
visitor.template apply<float>();
3837
break;
3938
case proto::VarType::FP64:
40-
visitor.template operator()<double>();
39+
visitor.template apply<double>();
4140
break;
4241
case proto::VarType::INT32:
43-
visitor.template operator()<int>();
42+
visitor.template apply<int>();
4443
break;
4544
case proto::VarType::INT64:
46-
visitor.template operator()<int64_t>();
45+
visitor.template apply<int64_t>();
4746
break;
4847
case proto::VarType::BOOL:
49-
visitor.template operator()<bool>();
48+
visitor.template apply<bool>();
5049
break;
5150
case proto::VarType::UINT8:
52-
visitor.template operator()<uint8_t>();
51+
visitor.template apply<uint8_t>();
5352
break;
5453
case proto::VarType::INT16:
55-
visitor.template operator()<int16_t>();
54+
visitor.template apply<int16_t>();
5655
break;
5756
case proto::VarType::INT8:
58-
visitor.template operator()<int8_t>();
57+
visitor.template apply<int8_t>();
5958
break;
6059
default:
6160
PADDLE_THROW("Not supported %d", type);
6261
}
6362
}
64-
#else
65-
// the msvc compiler do not implement two-stage name lookup correctly.
66-
template <typename Visitor>
67-
inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
68-
switch (type) {
69-
case proto::VarType::FP16:
70-
visitor.operator()<platform::float16>();
71-
break;
72-
case proto::VarType::FP32:
73-
visitor.operator()<float>();
74-
break;
75-
case proto::VarType::FP64:
76-
visitor.operator()<double>();
77-
break;
78-
case proto::VarType::INT32:
79-
visitor.operator()<int>();
80-
break;
81-
case proto::VarType::INT64:
82-
visitor.operator()<int64_t>();
83-
break;
84-
case proto::VarType::BOOL:
85-
visitor.operator()<bool>();
86-
break;
87-
case proto::VarType::UINT8:
88-
visitor.operator()<uint8_t>();
89-
break;
90-
case proto::VarType::INT16:
91-
visitor.operator()<int16_t>();
92-
break;
93-
default:
94-
PADDLE_THROW("Not supported %d", type);
95-
}
96-
}
97-
#endif // _WIN32
9863

9964
extern std::string DataTypeToString(const proto::VarType::Type type);
10065
extern size_t SizeOfType(std::type_index type);

paddle/fluid/framework/data_type_transform.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CastDataType {
3737
const platform::DeviceContext* ctx_;
3838

3939
template <typename OutType>
40-
void operator()() {
40+
void apply() {
4141
auto* in_begin = in_.data<InType>();
4242
auto* in_end = in_begin + in_.numel();
4343
auto* out_begin = out_->mutable_data<OutType>(in_.place());

paddle/fluid/framework/details/reduce_and_gather.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct ReduceLoDTensor {
3131
: src_tensors_(src), dst_tensor_(*dst) {}
3232

3333
template <typename T>
34-
void operator()() const {
34+
void apply() const {
3535
PADDLE_ENFORCE(!src_tensors_.empty());
3636
auto &t0 = *src_tensors_[0];
3737
PADDLE_ENFORCE_NE(t0.numel(), 0);

paddle/fluid/framework/selected_rows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct TensorCopyVisitor {
4949
size_(size) {}
5050

5151
template <typename T>
52-
void operator()() const {
52+
void apply() const {
5353
// TODO(Yancey1989): support other place
5454
platform::CPUPlace cpu;
5555
memory::Copy(cpu, dst_->mutable_data<T>(cpu) + dst_offset_, cpu,

paddle/fluid/framework/tensor_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ struct AnyDTypeVisitor {
149149
: predicate_(predicate), tensor_(tensor), ctx_(ctx), out_(out) {}
150150

151151
template <typename T>
152-
void operator()() const {
152+
void apply() const {
153153
auto t = EigenVector<T>::Flatten(tensor_);
154154
auto o = EigenScalar<bool>::From(*out_);
155155
// return any of predicate_(t) is true.
@@ -302,7 +302,7 @@ struct DeserializedDataFunctor {
302302
: buf_(buf), tensor_(tensor), place_(place) {}
303303

304304
template <typename T>
305-
void operator()() {
305+
void apply() {
306306
*buf_ = tensor_->mutable_data<T>(place_);
307307
}
308308

paddle/fluid/operators/beam_search_decode_op.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct BeamSearchDecodeFunctor {
7474
}
7575

7676
template <typename T>
77-
void operator()() const;
77+
void apply() const;
7878

7979
bool tensor_on_gpu_;
8080
size_t beam_size_;
@@ -88,7 +88,7 @@ struct BeamSearchDecodeFunctor {
8888
};
8989

9090
template <typename T>
91-
void BeamSearchDecodeFunctor::operator()() const {
91+
void BeamSearchDecodeFunctor::apply() const {
9292
BeamSearchDecoder<T> beam_search_decoder(beam_size_, end_id_);
9393
// Check if the tensor is on GPU. If so, use the CPU copy instead
9494
if (tensor_on_gpu_) {
@@ -101,7 +101,7 @@ void BeamSearchDecodeFunctor::operator()() const {
101101
}
102102

103103
template <>
104-
void BeamSearchDecodeFunctor::operator()<bool>() const {
104+
void BeamSearchDecodeFunctor::apply<bool>() const {
105105
PADDLE_THROW("beam search decode op does not support bool!");
106106
}
107107

paddle/fluid/operators/cast_op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct CastOpFunctor {
3737
: in_(in), out_(out), ctx_(ctx) {}
3838

3939
template <typename OutT>
40-
void operator()() const {
40+
void apply() const {
4141
auto* in_begin = in_->data<InT>();
4242
auto numel = in_->numel();
4343
auto* in_end = in_begin + numel;

paddle/fluid/operators/detection/generate_proposals_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct AppendProposalsFunctor {
3333
: out_(out), offset_(offset), to_add_(to_add) {}
3434

3535
template <typename T>
36-
void operator()() const {
36+
void apply() const {
3737
auto *out_data = out_->data<T>();
3838
auto *to_add_data = to_add_->data<T>();
3939
memcpy(out_data + offset_, to_add_data, to_add_->numel() * sizeof(T));

paddle/fluid/operators/fill_op.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct FillOpVisitor {
2525
: tensor_(tensor), value_(value) {}
2626

2727
template <typename T>
28-
void operator()() const {
28+
void apply() const {
2929
platform::CPUPlace cpu;
3030
auto *data = tensor_->mutable_data<T>(cpu);
3131
std::transform(value_.data(), value_.data() + tensor_->numel(), data,

0 commit comments

Comments
 (0)