@@ -610,13 +610,19 @@ void BindImperative(py::module *m_ptr) {
610610 [](const std::shared_ptr<imperative::Tracer> &tracer) {
611611 imperative::SetCurrentTracer (tracer);
612612 });
613- #if defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
614613 m.def (" _get_current_stream" ,
615614 [](int deviceId) {
615+ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
616616 return paddle::platform::stream::get_current_stream (deviceId);
617+ #else
618+ PADDLE_THROW (platform::errors::Unavailable (
619+ " Paddle is not compiled with CUDA. Cannot visit cuda current "
620+ " stream." ));
621+ #endif
617622 },
618623 py::return_value_policy::reference);
619624 m.def (" _device_synchronize" , [](int device_id) {
625+ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
620626 if (device_id == -1 ) {
621627 device_id = paddle::platform::GetCurrentDeviceId ();
622628 }
@@ -625,8 +631,11 @@ void BindImperative(py::module *m_ptr) {
625631 paddle::platform::SetDeviceId (device_id);
626632 PADDLE_ENFORCE_CUDA_SUCCESS (cudaDeviceSynchronize ());
627633 paddle::platform::SetDeviceId (curr_device_id);
628- });
634+ #else
635+ PADDLE_THROW (platform::errors::Unavailable (
636+ " Paddle is not compiled with CUDA. Cannot visit device synchronize." ));
629637#endif
638+ });
630639
631640 py::class_<imperative::VarBase, std::shared_ptr<imperative::VarBase>>(
632641 m, " VarBase" , R"DOC( )DOC" )
@@ -1692,24 +1701,28 @@ void BindImperative(py::module *m_ptr) {
16921701 return imperative::PyLayerApply (place, cls, args, kwargs);
16931702 });
16941703
1695- #if defined(PADDLE_WITH_CUDA) && !defined(PADDLE_WITH_HIP)
16961704 py::class_<paddle::platform::stream::CUDAStream>(m, " CUDAStream" )
1697- .def (" __init__" ,
1698- [](paddle:: platform::stream::CUDAStream &self,
1699- platform::CUDAPlace &device, int priority) {
1700- if (priority != 1 && priority != 2 ) {
1701- PADDLE_THROW (platform::errors::InvalidArgument (
1702- " Priority should be 1(high) or 2(normal) " ));
1703- }
1704- auto prio = paddle::platform::stream::Priority (priority);
1705+ .def (" __init__" , [](paddle::platform::stream::CUDAStream &self,
1706+ platform::CUDAPlace &device, int priority) {
1707+ # if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1708+ if (priority != 1 && priority != 2 ) {
1709+ PADDLE_THROW (platform::errors::InvalidArgument (
1710+ " Priority should be 1(high) or 2(normal) " ));
1711+ }
1712+ auto prio = paddle::platform::stream::Priority (priority);
17051713
1706- new (&self) paddle::platform::stream::CUDAStream (device, prio);
1707- })
1708- .def (" wait_event" ,
1709- [](paddle::platform::stream::CUDAStream &self,
1710- paddle::platform::CudaEvent &event) {
1711- self.WaitEvent (event.GetRawCudaEvent ());
1712- })
1714+ new (&self) paddle::platform::stream::CUDAStream (device, prio);
1715+ #else
1716+ PADDLE_THROW (platform::errors::Unavailable (
1717+ " Class CUDAStream can only be initialized on the GPU platform." ));
1718+ #endif
1719+ });
1720+ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1721+ m.def (" wait_event" ,
1722+ [](paddle::platform::stream::CUDAStream &self,
1723+ paddle::platform::CudaEvent &event) {
1724+ self.WaitEvent (event.GetRawCudaEvent ());
1725+ })
17131726 .def (" wait_stream" ,
17141727 [](paddle::platform::stream::CUDAStream &self,
17151728 paddle::platform::stream::CUDAStream &stream) {
@@ -1736,23 +1749,34 @@ void BindImperative(py::module *m_ptr) {
17361749 return event;
17371750
17381751 });
1752+ #endif
17391753
17401754 py::class_<paddle::platform::CudaEvent>(m, " CUDAEvent" )
17411755 .def (" __init__" ,
1742- [](paddle::platform::CudaEvent &self, bool enable_timing = false ,
1743- bool blocking = false , bool interprocess = false ) {
1756+ [](paddle::platform::CudaEvent &self, bool enable_timing,
1757+ bool blocking, bool interprocess) {
1758+ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
17441759 unsigned int flags = platform::get_cuda_flags (
17451760 enable_timing, blocking, interprocess);
17461761 new (&self) paddle::platform::CudaEvent (flags);
1747- })
1748- .def (" record" ,
1749- [](paddle::platform::CudaEvent &self,
1750- paddle::platform::stream::CUDAStream *stream) {
1751- if (stream == nullptr ) {
1752- stream = paddle::platform::stream::get_current_stream (-1 );
1753- }
1754- self.Record (*stream);
1755- })
1762+ #else
1763+ PADDLE_THROW (platform::errors::Unavailable (
1764+ " Class CUDAEvent can only be initialized on the GPU "
1765+ " platform." ));
1766+
1767+ #endif
1768+ },
1769+ py::arg (" enable_timing" ) = false , py::arg (" blocking" ) = false ,
1770+ py::arg (" interprocess" ) = false );
1771+ #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
1772+ m.def (" record" ,
1773+ [](paddle::platform::CudaEvent &self,
1774+ paddle::platform::stream::CUDAStream *stream) {
1775+ if (stream == nullptr ) {
1776+ stream = paddle::platform::stream::get_current_stream (-1 );
1777+ }
1778+ self.Record (*stream);
1779+ })
17561780 .def (" query" ,
17571781 [](paddle::platform::CudaEvent &self) { return self.Query (); })
17581782 .def (" synchronize" ,
0 commit comments