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 0d Tensor in ConditionalBlockOp #49842

Merged
merged 13 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 3 additions & 1 deletion paddle/fluid/operators/controlflow/conditional_block_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,10 @@ class ConditionalBlockGradOp : public ConditionalOp {
if (!input_tensor.IsInitialized() || input_tensor.numel() == 0) {
return;
}
if (!input_tensor.meta().is_scalar) {
Copy link
Contributor

Choose a reason for hiding this comment

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

这个is_scalar可能用不上。这个判断0D方法用 input_tensor.dims().size()吧

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

outside_tensor->Resize(input_tensor.dims());
}
VLOG(4) << "Assigning zero to " << outside_tensor;
outside_tensor->Resize(input_tensor.dims());
outside_tensor->mutable_data(place, input_tensor.dtype());
const platform::DeviceContext *dev_ctx =
platform::DeviceContextPool::Instance().Get(place);
Expand Down
4 changes: 3 additions & 1 deletion paddle/phi/core/tensor_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ void Copy(const Context& dev_ctx,
VLOG(3) << "TensorCopy " << src.dims() << " from " << src.place() << " to "
<< dst_place;

dst->Resize(src.dims());
dst->set_meta(src.meta());
VLOG(3) << "src.numel() = " << src.numel()
<< ", dst->numel() = " << dst->numel();

void* dst_ptr = nullptr;
if (paddle::platform::is_cpu_place(dst_place)) {
Expand Down
7 changes: 7 additions & 0 deletions paddle/phi/kernels/cpu/add_n_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ void AddNKernel(const Context& dev_ctx,
const std::vector<const TensorBase*>& x,
DenseTensor* out) {
size_t in_num = x.size();
for (const TensorBase* tb : x) {
if (tb->initialized() && DenseTensor::classof(tb)) {
auto* dt = static_cast<const DenseTensor*>(tb);
out->set_meta(dt->meta());
break;
}
}
dev_ctx.template Alloc<T>(out);

bool in_place = false;
Expand Down
9 changes: 9 additions & 0 deletions paddle/phi/kernels/gpu/add_n_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ void AddNKernel(const Context &dev_ctx,
grids = dim3(CEIL_DIV(length, tile_size), 1, 1);
blocks = dim3(tile_size, 1, 1);
};

for (const TensorBase *tb : x) {
if (tb->initialized() && DenseTensor::classof(tb)) {
auto *dt = static_cast<const DenseTensor *>(tb);
out->set_meta(dt->meta());
break;
}
}

auto *out_ptr = dev_ctx.template Alloc<T>(out);
bool in_place = false;
if (x.size() > 0 && x[0]->initialized() && DenseTensor::classof(x[0])) {
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def _create_loss_op_desc_(loss):
{},
{"Out": [_append_grad_suffix_(loss.name)]},
{
"shape": [1],
"shape": list(loss.shape),
"value": 1.0,
"dtype": loss.dtype,
"force_cpu": False,
Expand Down
32 changes: 31 additions & 1 deletion python/paddle/fluid/tests/unittests/test_cond.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def false_func():
exe = fluid.Executor(place)
(ret,) = exe.run(main_program, fetch_list=[out.name])
np.testing.assert_allclose(np.asarray(ret), np.array(2), rtol=1e-05)
self.assertEqual(ret.shape, ())

def test_0d_tensor_as_cond(self):
"""
Expand All @@ -129,7 +130,7 @@ def false_func():
y = paddle.full(shape=[], dtype='float32', fill_value=0.23)
pred = paddle.greater_equal(y, x)
out = paddle.static.nn.cond(pred, true_func, false_func)
# out is one tensor
# out is a tensor

place = (
fluid.CUDAPlace(0)
Expand Down Expand Up @@ -168,14 +169,41 @@ def test_0d_tensor_backward(self):
if core.is_compiled_with_cuda()
else fluid.CPUPlace()
)

exe = fluid.Executor(place)
ret = exe.run(main_program, fetch_list=[out.name, a.grad_name])
np.testing.assert_allclose(
np.asarray(ret[0]), np.array(2.0), rtol=1e-05
)
self.assertEqual(ret[0].shape, ())
np.testing.assert_allclose(
np.asarray(ret[1]), np.array(-1.0), rtol=1e-05
)
self.assertEqual(ret[1].shape, ())

def test_0d_tensor_dygraph(self):
"""
pseudocode:

a = -2.0
if a >= 0:
return a
else:
return -a
"""
paddle.disable_static()
a = paddle.full(shape=[], dtype='float32', fill_value=-2.0)
a.stop_gradient = False
out = paddle.static.nn.cond(a >= 0, lambda: a, lambda: -a)
out.backward()

np.testing.assert_allclose(np.asarray(out), np.array(2.0), rtol=1e-05)
self.assertEqual(out.shape, [])

np.testing.assert_allclose(
np.asarray(a.grad), np.array(-1.0), rtol=1e-05
)
self.assertEqual(a.grad.shape, [])

def test_return_var_tuple(self):
"""
Expand Down Expand Up @@ -527,9 +555,11 @@ def greater_equal_branch(i, a):
np.testing.assert_allclose(
np.asarray(ret[0]), np.array(7.0), rtol=1e-05
)
self.assertEqual(ret[0].shape, ())
np.testing.assert_allclose(
np.asarray(ret[1]), np.array(2.0), rtol=1e-05
)
self.assertEqual(ret[1].shape, ())

def test_cond_op_in_condition(self):
paddle.enable_static()
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/static/nn/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ def false_func():
if _non_static_mode():
assert isinstance(pred, Variable), "The pred in cond must be Variable"
assert pred.size == 1, "condition input's numel should be 1"
pred = pred.numpy()[0]
pred = pred.numpy().item()
if pred:
if true_fn is not None:
if not callable(true_fn):
Expand Down