cast_op_xpu.cc 2.5 KB
Newer Older
L
liuyuhui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 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. */

#ifdef PADDLE_WITH_XPU
#include <memory>
17

18
#include "paddle/fluid/framework/convert_utils.h"
L
liuyuhui 已提交
19
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/fluid/operators/cast_op.h"
L
liuyuhui 已提交
21
#include "paddle/fluid/platform/float16.h"
22
#include "xpu/refactor/math.h"
L
liuyuhui 已提交
23

24
#include "paddle/phi/kernels/cast_kernel.h"
C
chentianyu03 已提交
25

L
liuyuhui 已提交
26 27 28
namespace paddle {
namespace operators {

29 30 31
using var_type = framework::proto::VarType;
namespace plat = paddle::platform;

L
liuyuhui 已提交
32 33
template <typename DeviceContext, typename InT>
class CastXPUKernel : public framework::OpKernel<InT> {
T
taixiurong 已提交
34
  using XPUInTDType = typename XPUTypeTrait<InT>::Type;
35
  using float16 = typename XPUTypeTrait<paddle::platform::float16>::Type;
36

L
liuyuhui 已提交
37 38 39 40
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<framework::Tensor>("X");
    auto* out = context.Output<framework::Tensor>("Out");
C
chentianyu03 已提交
41 42
    auto out_dtype =
        static_cast<var_type::Type>(context.Attr<int>("out_dtype"));
43

L
liuyuhui 已提交
44
    auto& dev_ctx = context.template device_context<DeviceContext>();
C
chentianyu03 已提交
45 46 47 48

    out->mutable_data(dev_ctx.GetPlace(),
                      static_cast<framework::proto::VarType::Type>(out_dtype));

49
    auto pt_out_dtype = framework::TransToPtenDataType(
C
chentianyu03 已提交
50 51
        static_cast<framework::proto::VarType::Type>(out_dtype));
    // call pten kernel
52
    phi::CastKernel<InT>(
C
chentianyu03 已提交
53 54 55
        static_cast<const typename paddle::framework::ConvertToPtenContext<
            DeviceContext>::TYPE&>(dev_ctx),
        *in, pt_out_dtype, out);
L
liuyuhui 已提交
56 57 58 59 60 61 62 63
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
64
    cast, ops::CastXPUKernel<paddle::platform::XPUDeviceContext, int32_t>,
L
liuyuhui 已提交
65
    ops::CastXPUKernel<paddle::platform::XPUDeviceContext, float>,
66 67
    ops::CastXPUKernel<paddle::platform::XPUDeviceContext,
                       paddle::platform::float16>,
68 69
    ops::CastXPUKernel<paddle::platform::XPUDeviceContext, int64_t>,
    ops::CastXPUKernel<paddle::platform::XPUDeviceContext, bool>);
L
liuyuhui 已提交
70
#endif