未验证 提交 137d6563 编写于 作者: Z Zhang Ting 提交者: GitHub

add check for assigned data, test=develop (#22960)

上级 f0d193a2
...@@ -180,11 +180,29 @@ int VecReduceToInt(const std::vector<T> &v) { ...@@ -180,11 +180,29 @@ int VecReduceToInt(const std::vector<T> &v) {
return std::accumulate(v.begin(), v.end(), 1, [](T a, T b) { return a * b; }); return std::accumulate(v.begin(), v.end(), 1, [](T a, T b) { return a * b; });
} }
template <typename T>
void CheckAssignedData(const std::vector<std::vector<T>> &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 <typename T> template <typename T>
static void TensorAssignData(PaddleTensor *tensor, static void TensorAssignData(PaddleTensor *tensor,
const std::vector<std::vector<T>> &data) { const std::vector<std::vector<T>> &data) {
// Assign buffer // Assign buffer
int num_elems = VecReduceToInt(tensor->shape); int num_elems = VecReduceToInt(tensor->shape);
CheckAssignedData(data, num_elems);
tensor->data.Resize(sizeof(T) * num_elems); tensor->data.Resize(sizeof(T) * num_elems);
int c = 0; int c = 0;
for (const auto &f : data) { for (const auto &f : data) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册