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

Add value check & error message for gather_tree #47051

Merged
merged 6 commits into from
Oct 18, 2022
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
10 changes: 10 additions & 0 deletions paddle/phi/kernels/cpu/gather_tree_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "paddle/phi/kernels/gather_tree_kernel.h"

#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {
Expand Down Expand Up @@ -49,6 +50,15 @@ void GatherTreeKernel(const Context &dev_ctx,
out_data[idx] = ids_data[idx];
auto parent = parents_data[idx];
for (int step = max_length - 2; step >= 0; step--) {
PADDLE_ENFORCE_LT(
parent,
beam_size,
phi::errors::InvalidArgument(
"The parents must be less than beam size, but recieved"
"parents %d is greater than or equal to beam size %d. ",
parent,
beam_size));

idx = step * batch_size * beam_size + batch * beam_size;
out_data[idx + beam] = ids_data[idx + parent];
parent = parents_data[idx + parent];
Expand Down
7 changes: 7 additions & 0 deletions paddle/phi/kernels/gpu/gather_tree_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>

#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"

namespace phi {
Expand All @@ -35,6 +36,12 @@ __global__ void GatherTree(const T *ids_data,
out_data[idx] = ids_data[idx];
auto parent = parents_data[idx];
for (int step = max_length - 2; step >= 0; step--) {
PADDLE_ENFORCE((parent < beam_size),
"The parents must be less than beam size, but recieved"
"parents %ld is greater than or equal to beam size %ld. ",
parent,
beam_size);

idx = step * batch_size * beam_size + batch * beam_size;
out_data[idx + beam] = ids_data[idx + parent];
parent = parents_data[idx + parent];
Expand Down
19 changes: 19 additions & 0 deletions python/paddle/fluid/tests/unittests/test_gather_tree_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ def test_type_parents():
fluid.layers.gather_tree(ids, bad_parents)

self.assertRaises(TypeError, test_type_parents)

def test_ids_ndim():
bad_ids = fluid.layers.data(name='bad_test_ids',
shape=[5, 2],
dtype='int64',
append_batch_size=False)
paddle.nn.functional.gather_tree(bad_ids, parents)

self.assertRaises(ValueError, test_ids_ndim)

def test_parents_ndim():
bad_parents = fluid.layers.data(name='bad_test_parents',
shape=[5, 2],
dtype='int64',
append_batch_size=False)
paddle.nn.functional.gather_tree(ids, bad_parents)

self.assertRaises(ValueError, test_parents_ndim)

paddle.disable_static()


Expand Down
7 changes: 7 additions & 0 deletions python/paddle/nn/functional/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ def gather_tree(ids, parents):
# [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]]

"""
if ids.ndim != 3:
raise ValueError(
"The input ids must be a 3D tensor with shape [length, batch_size, beam_size]"
)
if ids.ndim != parents.ndim:
raise ValueError("The ids's shape must be the same as parents' shape. ")

if in_dygraph_mode():
return _C_ops.gather_tree(ids, parents)
else:
Expand Down