forked from PaddlePaddle/PaddleCustomDevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast_kernel.cc
More file actions
129 lines (122 loc) · 4.05 KB
/
cast_kernel.cc
File metadata and controls
129 lines (122 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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 "kernels/dnn_support.hpp"
#include "kernels/phi_funcs.h"
#include "paddle/phi/capi/all.h"
namespace phi {
template <typename T>
void CastKernel(const Context& dev_ctx,
const DenseTensor& x,
phi::DataType out_dtype,
DenseTensor* out) {
show_kernel("Cast-SYCL");
auto x_data = x.data<T>();
out->Resize(x.dims());
auto numel = x.numel();
auto* q = static_cast<sycl::queue*>(dev_ctx.stream());
switch (out_dtype) {
case phi::DataType::BFLOAT16: {
auto out_data = dev_ctx.template Alloc<phi::dtype::bfloat16>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] =
static_cast<phi::dtype::bfloat16>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::FLOAT16: {
auto out_data = dev_ctx.template Alloc<phi::dtype::float16>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] =
static_cast<phi::dtype::float16>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::FLOAT32: {
auto out_data = dev_ctx.template Alloc<float>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<float>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::FLOAT64: {
auto out_data = dev_ctx.template Alloc<double>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<double>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::INT8: {
auto out_data = dev_ctx.template Alloc<int8_t>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<int8_t>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::INT16: {
auto out_data = dev_ctx.template Alloc<int16_t>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<int16_t>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::INT32: {
auto out_data = dev_ctx.template Alloc<int32_t>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<int32_t>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::INT64: {
auto out_data = dev_ctx.template Alloc<int64_t>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<int64_t>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::UINT8: {
auto out_data = dev_ctx.template Alloc<uint8_t>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<uint8_t>(static_cast<float>(x_data[i]));
});
break;
}
case phi::DataType::BOOL: {
auto out_data = dev_ctx.template Alloc<bool>(out);
q->parallel_for(numel, [=](auto& i) {
out_data[i] = static_cast<bool>(static_cast<float>(x_data[i]));
});
break;
}
default:
break;
}
q->wait();
}
} // namespace phi
PD_BUILD_PHI_KERNEL(cast,
intel_gpu,
ALL_LAYOUT,
phi::CastKernel,
float,
double,
int,
int64_t,
int16_t,
bool,
int8_t,
uint8_t,
phi::dtype::float16,
phi::dtype::bfloat16) {
kernel->OutputAt(0).SetDataType(phi::DataType::UNDEFINED);
}