From fa449baf729385db3528dbf6743f258b84ab3fbf Mon Sep 17 00:00:00 2001 From: co63oc Date: Sun, 22 Sep 2024 14:36:07 +0800 Subject: [PATCH 1/3] Fix --- test/xpu/cpp/beam_search_decode_op_xpu_test.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/xpu/cpp/beam_search_decode_op_xpu_test.cc b/test/xpu/cpp/beam_search_decode_op_xpu_test.cc index ab4d1cb2a5bcf..51faff6d32b1d 100644 --- a/test/xpu/cpp/beam_search_decode_op_xpu_test.cc +++ b/test/xpu/cpp/beam_search_decode_op_xpu_test.cc @@ -12,8 +12,8 @@ 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 "paddle/fluid/operators/beam_search_decode_op_xpu.h" #include "paddle/phi/backends/xpu/xpu_info.h" +#include "paddle/phi/kernels/funcs/beam_search_decode_xpu.h" #include "gtest/gtest.h" @@ -23,11 +23,11 @@ using LoD = paddle::framework::LoD; using LoDTensorArray = phi::TensorArray; template -using BeamSearchDecoder = paddle::operators::BeamSearchDecoder; +using BeamSearchDecoder = phi::funcs::BeamSearchDecoder; template -using Sentence = paddle::operators::Sentence; +using Sentence = phi::funcs::Sentence; template -using SentenceVector = paddle::operators::SentenceVector; +using SentenceVector = phi::funcs::SentenceVector; namespace paddle { namespace test { @@ -177,7 +177,7 @@ void BeamSearchDecodeTestByXPUFrame() { phi::DenseTensor id_tensor_cpu; phi::DenseTensor score_tensor_cpu; - paddle::operators::BeamSearchDecodeXPUFunctor bs_xpu( + phi::funcs::BeamSearchDecodeXPUFunctor bs_xpu( ids, scores, &id_tensor_cpu, &score_tensor_cpu, 2, 1); bs_xpu.apply_xpu(); From 96df8b6ac8eddbfd82596c2ee3010e64263cbd26 Mon Sep 17 00:00:00 2001 From: co63oc Date: Sun, 22 Sep 2024 14:37:35 +0800 Subject: [PATCH 2/3] Fix --- .../operators/beam_search_decode_op_xpu.h | 171 ------------------ 1 file changed, 171 deletions(-) delete mode 100644 paddle/fluid/operators/beam_search_decode_op_xpu.h diff --git a/paddle/fluid/operators/beam_search_decode_op_xpu.h b/paddle/fluid/operators/beam_search_decode_op_xpu.h deleted file mode 100644 index f912841b8b202..0000000000000 --- a/paddle/fluid/operators/beam_search_decode_op_xpu.h +++ /dev/null @@ -1,171 +0,0 @@ -/* 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. */ -#pragma once - -#include "paddle/fluid/operators/beam_search_decode_op_def.h" -#include "paddle/phi/common/memory_utils.h" - -namespace paddle { -namespace operators { - -int SetMeta(const phi::DenseTensor& srcTensor, phi::DenseTensor* dstTensor) { - if (srcTensor.dtype() == phi::DataType::INT32 || - srcTensor.dtype() == phi::DataType::INT64 || - srcTensor.dtype() == phi::DataType::FLOAT32 || - srcTensor.dtype() == phi::DataType::FLOAT16 || - srcTensor.dtype() == phi::DataType::FLOAT64) { - const phi::DenseTensorMeta meta_data(srcTensor.dtype(), srcTensor.dims()); - dstTensor->set_meta(meta_data); - } else { - return xpu::Error_t::INVALID_PARAM; - } - - return xpu::Error_t::SUCCESS; -} -template -int CopyTensorByXPU(const phi::DenseTensor& srcTensor, - phi::DenseTensor* dstTensor, - int flag, - const Place& place) { - const T* srcData = srcTensor.template data(); - if (nullptr == srcData || nullptr == dstTensor || flag < 0 || flag > 1) - return xpu::Error_t::INVALID_PARAM; - - int r = SetMeta(srcTensor, dstTensor); - PADDLE_ENFORCE_EQ( - r, - xpu::Error_t::SUCCESS, - common::errors::External("Execute function SetMeta failed by [%d]", r)); - - if (flag == 0) { - T* dstData = dstTensor->template mutable_data(phi::CPUPlace()); - phi::memory_utils::Copy(phi::CPUPlace(), - dstData, - place, - srcData, - srcTensor.numel() * sizeof(T)); - } else { - T* dstData = dstTensor->template mutable_data(place); - phi::memory_utils::Copy(place, - dstData, - phi::CPUPlace(), - srcData, - srcTensor.numel() * sizeof(T)); - } - - return xpu::Error_t::SUCCESS; -} - -const int CopyTensorByType(const phi::DenseTensor& srcTensor, - phi::DenseTensor* dstTensor, - int flag, - const Place& place) { - int r = 0; - if (srcTensor.dtype() == phi::DataType::FLOAT32) - r = CopyTensorByXPU(srcTensor, dstTensor, flag, place); - else if (srcTensor.dtype() == phi::DataType::FLOAT16) - r = CopyTensorByXPU(srcTensor, dstTensor, flag, place); - else if (srcTensor.dtype() == phi::DataType::FLOAT64) - r = CopyTensorByXPU(srcTensor, dstTensor, flag, place); - else if (srcTensor.dtype() == phi::DataType::INT32) - r = CopyTensorByXPU(srcTensor, dstTensor, flag, place); - else if (srcTensor.dtype() == phi::DataType::INT64) - r = CopyTensorByXPU(srcTensor, dstTensor, flag, place); - else - return xpu::Error_t::INVALID_PARAM; - - PADDLE_ENFORCE_EQ(r, - xpu::Error_t::SUCCESS, - common::errors::External( - "Execute function CopyTensorByXPU failed by [%d]", r)); - - return xpu::Error_t::SUCCESS; -} - -struct BeamSearchDecodeXPUFunctor { - BeamSearchDecodeXPUFunctor(const phi::TensorArray& step_ids, - const phi::TensorArray& step_scores, - phi::DenseTensor* id_tensor, - phi::DenseTensor* score_tensor, - size_t beam_size, - int end_id) - : beam_size_(beam_size), - end_id_(end_id), - id_tensor_(id_tensor), - score_tensor_(score_tensor) { - int r = 0; - - // First make a copy of XPU data on CPU - if (step_ids[0].place().GetType() == phi::AllocationType::XPU) { - // Copy all tensors in the input tensor array - for (auto& step_id : step_ids) { - phi::DenseTensor out; - if (step_id.numel() > 0) { - r = CopyTensorByType(step_id, &out, 0, step_ids[0].place()); - PADDLE_ENFORCE_EQ( - r, - xpu::Error_t::SUCCESS, - common::errors::External( - "Execute function CopyTensorByXPU failed by [%d]", r)); - } - - out.set_lod(step_id.lod()); - step_ids_.push_back(out); - } - } - - if (step_scores[0].place().GetType() == phi::AllocationType::XPU) { - // Copy all tensors in the input tensor array - for (auto& step_score : step_scores) { - phi::DenseTensor out; - if (step_score.numel() > 0) { - r = CopyTensorByType(step_score, &out, 0, step_scores[0].place()); - PADDLE_ENFORCE_EQ( - r, - xpu::Error_t::SUCCESS, - common::errors::External( - "Execute function CopyTensorByType failed by [%d]", r)); - } - - out.set_lod(step_score.lod()); - step_scores_.push_back(out); - } - } - } - - template - void apply_xpu() const { - if (std::is_same::value) { - PADDLE_THROW(common::errors::InvalidArgument( - "beam search decode op does not support bool!")); - } else { - BeamSearchDecoder beam_search_decoder(beam_size_, end_id_); - beam_search_decoder.Backtrace( - step_ids_, step_scores_, id_tensor_, score_tensor_); - } - } - - size_t beam_size_; - int end_id_; - // TODO(Superjomn) Here might result serious performance issue in the - // concurrency - // scenarios. - phi::TensorArray step_ids_ = phi::TensorArray(); - phi::TensorArray step_scores_ = phi::TensorArray(); - phi::DenseTensor* id_tensor_; - phi::DenseTensor* score_tensor_; -}; - -} // namespace operators -}; // namespace paddle From 8de57fd374b1f1a82daf3daf751e4fdd92506cff Mon Sep 17 00:00:00 2001 From: co63oc Date: Sun, 22 Sep 2024 15:28:21 +0800 Subject: [PATCH 3/3] Fix --- .../xpu/cpp/beam_search_decode_op_xpu_test.cc | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/test/xpu/cpp/beam_search_decode_op_xpu_test.cc b/test/xpu/cpp/beam_search_decode_op_xpu_test.cc index 51faff6d32b1d..ed60596349f30 100644 --- a/test/xpu/cpp/beam_search_decode_op_xpu_test.cc +++ b/test/xpu/cpp/beam_search_decode_op_xpu_test.cc @@ -13,13 +13,14 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/backends/xpu/xpu_info.h" +#include "paddle/phi/common/memory_utils.h" #include "paddle/phi/kernels/funcs/beam_search_decode_xpu.h" #include "gtest/gtest.h" using CPUPlace = phi::CPUPlace; using XPUPlace = phi::XPUPlace; -using LoD = paddle::framework::LoD; +using LoD = phi::LoD; using LoDTensorArray = phi::TensorArray; template @@ -79,11 +80,11 @@ void GenerateXPUExample(const std::vector& level_0, tensor_id.set_lod(lod); int64_t* id_ptr = tensor_id.mutable_data(xpu_place); - paddle::memory::Copy(phi::XPUPlace(XPU_PlaceNo), - id_ptr, - phi::CPUPlace(), - id_cpu_ptr, - tensor_id_cpu.numel() * sizeof(int64_t)); + phi::memory_utils::Copy(phi::XPUPlace(XPU_PlaceNo), + id_ptr, + phi::CPUPlace(), + id_cpu_ptr, + tensor_id_cpu.numel() * sizeof(int64_t)); // Scores phi::DenseTensor tensor_score_cpu; @@ -123,11 +124,11 @@ void GenerateXPUExample(const std::vector& level_0, T* score_ptr = tensor_score.mutable_data(xpu_place); - paddle::memory::Copy(phi::XPUPlace(XPU_PlaceNo), - score_ptr, - phi::CPUPlace(), - score_cpu_ptr, - tensor_score_cpu.numel() * sizeof(T)); + phi::memory_utils::Copy(phi::XPUPlace(XPU_PlaceNo), + score_ptr, + phi::CPUPlace(), + score_cpu_ptr, + tensor_score_cpu.numel() * sizeof(T)); ids->push_back(tensor_id); scores->push_back(tensor_score);