diff --git a/paddle/fluid/operators/concat_op_mlu.cc b/paddle/fluid/operators/concat_op_mlu.cc index ebfd2895e783be7c9793ecf18763d9d03026a674..26bc2f190ce70e3aef0eb53f9179717503f305f8 100644 --- a/paddle/fluid/operators/concat_op_mlu.cc +++ b/paddle/fluid/operators/concat_op_mlu.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/fluid/operators/concat_op.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -32,7 +33,7 @@ class ConcatMLUKernel : public framework::OpKernel { bool need_resize_out_dims = false; if (ctx.HasInput("AxisTensor")) { auto* axis_tensor = ctx.Input("AxisTensor"); - axis = GetDataFromTensor(axis_tensor)[0]; + axis = phi::GetVectorFromTensor(axis_tensor)[0]; need_resize_out_dims = true; } axis = ComputeAxis(static_cast(axis), @@ -97,7 +98,7 @@ class ConcatGradMLUKernel : public framework::OpKernel { if (ctx.HasInput("AxisTensor")) { auto* axis_tensor = ctx.Input("AxisTensor"); - axis = GetDataFromTensor(axis_tensor)[0]; + axis = phi::GetVectorFromTensor(axis_tensor)[0]; } axis = ComputeAxis(static_cast(axis), diff --git a/paddle/fluid/operators/cudnn_lstm_op.cu.cc b/paddle/fluid/operators/cudnn_lstm_op.cu.cc index 2bdf1044d3cfa572b4efbd0c58497f070ea0e8b3..5f22f1853732a33511125d77db3011ca2a9f8a9f 100644 --- a/paddle/fluid/operators/cudnn_lstm_op.cu.cc +++ b/paddle/fluid/operators/cudnn_lstm_op.cu.cc @@ -13,8 +13,8 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/core/generator.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" #ifdef PADDLE_WITH_CUDA #include "paddle/fluid/operators/cudnn_lstm_cache.h" @@ -242,7 +242,7 @@ class CudnnLSTMGPUKernel : public framework::OpKernel { std::vector SequenceLength; if (has_seq_length) { auto *sequence_length = ctx.Input("SequenceLength"); - SequenceLength = operators::GetDataFromTensor(sequence_length); + SequenceLength = phi::GetVectorFromTensor(sequence_length); } auto &dev_ctx = ctx.template device_context(); @@ -532,7 +532,7 @@ class CudnnLSTMGPUGradKernel : public framework::OpKernel { std::vector SequenceLength; if (has_seq_length) { auto *sequence_length = ctx.Input("SequenceLength"); - SequenceLength = operators::GetDataFromTensor(sequence_length); + SequenceLength = phi::GetVectorFromTensor(sequence_length); } int seq_length = input_dims[0]; diff --git a/paddle/fluid/operators/interpolate_v2_op_mlu.cc b/paddle/fluid/operators/interpolate_v2_op_mlu.cc index e6f34539b1c010371a8cae32403e7c38273754ea..7072f050681f10bd4acda32584755bbd1fa665bb 100644 --- a/paddle/fluid/operators/interpolate_v2_op_mlu.cc +++ b/paddle/fluid/operators/interpolate_v2_op_mlu.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/interpolate_op.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" -#include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -96,7 +96,7 @@ class InterpolateV2MLUKernel : public framework::OpKernel { auto scale = ctx.Attr>("scale"); if (scale_tensor != nullptr) { std::vector scale_data; - scale_data = GetDataFromTensor(scale_tensor); + scale_data = phi::GetVectorFromTensor(scale_tensor); if (scale_data.size() > 1 && scale_data.size() <= 2) { scale_h = scale_data[0]; @@ -147,7 +147,7 @@ class InterpolateV2MLUKernel : public framework::OpKernel { auto out_size = ctx.Input("OutSize"); if (out_size != nullptr) { std::vector out_size_data; - out_size_data = GetDataFromTensor(out_size); + out_size_data = phi::GetVectorFromTensor(out_size); if (out_size_data.size() <= 2) { out_h = out_size_data[0]; out_w = out_size_data[1]; @@ -398,7 +398,7 @@ class InterpolateV2GradMLUKernel : public framework::OpKernel { auto scale = ctx.Attr>("scale"); if (scale_tensor != nullptr) { std::vector scale_data; - scale_data = GetDataFromTensor(scale_tensor); + scale_data = phi::GetVectorFromTensor(scale_tensor); if (scale_data.size() > 1) { scale_h = scale_data[0]; scale_w = scale_data[1]; @@ -430,7 +430,7 @@ class InterpolateV2GradMLUKernel : public framework::OpKernel { auto out_size = ctx.Input("OutSize"); if (out_size != nullptr) { std::vector out_size_data; - out_size_data = GetDataFromTensor(out_size); + out_size_data = phi::GetVectorFromTensor(out_size); out_h = out_size_data[0]; out_w = out_size_data[1]; } diff --git a/paddle/fluid/operators/one_hot_v2_op_mlu.cc b/paddle/fluid/operators/one_hot_v2_op_mlu.cc index 0b2fbfe85d4035d4bc9ac7e83e59af5d8b952d23..92c76e14e1a8484bd524b18a485f7c038d3bb980 100644 --- a/paddle/fluid/operators/one_hot_v2_op_mlu.cc +++ b/paddle/fluid/operators/one_hot_v2_op_mlu.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" -#include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -31,8 +31,8 @@ class OneHotV2MLUKernel : public framework::OpKernel { int depth = ctx.Attr("depth"); if (ctx.HasInput("depth_tensor")) { std::vector depth_data; - depth_data = - GetDataFromTensor(ctx.Input("depth_tensor")); + depth_data = phi::GetVectorFromTensor( + ctx.Input("depth_tensor")); depth = depth_data[0]; auto out_dims = out->dims(); diff --git a/paddle/fluid/operators/reshape_op_mlu.cc b/paddle/fluid/operators/reshape_op_mlu.cc index 870f9fb7a1d9f304070163acd10f88ff972b3b12..acba35f1bce2e053bd82faa3136b5a5182e19b9c 100644 --- a/paddle/fluid/operators/reshape_op_mlu.cc +++ b/paddle/fluid/operators/reshape_op_mlu.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -38,14 +38,15 @@ class Reshape2MLUKernel : public framework::OpKernel { "shape is [%d]", shape_tensor->dims().size())); - target_shape_vector.push_back(GetDataFromTensor(shape_tensor)[0]); + target_shape_vector.push_back( + phi::GetVectorFromTensor(shape_tensor)[0]); } } else { auto* shape_tensor = ctx.HasInput("Shape") ? ctx.Input("Shape") : nullptr; if (shape_tensor) { - target_shape_vector = GetDataFromTensor(shape_tensor); + target_shape_vector = phi::GetVectorFromTensor(shape_tensor); } else { target_shape_vector = ctx.Attr>("shape"); PADDLE_ENFORCE_GT( diff --git a/paddle/fluid/operators/reshape_op_npu.cc b/paddle/fluid/operators/reshape_op_npu.cc index d82fa9e366d9a3b74c3234f61c32b453593461da..50dbb7a7cf6d227312b3d96e091ab5d5307de5c7 100644 --- a/paddle/fluid/operators/reshape_op_npu.cc +++ b/paddle/fluid/operators/reshape_op_npu.cc @@ -16,8 +16,8 @@ limitations under the License. */ #include #include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -46,14 +46,15 @@ class Reshape2NPUKernel : public framework::OpKernel { "shape is [%d]", shape_tensor->dims().size())); - target_shape_vector.push_back(GetDataFromTensor(shape_tensor)[0]); + target_shape_vector.push_back( + phi::GetVectorFromTensor(shape_tensor)[0]); } } else { auto* shape_tensor = ctx.HasInput("Shape") ? ctx.Input("Shape") : nullptr; if (shape_tensor) { - target_shape_vector = GetDataFromTensor(shape_tensor); + target_shape_vector = phi::GetVectorFromTensor(shape_tensor); } else { target_shape_vector = ctx.Attr>("shape"); PADDLE_ENFORCE_GT( diff --git a/paddle/fluid/operators/rnn_op_mlu.cc b/paddle/fluid/operators/rnn_op_mlu.cc index 1773c526b46353ecdbff84fec88b44da3d4c6dee..0f4c81d25d8cc8c35f61eaf597984e7d4f7296b0 100644 --- a/paddle/fluid/operators/rnn_op_mlu.cc +++ b/paddle/fluid/operators/rnn_op_mlu.cc @@ -11,10 +11,10 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/fluid/platform/device/xpu/xpu_header.h" #include "paddle/fluid/platform/device_context.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { @@ -97,7 +97,7 @@ class RNNMLUKernel : public framework::OpKernel { std::vector seq_len_vec(batch_size, seq_len); if (has_seq_length) { // set seq_len if no padding, otherwise seq_len for // each element. - seq_len_vec = operators::GetDataFromTensor(sequence_length); + seq_len_vec = phi::GetVectorFromTensor(sequence_length); } cnnlDirectionMode_t direction = is_bidirec ? CNNL_RNN_BIDIRECTIONAL : CNNL_RNN_UNIDIRECTIONAL; @@ -480,7 +480,7 @@ class RNNMLUGradKernel : public framework::OpKernel { std::vector seq_len_vec(batch_size, seq_len); if (has_seq_length) { - seq_len_vec = operators::GetDataFromTensor(sequence_length); + seq_len_vec = phi::GetVectorFromTensor(sequence_length); } cnnlDirectionMode_t direction = is_bidirec ? CNNL_RNN_BIDIRECTIONAL : CNNL_RNN_UNIDIRECTIONAL; diff --git a/paddle/fluid/operators/slice_op_mlu.cc b/paddle/fluid/operators/slice_op_mlu.cc index 771fca6a5ef18dff9bc230119bcbbffaccd49390..107907ecc7642641f20988c13579e00432d5c2f0 100644 --- a/paddle/fluid/operators/slice_op_mlu.cc +++ b/paddle/fluid/operators/slice_op_mlu.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" #include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/slice_utils.h" namespace paddle { @@ -38,15 +39,16 @@ class SliceMLUKernel : public framework::OpKernel { auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { - starts = - GetDataFromTensor(ctx.Input("StartsTensor")); + starts = phi::GetVectorFromTensor( + ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { - ends = GetDataFromTensor(ctx.Input("EndsTensor")); + ends = phi::GetVectorFromTensor( + ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } @@ -141,15 +143,16 @@ class SliceGradMLUKernel : public framework::OpKernel { auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { - starts = - GetDataFromTensor(ctx.Input("StartsTensor")); + starts = phi::GetVectorFromTensor( + ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { - ends = GetDataFromTensor(ctx.Input("EndsTensor")); + ends = phi::GetVectorFromTensor( + ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } diff --git a/paddle/fluid/operators/slice_op_npu.cc b/paddle/fluid/operators/slice_op_npu.cc index 59d6e2c2e42c15784910229d3487d90b7201befa..88bf695d07f605f14d5c3e3580ac85f37dfb0ab7 100644 --- a/paddle/fluid/operators/slice_op_npu.cc +++ b/paddle/fluid/operators/slice_op_npu.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/slice_utils.h" namespace paddle { @@ -77,15 +78,16 @@ class SliceNPUKernel : public framework::OpKernel { auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { - starts = - GetDataFromTensor(ctx.Input("StartsTensor")); + starts = phi::GetVectorFromTensor( + ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { - ends = GetDataFromTensor(ctx.Input("EndsTensor")); + ends = phi::GetVectorFromTensor( + ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } @@ -172,15 +174,16 @@ class SliceGradNPUKernel : public framework::OpKernel { auto starts_tensor_list = ctx.MultiInput("StartsTensorList"); if (ctx.HasInput("StartsTensor")) { - starts = - GetDataFromTensor(ctx.Input("StartsTensor")); + starts = phi::GetVectorFromTensor( + ctx.Input("StartsTensor")); } else if (starts_tensor_list.size() > 0) { starts = GetDataFromTensorList(starts_tensor_list); } auto ends_tensor_list = ctx.MultiInput("EndsTensorList"); if (ctx.HasInput("EndsTensor")) { - ends = GetDataFromTensor(ctx.Input("EndsTensor")); + ends = phi::GetVectorFromTensor( + ctx.Input("EndsTensor")); } else if (ends_tensor_list.size() > 0) { ends = GetDataFromTensorList(ends_tensor_list); } diff --git a/paddle/fluid/operators/split_op_mlu.cc b/paddle/fluid/operators/split_op_mlu.cc index 77928c7efc8da0c367fdbae27bf13513b38f5632..af547afd0329437467a24986018f6dd4b7b17fa5 100644 --- a/paddle/fluid/operators/split_op_mlu.cc +++ b/paddle/fluid/operators/split_op_mlu.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/fluid/operators/mlu/mlu_baseop.h" #include "paddle/fluid/operators/split_op.h" +#include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace operators { @@ -35,7 +36,7 @@ class SplitMLUKernel : public framework::OpKernel { bool need_resize_outs_dims = false; if (ctx.HasInput("AxisTensor")) { auto* axis_tensor = ctx.Input("AxisTensor"); - axis = GetDataFromTensor(axis_tensor)[0]; + axis = phi::GetVectorFromTensor(axis_tensor)[0]; need_resize_outs_dims = true; } auto sections_tensor_list = diff --git a/paddle/fluid/operators/strided_slice_op_mlu.cc b/paddle/fluid/operators/strided_slice_op_mlu.cc index 21eb47f187b007f1a2a8e29f281f74b906d64593..ea4adbfe678c625f54082c28dafe160698444648 100644 --- a/paddle/fluid/operators/strided_slice_op_mlu.cc +++ b/paddle/fluid/operators/strided_slice_op_mlu.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" #include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/strided_slice.h" namespace paddle { @@ -168,21 +169,21 @@ class StridedSliceMLUKernel : public framework::OpKernel { starts = GetDataFromTensorList(list_new_starts_tensor); } else if (ctx.HasInput("StartsTensor")) { auto* starts_tensor = ctx.Input("StartsTensor"); - starts = GetDataFromTensor(starts_tensor); + starts = phi::GetVectorFromTensor(starts_tensor); } if (list_new_ends_tensor.size() > 0) { ends = GetDataFromTensorList(list_new_ends_tensor); } else if (ctx.HasInput("EndsTensor")) { auto* ends_tensor = ctx.Input("EndsTensor"); - ends = GetDataFromTensor(ends_tensor); + ends = phi::GetVectorFromTensor(ends_tensor); } if (list_new_strides_tensor.size() > 0) { strides = GetDataFromTensorList(list_new_strides_tensor); } else if (ctx.HasInput("StridesTensor")) { auto* strides_tensor = ctx.Input("StridesTensor"); - strides = GetDataFromTensor(strides_tensor); + strides = phi::GetVectorFromTensor(strides_tensor); } // out dims calculation @@ -336,21 +337,21 @@ class StridedSliceGradMLUKernel : public framework::OpKernel { starts = GetDataFromTensorList(list_new_starts_tensor); } else if (ctx.HasInput("StartsTensor")) { auto* starts_tensor = ctx.Input("StartsTensor"); - starts = GetDataFromTensor(starts_tensor); + starts = phi::GetVectorFromTensor(starts_tensor); } if (list_new_ends_tensor.size() > 0) { ends = GetDataFromTensorList(list_new_ends_tensor); } else if (ctx.HasInput("EndsTensor")) { auto* ends_tensor = ctx.Input("EndsTensor"); - ends = GetDataFromTensor(ends_tensor); + ends = phi::GetVectorFromTensor(ends_tensor); } if (list_new_strides_tensor.size() > 0) { strides = GetDataFromTensorList(list_new_strides_tensor); } else if (ctx.HasInput("StridesTensor")) { auto* strides_tensor = ctx.Input("StridesTensor"); - strides = GetDataFromTensor(strides_tensor); + strides = phi::GetVectorFromTensor(strides_tensor); } std::vector out_dims_vector(input_dims.size(), -1); diff --git a/paddle/fluid/operators/strided_slice_op_npu.cc b/paddle/fluid/operators/strided_slice_op_npu.cc index 23bf6ea689602e64dc51bde080ca4d4cee5bdc69..0a7861f255fa255f75251aa2e32b101d102d045a 100644 --- a/paddle/fluid/operators/strided_slice_op_npu.cc +++ b/paddle/fluid/operators/strided_slice_op_npu.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device/npu/npu_op_runner.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/strided_slice.h" namespace paddle { @@ -99,21 +100,21 @@ class StridedSliceNPUKernel : public framework::OpKernel { starts = GetDataFromTensorList(list_new_starts_tensor); } else if (ctx.HasInput("StartsTensor")) { auto* starts_tensor = ctx.Input("StartsTensor"); - starts = GetDataFromTensor(starts_tensor); + starts = phi::GetVectorFromTensor(starts_tensor); } if (list_new_ends_tensor.size() > 0) { ends = GetDataFromTensorList(list_new_ends_tensor); } else if (ctx.HasInput("EndsTensor")) { auto* ends_tensor = ctx.Input("EndsTensor"); - ends = GetDataFromTensor(ends_tensor); + ends = phi::GetVectorFromTensor(ends_tensor); } if (list_new_strides_tensor.size() > 0) { strides = GetDataFromTensorList(list_new_strides_tensor); } else if (ctx.HasInput("StridesTensor")) { auto* strides_tensor = ctx.Input("StridesTensor"); - strides = GetDataFromTensor(strides_tensor); + strides = phi::GetVectorFromTensor(strides_tensor); } // out dims calculation @@ -325,21 +326,21 @@ class StridedSliceGradNPUKernel : public framework::OpKernel { starts = GetDataFromTensorList(list_new_starts_tensor); } else if (ctx.HasInput("StartsTensor")) { auto* starts_tensor = ctx.Input("StartsTensor"); - starts = GetDataFromTensor(starts_tensor); + starts = phi::GetVectorFromTensor(starts_tensor); } if (list_new_ends_tensor.size() > 0) { ends = GetDataFromTensorList(list_new_ends_tensor); } else if (ctx.HasInput("EndsTensor")) { auto* ends_tensor = ctx.Input("EndsTensor"); - ends = GetDataFromTensor(ends_tensor); + ends = phi::GetVectorFromTensor(ends_tensor); } if (list_new_strides_tensor.size() > 0) { strides = GetDataFromTensorList(list_new_strides_tensor); } else if (ctx.HasInput("StridesTensor")) { auto* strides_tensor = ctx.Input("StridesTensor"); - strides = GetDataFromTensor(strides_tensor); + strides = phi::GetVectorFromTensor(strides_tensor); } std::vector out_dims_vector(input_dims.size(), -1); diff --git a/paddle/fluid/operators/unsqueeze_op.h b/paddle/fluid/operators/unsqueeze_op.h index 223dcad524cffdd24552b27caa52f94f4acd59df..94d8ede8e134c26a4996ba2aa3980f585be5a891 100644 --- a/paddle/fluid/operators/unsqueeze_op.h +++ b/paddle/fluid/operators/unsqueeze_op.h @@ -19,6 +19,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" #include "paddle/fluid/platform/device_context.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/blas/blas.h" #include "paddle/phi/kernels/funcs/math_function.h" @@ -42,7 +43,7 @@ class UnsqueezeKernel : public framework::OpKernel { axes = GetDataFromTensorList(axes_tensor_list); } else if (context.HasInput("AxesTensor")) { auto *axes_tensor = context.Input("AxesTensor"); - axes = GetDataFromTensor(axes_tensor); + axes = phi::GetVectorFromTensor(axes_tensor); } need_resize_out_dims = true; } diff --git a/paddle/fluid/operators/utils.h b/paddle/fluid/operators/utils.h index f6d3132ef267d53e99bdd575b5cb88d72330ea40..d50c8fe51c96bc435b14b6099c5f31c521e8a474 100644 --- a/paddle/fluid/operators/utils.h +++ b/paddle/fluid/operators/utils.h @@ -18,41 +18,11 @@ limitations under the License. */ #include #include +#include "paddle/phi/core/tensor_utils.h" + namespace paddle { namespace operators { -template -inline std::vector GetDataFromTensor(const phi::DenseTensor* x) { - std::vector vec_new_data; - if (framework::TransToProtoVarType(x->dtype()) == - framework::proto::VarType::INT32) { - auto* data = x->data(); - phi::DenseTensor cpu_attr_tensor; - if (!platform::is_cpu_place(x->place())) { - paddle::framework::TensorCopySync( - *x, platform::CPUPlace(), &cpu_attr_tensor); - data = cpu_attr_tensor.data(); - } - vec_new_data = std::vector(data, data + x->numel()); - } else if (framework::TransToProtoVarType(x->dtype()) == - framework::proto::VarType::INT64) { - auto* data = x->data(); - phi::DenseTensor cpu_attr_tensor; - if (!platform::is_cpu_place(x->place())) { - paddle::framework::TensorCopySync( - *x, platform::CPUPlace(), &cpu_attr_tensor); - data = cpu_attr_tensor.data(); - } - // NOTE: Converting int64 to int32 may cause data overflow. - vec_new_data = std::vector(data, data + x->numel()); - } else { - PADDLE_THROW(platform::errors::InvalidArgument( - "The dtype of Tensor must be int32 or int64, but received: %s", - framework::TransToProtoVarType(x->dtype()))); - } - return vec_new_data; -} - template inline std::vector GetDataFromTensorList( const std::vector& list_tensor) { @@ -100,7 +70,7 @@ inline framework::DDim GetShape(const framework::ExecutionContext& ctx) { // 1. shape is a Tensor if (ctx.HasInput("ShapeTensor")) { auto* shape_tensor = ctx.Input("ShapeTensor"); - auto vec_shape = GetDataFromTensor(shape_tensor); + auto vec_shape = phi::GetVectorFromTensor(shape_tensor); return phi::make_ddim(vec_shape); } diff --git a/paddle/phi/core/tensor_utils.h b/paddle/phi/core/tensor_utils.h index df6db077f0310da43610720a0bc6c42746b25baa..9203621931963a8aa60a5a674efa0f3faf391c01 100644 --- a/paddle/phi/core/tensor_utils.h +++ b/paddle/phi/core/tensor_utils.h @@ -14,12 +14,16 @@ limitations under the License. */ #pragma once +#include "paddle/phi/backends/all_context.h" +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/common/place.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/core/tensor_meta.h" +#include "paddle/phi/core/utils/data_type.h" namespace phi { @@ -144,4 +148,36 @@ inline T GetValue(const Context& dev_ctx, const DenseTensor& x) { return value; } +template +inline std::vector GetVectorFromTensor(const phi::DenseTensor* x) { + std::vector vec_new_data; + if (phi::TransToProtoVarType(x->dtype()) == ProtoDataType::INT32) { + auto* data = x->data(); + phi::DenseTensor cpu_attr_tensor; + if (!paddle::platform::is_cpu_place(x->place())) { + phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance(); + auto dev_ctx = pool.Get(x->place()); + phi::Copy(*dev_ctx, *x, CPUPlace(), true, &cpu_attr_tensor); + data = cpu_attr_tensor.data(); + } + vec_new_data = std::vector(data, data + x->numel()); + } else if (phi::TransToProtoVarType(x->dtype()) == ProtoDataType::INT64) { + auto* data = x->data(); + phi::DenseTensor cpu_attr_tensor; + if (!paddle::platform::is_cpu_place(x->place())) { + phi::DeviceContextPool& pool = phi::DeviceContextPool::Instance(); + auto dev_ctx = pool.Get(x->place()); + phi::Copy(*dev_ctx, *x, CPUPlace(), true, &cpu_attr_tensor); + data = cpu_attr_tensor.data(); + } + // NOTE: Converting int64 to int32 may cause data overflow. + vec_new_data = std::vector(data, data + x->numel()); + } else { + PADDLE_THROW(phi::errors::InvalidArgument( + "The dtype of Tensor must be int32 or int64, but received: %s", + phi::TransToProtoVarType(x->dtype()))); + } + return vec_new_data; +} + } // namespace phi diff --git a/paddle/phi/kernels/cpu/rnn_functor.h b/paddle/phi/kernels/cpu/rnn_functor.h index cdf024d1ec960434066756a177096b786ef94aea..49b0c7655c6df21f5af4c45f4523bd42237ce25e 100644 --- a/paddle/phi/kernels/cpu/rnn_functor.h +++ b/paddle/phi/kernels/cpu/rnn_functor.h @@ -14,7 +14,6 @@ #pragma once -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/generator.h" #include "paddle/phi/core/tensor_utils.h" @@ -47,8 +46,7 @@ void CreateMaskMatrix(const CPUContext& dev_ctx, DenseTensor* mask_matrix, const bool& is_reverse, int* min_seq_len) { - const auto& seq_len_vec = - paddle::operators::GetDataFromTensor(sequence_length); + const auto& seq_len_vec = phi::GetVectorFromTensor(sequence_length); const int table_width = mask_matrix->dims()[0]; DenseTensor temp = Empty(dev_ctx, {mask_matrix->dims()[1], mask_matrix->dims()[0]}); diff --git a/paddle/phi/kernels/gpu/rnn_grad_kernel.cu.cc b/paddle/phi/kernels/gpu/rnn_grad_kernel.cu.cc index ff1d295b11e6817a63cf5e0ca2967b768afcb04c..fe45d56bd9d9569c8d015affdb31a28c90d628f5 100644 --- a/paddle/phi/kernels/gpu/rnn_grad_kernel.cu.cc +++ b/paddle/phi/kernels/gpu/rnn_grad_kernel.cu.cc @@ -14,9 +14,9 @@ #include "paddle/phi/kernels/rnn_grad_kernel.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/gpu/rnn_functor.h" @@ -223,8 +223,7 @@ void RnnGradKernel(const Context &dev_ctx, #endif std::vector SequenceLength; if (has_seq_length) { - SequenceLength = - paddle::operators::GetDataFromTensor(sequence_length.get_ptr()); + SequenceLength = phi::GetVectorFromTensor(sequence_length.get_ptr()); } auto input_dims = x.dims(); diff --git a/paddle/phi/kernels/gpu/rnn_kernel.cu.cc b/paddle/phi/kernels/gpu/rnn_kernel.cu.cc index 44fab87d910d76e8a468480d5725290f0be63a60..6b4e8b45fb7ec49707b8223f4d942d90d064b3ba 100644 --- a/paddle/phi/kernels/gpu/rnn_kernel.cu.cc +++ b/paddle/phi/kernels/gpu/rnn_kernel.cu.cc @@ -14,10 +14,10 @@ #include "paddle/phi/kernels/rnn_kernel.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/generator.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/gpu/rnn_functor.h" @@ -205,8 +205,7 @@ void RnnKernel(const Context &dev_ctx, #endif std::vector SequenceLength; if (has_seq_length) { - SequenceLength = - paddle::operators::GetDataFromTensor(sequence_length.get_ptr()); + SequenceLength = phi::GetVectorFromTensor(sequence_length.get_ptr()); } auto handle = dev_ctx.cudnn_handle(); diff --git a/paddle/phi/kernels/xpu/rnn_grad_kernel.cc b/paddle/phi/kernels/xpu/rnn_grad_kernel.cc index c7ba5e7f765f05328bd963ca17b3ae5df48aaf66..fe0a8fc56961d2041f20a9218d2ef6cd3a9b9a3b 100644 --- a/paddle/phi/kernels/xpu/rnn_grad_kernel.cc +++ b/paddle/phi/kernels/xpu/rnn_grad_kernel.cc @@ -13,9 +13,9 @@ // limitations under the License. #include "paddle/phi/kernels/rnn_grad_kernel.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/xpu/rnn_util.h" @@ -165,8 +165,7 @@ void RnnGradKernel(const Context& dev_ctx, bool has_seq_length = sequence_length.is_initialized(); std::vector seq_len_tensor(batch_size, seq_len); if (has_seq_length) { - seq_len_tensor = - paddle::operators::GetDataFromTensor(sequence_length.get_ptr()); + seq_len_tensor = phi::GetVectorFromTensor(sequence_length.get_ptr()); } for (int i = num_layers - 1; i >= 0; --i) { diff --git a/paddle/phi/kernels/xpu/rnn_kernel.cc b/paddle/phi/kernels/xpu/rnn_kernel.cc index faa4dce29f77d880406fdbe72dabbaa503944476..b529423992d4954fbb515ffb2cd85f3ee490e995 100644 --- a/paddle/phi/kernels/xpu/rnn_kernel.cc +++ b/paddle/phi/kernels/xpu/rnn_kernel.cc @@ -13,9 +13,9 @@ // limitations under the License. #include "paddle/phi/kernels/rnn_kernel.h" -#include "paddle/fluid/operators/utils.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/xpu/rnn_util.h" @@ -119,8 +119,7 @@ void RnnKernel(const Context& dev_ctx, bool has_seq_length = sequence_length.is_initialized(); if (has_seq_length) { - seq_len_tensor = - paddle::operators::GetDataFromTensor(sequence_length.get_ptr()); + seq_len_tensor = phi::GetVectorFromTensor(sequence_length.get_ptr()); } int state_offset = pre_state[0]->dims()[1] * pre_state[0]->dims()[2];