diff --git a/paddle/fluid/inference/api/helper.h b/paddle/fluid/inference/api/helper.h index b58c300c2ed03c02a6936502d356745b799eb4cc..cddb0c8daf97b2b8142fcc3b207be2c56a43988a 100644 --- a/paddle/fluid/inference/api/helper.h +++ b/paddle/fluid/inference/api/helper.h @@ -180,11 +180,29 @@ int VecReduceToInt(const std::vector &v) { return std::accumulate(v.begin(), v.end(), 1, [](T a, T b) { return a * b; }); } +template +void CheckAssignedData(const std::vector> &data, + const int num_elems) { + int num = 0; + for (auto it = data.begin(); it != data.end(); ++it) { + num += (*it).size(); + } + PADDLE_ENFORCE_EQ( + num, num_elems, + platform::errors::OutOfRange( + "The number of elements out of bounds. " + "Expected number of elements = %d. But received %d. Suggested Fix: " + "If the tensor is expected to assign %d elements, check the number " + "of elements of your 'infer_data'.", + num_elems, num, num_elems)); +} + template static void TensorAssignData(PaddleTensor *tensor, const std::vector> &data) { // Assign buffer int num_elems = VecReduceToInt(tensor->shape); + CheckAssignedData(data, num_elems); tensor->data.Resize(sizeof(T) * num_elems); int c = 0; for (const auto &f : data) {