sequence_arithmetic_compute.cc 4.1 KB
Newer Older
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
// Copyright (c) 2019 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 "lite/kernels/xpu/sequence_arithmetic_compute.h"
#include "lite/backends/xpu/xpu_header_sitter.h"
#include "lite/core/op_registry.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace xpu {

void SequenceArithmeticCompute::Run() {
  auto& param = this->template Param<param_t>();
  auto& ctx = this->ctx_->template As<XPUContext>();

  auto* bottom0 = param.X;
  auto* bottom1 = param.Y;
  auto* top = param.Out;

  int op_type = param.op_type;

  auto len1 = bottom0->numel();
  auto len2 = bottom1->numel();
  const auto* bottom_data0 = bottom0->data<float>();
  const auto* bottom_data1 = bottom1->data<float>();
  auto* top_data = top->mutable_data<float>(TARGET(kXPU));

40
  int r = 0;
41 42 43
  switch (op_type) {
    case 1:  // addition: top[0] = bottom[0] + bottom[1]
      if (len1 > len2) {
44
        r = xdnn::elementwise_add(
45
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len2);
46 47 48 49 50 51
        CHECK_EQ(r, 0);
        r = xdnn::memcpy_device(ctx.GetRawContext(),
                                &top_data[len2],
                                &bottom_data0[len2],
                                (len1 - len2) * sizeof(float));
        CHECK_EQ(r, 0);
52
      } else {
53
        r = xdnn::elementwise_add(
54
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len1);
55
        CHECK_EQ(r, 0);
56 57 58 59
      }
      break;
    case 2:  // substraction: top[0] = bottom[0] - bottom[1]
      if (len1 > len2) {
60
        r = xdnn::elementwise_sub(
61
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len2);
62 63 64 65 66 67
        CHECK_EQ(r, 0);
        r = xdnn::memcpy_device(ctx.GetRawContext(),
                                &top_data[len2],
                                &bottom_data0[len2],
                                (len1 - len2) * sizeof(float));
        CHECK_EQ(r, 0);
68
      } else {
69
        r = xdnn::elementwise_sub(
70
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len1);
71
        CHECK_EQ(r, 0);
72 73 74 75
      }
      break;
    case 3:  // multiplication: top[0] = bottom[0] * bottom[1]
      if (len1 > len2) {
76
        r = xdnn::elementwise_mul(
77
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len2);
78 79 80 81 82 83
        CHECK_EQ(r, 0);
        r = xdnn::memcpy_device(ctx.GetRawContext(),
                                &top_data[len2],
                                &bottom_data0[len2],
                                (len1 - len2) * sizeof(float));
        CHECK_EQ(r, 0);
84
      } else {
85
        r = xdnn::elementwise_mul(
86
            ctx.GetRawContext(), bottom_data0, bottom_data1, top_data, len1);
87
        CHECK_EQ(r, 0);
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
      }
      break;
    default:
      break;
  }
}

}  // namespace xpu
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(sequence_arithmetic,
                     kXPU,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::xpu::SequenceArithmeticCompute,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kXPU))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kXPU))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kXPU))})
    .Finalize();

REGISTER_LITE_KERNEL(search_seq_arithmetic,
                     kXPU,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::xpu::SequenceArithmeticCompute,
                     def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kXPU))})
    .BindInput("Y", {LiteType::GetTensorTy(TARGET(kXPU))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kXPU))})
    .Finalize();