未验证 提交 905cefd4 编写于 作者: H Huang Jiyi 提交者: GitHub

[phi decoupling] remove variable.h in phi (#50407)

* move variable_utils from phi_api_utils to fluid

* fix coment

* update include

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* fix bugs

* update

* update

* fix CI-Windows-OpenBLAS

* fix bugs

* fix bugs

* fix bugs

* update include

* move variable_utils to phi_utils

* fix namespace
上级 df0ed4d6
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "paddle/fluid/framework/variable.h" #include "paddle/fluid/framework/variable.h"
// Phi deps // Phi deps
#include "paddle/phi/api/include/tensor.h" #include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/compat/convert_utils.h"
namespace egr { namespace egr {
......
...@@ -435,24 +435,28 @@ if(WITH_XPU) ...@@ -435,24 +435,28 @@ if(WITH_XPU)
phi_utils phi_utils
SRCS phi_utils.cc SRCS phi_utils.cc
DEPS lod_tensor DEPS lod_tensor
dense_tensor
selected_rows_utils selected_rows_utils
place place
phi phi
var_type_traits var_type_traits
phi_api_utils
op_info op_info
xpu_op_list) xpu_op_list
convert_utils
phi_api_utils)
else() else()
cc_library( cc_library(
phi_utils phi_utils
SRCS phi_utils.cc SRCS phi_utils.cc
DEPS lod_tensor DEPS lod_tensor
dense_tensor
selected_rows_utils selected_rows_utils
place place
phi phi
var_type_traits var_type_traits
phi_api_utils op_info
op_info) convert_utils
phi_api_utils)
endif() endif()
if(WITH_XPU) if(WITH_XPU)
...@@ -1158,7 +1162,7 @@ cc_library( ...@@ -1158,7 +1162,7 @@ cc_library(
place place
var_type_traits var_type_traits
phi phi
phi_api_utils phi_utils
op_info op_info
shape_inference shape_inference
sparse_coo_tensor) sparse_coo_tensor)
......
...@@ -37,7 +37,6 @@ limitations under the License. */ ...@@ -37,7 +37,6 @@ limitations under the License. */
#include "paddle/fluid/platform/dynload/dynamic_loader.h" #include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/string/string_helper.h" #include "paddle/fluid/string/string_helper.h"
#include "paddle/phi/api/all.h" #include "paddle/phi/api/all.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/tensor_utils.h"
#include "paddle/utils/any.h" #include "paddle/utils/any.h"
......
...@@ -618,7 +618,7 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -618,7 +618,7 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
if (ctx->IsRuntime()) { if (ctx->IsRuntime()) {
Variable* var = PADDLE_GET_CONST(Variable*, infershape_input[0]); Variable* var = PADDLE_GET_CONST(Variable*, infershape_input[0]);
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
std::move(experimental::MakePhiScalarFromVar(*var))); std::move(framework::MakePhiScalarFromVar(*var)));
} else { } else {
phi::Scalar tensor_scalar(-1); phi::Scalar tensor_scalar(-1);
tensor_scalar.SetFromTensor(true); tensor_scalar.SetFromTensor(true);
...@@ -670,10 +670,10 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx, ...@@ -670,10 +670,10 @@ CompatInferMetaContext BuildInferMetaContext(InferShapeContext* ctx,
} }
if (infershape_inputs.size() != 1) { if (infershape_inputs.size() != 1) {
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
std::move(experimental::MakePhiIntArrayFromVarList(vars))); std::move(framework::MakePhiIntArrayFromVarList(vars)));
} else { } else {
infer_meta_context.EmplaceBackAttr( infer_meta_context.EmplaceBackAttr(
std::move(experimental::MakePhiIntArrayFromVar(*vars[0]))); std::move(framework::MakePhiIntArrayFromVar(*vars[0])));
} }
} else { } else {
// If is not in runtime, we will set default value(-1) for IntArray // If is not in runtime, we will set default value(-1) for IntArray
......
...@@ -3227,8 +3227,8 @@ void OperatorWithKernel::BuildPhiKernelContext( ...@@ -3227,8 +3227,8 @@ void OperatorWithKernel::BuildPhiKernelContext(
} else { // scalar is in the input } else { // scalar is in the input
need_prepare_phi_data_ = true; need_prepare_phi_data_ = true;
auto& ins_vector = ctx.inputs.at(attr_names[i]); auto& ins_vector = ctx.inputs.at(attr_names[i]);
phi_kernel_context->EmplaceBackAttr(std::move( phi_kernel_context->EmplaceBackAttr(
experimental::MakePhiScalarFromVar(*ins_vector.front()))); std::move(framework::MakePhiScalarFromVar(*ins_vector.front())));
} }
break; break;
case phi::AttributeType::INT_ARRAY: case phi::AttributeType::INT_ARRAY:
...@@ -3261,10 +3261,10 @@ void OperatorWithKernel::BuildPhiKernelContext( ...@@ -3261,10 +3261,10 @@ void OperatorWithKernel::BuildPhiKernelContext(
auto& ins_vector = ctx.inputs.at(attr_names[i]); auto& ins_vector = ctx.inputs.at(attr_names[i]);
if (ins_vector.size() == 1) { // ShapeTensor if (ins_vector.size() == 1) { // ShapeTensor
phi_kernel_context->EmplaceBackAttr(std::move( phi_kernel_context->EmplaceBackAttr(std::move(
experimental::MakePhiIntArrayFromVar(*ins_vector.front()))); framework::MakePhiIntArrayFromVar(*ins_vector.front())));
} else { // ShapeTensorList } else { // ShapeTensorList
phi_kernel_context->EmplaceBackAttr(std::move( phi_kernel_context->EmplaceBackAttr(
experimental::MakePhiIntArrayFromVarList(ins_vector))); std::move(framework::MakePhiIntArrayFromVarList(ins_vector)));
} }
} }
break; break;
......
...@@ -20,11 +20,11 @@ limitations under the License. */ ...@@ -20,11 +20,11 @@ limitations under the License. */
#include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_info.h" #include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/framework/selected_rows_utils.h" #include "paddle/fluid/framework/selected_rows_utils.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/string/string_helper.h" #include "paddle/fluid/string/string_helper.h"
#include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/compat/op_utils.h" #include "paddle/phi/core/compat/op_utils.h"
#include "paddle/phi/core/kernel_factory.h" #include "paddle/phi/core/kernel_factory.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/core/type_defs.h" #include "paddle/phi/core/type_defs.h"
namespace paddle { namespace paddle {
...@@ -280,5 +280,100 @@ static void SetAllocationForUninitializedDenseTensor( ...@@ -280,5 +280,100 @@ static void SetAllocationForUninitializedDenseTensor(
dense_tensor->ResetHolder(shared_allocation); dense_tensor->ResetHolder(shared_allocation);
} }
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable) {
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU);
if (variable.IsType<phi::DenseTensor>()) {
const auto& tensor = variable.Get<phi::DenseTensor>();
PADDLE_ENFORCE_EQ(
tensor.numel(),
1UL,
platform::errors::InvalidArgument("The DenseTensor used to construct "
"the Scalar contains more than 1 "
"value, it contains `%d` values.",
tensor.numel()));
if (!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
return {tmp_tensor};
} else {
return {tensor};
}
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupport casting input `%s` type to Scalar when call pt "
"kernel.",
framework::ToTypeName(variable.Type())));
}
}
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable) {
if (variable.IsType<phi::DenseTensor>()) {
const auto& tensor = variable.Get<phi::DenseTensor>();
return paddle::experimental::MakePhiIntArray(tensor);
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupport casting input `%s` type to IntArray when call pt "
"kernel.",
framework::ToTypeName(variable.Type())));
}
}
// TODO(chentianyu03): Inplace with IntArray constructor
phi::IntArray MakePhiIntArrayFromVarList(
const std::vector<framework::Variable*>& variable_list) {
if (variable_list.size() == 0) {
return phi::IntArray();
}
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU);
std::vector<int64_t> vector_data;
vector_data.reserve(variable_list.size());
for (auto* var : variable_list) {
paddle::experimental::DataType data_type;
if (var->IsType<phi::DenseTensor>()) {
const auto& tensor = var->Get<phi::DenseTensor>();
data_type = tensor.dtype();
if (data_type == paddle::experimental::DataType::INT64) {
const auto& tensor = var->Get<phi::DenseTensor>();
if (tensor.IsInitialized() &&
!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
vector_data.push_back(*tmp_tensor.data<int64_t>());
} else {
vector_data.push_back(*tensor.data<int64_t>());
}
} else if (data_type == paddle::experimental::DataType::INT32) {
const auto& tensor = var->Get<phi::DenseTensor>();
if (tensor.IsInitialized() &&
!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
vector_data.push_back(*tmp_tensor.data<int32_t>());
} else {
vector_data.push_back(*tensor.data<int32_t>());
}
} else {
PADDLE_THROW(phi::errors::InvalidArgument(
"Data type error. When cast a LoDTensor to VectorTensor, "
"the data type of LoDTensor must be int32 or int64, "
"but now data type is %s.",
data_type));
}
} else {
PADDLE_THROW(phi::errors::Unimplemented(
"Unsupport casting input `%s` type to VectorTensor when call pt "
"kernel.",
framework::ToTypeName(var->Type())));
}
}
phi::IntArray result{vector_data};
result.SetFromTensor(true);
return result;
}
} // namespace framework } // namespace framework
} // namespace paddle } // namespace paddle
...@@ -23,6 +23,7 @@ limitations under the License. */ ...@@ -23,6 +23,7 @@ limitations under the License. */
#include "paddle/fluid/framework/op_kernel_type.h" #include "paddle/fluid/framework/op_kernel_type.h"
#include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/variable.h"
#include "paddle/fluid/platform/macros.h" #include "paddle/fluid/platform/macros.h"
#include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/place.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/api/lib/utils/tensor_utils.h"
...@@ -86,5 +87,15 @@ struct ConvertToPhiContext<platform::XPUDeviceContext> { ...@@ -86,5 +87,15 @@ struct ConvertToPhiContext<platform::XPUDeviceContext> {
}; };
#endif #endif
/* Make Phi Tensor from framework::Variable */
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable);
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable);
// TODO(chentianyu03): Inplace with IntArray constructor
phi::IntArray MakePhiIntArrayFromVarList(
const std::vector<framework::Variable*>& variable_list);
} // namespace framework } // namespace framework
} // namespace paddle } // namespace paddle
...@@ -434,8 +434,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -434,8 +434,8 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
} }
} else { // scalar is in the input } else { // scalar is in the input
auto& ins_vector = ins.at(attr_names[i]); auto& ins_vector = ins.at(attr_names[i]);
kernel_ctx->EmplaceBackAttr(std::move( kernel_ctx->EmplaceBackAttr(
experimental::MakePhiScalarFromVar(ins_vector[0]->Var()))); std::move(framework::MakePhiScalarFromVar(ins_vector[0]->Var())));
} }
break; break;
case phi::AttributeType::INT_ARRAY: case phi::AttributeType::INT_ARRAY:
...@@ -468,7 +468,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -468,7 +468,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
auto& ins_vector = ins.at(attr_names[i]); auto& ins_vector = ins.at(attr_names[i]);
if (ins_vector.size() == 1) { // ShapeTensor if (ins_vector.size() == 1) { // ShapeTensor
kernel_ctx->EmplaceBackAttr(std::move( kernel_ctx->EmplaceBackAttr(std::move(
experimental::MakePhiIntArrayFromVar(ins_vector[0]->Var()))); framework::MakePhiIntArrayFromVar(ins_vector[0]->Var())));
} else { // ShapeTensorList } else { // ShapeTensorList
std::vector<framework::Variable*> variables; std::vector<framework::Variable*> variables;
variables.reserve(ins_vector.size()); variables.reserve(ins_vector.size());
...@@ -476,7 +476,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature, ...@@ -476,7 +476,7 @@ void BuildDygraphPhiKernelContext(const phi::KernelSignature& kernel_signature,
variables.push_back(var_base->MutableVar()); variables.push_back(var_base->MutableVar());
} }
kernel_ctx->EmplaceBackAttr( kernel_ctx->EmplaceBackAttr(
std::move(experimental::MakePhiIntArrayFromVarList(variables))); std::move(framework::MakePhiIntArrayFromVarList(variables)));
} }
} }
break; break;
......
...@@ -46,7 +46,7 @@ cc_test( ...@@ -46,7 +46,7 @@ cc_test(
math_function math_function
phi_tensor phi_tensor
phi_api phi_api
phi_api_utils) phi_utils)
cc_test( cc_test(
test_layer test_layer
SRCS test_layer.cc SRCS test_layer.cc
......
...@@ -95,7 +95,7 @@ if(WITH_UNITY_BUILD) ...@@ -95,7 +95,7 @@ if(WITH_UNITY_BUILD)
include(unity_build_rule.cmake) include(unity_build_rule.cmake)
endif() endif()
set(OP_HEADER_DEPS ${OP_HEADER_DEPS} phi phi_api_utils backward_infermeta sparse_backward_infermeta static_prim_api) set(OP_HEADER_DEPS ${OP_HEADER_DEPS} phi phi_utils backward_infermeta sparse_backward_infermeta static_prim_api)
register_operators(EXCLUDES py_func_op warpctc_op dgc_op load_combine_op lstm_op run_program_op eye_op quantize_linear_op register_operators(EXCLUDES py_func_op warpctc_op dgc_op load_combine_op lstm_op run_program_op eye_op quantize_linear_op
recurrent_op save_combine_op sparse_attention_op sync_batch_norm_op ${OP_MKL_DEPS} DEPS ${OP_HEADER_DEPS}) recurrent_op save_combine_op sparse_attention_op sync_batch_norm_op ${OP_MKL_DEPS} DEPS ${OP_HEADER_DEPS})
......
...@@ -16,8 +16,8 @@ limitations under the License. */ ...@@ -16,8 +16,8 @@ limitations under the License. */
#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/platform/transform.h" #include "paddle/fluid/platform/transform.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/kernels/cast_kernel.h" #include "paddle/phi/kernels/cast_kernel.h"
namespace paddle { namespace paddle {
......
...@@ -24,11 +24,11 @@ limitations under the License. */ ...@@ -24,11 +24,11 @@ limitations under the License. */
#include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/memory/malloc.h"
#include "paddle/fluid/operators/elementwise/elementwise_functor.h" #include "paddle/fluid/operators/elementwise/elementwise_functor.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.h" #include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/transform.h" #include "paddle/fluid/platform/transform.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/kernels/cpu/elementwise.h" #include "paddle/phi/kernels/cpu/elementwise.h"
#include "paddle/phi/kernels/cpu/elementwise_grad.h" #include "paddle/phi/kernels/cpu/elementwise_grad.h"
......
...@@ -18,7 +18,6 @@ limitations under the License. */ ...@@ -18,7 +18,6 @@ limitations under the License. */
#include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/tensor.h"
// only can include the headers in paddle/top/api dirs // only can include the headers in paddle/top/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/kernels/funcs/broadcast_function.h" #include "paddle/phi/kernels/funcs/broadcast_function.h"
namespace paddle { namespace paddle {
......
...@@ -28,7 +28,7 @@ math_library(sampler DEPS generator) ...@@ -28,7 +28,7 @@ math_library(sampler DEPS generator)
# math_library(math_function DEPS blas dense_tensor tensor) # math_library(math_function DEPS blas dense_tensor tensor)
math_library(sequence_padding) math_library(sequence_padding DEPS lod_tensor)
math_library(sequence_pooling DEPS math_function jit_kernel_helper) math_library(sequence_pooling DEPS math_function jit_kernel_helper)
if(WITH_ASCEND_CL) if(WITH_ASCEND_CL)
math_library(beam_search DEPS math_function beam_search_npu) math_library(beam_search DEPS math_function beam_search_npu)
......
...@@ -48,8 +48,8 @@ inline static size_t TotalSequenceLength( ...@@ -48,8 +48,8 @@ inline static size_t TotalSequenceLength(
return total_seq_len; return total_seq_len;
} }
inline static void CheckDims(const framework::DDim& seq_tensor_dims, inline static void CheckDims(const phi::DDim& seq_tensor_dims,
const framework::DDim& pad_tensor_dims, const phi::DDim& pad_tensor_dims,
const phi::Vector<size_t>& seq_offset, const phi::Vector<size_t>& seq_offset,
int64_t padded_seq_len, int64_t padded_seq_len,
int64_t step_width, int64_t step_width,
...@@ -57,7 +57,7 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims, ...@@ -57,7 +57,7 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims,
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
static_cast<size_t>(seq_tensor_dims[0]), static_cast<size_t>(seq_tensor_dims[0]),
seq_offset.back(), seq_offset.back(),
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"Value of 1st dimension of the sequence tensor should be " "Value of 1st dimension of the sequence tensor should be "
"equal to sum of lengths of all sequences. Expected %ld == %ld, but " "equal to sum of lengths of all sequences. Expected %ld == %ld, but "
"got %ld != %ld. Please check the input value.", "got %ld != %ld. Please check the input value.",
...@@ -70,7 +70,7 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims, ...@@ -70,7 +70,7 @@ inline static void CheckDims(const framework::DDim& seq_tensor_dims,
seq_tensor_dims.size() + 1 == pad_tensor_dims.size() || seq_tensor_dims.size() + 1 == pad_tensor_dims.size() ||
seq_tensor_dims.size() == pad_tensor_dims.size(), seq_tensor_dims.size() == pad_tensor_dims.size(),
true, true,
platform::errors::InvalidArgument( phi::errors::InvalidArgument(
"pad_tensor's rank should be 1 greater than seq_tensor's " "pad_tensor's rank should be 1 greater than seq_tensor's "
"rank, or be equal with it. The pad_tensor's rank is %ld, " "rank, or be equal with it. The pad_tensor's rank is %ld, "
"expected the seq_tensor's rank is %ld or %ld, but got %ld. " "expected the seq_tensor's rank is %ld or %ld, but got %ld. "
......
...@@ -26,7 +26,7 @@ limitations under the License. */ ...@@ -26,7 +26,7 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/complex_functors.h" #include "paddle/phi/kernels/funcs/complex_functors.h"
// only can include the headers in paddle/phi/api dirs // only can include the headers in paddle/phi/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/fluid/framework/phi_utils.h"
#include "paddle/phi/kernels/matmul_grad_kernel.h" #include "paddle/phi/kernels/matmul_grad_kernel.h"
#include "paddle/phi/kernels/matmul_kernel.h" #include "paddle/phi/kernels/matmul_kernel.h"
......
...@@ -26,7 +26,7 @@ limitations under the License. */ ...@@ -26,7 +26,7 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
// only can include the headers in paddle/phi/api dirs // only can include the headers in paddle/phi/api dirs
#include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/convert_utils.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/fluid/framework/phi_utils.h"
#include "paddle/phi/kernels/cpu/reduce.h" #include "paddle/phi/kernels/cpu/reduce.h"
#if defined(__HIPCC__) || defined(__NVCC__) || defined(__xpu__) #if defined(__HIPCC__) || defined(__NVCC__) || defined(__xpu__)
......
...@@ -19,7 +19,6 @@ limitations under the License. */ ...@@ -19,7 +19,6 @@ limitations under the License. */
#include "paddle/fluid/framework/phi_utils.h" #include "paddle/fluid/framework/phi_utils.h"
// only can include the headers in paddle/phi/api dirs // only can include the headers in paddle/phi/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/common/int_array.h" #include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/infermeta_utils.h" #include "paddle/phi/core/infermeta_utils.h"
......
...@@ -17,6 +17,7 @@ limitations under the License. */ ...@@ -17,6 +17,7 @@ limitations under the License. */
#include <string> #include <string>
#include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/phi/infermeta/unary.h" #include "paddle/phi/infermeta/unary.h"
namespace paddle { namespace paddle {
...@@ -61,7 +62,7 @@ class SplitOp : public framework::OperatorWithKernel { ...@@ -61,7 +62,7 @@ class SplitOp : public framework::OperatorWithKernel {
if (ctx->IsRuntime() && ctx->HasInput("AxisTensor")) { if (ctx->IsRuntime() && ctx->HasInput("AxisTensor")) {
Variable *var = Variable *var =
PADDLE_GET_CONST(Variable *, ctx->GetInputVarPtrs("AxisTensor")[0]); PADDLE_GET_CONST(Variable *, ctx->GetInputVarPtrs("AxisTensor")[0]);
axis_final = std::move(experimental::MakePhiScalarFromVar(*var)); axis_final = std::move(framework::MakePhiScalarFromVar(*var));
} else if (!ctx->IsRuntime() && ctx->HasInput("AxisTensor")) { } else if (!ctx->IsRuntime() && ctx->HasInput("AxisTensor")) {
axis_final = std::move(phi::Scalar(-1)); axis_final = std::move(phi::Scalar(-1));
axis_final.SetFromTensor(true); axis_final.SetFromTensor(true);
......
...@@ -36,10 +36,10 @@ limitations under the License. */ ...@@ -36,10 +36,10 @@ limitations under the License. */
#include "pybind11/numpy.h" #include "pybind11/numpy.h"
#include "pybind11/pybind11.h" #include "pybind11/pybind11.h"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers" #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/framework/python_headers.h" #include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/pybind/exception.h" #include "paddle/fluid/pybind/exception.h"
#include "paddle/fluid/pybind/tensor_py.h" #include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/core/string_tensor.h" #include "paddle/phi/core/string_tensor.h"
namespace paddle { namespace paddle {
namespace pybind { namespace pybind {
......
...@@ -33,6 +33,7 @@ typedef SSIZE_T ssize_t; ...@@ -33,6 +33,7 @@ typedef SSIZE_T ssize_t;
#include "paddle/fluid/framework/convert_utils.h" #include "paddle/fluid/framework/convert_utils.h"
#include "paddle/fluid/framework/custom_operator.h" #include "paddle/fluid/framework/custom_operator.h"
#include "paddle/fluid/framework/op_meta_info_helper.h" #include "paddle/fluid/framework/op_meta_info_helper.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/framework/python_headers.h" #include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/memory/allocation/allocator.h" #include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/memory/memcpy.h"
...@@ -47,7 +48,6 @@ typedef SSIZE_T ssize_t; ...@@ -47,7 +48,6 @@ typedef SSIZE_T ssize_t;
#include "paddle/fluid/pybind/tensor_py.h" #include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/ext/op_meta_info.h" #include "paddle/phi/api/ext/op_meta_info.h"
#include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/common/data_type.h" #include "paddle/phi/common/data_type.h"
#include "paddle/phi/core/compat/convert_utils.h" #include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
......
cc_library( cc_library(
phi_api_utils phi_api_utils
SRCS tensor_utils.cc SRCS tensor_utils.cc
DEPS tensor_base DEPS dense_tensor int_array scalar)
convert_utils
dense_tensor
lod_tensor
selected_rows_utils
place
var_type_traits
string_tensor
int_array
scalar)
...@@ -17,8 +17,6 @@ limitations under the License. */ ...@@ -17,8 +17,6 @@ limitations under the License. */
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "paddle/phi/core/tensor_utils.h"
namespace paddle { namespace paddle {
namespace experimental { namespace experimental {
...@@ -36,102 +34,7 @@ std::unique_ptr<phi::DenseTensor> MakePhiDenseTensor( ...@@ -36,102 +34,7 @@ std::unique_ptr<phi::DenseTensor> MakePhiDenseTensor(
return std::make_unique<phi::DenseTensor>(src); return std::make_unique<phi::DenseTensor>(src);
} }
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable) {
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU);
if (variable.IsType<phi::DenseTensor>()) {
const auto& tensor = variable.Get<phi::DenseTensor>();
PADDLE_ENFORCE_EQ(
tensor.numel(),
1UL,
platform::errors::InvalidArgument("The DenseTensor used to construct "
"the Scalar contains more than 1 "
"value, it contains `%d` values.",
tensor.numel()));
if (!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
return {tmp_tensor};
} else {
return {tensor};
}
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupport casting input `%s` type to Scalar when call pt "
"kernel.",
framework::ToTypeName(variable.Type())));
}
}
phi::IntArray MakePhiIntArray(const phi::DenseTensor& src) { return {src}; } phi::IntArray MakePhiIntArray(const phi::DenseTensor& src) { return {src}; }
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable) {
if (variable.IsType<phi::DenseTensor>()) {
const auto& tensor = variable.Get<phi::DenseTensor>();
return MakePhiIntArray(tensor);
} else {
PADDLE_THROW(platform::errors::Unimplemented(
"Unsupport casting input `%s` type to IntArray when call pt "
"kernel.",
framework::ToTypeName(variable.Type())));
}
}
// TODO(chentianyu03): Inplace with IntArray constructor
phi::IntArray MakePhiIntArrayFromVarList(
const std::vector<framework::Variable*>& variable_list) {
if (variable_list.size() == 0) {
return phi::IntArray();
}
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU);
std::vector<int64_t> vector_data;
vector_data.reserve(variable_list.size());
for (auto* var : variable_list) {
paddle::experimental::DataType data_type;
if (var->IsType<phi::DenseTensor>()) {
const auto& tensor = var->Get<phi::DenseTensor>();
data_type = tensor.dtype();
if (data_type == paddle::experimental::DataType::INT64) {
const auto& tensor = var->Get<phi::DenseTensor>();
if (tensor.IsInitialized() &&
!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
vector_data.push_back(*tmp_tensor.data<int64_t>());
} else {
vector_data.push_back(*tensor.data<int64_t>());
}
} else if (data_type == paddle::experimental::DataType::INT32) {
const auto& tensor = var->Get<phi::DenseTensor>();
if (tensor.IsInitialized() &&
!platform::is_same_place(tensor.place(), expected_place)) {
phi::DenseTensor tmp_tensor;
framework::TensorCopySync(tensor, expected_place, &tmp_tensor);
vector_data.push_back(*tmp_tensor.data<int32_t>());
} else {
vector_data.push_back(*tensor.data<int32_t>());
}
} else {
PADDLE_THROW(phi::errors::InvalidArgument(
"Data type error. When cast a LoDTensor to VectorTensor, "
"the data type of LoDTensor must be int32 or int64, "
"but now data type is %s.",
data_type));
}
} else {
PADDLE_THROW(phi::errors::Unimplemented(
"Unsupport casting input `%s` type to VectorTensor when call pt "
"kernel.",
framework::ToTypeName(var->Type())));
}
}
phi::IntArray result{vector_data};
result.SetFromTensor(true);
return result;
}
} // namespace experimental } // namespace experimental
} // namespace paddle } // namespace paddle
...@@ -14,15 +14,10 @@ limitations under the License. */ ...@@ -14,15 +14,10 @@ limitations under the License. */
#pragma once #pragma once
#include <memory>
#include "paddle/fluid/framework/variable.h"
#include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/common/int_array.h" #include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h" #include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/kernel_factory.h"
namespace paddle { namespace paddle {
namespace experimental { namespace experimental {
...@@ -32,12 +27,5 @@ std::unique_ptr<phi::DenseTensor> MakePhiDenseTensor( ...@@ -32,12 +27,5 @@ std::unique_ptr<phi::DenseTensor> MakePhiDenseTensor(
phi::IntArray MakePhiIntArray(const phi::DenseTensor& src); phi::IntArray MakePhiIntArray(const phi::DenseTensor& src);
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable);
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable);
phi::IntArray MakePhiIntArrayFromVarList(
const std::vector<framework::Variable*>& variable_list);
} // namespace experimental } // namespace experimental
} // namespace paddle } // namespace paddle
...@@ -18,6 +18,19 @@ ...@@ -18,6 +18,19 @@
namespace phi { namespace phi {
LoD ToAbsOffset(const LoD &in) {
// the lowest level stores relative offsets
if (in.empty() || in.size() == 1) return in;
LoD result = in;
for (auto level = static_cast<int>(in.size() - 2); level >= 0; level--) {
for (size_t i = 0; i < in[level].size(); ++i) {
size_t index = in[level][i];
result[level][i] = result[level + 1][index];
}
}
return result;
}
void AppendLoD(LoD *lod, const LoD &lod_length) { void AppendLoD(LoD *lod, const LoD &lod_length) {
PADDLE_ENFORCE( PADDLE_ENFORCE(
lod->empty() || lod->size() == lod_length.size(), lod->empty() || lod->size() == lod_length.size(),
......
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
namespace phi { namespace phi {
using LoD = std::vector<std::vector<std::size_t>>; using LoD = std::vector<std::vector<std::size_t>>;
/*
* Transform an LoD from relative offsets to absolute offsets.
*/
LoD ToAbsOffset(const LoD& in);
void AppendLoD(LoD* lod, const LoD& lod_length); void AppendLoD(LoD* lod, const LoD& lod_length);
/* /*
......
...@@ -47,7 +47,7 @@ set(COMMON_KERNEL_DEPS ...@@ -47,7 +47,7 @@ set(COMMON_KERNEL_DEPS
concat_and_split_functor concat_and_split_functor
selected_rows_functor) selected_rows_functor)
# remove this dep after removing fluid deps on tensor creation # remove this dep after removing fluid deps on tensor creation
set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} phi_api_utils) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} phi_api_utils lod_utils)
set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} infermeta infermeta_utils set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} infermeta infermeta_utils
sparse_infermeta) sparse_infermeta)
set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} switch_autotune) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} switch_autotune)
......
...@@ -32,7 +32,7 @@ class ScaleLoDTensorFunctor<phi::CPUContext, T> { ...@@ -32,7 +32,7 @@ class ScaleLoDTensorFunctor<phi::CPUContext, T> {
auto lod = seq->lod(); auto lod = seq->lod();
const size_t num_seq = lod[level].size() - 1; const size_t num_seq = lod[level].size() - 1;
size_t seq_width = seq->dims()[1]; size_t seq_width = seq->dims()[1];
paddle::framework::LoD abs_offset_lod = paddle::framework::ToAbsOffset(lod); phi::LoD abs_offset_lod = phi::ToAbsOffset(lod);
T* seq_data = context.template Alloc<T>(seq); T* seq_data = context.template Alloc<T>(seq);
for (size_t i = 0; i < num_seq; ++i) { for (size_t i = 0; i < num_seq; ++i) {
......
...@@ -15,6 +15,7 @@ limitations under the License. */ ...@@ -15,6 +15,7 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/sequence_scale.h" #include "paddle/phi/kernels/funcs/sequence_scale.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/backends/gpu/gpu_primitives.h" #include "paddle/phi/backends/gpu/gpu_primitives.h"
#include "paddle/phi/core/mixed_vector.h"
namespace phi { namespace phi {
namespace funcs { namespace funcs {
...@@ -44,7 +45,7 @@ class ScaleLoDTensorFunctor<phi::GPUContext, T> { ...@@ -44,7 +45,7 @@ class ScaleLoDTensorFunctor<phi::GPUContext, T> {
auto lod = seq->lod(); auto lod = seq->lod();
const size_t num_seq = lod[level].size() - 1; const size_t num_seq = lod[level].size() - 1;
const size_t seq_width = seq->numel() / seq->dims()[0]; const size_t seq_width = seq->numel() / seq->dims()[0];
auto abs_offset_lod = paddle::framework::ToAbsOffset(lod); auto abs_offset_lod = phi::ToAbsOffset(lod);
T* seq_data = context.template Alloc<T>(seq); T* seq_data = context.template Alloc<T>(seq);
phi::MixVector<size_t> mix_vector(&(abs_offset_lod[level])); phi::MixVector<size_t> mix_vector(&(abs_offset_lod[level]));
......
...@@ -14,8 +14,9 @@ limitations under the License. */ ...@@ -14,8 +14,9 @@ limitations under the License. */
#pragma once #pragma once
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/device_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/lod_utils.h"
namespace phi { namespace phi {
namespace funcs { namespace funcs {
......
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
#include "paddle/phi/kernels/multiplex_grad_kernel.h" #include "paddle/phi/kernels/multiplex_grad_kernel.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/fluid/memory/memcpy.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/eigen/common.h" #include "paddle/phi/kernels/funcs/eigen/common.h"
namespace phi { namespace phi {
...@@ -40,7 +41,7 @@ void MultiplexGradKernel(const Context& ctx, ...@@ -40,7 +41,7 @@ void MultiplexGradKernel(const Context& ctx,
auto rows = ins_grad[idx]->dims()[0]; auto rows = ins_grad[idx]->dims()[0];
auto cols = ins_grad[idx]->numel() / rows; auto cols = ins_grad[idx]->numel() / rows;
DenseTensor index_t_cpu; DenseTensor index_t_cpu;
paddle::framework::TensorCopySync(ids, phi::CPUPlace(), &index_t_cpu); phi::Copy(ctx, ids, phi::CPUPlace(), true, &index_t_cpu);
auto* index = index_t_cpu.data<int32_t>(); auto* index = index_t_cpu.data<int32_t>();
auto stream = ctx.stream(); auto stream = ctx.stream();
for (auto i = 0; i < rows; i++) { for (auto i = 0; i < rows; i++) {
......
...@@ -14,9 +14,10 @@ ...@@ -14,9 +14,10 @@
#include "paddle/phi/kernels/multiplex_kernel.h" #include "paddle/phi/kernels/multiplex_kernel.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/fluid/memory/memcpy.h"
#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"
namespace phi { namespace phi {
...@@ -38,7 +39,7 @@ void MultiplexKernel(const Context& ctx, ...@@ -38,7 +39,7 @@ void MultiplexKernel(const Context& ctx,
auto rows = ins[0]->dims()[0]; auto rows = ins[0]->dims()[0];
auto cols = ins[0]->numel() / rows; auto cols = ins[0]->numel() / rows;
DenseTensor index_t_cpu; DenseTensor index_t_cpu;
paddle::framework::TensorCopySync(ids, phi::CPUPlace(), &index_t_cpu); phi::Copy(ctx, ids, phi::CPUPlace(), true, &index_t_cpu);
auto* index = index_t_cpu.data<int32_t>(); auto* index = index_t_cpu.data<int32_t>();
auto stream = ctx.stream(); auto stream = ctx.stream();
for (auto i = 0; i < ids.dims()[0]; i++) { for (auto i = 0; i < ids.dims()[0]; i++) {
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "paddle/fluid/operators/math/sequence_padding.h" #include "paddle/fluid/operators/math/sequence_padding.h"
#include "paddle/phi/backends/dynload/warpctc.h" #include "paddle/phi/backends/dynload/warpctc.h"
#include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/lod_utils.h"
#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/empty_kernel.h" #include "paddle/phi/kernels/empty_kernel.h"
#include "paddle/phi/kernels/funcs/math_function.h" #include "paddle/phi/kernels/funcs/math_function.h"
...@@ -293,7 +294,7 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -293,7 +294,7 @@ void WarpctcKernel(const Context& dev_ctx,
phi::errors::InvalidArgument("Input(Label) Tensor of WarpCTC " phi::errors::InvalidArgument("Input(Label) Tensor of WarpCTC "
"does not contain LoD information.")); "does not contain LoD information."));
logits_lod = paddle::framework::ToAbsOffset(logits.lod())[0]; logits_lod = phi::ToAbsOffset(logits.lod())[0];
auto logits_dims = logits.dims(); auto logits_dims = logits.dims();
PADDLE_ENFORCE_GT(logits_dims[0], PADDLE_ENFORCE_GT(logits_dims[0],
...@@ -313,7 +314,7 @@ void WarpctcKernel(const Context& dev_ctx, ...@@ -313,7 +314,7 @@ void WarpctcKernel(const Context& dev_ctx,
static_cast<int64_t>(logits_lod.back()), static_cast<int64_t>(logits_lod.back()),
logits_dims[0])); logits_dims[0]));
label_lod = paddle::framework::ToAbsOffset(label.lod())[0]; label_lod = phi::ToAbsOffset(label.lod())[0];
auto label_dims = label.dims(); auto label_dims = label.dims();
PADDLE_ENFORCE_EQ(label_dims[1], PADDLE_ENFORCE_EQ(label_dims[1],
1, 1,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册