From 266955a9e869ca325d7d74cd6c603744641aacd5 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 9 Feb 2022 11:36:18 +0800 Subject: [PATCH] move stream into pten (#39392) --- paddle/fluid/framework/tensor.h | 2 +- .../memory/allocation/allocator_facade.cc | 4 ++-- .../memory/allocation/allocator_facade.h | 8 +++---- paddle/fluid/memory/malloc.cc | 6 ++--- paddle/fluid/memory/malloc.h | 6 ++--- .../memory/stream_safe_cuda_alloc_test.cu | 22 +++++++++---------- paddle/fluid/operators/memcpy_h2d_op.h | 4 ++-- paddle/pten/core/dense_tensor.h | 2 +- paddle/pten/core/dense_tensor.inl | 2 +- paddle/pten/core/dense_tensor_impl.cc | 2 +- .../platform/stream => pten/core}/stream.h | 6 ++--- 11 files changed, 31 insertions(+), 33 deletions(-) rename paddle/{fluid/platform/stream => pten/core}/stream.h (90%) diff --git a/paddle/fluid/framework/tensor.h b/paddle/fluid/framework/tensor.h index 8c7345f3e2f..204c620ff80 100644 --- a/paddle/fluid/framework/tensor.h +++ b/paddle/fluid/framework/tensor.h @@ -28,7 +28,7 @@ limitations under the License. */ #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/place.h" -#include "paddle/fluid/platform/stream/stream.h" +#include "paddle/pten/core/stream.h" #include "paddle/pten/core/dense_tensor.h" diff --git a/paddle/fluid/memory/allocation/allocator_facade.cc b/paddle/fluid/memory/allocation/allocator_facade.cc index f2bfaccd1d4..35131446d86 100644 --- a/paddle/fluid/memory/allocation/allocator_facade.cc +++ b/paddle/fluid/memory/allocation/allocator_facade.cc @@ -940,7 +940,7 @@ uint64_t AllocatorFacade::Release(const platform::Place& place) { } std::shared_ptr AllocatorFacade::AllocShared( - const platform::Place& place, size_t size, const platform::Stream& stream) { + const platform::Place& place, size_t size, const pten::Stream& stream) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PADDLE_ENFORCE_EQ( FLAGS_use_stream_safe_cuda_allocator, true, @@ -965,7 +965,7 @@ std::shared_ptr AllocatorFacade::AllocShared( bool AllocatorFacade::InSameStream( const std::shared_ptr& allocation, - const platform::Stream& stream) { + const pten::Stream& stream) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) PADDLE_ENFORCE_EQ( FLAGS_use_stream_safe_cuda_allocator, true, diff --git a/paddle/fluid/memory/allocation/allocator_facade.h b/paddle/fluid/memory/allocation/allocator_facade.h index f4aea98003a..6b9e82e8579 100644 --- a/paddle/fluid/memory/allocation/allocator_facade.h +++ b/paddle/fluid/memory/allocation/allocator_facade.h @@ -22,7 +22,7 @@ #include "paddle/fluid/platform/device/gpu/gpu_info.h" #endif #include "paddle/fluid/platform/place.h" -#include "paddle/fluid/platform/stream/stream.h" +#include "paddle/pten/core/stream.h" namespace paddle { namespace memory { @@ -71,13 +71,13 @@ class AllocatorFacade { std::shared_ptr AllocShared(const platform::Place& place, size_t size, - const platform::Stream& stream); + const pten::Stream& stream); bool InSameStream(const std::shared_ptr& allocation, - const platform::Stream& stream); + const pten::Stream& stream); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - // TODO(zhiqiu): change gpuStream_t to platform::Stream if needed. + // TODO(zhiqiu): change gpuStream_t to pten::Stream if needed. AllocationPtr Alloc(const platform::Place& place, size_t size, const gpuStream_t& stream); uint64_t Release(const platform::CUDAPlace& place, const gpuStream_t& stream); diff --git a/paddle/fluid/memory/malloc.cc b/paddle/fluid/memory/malloc.cc index 63c562be97f..78166cb3893 100644 --- a/paddle/fluid/memory/malloc.cc +++ b/paddle/fluid/memory/malloc.cc @@ -16,7 +16,7 @@ limitations under the License. */ #include "paddle/fluid/memory/allocation/allocator_facade.h" #include "paddle/fluid/platform/place.h" -#include "paddle/fluid/platform/stream/stream.h" +#include "paddle/pten/core/stream.h" namespace paddle { namespace memory { @@ -36,13 +36,13 @@ uint64_t Release(const platform::Place& place) { std::shared_ptr AllocShared(const platform::Place& place, size_t size, - const platform::Stream& stream) { + const pten::Stream& stream) { return allocation::AllocatorFacade::Instance().AllocShared(place, size, stream); } bool InSameStream(const std::shared_ptr& allocation, - const platform::Stream& stream) { + const pten::Stream& stream) { return allocation::AllocatorFacade::Instance().InSameStream(allocation, stream); } diff --git a/paddle/fluid/memory/malloc.h b/paddle/fluid/memory/malloc.h index 855cbb775a1..fe988ddccd7 100644 --- a/paddle/fluid/memory/malloc.h +++ b/paddle/fluid/memory/malloc.h @@ -18,8 +18,8 @@ limitations under the License. */ #include "paddle/fluid/memory/allocation/allocator.h" #include "paddle/fluid/platform/place.h" -#include "paddle/fluid/platform/stream/stream.h" #include "paddle/pten/core/device_context.h" +#include "paddle/pten/core/stream.h" namespace paddle { namespace memory { @@ -39,10 +39,10 @@ extern uint64_t Release(const platform::Place& place); extern std::shared_ptr AllocShared(const platform::Place& place, size_t size, - const platform::Stream& stream); + const pten::Stream& stream); extern bool InSameStream(const std::shared_ptr& allocation, - const platform::Stream& stream); + const pten::Stream& stream); extern void* GetBasePtr(const std::shared_ptr& allocation); diff --git a/paddle/fluid/memory/stream_safe_cuda_alloc_test.cu b/paddle/fluid/memory/stream_safe_cuda_alloc_test.cu index bb44b29ac5b..9fdc5284d15 100644 --- a/paddle/fluid/memory/stream_safe_cuda_alloc_test.cu +++ b/paddle/fluid/memory/stream_safe_cuda_alloc_test.cu @@ -30,7 +30,7 @@ #include "paddle/fluid/platform/cuda_graph_with_memory_pool.h" #include "paddle/fluid/platform/device/gpu/gpu_info.h" #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/stream/stream.h" +#include "paddle/pten/core/stream.h" namespace paddle { namespace memory { @@ -70,9 +70,9 @@ class StreamSafeCUDAAllocTest : public ::testing::Test { PADDLE_ENFORCE_GPU_SUCCESS(hipStreamCreate(&stream)); #endif - std::shared_ptr allocation = AllocShared( - place_, workspace_size_, - platform::Stream(reinterpret_cast(stream))); + std::shared_ptr allocation = + AllocShared(place_, workspace_size_, + pten::Stream(reinterpret_cast(stream))); #ifdef PADDLE_WITH_CUDA PADDLE_ENFORCE_GPU_SUCCESS( cudaMemset(allocation->ptr(), 0, allocation->size())); @@ -286,9 +286,9 @@ TEST(StreamSafeCUDAAllocInterfaceTest, GetStreamInterfaceTest) { PADDLE_ENFORCE_GPU_SUCCESS(hipStreamCreate(&new_stream)); #endif - std::shared_ptr allocation_new_stream = AllocShared( - place, alloc_size, - platform::Stream(reinterpret_cast(new_stream))); + std::shared_ptr allocation_new_stream = + AllocShared(place, alloc_size, + pten::Stream(reinterpret_cast(new_stream))); EXPECT_EQ(GetStream(allocation_new_stream), new_stream); #ifdef PADDLE_WITH_CUDA @@ -315,10 +315,10 @@ TEST(StreamSafeCUDAAllocInterfaceTest, CUDAGraphExceptionTest) { EXPECT_THROW(Release(place), paddle::platform::EnforceNotMet); EXPECT_THROW(allocation::AllocatorFacade::Instance().GetAllocator(place), paddle::platform::EnforceNotMet); - EXPECT_THROW(AllocShared(place, alloc_size, - platform::Stream( - reinterpret_cast(nullptr))), - paddle::platform::EnforceNotMet); + EXPECT_THROW( + AllocShared(place, alloc_size, + pten::Stream(reinterpret_cast(nullptr))), + paddle::platform::EnforceNotMet); EXPECT_THROW(Alloc(place, alloc_size, nullptr), paddle::platform::EnforceNotMet); EXPECT_THROW(Release(place, nullptr), paddle::platform::EnforceNotMet); diff --git a/paddle/fluid/operators/memcpy_h2d_op.h b/paddle/fluid/operators/memcpy_h2d_op.h index c9995eeca16..6d1b8eee043 100644 --- a/paddle/fluid/operators/memcpy_h2d_op.h +++ b/paddle/fluid/operators/memcpy_h2d_op.h @@ -15,7 +15,7 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/var_type.h" #include "paddle/fluid/platform/device_context.h" -#include "paddle/fluid/platform/stream/stream.h" +#include "paddle/pten/core/stream.h" namespace pten { class DenseTensor; @@ -47,7 +47,7 @@ class MemcpyH2DFunctor { #endif out_tensor.mutable_data( dev_ctx_.GetPlace(), lod_tensor.type(), - platform::Stream(reinterpret_cast(stream))); + pten::Stream(reinterpret_cast(stream))); if (dst_place_type_ == 0 || dst_place_type_ == 1) { framework::TensorCopy(lod_tensor, dev_ctx_.GetPlace(), dev_ctx_, diff --git a/paddle/pten/core/dense_tensor.h b/paddle/pten/core/dense_tensor.h index 8d10753b58d..ef8520e29c3 100644 --- a/paddle/pten/core/dense_tensor.h +++ b/paddle/pten/core/dense_tensor.h @@ -16,12 +16,12 @@ limitations under the License. */ #include "paddle/pten/core/allocator.h" #include "paddle/pten/core/storage.h" +#include "paddle/pten/core/stream.h" #include "paddle/pten/core/tensor_base.h" #include "paddle/pten/core/tensor_meta.h" // See Note [ Why still include the fluid headers? ] #include "paddle/fluid/framework/data_type.h" -#include "paddle/fluid/platform/stream/stream.h" /* @jim19930609: Move to MKLDNN_Tensor in the future */ diff --git a/paddle/pten/core/dense_tensor.inl b/paddle/pten/core/dense_tensor.inl index 754baeb73c9..d5d207aba40 100644 --- a/paddle/pten/core/dense_tensor.inl +++ b/paddle/pten/core/dense_tensor.inl @@ -76,7 +76,7 @@ void* mutable_data(const paddle::platform::Place& place, void* mutable_data(const paddle::platform::Place& place, paddle::framework::proto::VarType::Type type, - const paddle::platform::Stream& stream); + const pten::Stream& stream); /* @jim19930609: Remove dependency on protobuf after Tensor Unification. */ diff --git a/paddle/pten/core/dense_tensor_impl.cc b/paddle/pten/core/dense_tensor_impl.cc index 85714373472..a798ebe98b5 100644 --- a/paddle/pten/core/dense_tensor_impl.cc +++ b/paddle/pten/core/dense_tensor_impl.cc @@ -144,7 +144,7 @@ void* DenseTensor::mutable_data(const paddle::platform::Place& place, void* DenseTensor::mutable_data(const paddle::platform::Place& place, paddle::framework::proto::VarType::Type type, - const paddle::platform::Stream& stream) { + const pten::Stream& stream) { set_type(type); PADDLE_ENFORCE_GE( numel(), diff --git a/paddle/fluid/platform/stream/stream.h b/paddle/pten/core/stream.h similarity index 90% rename from paddle/fluid/platform/stream/stream.h rename to paddle/pten/core/stream.h index 79ca51220bd..30bd1b0ba47 100644 --- a/paddle/fluid/platform/stream/stream.h +++ b/paddle/pten/core/stream.h @@ -17,8 +17,7 @@ limitations under the License. */ #include #include -namespace paddle { -namespace platform { +namespace pten { using StreamId = uint64_t; class Stream final { @@ -30,5 +29,4 @@ class Stream final { StreamId id_; }; -} // namespace platform -} // namespace paddle +} // namespace pten -- GitLab