未验证 提交 d7c4af74 编写于 作者: H HappyAngel 提交者: GitHub

[arm] add sequence_expand_as op on arm. test=develop (#4291)

* add sequence_expand_as op on arm. test=develop

* fix format. test=develop
上级 eca4dcbb
......@@ -79,6 +79,7 @@ add_kernel(collect_fpn_proposals_compute_arm ARM extra SRCS collect_fpn_proposal
add_kernel(distribute_fpn_proposals_compute_arm ARM extra SRCS distribute_fpn_proposals_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(clip_compute_arm ARM extra SRCS clip_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(pixel_shuffle_compute_arm ARM extra SRCS pixel_shuffle_compute.cc DEPS ${lite_kernel_deps} math_arm)
add_kernel(sequence_expand_as_compute_arm ARM extra SRCS sequence_expand_as_compute.cc DEPS ${lite_kernel_deps} math_arm)
# for OCR specific
add_kernel(gru_unit_compute_arm ARM extra SRCS gru_unit_compute.cc DEPS ${lite_kernel_deps} math_arm)
......
// 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/arm/sequence_expand_as_compute.h"
#include <vector>
namespace paddle {
namespace lite {
namespace kernels {
namespace arm {
void SequenceExpandAsCompute::Run() {
auto& param = Param<operators::SequenceExpandAsParam>();
auto* x = param.x;
auto* y = param.y;
auto* out = param.out;
auto x_lod = x->lod();
auto y_lod = y->lod();
auto dims = x->dims();
auto out_data = out->mutable_data<float>();
auto x_data = x->data<float>();
int seq_size = x->numel() / dims[0];
std::vector<uint64_t> out_lod;
out_lod.push_back(0);
int sum = 0;
for (int i = 0; i < y_lod[0].size(); i++) {
int repeat_num = y_lod[0][i];
if (repeat_num == 0) {
continue;
} else {
for (int j = 0; j < repeat_num; j++) {
memcpy(out_data, x_data, sizeof(float) * seq_size);
out_data += seq_size;
}
x_data += seq_size;
}
sum += repeat_num;
out_lod.push_back(sum);
}
std::vector<std::vector<uint64_t>> lod;
lod.push_back(out_lod);
out->set_lod(lod);
}
} // namespace arm
} // namespace kernels
} // namespace lite
} // namespace paddle
REGISTER_LITE_KERNEL(sequence_expand_as,
kARM,
kFloat,
kNCHW,
paddle::lite::kernels::arm::SequenceExpandAsCompute,
def)
.BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kARM))})
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
.Finalize();
// 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.
#pragma once
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace arm {
class SequenceExpandAsCompute
: public KernelLite<TARGET(kARM), PRECISION(kFloat)> {
public:
void Run() override;
virtual ~SequenceExpandAsCompute() = default;
private:
};
} // namespace arm
} // namespace kernels
} // namespace lite
} // namespace paddle
......@@ -66,6 +66,7 @@ if(LITE_BUILD_EXTRA)
lite_cc_test(test_kernel_ctc_align_compute SRCS ctc_align_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${huawei_ascend_npu_kernels} ${x86_kernels} ${bm_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
lite_cc_test(test_kernel_clip_compute SRCS clip_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${huawei_ascend_npu_kernels} ${x86_kernels} ${bm_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
lite_cc_test(test_kernel_pixel_shuffle_compute SRCS pixel_shuffle_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${x86_kernels} ${bm_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
lite_cc_test(test_kernel_sequence_expand_as_compute SRCS sequence_expand_as_compute_test.cc DEPS arena_framework ${xpu_kernels} ${npu_kernels} ${x86_kernels} ${bm_kernels} ${cuda_kernels} ${arm_kernels} ${lite_ops} ${host_kernels})
# for training kernel
if (LITE_WITH_TRAIN)
......
// 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 <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include "lite/core/op_registry.h"
#ifdef LITE_WITH_ARM
#include "lite/kernels/arm/sequence_expand_as_compute.h"
namespace paddle {
namespace lite {
TEST(sequence_expand_as, retrive_op) {
auto sequence_expand_as =
KernelRegistry::Global().Create("sequence_expand_as");
ASSERT_FALSE(sequence_expand_as.empty());
ASSERT_TRUE(sequence_expand_as.front());
}
TEST(sequence_expand_as, init) {
paddle::lite::kernels::arm::SequenceExpandAsCompute sequence_expand_as;
ASSERT_EQ(sequence_expand_as.precision(), PRECISION(kFloat));
ASSERT_EQ(sequence_expand_as.target(), TARGET(kARM));
}
TEST(sequence_expand_as, run_test) {
lite::Tensor x, y, out;
std::vector<int64_t> x_shape{4, 1};
x.Resize(lite::DDim(x_shape));
std::vector<int64_t> y_shape{1, 5};
y.Resize(lite::DDim(y_shape));
std::vector<int64_t> out_shape{8, 1};
out.Resize(lite::DDim(out_shape));
auto x_data = x.mutable_data<float>();
auto y_data = y.mutable_data<float>();
for (int64_t i = 0; i < x.dims().production(); i++) {
x_data[i] = static_cast<float>(i);
}
for (int64_t i = 0; i < y.dims().production(); i++) {
y_data[i] = static_cast<float>(i);
}
std::vector<std::vector<uint64_t>> lod{{0, 3, 3, 1, 1}};
y.set_lod(lod);
paddle::lite::kernels::arm::SequenceExpandAsCompute sequence_expand_as;
operators::SequenceExpandAsParam param;
param.x = &x;
param.y = &y;
param.out = &out;
std::unique_ptr<KernelContext> ctx(new KernelContext);
ctx->As<ARMContext>();
sequence_expand_as.SetContext(std::move(ctx));
sequence_expand_as.SetParam(param);
sequence_expand_as.Run();
auto out_data = out.mutable_data<float>();
int index = 1;
auto out_lod = param.out->lod()[0];
int lod_sum = out_lod[index];
LOG(INFO) << "output: ";
for (int i = 0; i < out.dims().production(); i++) {
LOG(INFO) << out_data[i];
if (i >= lod_sum) {
index++;
lod_sum = out_lod[index];
}
ASSERT_EQ(out_data[i], x_data[index - 1]);
}
}
} // namespace lite
} // namespace paddle
USE_LITE_KERNEL(sequence_expand_as, kARM, kFloat, kNCHW, def);
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册