diff --git a/CMakeLists.txt b/CMakeLists.txt index ea4bc8a2d6c3e..78ebbccfb2e7a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/paddle/phi/core/tensor_utils.cc b/paddle/phi/core/tensor_utils.cc index e2d8d5a03651c..45f6c00affe05 100644 --- a/paddle/phi/core/tensor_utils.cc +++ b/paddle/phi/core/tensor_utils.cc @@ -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 } diff --git a/python/paddle/fluid/tests/custom_kernel/CMakeLists.txt b/python/paddle/fluid/tests/custom_kernel/CMakeLists.txt index b2bdfac908069..af700c22038e3 100644 --- a/python/paddle/fluid/tests/custom_kernel/CMakeLists.txt +++ b/python/paddle/fluid/tests/custom_kernel/CMakeLists.txt @@ -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 +) + +foreach(TEST_OP ${TEST_OPS}) + py_test(${TEST_OP} SRCS ${TEST_OP}.py ENVS ${CUSTOM_ENVS}) +endforeach() diff --git a/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_c_setup.py b/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_c_setup.py index 11fdc9d0addfa..e162daf2b87e1 100644 --- a/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_c_setup.py +++ b/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_c_setup.py @@ -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') 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 diff --git a/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_setup.py b/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_setup.py index 8147fc3d343d6..efe5368cdca56 100644 --- a/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_setup.py +++ b/python/paddle/fluid/tests/custom_kernel/custom_kernel_dot_setup.py @@ -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 diff --git a/python/paddle/fluid/tests/custom_kernel/test_custom_kernel_dot.py b/python/paddle/fluid/tests/custom_kernel/test_custom_kernel_dot.py index e28bfe00e7c4f..130f74c06d554 100644 --- a/python/paddle/fluid/tests/custom_kernel/test_custom_kernel_dot.py +++ b/python/paddle/fluid/tests/custom_kernel/test_custom_kernel_dot.py @@ -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 - def test_custom_kernel_dot_run(self): # test dot run x_data = np.random.uniform(1, 5, [2, 10]).astype(np.int8) @@ -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): @@ -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) @@ -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'): diff --git a/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt b/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt index fa2ea2726cfab..482dc9cb1f3f6 100644 --- a/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt +++ b/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt @@ -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() diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py index 7da4f38a83686..00d7255a83f21 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py @@ -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() def _test_custom_device_dataloader(self): import paddle @@ -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 @@ -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']