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

Support 0-size tensor in parameter creating, forward and backward #70504

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
02aa39d
support 0-size tensor forward and backward under simple operations pr…
HydrogenSulfate Dec 27, 2024
a18f2cc
Merge branch 'develop' into support_0-size_fwd_bwd
HydrogenSulfate Dec 27, 2024
288b863
fix error message format
HydrogenSulfate Dec 31, 2024
665698a
support initializing for 0-size tensor
HydrogenSulfate Dec 31, 2024
a157537
add TestZeroSizeParameter
HydrogenSulfate Dec 31, 2024
130251d
update forward, backward with 0-size tensor in eager/static mode
HydrogenSulfate Dec 31, 2024
7b396b4
Merge branch 'develop' into support_0-size_fwd_bwd
HydrogenSulfate Dec 31, 2024
2442cde
TestZeroSizeBackward skip XPU for place issue
HydrogenSulfate Jan 2, 2025
fc4aff3
Merge branch 'develop' into support_0-size_fwd_bwd
HydrogenSulfate Jan 2, 2025
c9267a9
[PIR] Insert backward ops into insertion point
SigureMo Jan 2, 2025
b0a87da
fix 2 ut
SigureMo Jan 2, 2025
68f0af9
Merge branch 'PaddlePaddle:develop' into support_0-size_fwd_bwd
HydrogenSulfate Jan 3, 2025
49f235d
Merge branch '70595' into support_0-size_fwd_bwd
HydrogenSulfate Jan 3, 2025
7614038
fix add_n infermeta when meeting 0-size and update UT
HydrogenSulfate Jan 3, 2025
16744ee
Merge branch 'develop' into support_0-size_fwd_bwd
HydrogenSulfate Jan 3, 2025
2d7cbf1
remove warnings
HydrogenSulfate Jan 3, 2025
5f71752
remove print
HydrogenSulfate Jan 3, 2025
89f3195
Merge branch 'PaddlePaddle:develop' into support_0-size_fwd_bwd
HydrogenSulfate Jan 3, 2025
e026da1
remove unnecessary code
HydrogenSulfate Jan 5, 2025
d416e30
Merge branch 'support_0-size_fwd_bwd' of https://github.com/HydrogenS…
HydrogenSulfate Jan 5, 2025
942f58b
remove condition of define() for paddle::Tensor
HydrogenSulfate Jan 6, 2025
da2ede8
Merge branch 'develop' into support_0-size_fwd_bwd
HydrogenSulfate Jan 6, 2025
aca7ba3
fix
HydrogenSulfate Jan 6, 2025
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
8 changes: 6 additions & 2 deletions paddle/fluid/eager/accumulation/accumulation_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ static void CopyOrAddTensor(paddle::Tensor* tensor,
*reinterpret_cast<phi::DenseTensor*>(tensor->impl().get()),
*reinterpret_cast<phi::DenseTensor*>(t.impl().get()),
reinterpret_cast<phi::DenseTensor*>(tensor->impl().get()));
} else {
} else if (t.initialized() && tensor->initialized()) {
paddle::imperative::TensorAdd<paddle::Tensor>(t, tensor);
} else {
PADDLE_THROW(common::errors::PreconditionNotMet(
"DenseTensor 't' and 'tensor' should be "
"both initialized when accumulating gradient."));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

这里建议保留一个抛出异常的else分支

} else {
// TODO(jiabin): Support Other TensorBase later
Expand Down Expand Up @@ -179,7 +183,7 @@ GradNodeAccumulation::operator()(
if (!weak_grad_.expired() && !is_new_grad) {
auto grad = weak_grad_.lock();
if (grad_out.defined() &&
(grad_out.is_dist_tensor() || grad_out.initialized())) {
(grad_out.is_dist_tensor() || grad_out.has_allocation())) {
CopyOrAddTensor(grad.get(), grad_out, is_fake_empty_);
}
// else { do nothing since there is no valid value in grad out tensor }
Expand Down
6 changes: 3 additions & 3 deletions paddle/fluid/eager/auto_code_generator/generator/eager_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ class {} : public egr::GradNodeBase {{
"""
CREATE_PLAIN_OPTIONAL_TENSOR_TEMPLATE = """
paddle::optional<paddle::Tensor> {name}_optional;
if({name}.initialized() ||
if(({name}.has_allocation()) ||
({name}.defined() && {name}.is_dist_tensor() &&
phi::distributed::NeedComputationClipForPP({name}.impl()))) {name}_optional = paddle::make_optional<paddle::Tensor>({name});
"""
Expand Down Expand Up @@ -731,7 +731,7 @@ class {} : public egr::GradNodeBase {{

CHECK_BACKWARD_INPLACE_TEMPLATE = """
bool can_be_inplaced = false;
if ({}.initialized()) {{
if ({}.has_allocation()) {{
VLOG(10) << {}.name() << "({}) use_count: " << {}.impl().use_count();
if ({}.impl().use_count() == 1 || ({}.impl().use_count() == 2 && {}.impl().get() == {}.impl().get())) {{
if ({}.is_dense_tensor() && !std::dynamic_pointer_cast<phi::DenseTensor>({}.impl())->meta().is_contiguous()) {{
Expand Down Expand Up @@ -2949,7 +2949,7 @@ def _gen_api_call_code_block(
if IsPlainTensorType(rtype):
output_autograd_meta = f"""
auto& {transformed_tensor_name} = returns[{pos}][0];
egr::AutogradMeta* {output_autograd_meta_name} = returns[{pos}][0].initialized() ? egr::EagerUtils::autograd_meta(&{transformed_tensor_name}) : nullptr;
egr::AutogradMeta* {output_autograd_meta_name} = returns[{pos}][0].has_allocation() ? egr::EagerUtils::autograd_meta(&{transformed_tensor_name}) : nullptr;
if ({output_autograd_meta_name}) {output_autograd_meta_name}->SetStopGradient(false);
"""

Expand Down
8 changes: 5 additions & 3 deletions paddle/fluid/eager/backward.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ std::vector<paddle::Tensor> RunBackward(

// copy grad tensor since we should totally run grad without affect forward
// value
if (!grad_tensors.empty() && grad_tensors[i].initialized()) {
if (!grad_tensors.empty() &&
(grad_tensors[i].defined() && grad_tensors[i].has_allocation())) {
PADDLE_ENFORCE(
grad_tensors.size() == tensors.size(),
common::errors::Fatal(
Expand Down Expand Up @@ -354,9 +355,10 @@ std::vector<paddle::Tensor> RunBackward(
paddle::Tensor& grad_output_tensor = grad_output_tensors[i][j];

if ((!grad_output_tensor.defined() ||
!grad_output_tensor.initialized())) {
!grad_output_tensor.has_allocation())) {
VLOG(7) << "We get grad_output_tensor with slot: " << i
<< ", rank: " << j << " as uninitialized or undefined tensor";
<< ", rank: " << j
<< " as undefined tensor or without allocation.";
}

VLOG(7) << "Get Edge and grad_output_tensor with slot: " << i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ void run_custom_op_impl(const paddle::OpMetaInfo& op_info,
std::vector<Tensor>* all_inputs = ctx.AllMutableInput();
for (size_t i = 0; i < all_inputs->size(); ++i) {
auto& tensor = all_inputs->at(i);
if (tensor.initialized() && tensor.is_dense_tensor() &&
if (tensor.has_allocation() && tensor.is_dense_tensor() &&
!std::dynamic_pointer_cast<phi::DenseTensor>(tensor.impl())
->meta()
.is_contiguous()) {
Expand Down
12 changes: 6 additions & 6 deletions paddle/fluid/eager/grad_node_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
namespace egr {

static void CheckTensor(const paddle::Tensor& pre, const paddle::Tensor& post) {
if (!pre.initialized() && post.initialized()) {
if (!pre.has_allocation() && post.has_allocation()) {
PADDLE_THROW(common::errors::PermissionDenied(
"The tensor in before and after hook are not consistent"));
}
if (pre.initialized() && post.initialized()) {
if (pre.has_allocation() && post.has_allocation()) {
VLOG(7) << phi::DataTypeToString(pre.dtype()) << " "
<< phi::DataTypeToString(post.dtype());
PADDLE_ENFORCE_EQ(
Expand Down Expand Up @@ -105,7 +105,7 @@ void GradNodeBase::SetGradInMeta(const paddle::Tensor& fwd_out,
meta.SetStopGradient(fwd_out_meta->StopGradient());
}

if (!fwd_out.initialized()) {
if (!fwd_out.has_allocation()) {
if (fwd_out.defined() && fwd_out.is_dist_tensor() &&
phi::distributed::NeedComputationClipForPP(fwd_out.impl())) {
VLOG(3) << "Tensor " << fwd_out.name() << " is DistTensor,"
Expand Down Expand Up @@ -195,7 +195,7 @@ void GradNodeBase::SetGradInMeta(const std::vector<paddle::Tensor>& fwd_out,
meta.SetStopGradient(fwd_out_meta->StopGradient());
}

if (!fwd_out_tensor.initialized()) {
if (!fwd_out_tensor.has_allocation()) {
if (fwd_out_tensor.defined() && fwd_out_tensor.is_dist_tensor() &&
phi::distributed::NeedComputationClipForPP(fwd_out_tensor.impl())) {
VLOG(3) << "Tensor " << fwd_out_tensor.name() << " is DistTensor,"
Expand Down Expand Up @@ -295,7 +295,7 @@ void GradNodeBase::SetGradInMeta(const std::vector<paddle::Tensor*>& fwd_out,
meta.SetStopGradient(fwd_out_meta->StopGradient());
}

if (!fwd_out_tensor.initialized()) {
if (!fwd_out_tensor.has_allocation()) {
if (fwd_out_tensor.defined() && fwd_out_tensor.is_dist_tensor() &&
phi::distributed::NeedComputationClipForPP(fwd_out_tensor.impl())) {
VLOG(3) << "Tensor " << fwd_out_tensor.name() << " is DistTensor,"
Expand Down Expand Up @@ -762,7 +762,7 @@ GradNodeBase::ApplyGradientHooks(
std::vector<paddle::Tensor>& slot_out = outs[slot_id];
slot_out.resize(tensors[slot_id].size());
paddle::Tensor& out = slot_out[rank];
if (!out.defined() || !out.initialized()) {
if (!out.defined() || !out.has_allocation()) {
out = (*hook)(tensors[slot_id][rank]);
} else {
// If more than one hook is registered, the input to the next hook func
Expand Down
21 changes: 11 additions & 10 deletions paddle/fluid/eager/grad_tensor_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void GradTensorHolder::CopyValueFromTensor(size_t slot_id,
rank));
if (!fill_one) {
paddle::Tensor& buffer_tensor = buffer_[slot_id][rank];
if ((!buffer_tensor.defined() || !buffer_tensor.initialized())) {
if ((!buffer_tensor.defined() || !buffer_tensor.has_allocation())) {
// Perform deep copy here
buffer_tensor.copy_(t, t.place(), false);
auto* meta = egr::EagerUtils::autograd_meta(&buffer_tensor);
Expand Down Expand Up @@ -112,7 +112,7 @@ void GradTensorHolder::add(size_t slot_id,
size_t rank,
const paddle::Tensor& t,
bool create_graph) {
if (!t.initialized()) {
if (!t.has_allocation()) {
if (t.defined() && t.is_dist_tensor() &&
phi::distributed::NeedComputationClipForPP(t.impl())) {
// Pipeline parallel still needs to construct GradNode graph
Expand Down Expand Up @@ -152,7 +152,7 @@ void GradTensorHolder::add(size_t slot_id,
// related code later.
// This if statement is trying to test neither phi::Tensor nor
// framework::Variable is initialized.
if ((!buffer_tensor.defined() || !buffer_tensor.initialized())) {
if ((!buffer_tensor.defined() || !buffer_tensor.has_allocation())) {
// Simply copy tensor->impl
VLOG(6) << "Move Tensor for buffer_ slot: " << slot_id
<< ", size: " << buffer_[slot_id].size();
Expand All @@ -161,13 +161,14 @@ void GradTensorHolder::add(size_t slot_id,
VLOG(6) << "Add Tensor for buffer_ slot: " << slot_id
<< ", size: " << buffer_[slot_id].size();
// Accumulation
PADDLE_ENFORCE_EQ(t.initialized(),
true,
common::errors::Fatal(
"We can only accumulate initialized tensor, but we "
"got tensor: %s is empty please check you network "
"and make sure it creates grads.",
t.name()));
PADDLE_ENFORCE_EQ(
t.has_allocation(),
true,
common::errors::Fatal(
"We can only accumulate tensor having allocation, but we "
"got tensor: %s without allocation, please check you network "
"and make sure it creates grads.",
t.name()));

if (t.is_dense_tensor()) {
if (buffer_tensor.is_dense_tensor()) {
Expand Down
8 changes: 4 additions & 4 deletions paddle/fluid/eager/tensor_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class TensorWrapper {
bool no_need_buffer = false) {
// set inplace_version_snapshot_ according to tensor's current inplace
// version.
if (tensor.initialized() && tensor.is_dense_tensor()) {
if (tensor.has_allocation() && tensor.is_dense_tensor()) {
phi::DenseTensor* dense_tensor =
static_cast<phi::DenseTensor*>(tensor.impl().get());
auto& inplace_version_counter = dense_tensor->InplaceVersionCounter();
inplace_version_snapshot_ = inplace_version_counter.CurrentVersion();
} else if (tensor.initialized() && tensor.is_dist_tensor()) {
} else if (tensor.has_allocation() && tensor.is_dist_tensor()) {
phi::DenseTensor* dense_tensor =
static_cast<phi::distributed::DistTensor*>(tensor.impl().get())
->unsafe_mutable_value();
Expand Down Expand Up @@ -92,7 +92,7 @@ class TensorWrapper {
} else {
#ifndef PADDLE_NO_PYTHON
if (egr::SavedTensorsHooks::GetInstance().IsEnable() &&
tensor.is_dense_tensor() && tensor.initialized()) {
tensor.has_allocation() && tensor.is_dense_tensor()) {
phi::DenseTensor* dense_tensor =
static_cast<phi::DenseTensor*>(tensor.impl().get());
intermediate_tensor_.set_impl(std::make_shared<phi::DenseTensor>(
Expand All @@ -102,7 +102,7 @@ class TensorWrapper {
unpack_hook_ = egr::SavedTensorsHooks::GetInstance().GetUnPackHook();
packed_value_ = (*pack_hook)(tensor);
} else if (egr::SavedTensorsHooks::GetInstance().IsEnable() &&
tensor.is_dist_tensor() && tensor.initialized()) {
tensor.initialized() && tensor.is_dist_tensor()) {
intermediate_tensor_.set_impl(
std::make_shared<phi::distributed::DistTensor>(
tensor.dims(),
Expand Down
24 changes: 12 additions & 12 deletions paddle/fluid/eager/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ void EagerUtils::HandleViewBetweenInputAndOutput(
void EagerUtils::HandleViewBetweenInputAndOutput(
const paddle::Tensor& input_tensor, paddle::Tensor* view_output_tensor) {
PADDLE_ENFORCE_EQ(
input_tensor.initialized(),
input_tensor.has_allocation(),
true,
common::errors::InvalidArgument("Tensor %s has not been initialized!",
input_tensor.name()));
Expand Down Expand Up @@ -814,7 +814,7 @@ std::string EagerUtils::TensorStr(const paddle::Tensor& t) {
dist_t->dist_attr());
}
} else {
if (t.initialized()) {
if (t.has_allocation()) {
tensor_info_str += paddle::string::Sprintf(TENSOR_INFO_TEMPLATE,
t.impl()->type_info().name(),
t.dtype(),
Expand Down Expand Up @@ -847,37 +847,37 @@ std::string EagerUtils::TensorStr(const paddle::Tensor& t) {
GradNodeStr(t),
ad_meta->StopGradient());
auto* data_ptr = dynamic_cast<phi::DenseTensor*>(t.impl().get());
if (t.initialized() && data_ptr) {
if (t.has_allocation() && data_ptr) {
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
*data_ptr,
ad_info_str);
} else {
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
"None",
ad_info_str);
}
} else {
auto* data_ptr = dynamic_cast<phi::DenseTensor*>(t.impl().get());
if (t.initialized() && data_ptr) {
if (t.has_allocation() && data_ptr) {
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
*data_ptr,
"None");
} else {
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
"None",
Expand All @@ -899,14 +899,14 @@ std::string EagerUtils::TensorStr(const paddle::Tensor& t) {
ad_meta->StopGradient());
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
ad_info_str);
} else {
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str,
"None");
Expand All @@ -917,14 +917,14 @@ std::string EagerUtils::TensorStr(const paddle::Tensor& t) {
"TensorInfo: [ %s ]}";
return paddle::string::Sprintf(TENSOR_PRINT_TEMPLATE,
tensor_name_str,
t.initialized(),
t.has_allocation(),
t.impl(),
tensor_info_str);
} else if (VLOG_IS_ON(4)) {
const char* TENSOR_PRINT_TEMPLATE =
"{ Name: %s, Initialized: %d, Ptr: %d }";
return paddle::string::Sprintf(
TENSOR_PRINT_TEMPLATE, tensor_name_str, t.initialized(), t.impl());
TENSOR_PRINT_TEMPLATE, tensor_name_str, t.has_allocation(), t.impl());
} else {
return "[ Not specified tensor log level ]";
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/framework/tensor_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ TEST_API std::ostream& operator<<(std::ostream& os, const phi::DenseTensor& t) {
os << " - shape: [" << t.dims() << "]\n";
os << " - layout: " << common::DataLayoutToString(t.layout()) << "\n";

if (!t.initialized()) {
if (!t.has_allocation()) {
os << "uninited\n";
return os;
}
Expand Down
2 changes: 1 addition & 1 deletion paddle/fluid/imperative/gradient_accumulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const TType& GetInnerTensor(const framework::Variable& src) {
template <typename TType>
TType& GetInnerTensor(const paddle::Tensor& src) {
PADDLE_ENFORCE_EQ(
src.initialized(),
(src.has_allocation()),
true,
common::errors::Fatal("We only add tensor with value if a tensor is "
"NOT INITIALIZED, it should just move instead of "
Expand Down
4 changes: 2 additions & 2 deletions paddle/fluid/pybind/eager_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ PyObject* eager_api_get_grads_types(PyObject* self,
}

auto& grad = meta->Grad();
if (meta && grad.initialized()) {
if (meta && grad.has_allocation()) {
if ((grad.is_dense_tensor() || grad.is_dist_tensor()) &&
(tensor.dtype() == phi::DataType::FLOAT32 ||
tensor.dtype() == phi::DataType::FLOAT16 ||
Expand Down Expand Up @@ -690,7 +690,7 @@ PyObject* eager_api_run_custom_op(PyObject* self,
const auto& input_tensor = ctx.InputAt(input_range.first);
// inplace optional [Tensor or vector<Tensor>], un-initialized tensor.
if (paddle::framework::detail::IsOptionalVar(output) &&
!input_tensor.initialized()) {
!input_tensor.has_allocation()) {
VLOG(7) << "Custom operator add output " << output
<< " to CustomOpKernelContext. Add un-initialized tensor "
"because the inplace optional input is None";
Expand Down
9 changes: 5 additions & 4 deletions paddle/fluid/pybind/eager_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1600,22 +1600,23 @@ static std::pair<std::string, std::string> GenerateForwardFunctionContents(
LegalizeVarName(input_name));
} else {
const char* FWD_INS_CONTENT_TEMPLATE =
" if(%s.initialized()) "
" if(%s.has_allocation()) "
"ins[\"%s\"] = egr::EagerUtils::TrySyncToVars(%s);\n";
dispensable_ins_contents_str +=
paddle::string::Sprintf(FWD_INS_CONTENT_TEMPLATE,
LegalizeVarName(input_name),
input_name,
LegalizeVarName(input_name));
const char* FWD_AMP_TENSORS_VECTOR_TEMPLATE =
" if(%s.initialized()) "
" if(%s.has_allocation()) "
"amp_tensors_vector.push_back({ %s });\n";
dispensable_amp_tensors_vector_str +=
paddle::string::Sprintf(FWD_AMP_TENSORS_VECTOR_TEMPLATE,
LegalizeVarName(input_name),
LegalizeVarName(input_name));
const char* DISPENSABLE_AMP_AUTO_CAST_TEMPLATE =
" auto NEW_%s = ((%s.initialized()) ? egr::AmpAutoCast(\"%s\", "
" auto NEW_%s = ((%s.has_allocation()) ? "
"egr::AmpAutoCast(\"%s\", "
"%s, amp_dst_dtype, \"%s\") : %s);\n";
dispensable_amp_auto_cast_str +=
paddle::string::Sprintf(DISPENSABLE_AMP_AUTO_CAST_TEMPLATE,
Expand Down Expand Up @@ -2189,7 +2190,7 @@ static std::string GenerateSingleOpBase(
" // Check backward inplace info\n"
" bool %s = false;\n"
" %s\n"
" if (%s.initialized()) {\n"
" if (%s.has_allocation()) {\n"
" VLOG(10) << %s.name() << \"(%s) use_count: \" << "
"%s.impl().use_count();\n"
" if (%s.impl().use_count() == 1 || (%s.impl().use_count() == 2 && "
Expand Down
Loading
Loading