Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostML committed Oct 17, 2022
1 parent 166ff39 commit ee6e6d5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions paddle/phi/kernels/cpu/gather_tree_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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,
paddle::platform::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
1 change: 1 addition & 0 deletions paddle/phi/kernels/gather_tree_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include "paddle/fluid/platform/enforce.h"
#include "paddle/phi/core/dense_tensor.h"

namespace phi {
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/kernels/gpu/gather_tree_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ __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--) {
assert(parent < beam_size &&
"The parents of gather_tree op must be less than 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 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

0 comments on commit ee6e6d5

Please sign in to comment.