-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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'): | ||
|
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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'] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的修改是为了将单测所需的环境变量赋值给 ctest 命令