From 0e28c8bb00a782ca876878037dd059aa47df6308 Mon Sep 17 00:00:00 2001 From: zhulei <563755780@qq.com> Date: Thu, 12 Aug 2021 11:52:59 +0800 Subject: [PATCH] Fix safety-bug of functional.linear (#34696) * Fix safety-bug of functional.linear * Fix safety-bug of functional.linear * Fix safety-bug of functional.linear * Fix safety-bug of functional.linear --- paddle/fluid/operators/math/blas_impl.h | 6 ++++++ python/paddle/fluid/tests/unittests/test_linear.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/paddle/fluid/operators/math/blas_impl.h b/paddle/fluid/operators/math/blas_impl.h index 55151c5483a..cb4044b1b08 100644 --- a/paddle/fluid/operators/math/blas_impl.h +++ b/paddle/fluid/operators/math/blas_impl.h @@ -1041,6 +1041,12 @@ void Blas::BatchedGEMM( CBLAS_TRANSPOSE transA, CBLAS_TRANSPOSE transB, int M, int N, int K, T alpha, const T *A, const T *B, T beta, T *C, int batchCount, int64_t strideA, int64_t strideB) const { + PADDLE_ENFORCE_NOT_NULL( + A, platform::errors::InvalidArgument("Pointer A should not be null.")); + PADDLE_ENFORCE_NOT_NULL( + B, platform::errors::InvalidArgument("Pointer B should not be null.")); + PADDLE_ENFORCE_NOT_NULL( + C, platform::errors::InvalidArgument("Pointer C should not be null.")); #ifdef PADDLE_WITH_MKLML int lda = (transA == CblasNoTrans) ? K : M; int ldb = (transB == CblasNoTrans) ? N : K; diff --git a/python/paddle/fluid/tests/unittests/test_linear.py b/python/paddle/fluid/tests/unittests/test_linear.py index 9d07a80da15..2f722fc8005 100644 --- a/python/paddle/fluid/tests/unittests/test_linear.py +++ b/python/paddle/fluid/tests/unittests/test_linear.py @@ -73,6 +73,15 @@ class LinearTestCase(unittest.TestCase): np.testing.assert_array_almost_equal(res_f, res_nn) np.testing.assert_array_almost_equal(res_nn, res_np) + def test_error_dummy_input(self, place=paddle.CPUPlace()): + with self.assertRaises(ValueError): + x_arr = np.array([], dtype=np.float32) + x = paddle.to_tensor( + np.reshape(x_arr, (0, 4, 4, 4)), dtype='float32') + weight = paddle.zeros([4, 4, 4], dtype='float32') + bias = paddle.to_tensor([], dtype='float32') + paddle.nn.functional.linear(x, weight, bias=bias) + if __name__ == "__main__": unittest.main() -- GitLab