Paddle GCC12: mismatched-new-delete error in custom_device.cc

doinxwow  于 2022-12-31  发布在  其他
关注(0)|答案(4)|浏览(215)

bug描述 Describe the Bug

When compiling PaddlePaddle with GCC 12.1 several mismatched-new-delete errors arise in custom_device.cc of the Phi backend:

n file included from /usr/include/c++/12.1.0/memory:76,
                 from ./Paddle/paddle/fluid/platform/enforce.h:52,
                 from ./Paddle/paddle/fluid/platform/device/custom/enforce_custom.h:19,
                 from ./Paddle/paddle/phi/backends/custom/custom_device.cc:15:
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:354:7:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:351:54: note: returned from 'void* operator new [](std::size_t)'
  351 |         std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                      ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:354:7:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:351:54: note: returned from 'void* operator new [](std::size_t)'
  351 |         std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                      ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:446:5:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:443:52: note: returned from 'void* operator new [](std::size_t)'
  443 |       std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                    ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:446:5:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:443:52: note: returned from 'void* operator new [](std::size_t)'
  443 |       std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                    ^
cc1plus: all warnings being treated as errors
make[2]: *** [paddle/phi/backends/custom/CMakeFiles/custom_device.dir/build.make:76: paddle/phi/backends/custom/CMakeFiles/custom_device.dir/custom_device.cc.o] Error 1

其他补充信息 Additional Supplementary Information

check out git master;

cmake
make
vh0rcniy

vh0rcniy1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看 官网API文档常见问题历史IssueAI社区 来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

eoxn13cs

eoxn13cs2#

Please try compiling with the patch below

diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc
index df757b2..6c74ebb 100644
--- a/paddle/phi/backends/custom/custom_device.cc
+++ b/paddle/phi/backends/custom/custom_device.cc
@@ -348,7 +348,7 @@ class CustomDevice : public DeviceInterface {
       }
     } else {
       if (!pimpl_->memory_copy_p2p) {
-        std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
+        std::unique_ptr<uint8_t> tmp(reinterpret_cast<uint8_t*>(new uint8_t[size]));
         MemoryCopyD2H(src_dev_id, tmp.get(), src, size);
         MemoryCopyH2D(dst_dev_id, dst, tmp.get(), size);
       } else {
@@ -440,7 +440,7 @@ class CustomDevice : public DeviceInterface {
       PADDLE_ENFORCE_CUSTOM_DEVICE_SUCCESS(
           pimpl_->device_memory_set(device, ptr, value, size));
     } else {
-      std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
+      std::unique_ptr<uint8_t> tmp(reinterpret_cast<uint8_t*>(new uint8_t[size]));
       memset(tmp.get(), value, size);
       MemoryCopyH2D(dev_id, ptr, tmp.get(), size);
     }
fcy6dtqo

fcy6dtqo3#

bug描述 Describe the Bug

When compiling PaddlePaddle with GCC 12.1 several mismatched-new-delete errors arise in custom_device.cc of the Phi backend:

n file included from /usr/include/c++/12.1.0/memory:76,
                 from ./Paddle/paddle/fluid/platform/enforce.h:52,
                 from ./Paddle/paddle/fluid/platform/device/custom/enforce_custom.h:19,
                 from ./Paddle/paddle/phi/backends/custom/custom_device.cc:15:
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:354:7:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:351:54: note: returned from 'void* operator new [](std::size_t)'
  351 |         std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                      ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:354:7:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemoryCopyP2P(const phi::Place&, void*, size_t, const void*, size_t, const phi::stream::Stream*)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:351:54: note: returned from 'void* operator new [](std::size_t)'
  351 |         std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                      ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:446:5:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:443:52: note: returned from 'void* operator new [](std::size_t)'
  443 |       std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                    ^
In member function 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = unsigned char]',
    inlined from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = unsigned char; _Dp = std::default_delete<unsigned char>]' at /usr/include/c++/12.1.0/bits/unique_ptr.h:396:17,
    inlined from 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)' at ./Paddle/paddle/phi/backends/custom/custom_device.cc:446:5:
/usr/include/c++/12.1.0/bits/unique_ptr.h:95:9: error: 'void operator delete(void*, std::size_t)' called on pointer returned from a mismatched allocation function [-Werror=mismatched-new-delete]
   95 |         delete __ptr;
      |         ^~~~~~~~~~~~
./Paddle/paddle/phi/backends/custom/custom_device.cc: In member function 'virtual void phi::CustomDevice::MemorySet(size_t, void*, uint8_t, size_t)':
./Paddle/paddle/phi/backends/custom/custom_device.cc:443:52: note: returned from 'void* operator new [](std::size_t)'
  443 |       std::unique_ptr<uint8_t> tmp(new uint8_t[size]);
      |                                                    ^
cc1plus: all warnings being treated as errors
make[2]: *** [paddle/phi/backends/custom/CMakeFiles/custom_device.dir/build.make:76: paddle/phi/backends/custom/CMakeFiles/custom_device.dir/custom_device.cc.o] Error 1

其他补充信息 Additional Supplementary Information

check out git master;

cmake
make

The correct fix for this problem is shown below.

diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc
index 928101b09f..d7397353ef 100644
--- a/paddle/phi/backends/custom/custom_device.cc
+++ b/paddle/phi/backends/custom/custom_device.cc
@@ -358,8 +358,7 @@ class CustomDevice : public DeviceInterface {
       }
     } else {
       if (!pimpl_->memory_copy_p2p) {
-        std::unique_ptr<uint8_t> tmp(
-            reinterpret_cast<uint8_t*>(new uint8_t[size]));
+        std::unique_ptr<uint8_t[]> tmp(new uint8_t[size]);
         MemoryCopyD2H(src_dev_id, tmp.get(), src, size);
         MemoryCopyH2D(dst_dev_id, dst, tmp.get(), size);
       } else {
@@ -451,8 +450,7 @@ class CustomDevice : public DeviceInterface {
       PADDLE_ENFORCE_CUSTOM_DEVICE_SUCCESS(
           pimpl_->device_memory_set(device, ptr, value, size));
     } else {
-      std::unique_ptr<uint8_t> tmp(
-          reinterpret_cast<uint8_t*>(new uint8_t[size]));
+      std::unique_ptr<uint8_t[]> tmp(new uint8_t[size]);
       memset(tmp.get(), value, size);
       MemoryCopyH2D(dev_id, ptr, tmp.get(), size);
     }

reference: https://en.cppreference.com/w/cpp/memory/unique_ptr

template <class T,class Deleter> class unique_ptr<T[], Deleter>; Manages a dynamically-allocated array of objects (e.g. allocated with new[])

template<class T, class Deleter = std::default_delete<T> > class unique_ptr; Manages a single object (e.g. allocated with new)

wydwbb8l

wydwbb8l4#

Here is the patch to fix this issue:

From e98f569707ba9fc7669000f593fd51094594c992 Mon Sep 17 00:00:00 2001
From: Si Jinyuan <sijinyuan@foxmail.com>
Date: Fri, 30 Dec 2022 01:42:59 +0900
Subject: [PATCH] Fix GCC12: mismatched-new-delete error in custom_device.cc

according to https://en.cppreference.com/w/cpp/memory/unique_ptr

When using std::unique_ptr to manage array pointers,
should use

template <class T,class Deleter> class unique_ptr<T[], Deleter>;

instead of

template<class T, class Deleter = std::default_delete<T> >
class unique_ptr;
---
 paddle/phi/backends/custom/custom_device.cc | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc
index 928101b09f..d7397353ef 100644
--- a/paddle/phi/backends/custom/custom_device.cc
+++ b/paddle/phi/backends/custom/custom_device.cc
@@ -358,8 +358,7 @@ class CustomDevice : public DeviceInterface {
       }
     } else {
       if (!pimpl_->memory_copy_p2p) {
-        std::unique_ptr<uint8_t> tmp(
-            reinterpret_cast<uint8_t*>(new uint8_t[size]));
+        std::unique_ptr<uint8_t[]> tmp(new uint8_t[size]);
         MemoryCopyD2H(src_dev_id, tmp.get(), src, size);
         MemoryCopyH2D(dst_dev_id, dst, tmp.get(), size);
       } else {
@@ -451,8 +450,7 @@ class CustomDevice : public DeviceInterface {
       PADDLE_ENFORCE_CUSTOM_DEVICE_SUCCESS(
           pimpl_->device_memory_set(device, ptr, value, size));
     } else {
-      std::unique_ptr<uint8_t> tmp(
-          reinterpret_cast<uint8_t*>(new uint8_t[size]));
+      std::unique_ptr<uint8_t[]> tmp(new uint8_t[size]);
       memset(tmp.get(), value, size);
       MemoryCopyH2D(dev_id, ptr, tmp.get(), size);
     }
-- 
2.37.1 (Apple Git-137.1)

相关问题