Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Plugin] Fix Custom device in eager mode, test=develop #43952

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,12 @@ endif()
if(LINUX
AND NOT WITH_CUSTOM_DEVICE
AND NOT ON_INFER)
set(WITH_CUSTOM_DEVICE ON)
set(WITH_CUSTOM_DEVICE
ON
CACHE BOOL "Enable Custom Device when compiling for Linux" FORCE)
message(
"Enable Custom Device when compiling for Linux. Force WITH_CUSTOM_DEVICE=ON."
)
endif()

if(WIN32)
Expand Down
4 changes: 4 additions & 0 deletions paddle/phi/core/tensor_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void Copy(const Context& dev_ctx,
#ifdef PADDLE_WITH_XPU
} else if (paddle::platform::is_xpu_place(dst_place)) {
dst_ptr = dev_ctx.Alloc(dst, src.dtype());
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
} else if (paddle::platform::is_custom_place(dst_place)) {
dst_ptr = dev_ctx.Alloc(dst, src.dtype());
#endif
}

Expand Down
17 changes: 15 additions & 2 deletions python/paddle/fluid/tests/custom_kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
py_test(test_custom_kernel_dot SRCS test_custom_kernel_dot.py)
py_test(test_custom_kernel_load SRCS test_custom_kernel_load.py)
file(
GLOB TEST_OPS
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")

set(CUSTOM_ENVS
PADDLE_SOURCE_DIR=${PADDLE_SOURCE_DIR}
PADDLE_BINARY_DIR=${PADDLE_BINARY_DIR}
CUSTOM_DEVICE_ROOT=${CMAKE_BINARY_DIR}/python/paddle/fluid/tests/custom_kernel
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的修改是为了将单测所需的环境变量赋值给 ctest 命令


foreach(TEST_OP ${TEST_OPS})
py_test(${TEST_OP} SRCS ${TEST_OP}.py ENVS ${CUSTOM_ENVS})
endforeach()
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def build_extensions(self):
os.path.join(site_packages_path, 'paddle', 'include'),
]
# include path third_party
compile_third_party_path = os.path.join(os.environ['PADDLE_ROOT'],
'build/third_party')
compile_third_party_path = os.path.join(os.environ['PADDLE_BINARY_DIR'],
'third_party')
Copy link
Contributor Author

@qili93 qili93 Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的修改是为了支持在手动编译之后在对应目录下可以直接执行 ctest,因为PADDLE_ROOT是 paddle_build.sh 里面定义的环境变量,手动编译情况下如果用户不指定,直接跑 ctest 就会失败;

根据上一个代码的修改,支持通过 CMAKE 中指定 ctest 运行所需的环境变量,从而支持手工编译时,可以直接在目录下执行 ctest

paddle_custom_kernel_include += [
os.path.join(compile_third_party_path, 'install/gflags/include'), # gflags
os.path.join(compile_third_party_path, 'install/glog/include'), # glog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def build_extensions(self):
site_packages_path))

# include path third_party
compile_third_party_path = os.path.join(os.environ['PADDLE_ROOT'],
'build/third_party')
compile_third_party_path = os.path.join(os.environ['PADDLE_BINARY_DIR'],
'third_party')
paddle_custom_kernel_include += [
os.path.join(compile_third_party_path, 'install/gflags/include'), # gflags
os.path.join(compile_third_party_path, 'install/glog/include'), # glog
Expand Down
14 changes: 0 additions & 14 deletions python/paddle/fluid/tests/custom_kernel/test_custom_kernel_dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def setUp(self):
cur_dir, sys.executable)
os.system(cmd)

# set environment for loading and registering compiled custom kernels
# only valid in current process
os.environ['CUSTOM_DEVICE_ROOT'] = cur_dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的修改是因为环境变量已经通过 CMAKE 环境制定,因此不需要在单测文件中设置CUSTOM_DEVICE_ROOT环境变量

def test_custom_kernel_dot_run(self):
# test dot run
x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
Expand All @@ -52,9 +48,6 @@ def test_custom_kernel_dot_run(self):
"custom kernel dot out: {},\n numpy dot out: {}".format(
out.numpy(), result))

def tearDown(self):
del os.environ['CUSTOM_DEVICE_ROOT']


class TestCustomKernelDotC(unittest.TestCase):

Expand All @@ -67,10 +60,6 @@ def setUp(self):
cur_dir, sys.executable)
os.system(cmd)

# set environment for loading and registering compiled custom kernels
# only valid in current process
os.environ['CUSTOM_DEVICE_ROOT'] = cur_dir

def test_custom_kernel_dot_run(self):
# test dot run
x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8)
Expand All @@ -88,9 +77,6 @@ def test_custom_kernel_dot_run(self):
"custom kernel dot out: {},\n numpy dot out: {}".format(
out.numpy(), result))

def tearDown(self):
del os.environ['CUSTOM_DEVICE_ROOT']


if __name__ == '__main__':
if os.name == 'nt' or sys.platform.startswith('darwin'):
Expand Down
11 changes: 9 additions & 2 deletions python/paddle/fluid/tests/custom_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
if(WITH_CUSTOM_DEVICE)
py_test(test_custom_cpu_plugin SRCS test_custom_cpu_plugin.py)
set_tests_properties(test_custom_cpu_plugin PROPERTIES TIMEOUT 120)
file(
GLOB TEST_OPS
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
"test_*.py")
string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}")

foreach(TEST_OP ${TEST_OPS})
py_test(${TEST_OP} SRCS ${TEST_OP}.py)
endforeach()
endif()
29 changes: 21 additions & 8 deletions python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ def setUp(self):
os.environ['CUSTOM_DEVICE_ROOT'] = os.path.join(
cur_dir, 'PaddleCustomDevice/backends/custom_cpu/build')

def test_custom_device_dataloader(self):
def test_custom_device(self):
import paddle

with paddle.fluid.framework._test_eager_guard():
self._test_custom_device_dataloader()
self._test_custom_device_mnist()
self._test_eager_backward_api()
self._test_custom_device_dataloader()
self._test_custom_device_mnist()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的修改是为了避免每次调用 test_xxx 都会自动 call 一次 setUp,即3次 test case 就需要 git clone 三次 PaddleCustomDevice 并编译,放到一个 test 函数内部就只需要 call 一次 setUp


def _test_custom_device_dataloader(self):
import paddle
Expand All @@ -60,13 +63,6 @@ def _test_custom_device_dataloader(self):
self.assertTrue(label.place.is_custom_place())
break

def test_custom_device_mnist(self):
import paddle

with paddle.fluid.framework._test_eager_guard():
self._test_custom_device_mnist()
self._test_custom_device_mnist()

def _test_custom_device_mnist(self):
import paddle

Expand Down Expand Up @@ -120,6 +116,23 @@ def forward(self, inputs, label=None):

self.assertTrue(pred.place.is_custom_place())

def _test_eager_backward_api(self):
x = np.random.random([2, 2]).astype("float32")
y = np.random.random([2, 2]).astype("float32")
grad = np.ones([2, 2]).astype("float32")

import paddle
paddle.set_device('custom_cpu')
x_tensor = paddle.to_tensor(x, stop_gradient=False)
y_tensor = paddle.to_tensor(y)
z1_tensor = paddle.matmul(x_tensor, y_tensor)
z2_tensor = paddle.matmul(x_tensor, y_tensor)

grad_tensor = paddle.to_tensor(grad)
paddle.autograd.backward([z1_tensor, z2_tensor], [grad_tensor, None])

self.assertTrue(x_tensor.grad.place.is_custom_place())

def tearDown(self):
del os.environ['CUSTOM_DEVICE_ROOT']

Expand Down