Skip to content

support 0-Size Tensor #71640

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

Merged
merged 5 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all 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/accumulation/accumulation_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void CopyOrAddTensor(paddle::Tensor* tensor,
VLOG(3) << "Move Tensor ptr: " << t.impl();
*tensor = t;
} else {
if (!tensor->defined() || !tensor->initialized()) {
if (!tensor->defined() || !tensor->has_allocation()) {
// Simply copy tensor->impl
VLOG(3) << "Move Tensor ptr: " << t.impl();
*tensor = t;
Expand Down
26 changes: 15 additions & 11 deletions paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ Conv2dGradNodeFinal::operator()(

auto& grad_input = returns[0][0];
egr::AutogradMeta* grad_input_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_input)
: nullptr;
returns[0][0].has_allocation()
? egr::EagerUtils::autograd_meta(&grad_input)
: nullptr;
if (grad_input_autograd_meta)
grad_input_autograd_meta->SetStopGradient(false);
VLOG(3) << "Conv2dGradNodeFinal grad_input_autograd_meta: "
<< grad_input_autograd_meta;

auto& grad_filter = returns[1][0];
egr::AutogradMeta* grad_filter_autograd_meta =
returns[1][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_filter)
: nullptr;
returns[1][0].has_allocation()
? egr::EagerUtils::autograd_meta(&grad_filter)
: nullptr;
if (grad_filter_autograd_meta)
grad_filter_autograd_meta->SetStopGradient(false);
VLOG(3) << "Conv2dGradNodeFinal grad_filter_autograd_meta: "
Expand Down Expand Up @@ -268,14 +270,14 @@ Conv2dDoubleGradNodeFinal::operator()(
auto& grad_input_grad = hooked_grads[0][0];

paddle::optional<paddle::Tensor> grad_input_grad_optional;
if (grad_input_grad.initialized())
if (grad_input_grad.has_allocation())
grad_input_grad_optional =
paddle::make_optional<paddle::Tensor>(grad_input_grad);

auto& grad_filter_grad = hooked_grads[1][0];

paddle::optional<paddle::Tensor> grad_filter_grad_optional;
if (grad_filter_grad.initialized())
if (grad_filter_grad.has_allocation())
grad_filter_grad_optional =
paddle::make_optional<paddle::Tensor>(grad_filter_grad);

Expand Down Expand Up @@ -339,21 +341,23 @@ Conv2dDoubleGradNodeFinal::operator()(

auto& input_grad = returns[0][0];
egr::AutogradMeta* input_grad_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&input_grad)
: nullptr;
returns[0][0].has_allocation()
? egr::EagerUtils::autograd_meta(&input_grad)
: nullptr;
if (input_grad_autograd_meta)
input_grad_autograd_meta->SetStopGradient(false);

auto& filter_grad = returns[1][0];
egr::AutogradMeta* filter_grad_autograd_meta =
returns[1][0].initialized() ? egr::EagerUtils::autograd_meta(&filter_grad)
: nullptr;
returns[1][0].has_allocation()
? egr::EagerUtils::autograd_meta(&filter_grad)
: nullptr;
if (filter_grad_autograd_meta)
filter_grad_autograd_meta->SetStopGradient(false);

auto& grad_out_grad = returns[2][0];
egr::AutogradMeta* grad_out_grad_autograd_meta =
returns[2][0].initialized()
returns[2][0].has_allocation()
? egr::EagerUtils::autograd_meta(&grad_out_grad)
: nullptr;
if (grad_out_grad_autograd_meta)
Expand Down
32 changes: 16 additions & 16 deletions paddle/fluid/eager/api/manual/eager_manual/nodes/multiply_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ MultiplyGradNode::operator()(

auto& grad_x = returns[0][0];
egr::AutogradMeta* grad_x_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_x)
: nullptr;
returns[0][0].has_allocation() ? egr::EagerUtils::autograd_meta(&grad_x)
: nullptr;
if (grad_x_autograd_meta) grad_x_autograd_meta->SetStopGradient(false);

auto& grad_y = returns[1][0];
egr::AutogradMeta* grad_y_autograd_meta =
returns[1][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_y)
: nullptr;
returns[1][0].has_allocation() ? egr::EagerUtils::autograd_meta(&grad_y)
: nullptr;
if (grad_y_autograd_meta) grad_y_autograd_meta->SetStopGradient(false);

// Create Grad Node
Expand Down Expand Up @@ -299,14 +299,14 @@ MultiplyDoubleGradNode::operator()(
auto& fwd_grad_grad_x = hooked_grads[0][0];

paddle::optional<paddle::Tensor> fwd_grad_grad_x_optional;
if (fwd_grad_grad_x.initialized())
if (fwd_grad_grad_x.has_allocation())
fwd_grad_grad_x_optional =
paddle::make_optional<paddle::Tensor>(fwd_grad_grad_x);

auto& fwd_grad_grad_y = hooked_grads[1][0];

paddle::optional<paddle::Tensor> fwd_grad_grad_y_optional;
if (fwd_grad_grad_y.initialized())
if (fwd_grad_grad_y.has_allocation())
fwd_grad_grad_y_optional =
paddle::make_optional<paddle::Tensor>(fwd_grad_grad_y);

Expand Down Expand Up @@ -339,7 +339,7 @@ MultiplyDoubleGradNode::operator()(
// Inplace Check

bool can_be_inplaced = false;
if (fwd_grad_grad_x.initialized()) {
if (fwd_grad_grad_x.has_allocation()) {
VLOG(10) << fwd_grad_grad_x.name() << "(grad_x_grad) use_count: "
<< fwd_grad_grad_x.impl().use_count();
if (fwd_grad_grad_x.impl().use_count() == 1 ||
Expand Down Expand Up @@ -450,19 +450,19 @@ MultiplyDoubleGradNode::operator()(

auto& grad_x = returns[0][0];
egr::AutogradMeta* grad_x_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_x)
: nullptr;
returns[0][0].has_allocation() ? egr::EagerUtils::autograd_meta(&grad_x)
: nullptr;
if (grad_x_autograd_meta) grad_x_autograd_meta->SetStopGradient(false);

auto& grad_y = returns[1][0];
egr::AutogradMeta* grad_y_autograd_meta =
returns[1][0].initialized() ? egr::EagerUtils::autograd_meta(&grad_y)
: nullptr;
returns[1][0].has_allocation() ? egr::EagerUtils::autograd_meta(&grad_y)
: nullptr;
if (grad_y_autograd_meta) grad_y_autograd_meta->SetStopGradient(false);

auto& grad_grad_out = returns[2][0];
egr::AutogradMeta* grad_grad_out_autograd_meta =
returns[2][0].initialized()
returns[2][0].has_allocation()
? egr::EagerUtils::autograd_meta(&grad_grad_out)
: nullptr;
if (grad_grad_out_autograd_meta)
Expand Down Expand Up @@ -638,14 +638,14 @@ MultiplyGradNode::operator()(

auto& x_grad = returns[0][0];
egr::AutogradMeta* x_grad_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
returns[0][0].has_allocation() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
if (x_grad_autograd_meta) x_grad_autograd_meta->SetStopGradient(false);

auto& y_grad = returns[1][0];
egr::AutogradMeta* y_grad_autograd_meta =
returns[1][0].initialized() ? egr::EagerUtils::autograd_meta(&y_grad)
: nullptr;
returns[1][0].has_allocation() ? egr::EagerUtils::autograd_meta(&y_grad)
: nullptr;
if (y_grad_autograd_meta) y_grad_autograd_meta->SetStopGradient(false);

// Create Grad Node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,23 @@ SyncBatchNormGradNode::operator()(

auto& x_grad = returns[0][0];
egr::AutogradMeta* x_grad_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
returns[0][0].has_allocation() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
if (x_grad_autograd_meta) x_grad_autograd_meta->SetStopGradient(false);

auto& scale_grad = returns[3][0];
egr::AutogradMeta* scale_grad_autograd_meta =
returns[3][0].initialized() ? egr::EagerUtils::autograd_meta(&scale_grad)
: nullptr;
returns[3][0].has_allocation()
? egr::EagerUtils::autograd_meta(&scale_grad)
: nullptr;
if (scale_grad_autograd_meta)
scale_grad_autograd_meta->SetStopGradient(false);

auto& bias_grad = returns[4][0];
egr::AutogradMeta* bias_grad_autograd_meta =
returns[4][0].initialized() ? egr::EagerUtils::autograd_meta(&bias_grad)
: nullptr;
returns[4][0].has_allocation()
? egr::EagerUtils::autograd_meta(&bias_grad)
: nullptr;
if (bias_grad_autograd_meta) bias_grad_autograd_meta->SetStopGradient(false);

// Create Grad Node
Expand Down Expand Up @@ -409,21 +411,23 @@ SyncBatchNormGradNode::operator()(

auto& x_grad = returns[0][0];
egr::AutogradMeta* x_grad_autograd_meta =
returns[0][0].initialized() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
returns[0][0].has_allocation() ? egr::EagerUtils::autograd_meta(&x_grad)
: nullptr;
if (x_grad_autograd_meta) x_grad_autograd_meta->SetStopGradient(false);

auto& scale_grad = returns[3][0];
egr::AutogradMeta* scale_grad_autograd_meta =
returns[3][0].initialized() ? egr::EagerUtils::autograd_meta(&scale_grad)
: nullptr;
returns[3][0].has_allocation()
? egr::EagerUtils::autograd_meta(&scale_grad)
: nullptr;
if (scale_grad_autograd_meta)
scale_grad_autograd_meta->SetStopGradient(false);

auto& bias_grad = returns[4][0];
egr::AutogradMeta* bias_grad_autograd_meta =
returns[4][0].initialized() ? egr::EagerUtils::autograd_meta(&bias_grad)
: nullptr;
returns[4][0].has_allocation()
? egr::EagerUtils::autograd_meta(&bias_grad)
: nullptr;
if (bias_grad_autograd_meta) bias_grad_autograd_meta->SetStopGradient(false);

// Create Grad Node
Expand Down
Loading