Skip to content

Commit

Permalink
[Plugin] Fix Custom device in eager mode, test=develop
Browse files Browse the repository at this point in the history
  • Loading branch information
qili93 committed Jul 15, 2022
1 parent 1f7f719 commit ae935e9
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
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
)

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')
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

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
17 changes: 15 additions & 2 deletions python/paddle/fluid/tests/custom_runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
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}")

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_runtime
)

foreach(TEST_OP ${TEST_OPS})
py_test(${TEST_OP} SRCS ${TEST_OP}.py ENVS ${CUSTOM_ENVS})
endforeach()
endif()
16 changes: 16 additions & 0 deletions python/paddle/fluid/tests/custom_runtime/test_custom_cpu_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ 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)
z_tensor = paddle.add(x_tensor, y_tensor)

grad_tensor = paddle.to_tensor(grad)
paddle.autograd.backward([z_tensor], [grad_tensor], False)

self.assertTrue(np.allclose(grad, x_tensor.grad.numpy()))

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

Expand Down

0 comments on commit ae935e9

Please sign in to comment.