Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 20 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/fluid/eager/eager_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/variable.h"
// Phi deps
#include "paddle/fluid/framework/variable_utils.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"

namespace egr {
Expand Down
21 changes: 18 additions & 3 deletions paddle/fluid/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,21 @@ cc_library(
SRCS op_kernel_type.cc
DEPS device_context place)

cc_library(
variable_utils
SRCS variable_utils.cc
DEPS tensor_base
convert_utils
dense_tensor
lod_tensor
selected_rows_utils
place
var_type_traits
string_tensor
int_array
scalar
phi_api_utils)

if(WITH_XPU)
cc_library(
phi_utils
Expand All @@ -439,7 +454,7 @@ if(WITH_XPU)
place
phi
var_type_traits
phi_api_utils
variable_utils
op_info
xpu_op_list)
else()
Expand All @@ -451,7 +466,7 @@ else()
place
phi
var_type_traits
phi_api_utils
variable_utils
op_info)
endif()

Expand Down Expand Up @@ -1163,7 +1178,7 @@ cc_library(
place
var_type_traits
phi
phi_api_utils
variable_utils
op_info
shape_inference
sparse_coo_tensor)
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/custom_operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ limitations under the License. */
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/phi_utils.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/string/string_helper.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/tensor_utils.h"
#include "paddle/utils/any.h"
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/phi_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ limitations under the License. */
#include "paddle/fluid/framework/op_kernel_type.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/platform/macros.h"
#include "paddle/fluid/platform/place.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/common/backend.h"
#include "paddle/phi/core/compat/arg_map_context.h"
#include "paddle/phi/core/kernel_factory.h"
Expand Down
122 changes: 122 additions & 0 deletions paddle/fluid/framework/variable_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (c) 2023 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 "paddle/fluid/framework/variable_utils.h"

#include "paddle/fluid/framework/variable.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/kernel_factory.h"
#include "paddle/phi/core/tensor_utils.h"

namespace paddle {
namespace experimental {

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 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 paddle
32 changes: 32 additions & 0 deletions paddle/fluid/framework/variable_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个文件尝试合入到phi_utils.h里吧,感觉又多了一个utils文件有点重复了

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//
// 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/framework/variable.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/common/scalar.h"

namespace paddle {
namespace experimental {

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 experimental
} // namespace paddle
2 changes: 1 addition & 1 deletion paddle/fluid/imperative/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cc_test(
math_function
phi_tensor
phi_api
phi_api_utils)
variable_utils)
cc_test(
test_layer
SRCS test_layer.cc
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ if(WITH_UNITY_BUILD)
include(unity_build_rule.cmake)
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 variable_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
recurrent_op save_combine_op sparse_attention_op sync_batch_norm_op ${OP_MKL_DEPS} DEPS ${OP_HEADER_DEPS})
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/cast_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ limitations under the License. */

#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/platform/transform.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/kernels/cast_kernel.h"

namespace paddle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ limitations under the License. */
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/memory/malloc.h"
#include "paddle/fluid/operators/elementwise/elementwise_functor.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.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_grad.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License. */
#include "paddle/fluid/framework/tensor.h"

// only can include the headers in paddle/top/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/phi/kernels/funcs/broadcast_function.h"

namespace paddle {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ math_library(sampler DEPS generator)

# 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)
if(WITH_ASCEND_CL)
math_library(beam_search DEPS math_function beam_search_npu)
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/operators/math/sequence_padding.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ inline static size_t TotalSequenceLength(
return total_seq_len;
}

inline static void CheckDims(const framework::DDim& seq_tensor_dims,
const framework::DDim& pad_tensor_dims,
inline static void CheckDims(const phi::DDim& seq_tensor_dims,
const phi::DDim& pad_tensor_dims,
const phi::Vector<size_t>& seq_offset,
int64_t padded_seq_len,
int64_t step_width,
const PadLayout& layout) {
PADDLE_ENFORCE_EQ(
static_cast<size_t>(seq_tensor_dims[0]),
seq_offset.back(),
platform::errors::InvalidArgument(
phi::errors::InvalidArgument(
"Value of 1st dimension of the sequence tensor should be "
"equal to sum of lengths of all sequences. Expected %ld == %ld, but "
"got %ld != %ld. Please check the input value.",
Expand All @@ -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() == pad_tensor_dims.size(),
true,
platform::errors::InvalidArgument(
phi::errors::InvalidArgument(
"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, "
"expected the seq_tensor's rank is %ld or %ld, but got %ld. "
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/matmul_v2_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/complex_functors.h"

// only can include the headers in paddle/phi/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/phi/kernels/matmul_grad_kernel.h"
#include "paddle/phi/kernels/matmul_kernel.h"

Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/reduce_ops/reduce_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/math_function.h"
// only can include the headers in paddle/phi/api dirs
#include "paddle/fluid/framework/convert_utils.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/phi/kernels/cpu/reduce.h"

#if defined(__HIPCC__) || defined(__NVCC__) || defined(__xpu__)
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/operators/reshape_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License. */
#include "paddle/fluid/framework/phi_utils.h"

// only can include the headers in paddle/phi/api dirs
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/infermeta_utils.h"
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/operators/split_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License. */
#include <string>

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/phi/infermeta/unary.h"

namespace paddle {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/eager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ limitations under the License. */
#include "pybind11/pybind11.h"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/pybind/exception.h"
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/core/string_tensor.h"
namespace paddle {
namespace pybind {
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/pybind/eager_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef SSIZE_T ssize_t;
#include "paddle/fluid/framework/custom_operator.h"
#include "paddle/fluid/framework/op_meta_info_helper.h"
#include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/framework/variable_utils.h"
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
Expand All @@ -41,7 +42,6 @@ typedef SSIZE_T ssize_t;
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/ext/op_meta_info.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/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
Expand Down
Loading