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 "paddle/phi/kernels/cast_kernel.h"
23
#include "xpu/refactor/math.h"
C
chentianyu03 已提交
24

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

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

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

L
liuyuhui 已提交
36 37 38 39
 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 已提交
40 41
    auto out_dtype =
        static_cast<var_type::Type>(context.Attr<int>("out_dtype"));
42

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

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

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

}  // namespace operators
}  // namespace paddle

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