diff --git a/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt b/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt index 00eef2d5a77316dcb3918ff32dde55b4fe9a1c73..d1885f9f9df069d462c33246b3dfa8c431b97c8a 100644 --- a/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt +++ b/python/paddle/fluid/tests/custom_runtime/CMakeLists.txt @@ -28,5 +28,5 @@ if(WITH_CUSTOM_DEVICE AND NOT WITH_GPU) set_tests_properties(test_custom_cpu_profiler_plugin PROPERTIES TIMEOUT 120) set_tests_properties(test_fleet_launch_custom_device PROPERTIES TIMEOUT 120) set_tests_properties(test_custom_cpu_to_static PROPERTIES TIMEOUT 120) - set_tests_properties(test_custom_device_relu_setup PROPERTIES TIMEOUT 120) + set_tests_properties(test_custom_op_setup PROPERTIES TIMEOUT 120) endif() diff --git a/python/paddle/fluid/tests/custom_runtime/custom_relu_op.cc b/python/paddle/fluid/tests/custom_runtime/custom_op.cc similarity index 91% rename from python/paddle/fluid/tests/custom_runtime/custom_relu_op.cc rename to python/paddle/fluid/tests/custom_runtime/custom_op.cc index da0563ffeb10e3762dc874676ffc9402d0529bc7..01dec0660453ad6db227b8cc6cbf373d7c75401b 100644 --- a/python/paddle/fluid/tests/custom_runtime/custom_relu_op.cc +++ b/python/paddle/fluid/tests/custom_runtime/custom_op.cc @@ -16,6 +16,7 @@ #include #include "paddle/extension.h" +#include "paddle/phi/backends/all_context.h" #define CHECK_CPU_INPUT(x) PD_CHECK(x.is_cpu(), #x " must be a CPU Tensor.") #define CHECK_CUSTOM_INPUT(x) \ @@ -191,3 +192,24 @@ PD_BUILD_DOUBLE_GRAD_OP(custom_relu) .Outputs({paddle::Grad(paddle::Grad("Out"))}) .SetKernelFn(PD_KERNEL(ReluDoubleBackward)) .SetInferShapeFn(PD_INFER_SHAPE(ReluDoubleBackwardInferShape)); + +std::vector StreamForward(const paddle::Tensor& x) { + CHECK_CUSTOM_INPUT(x); + + auto dev_ctx = + paddle::experimental::DeviceContextPool::Instance().Get(x.place()); + auto custom_ctx = static_cast(dev_ctx); + void* stream = custom_ctx->stream(); + + PD_CHECK(stream != nullptr); + std::cout << "Check stream != nullptr successfully" << std::endl; + custom_ctx->Wait(); + std::cout << "Check Wait successfully" << std::endl; + + return {x}; +} + +PD_BUILD_OP(custom_stream) + .Inputs({"X"}) + .Outputs({"Out"}) + .SetKernelFn(PD_KERNEL(StreamForward)); diff --git a/python/paddle/fluid/tests/custom_runtime/test_custom_device_relu_setup.py b/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py similarity index 96% rename from python/paddle/fluid/tests/custom_runtime/test_custom_device_relu_setup.py rename to python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py index 760ad56cc3380e4d5b53fd65e07638e14d5859f5..969fdb2f8a6b2229d77c805fcf977ee6dbcc926a 100644 --- a/python/paddle/fluid/tests/custom_runtime/test_custom_device_relu_setup.py +++ b/python/paddle/fluid/tests/custom_runtime/test_custom_op_setup.py @@ -177,14 +177,15 @@ class TestNewCustomOpSetUpInstall(unittest.TestCase): ) custom_module = paddle.utils.cpp_extension.load( - name='custom_device_relu', - sources=['custom_relu_op.cc'], + name='custom_device', + sources=['custom_op.cc'], extra_include_paths=paddle_includes, # add for Coverage CI extra_cxx_cflags=["-w", "-g"], # test for cc flags # build_directory=self.cur_dir, verbose=True, ) self.custom_op = custom_module.custom_relu + self.custom_stream_op = custom_module.custom_stream self.dtypes = ["float32", "float64"] self.device = "custom_cpu" @@ -204,6 +205,7 @@ class TestNewCustomOpSetUpInstall(unittest.TestCase): self._test_dynamic() self._test_double_grad_dynamic() self._test_with_dataloader() + self._test_stream() def _test_static(self): for dtype in self.dtypes: @@ -317,6 +319,15 @@ class TestNewCustomOpSetUpInstall(unittest.TestCase): if batch_id == 5: break + def _test_stream(self): + import paddle + + paddle.set_device(self.device) + x = paddle.ones([2, 2], dtype='float32') + out = self.custom_stream_op(x) + + np.testing.assert_array_equal(x.numpy(), out.numpy()) + if __name__ == "__main__": if os.name == 'nt' or sys.platform.startswith('darwin'):